-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[BCN] Add MoralisAdapter and multiProvider error handling #4143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
leolambo
wants to merge
5
commits into
bitpay:master
Choose a base branch
from
leolambo:multiProviderMoralisSupport
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
63f86ca
Extract shared Moralis utilities and add URL redaction
leolambo 89f23f7
Add MoralisAdapter and register in factory
leolambo f1f62bb
Map provider errors to HTTP 503 and 400 in API routes
leolambo aeb75c0
Add multi-provider integration tests
leolambo e7ae3e2
Fix order override and confirmations bugs in Moralis streams
leolambo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 3 additions & 1 deletion
4
packages/bitcore-node/src/providers/chain-state/external/adapters/factory.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
packages/bitcore-node/src/providers/chain-state/external/adapters/moralis-utils.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { Web3 } from '@bitpay-labs/crypto-wallet-core'; | ||
| import { EVMTransactionStorage } from '../../evm/models/transaction'; | ||
| import type { IEVMTransactionTransformed } from '../../evm/types'; | ||
|
|
||
| /** | ||
| * Shared Moralis transformation and query-building utilities. | ||
| * Used by both MoralisAdapter (multi-provider) and MoralisStateProvider (standalone CSP). | ||
| */ | ||
|
|
||
| export function transformMoralisTransaction(tx: any): IEVMTransactionTransformed { | ||
| const txid = tx.hash || tx.transaction_hash; | ||
| const transformed = { | ||
| chain: tx.chain, | ||
| network: tx.network, | ||
| txid, | ||
| blockHeight: Number(tx.block_number ?? tx.blockNumber), | ||
| blockHash: tx.block_hash ?? tx.blockHash, | ||
| blockTime: new Date(tx.block_timestamp ?? tx.blockTimestamp), | ||
| blockTimeNormalized: new Date(tx.block_timestamp ?? tx.blockTimestamp), | ||
| value: tx.value, | ||
| gasLimit: tx.gas ?? 0, | ||
| gasPrice: tx.gas_price ?? tx.gasPrice ?? 0, | ||
| fee: Number(tx.receipt_gas_used ?? tx.receiptGasUsed ?? 0) * Number(tx.gas_price ?? tx.gasPrice ?? 0), | ||
| nonce: tx.nonce, | ||
| to: (tx.to_address ?? tx.toAddress) | ||
| ? Web3.utils.toChecksumAddress(tx.to_address ?? tx.toAddress) | ||
| : '', | ||
| from: Web3.utils.toChecksumAddress(tx.from_address ?? tx.fromAddress), | ||
| data: tx.input, | ||
| internal: [], | ||
| calls: tx?.internal_transactions?.map((t: any) => transformMoralisInternalTx(t)) || [], | ||
| effects: [], | ||
| category: tx.category, | ||
| wallets: [], | ||
| transactionIndex: tx.transaction_index ?? tx.transactionIndex | ||
| } as IEVMTransactionTransformed; | ||
| EVMTransactionStorage.addEffectsToTxs([transformed]); | ||
| return transformed; | ||
| } | ||
|
|
||
| export function transformMoralisInternalTx(tx: any) { | ||
| return { | ||
| from: Web3.utils.toChecksumAddress(tx.from), | ||
| to: Web3.utils.toChecksumAddress(tx.to), | ||
| gas: tx.gas, | ||
| gasUsed: tx.gas_used, | ||
| input: tx.input, | ||
| output: tx.output, | ||
| type: tx.type, | ||
| value: tx.value, | ||
| abiType: EVMTransactionStorage.abiDecode(tx.input) | ||
| }; | ||
| } | ||
|
|
||
| export function transformMoralisTokenTransfer(transfer: any) { | ||
| const base = transformMoralisTransaction(transfer); | ||
| return { | ||
| ...base, | ||
| transactionHash: transfer.transaction_hash, | ||
| transactionIndex: transfer.transaction_index, | ||
| contractAddress: transfer.contract_address ?? transfer.address, | ||
| name: transfer.token_name | ||
| }; | ||
| } | ||
|
|
||
| export function transformMoralisQueryParams(params: { chainId: string | bigint; args: any }) { | ||
| const { chainId, args } = params; | ||
| const query: any = { chain: formatMoralisChainId(chainId) }; | ||
| if (args) { | ||
| if (args.startBlock || args.endBlock) { | ||
| if (args.startBlock) query.from_block = Number(args.startBlock); | ||
| if (args.endBlock) query.to_block = Number(args.endBlock); | ||
| } else { | ||
| if (args.startDate) query.from_date = args.startDate; | ||
| if (args.endDate) query.to_date = args.endDate; | ||
| } | ||
| if (args.direction) { | ||
| query.order = Number(args.direction) > 0 ? 'ASC' : 'DESC'; | ||
| } | ||
| if (args.date) { | ||
| query.date = new Date(args.date).getTime(); | ||
| } | ||
| } | ||
| return query; | ||
| } | ||
|
|
||
| export function buildMoralisQueryString(params: Record<string, any>): string { | ||
| const query: string[] = []; | ||
| for (const [key, value] of Object.entries(params)) { | ||
| if (Array.isArray(value)) { | ||
| for (let i = 0; i < value.length; i++) { | ||
| if (value[i] != null) query.push(`${key}%5B${i}%5D=${value[i]}`); | ||
| } | ||
| } else if (value != null) { | ||
| query.push(`${key}=${value}`); | ||
| } | ||
| } | ||
| return query.length ? `?${query.join('&')}` : ''; | ||
| } | ||
|
|
||
| export function formatMoralisChainId(chainId: string | bigint): string { | ||
| const str = String(chainId); | ||
| if (str.startsWith('0x')) return str; | ||
| return '0x' + BigInt(str).toString(16); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.