Skip to content

Commit

Permalink
fix: methodId might not exist in response of Etherscan (#11398)
Browse files Browse the repository at this point in the history
  • Loading branch information
UncleBill authored and guanbinrui committed Feb 18, 2024
1 parent 2a4eaf1 commit bbe352d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
22 changes: 19 additions & 3 deletions packages/web3-providers/src/Chainbase/apis/RedPacketAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,30 @@ import {
createPageable,
pageableToIterator,
} from '@masknet/shared-base'
import type { ChainId, SchemaType } from '@masknet/web3-shared-evm'
import { ChainId, type SchemaType } from '@masknet/web3-shared-evm'
import { TRANSACTIONS_BY_CONTRACT_METHOD_ENDPOINT, MAX_SIZE_PER_PAGE } from '../constants.js'
import type { Tx } from '../types.js'
import { fetchJSON } from '../../helpers/fetchJSON.js'
import type { RedPacketBaseAPI } from '../../entry-types.js'

class ChainbaseRedPacketAPI implements RedPacketBaseAPI.Provider<ChainId, SchemaType> {
async getHistoryTransactions(
export class ChainbaseRedPacketAPI implements RedPacketBaseAPI.Provider<ChainId, SchemaType> {
/**
* @see https://docs.chainbase.com/reference/supported-chains
*/
static isSupportedChain(chainId: ChainId) {
const supported = [
ChainId.Mainnet,
ChainId.Matic,
ChainId.BSC,
ChainId.Avalanche,
ChainId.Arbitrum,
ChainId.Base,
/** zkSync */ 324,
].includes(chainId)
console.error('Unsupported chain by ChainBase, see https://docs.chainbase.com/reference/supported-chains')
return supported
}
static async getHistoryTransactions(
chainId: ChainId,
senderAddress: string,
contractAddress: string,
Expand Down
6 changes: 5 additions & 1 deletion packages/web3-providers/src/Etherscan/apis/RedPacketAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,12 @@ class EtherscanRedPacketAPI implements RedPacketBaseAPI.Provider<ChainId, Schema
if (!result) return

methodId = methodId.toLowerCase()
const methodIdLength = methodId.length
return result
.filter((x) => x.methodId?.toLowerCase() === methodId && isSameAddress(x.from, senderAddress))
.filter((x) => {
const txMethodId = x.methodId || x.input?.slice(0, methodIdLength)
return txMethodId?.toLowerCase() === methodId && isSameAddress(x.from, senderAddress)
})
.map((x) => ({ ...x, chainId }))
}
}
Expand Down
38 changes: 19 additions & 19 deletions packages/web3-providers/src/RedPacket/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import REDPACKET_ABI from '@masknet/web3-contracts/abis/HappyRedPacketV4.json'
import NFT_REDPACKET_ABI from '@masknet/web3-contracts/abis/NftRedPacket.json'
import { DSEARCH_BASE_URL } from '../DSearch/constants.js'
import { fetchFromDSearch } from '../DSearch/helpers.js'
import { ChainbaseRedPacket } from '../Chainbase/index.js'
import { ChainbaseRedPacketAPI } from '../Chainbase/index.js'
import { EtherscanRedPacket } from '../Etherscan/index.js'
import { ContractRedPacket } from './api.js'
import {
Expand Down Expand Up @@ -89,24 +89,24 @@ class RedPacketAPI implements RedPacketBaseAPI.Provider<ChainId, SchemaType> {
fromBlock: number,
endBlock: number,
) {
return attemptUntil(
[
() => {
return ChainbaseRedPacket.getHistoryTransactions(chainId, senderAddress, contractAddress, methodId)
},
() => {
return EtherscanRedPacket.getHistoryTransactions(
chainId,
senderAddress,
contractAddress,
methodId,
fromBlock,
endBlock,
)
},
],
[],
)
const attempts = [
() => {
return EtherscanRedPacket.getHistoryTransactions(
chainId,
senderAddress,
contractAddress,
methodId,
fromBlock,
endBlock,
)
},
]
if (ChainbaseRedPacketAPI.isSupportedChain(chainId)) {
attempts.unshift(() => {
return ChainbaseRedPacketAPI.getHistoryTransactions(chainId, senderAddress, contractAddress, methodId)
})
}
return attemptUntil(attempts, [])
}

async getCollectionsByOwner(
Expand Down

0 comments on commit bbe352d

Please sign in to comment.