-
Notifications
You must be signed in to change notification settings - Fork 306
/
RedPacketAPI.ts
43 lines (39 loc) · 1.65 KB
/
RedPacketAPI.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import urlcat from 'urlcat'
import { isSameAddress, type Transaction } from '@masknet/web3-shared-base'
import { EtherscanURL } from '@masknet/web3-shared-evm'
import type { ChainId, SchemaType } from '@masknet/web3-shared-evm'
import { fetchJSON } from '../../helpers/fetchJSON.js'
import type { RedPacketBaseAPI } from '../../entry-types.js'
class EtherscanRedPacketAPI implements RedPacketBaseAPI.Provider<ChainId, SchemaType> {
async getHistoryTransactions(
chainId: ChainId,
senderAddress: string,
contractAddress: string,
methodId: string,
startBlock: number,
endBlock: number,
): Promise<Array<Transaction<ChainId, SchemaType>> | undefined> {
if (!senderAddress || !contractAddress || !startBlock || !endBlock || !methodId) return
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
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()