Skip to content

Commit

Permalink
[Release] Hotfix 2.24.3 => 2.24.4 (patch) (#11396)
Browse files Browse the repository at this point in the history
* chore: bump version to 2.24.4

* fix: do not discard exceptions of getting redpacket histories (#11397)

* fix: methodId might not exist in response of Etherscan (#11398)

---------

Co-authored-by: UncleBill <billbill290@gmail.com>
  • Loading branch information
guanbinrui and UncleBill committed Feb 13, 2024
1 parent 7d3b802 commit 3d13e9f
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 79 deletions.
2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -8,7 +8,7 @@
"yarn": ">=999.0.0",
"npm": ">=999.0.0"
},
"version": "2.24.3",
"version": "2.24.4",
"private": true,
"license": "AGPL-3.0-or-later",
"scripts": {
Expand Down
90 changes: 51 additions & 39 deletions packages/web3-providers/src/Chainbase/apis/RedPacketAPI.ts
Expand Up @@ -7,58 +7,70 @@ 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,
methodId: string,
): Promise<Array<Transaction<ChainId, SchemaType>> | undefined> {
try {
const txes = await asyncIteratorToArray(
pageableToIterator(async (indicator) => {
const { records } = await fetchJSON<{ records: { data: { result: Tx[] } } }>(
urlcat(TRANSACTIONS_BY_CONTRACT_METHOD_ENDPOINT, {
senderAddress,
contractAddress,
chainId,
methodId: `${methodId}%`, // '%' for sql string match.
size: MAX_SIZE_PER_PAGE,
offset: Number(indicator?.id ?? 0) * MAX_SIZE_PER_PAGE,
}),
)
const txes = await asyncIteratorToArray(
pageableToIterator(async (indicator) => {
const { records } = await fetchJSON<{ records: { data: { result: Tx[] } } }>(
urlcat(TRANSACTIONS_BY_CONTRACT_METHOD_ENDPOINT, {
senderAddress,
contractAddress,
chainId,
methodId: `${methodId}%`, // '%' for sql string match.
size: MAX_SIZE_PER_PAGE,
offset: Number(indicator?.id ?? 0) * MAX_SIZE_PER_PAGE,
}),
)

return createPageable(
records.data.result,
createIndicator(indicator),
records.data.result.length === 0 ? undefined : createNextIndicator(indicator),
)
}),
)
return createPageable(
records.data.result,
createIndicator(indicator),
records.data.result.length === 0 ? undefined : createNextIndicator(indicator),
)
}),
)

if (!txes?.length) return
if (!txes?.length) return

return txes
.sort((a, b) => new Date(b.block_timestamp).getTime() - new Date(a.block_timestamp).getTime())
.map((x) => {
return {
input: x.input,
to: x.to_address,
from: x.from_address,
hash: x.transaction_hash,
chainId,
blockNumber: Number(x.block_number),
} as Transaction<ChainId, SchemaType>
})
} catch {
return
}
return txes
.sort((a, b) => new Date(b.block_timestamp).getTime() - new Date(a.block_timestamp).getTime())
.map((x) => {
return {
input: x.input,
to: x.to_address,
from: x.from_address,
hash: x.transaction_hash,
chainId,
blockNumber: Number(x.block_number),
} as Transaction<ChainId, SchemaType>
})
}
}
export const ChainbaseRedPacket = new ChainbaseRedPacketAPI()
40 changes: 20 additions & 20 deletions packages/web3-providers/src/Etherscan/apis/RedPacketAPI.ts
Expand Up @@ -16,28 +16,28 @@ class EtherscanRedPacketAPI implements RedPacketBaseAPI.Provider<ChainId, Schema
): Promise<Array<Transaction<ChainId, SchemaType>> | undefined> {
if (!senderAddress || !contractAddress || !startBlock || !endBlock || !methodId) return

try {
const { result } = await fetchJSON<{ result: Array<Transaction<ChainId, SchemaType>> }>(
urlcat(EtherscanURL.from(chainId), {
action: 'txlist',
module: 'account',
sort: 'desc',
startBlock,
endBlock,
address: contractAddress,
chain_id: chainId,
}),
)
const { result } = await fetchJSON<{ result: Array<Transaction<ChainId, SchemaType>> }>(
urlcat(EtherscanURL.from(chainId), {
action: 'txlist',
module: 'account',
sort: 'desc',
startBlock,
endBlock,
address: contractAddress,
chain_id: chainId,
}),
)

if (!result) return
if (!result) return

methodId = methodId.toLowerCase()
return result
.filter((x) => x.methodId?.toLowerCase() === methodId && isSameAddress(x.from, senderAddress))
.map((x) => ({ ...x, chainId }))
} catch {
return
}
methodId = methodId.toLowerCase()
const methodIdLength = methodId.length
return result
.filter((x) => {
const txMethodId = x.methodId || x.input?.slice(0, methodIdLength)
return txMethodId?.toLowerCase() === methodId && isSameAddress(x.from, senderAddress)
})
.map((x) => ({ ...x, chainId }))
}
}
export const EtherscanRedPacket = new EtherscanRedPacketAPI()
38 changes: 19 additions & 19 deletions packages/web3-providers/src/RedPacket/index.ts
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 3d13e9f

Please sign in to comment.