-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
542 additions
and
29 deletions.
There are no files selected for viewing
This file contains 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,9 @@ | ||
--- | ||
'@moralisweb3/common-evm-utils': minor | ||
'@moralisweb3/api-utils': minor | ||
'@moralisweb3/evm-api': minor | ||
'@moralisweb3/react': minor | ||
'@moralisweb3/next': minor | ||
--- | ||
|
||
Add `getErc20Transfers` endpoint at `Moralis.EvmApi.token.getErc20Transfers()` |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
63 changes: 63 additions & 0 deletions
63
packages/common/evmUtils/src/operations/token/getErc20TransfersOperation.test.ts
This file contains 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,63 @@ | ||
import MoralisCore from '@moralisweb3/common-core'; | ||
import { EvmAddress, EvmChain } from '../../dataTypes'; | ||
import { getErc20TransfersOperation, GetErc20TransfersRequest } from './getErc20TransfersOperation'; | ||
|
||
describe('getErc20TransfersOperation', () => { | ||
let core: MoralisCore; | ||
|
||
beforeAll(() => { | ||
core = MoralisCore.create(); | ||
}); | ||
|
||
it('serializeRequest() serializes correctly and deserializeRequest() deserializes correctly', () => { | ||
const contractAddresses = ['0xA1ec0345033E7817FB532F68ceb83cD43B05A867']; | ||
const excludeContracts = ['0x65d10A783486d778b036E4715ce69e504aC72536']; | ||
const walletAddresses = ['0x80454f1785347e23f8CC232159FF26fB2a4D3F38']; | ||
const excludeWallets = ['0xD667dC4da4469C064c9200C7CdfC3E60f0f22ba2']; | ||
const chain = '0x10'; | ||
|
||
const request: Required<GetErc20TransfersRequest> = { | ||
chain: EvmChain.create(chain, core), | ||
fromBlock: 10, | ||
toBlock: 20, | ||
cursor: 'CURSOR1', | ||
limit: 333, | ||
contractAddresses: contractAddresses, | ||
excludeContracts: excludeContracts, | ||
walletAddresses: walletAddresses, | ||
excludeWallets: excludeWallets, | ||
}; | ||
|
||
const serializedRequest = getErc20TransfersOperation.serializeRequest(request, core); | ||
|
||
expect(serializedRequest.chain).toBe(chain); | ||
expect(serializedRequest.fromBlock).toBe(request.fromBlock); | ||
expect(serializedRequest.toBlock).toBe(request.toBlock); | ||
expect(serializedRequest.cursor).toBe(request.cursor); | ||
expect(serializedRequest.limit).toBe(request.limit); | ||
expect(serializedRequest.contractAddresses).toStrictEqual(request.contractAddresses); | ||
expect(serializedRequest.excludeContracts).toStrictEqual(request.excludeContracts); | ||
expect(serializedRequest.walletAddresses).toStrictEqual(request.walletAddresses); | ||
expect(serializedRequest.excludeWallets).toStrictEqual(request.excludeWallets); | ||
|
||
const deserializedRequest = getErc20TransfersOperation.deserializeRequest(serializedRequest, core); | ||
|
||
expect((deserializedRequest.chain as EvmChain).apiHex).toBe(chain); | ||
expect(deserializedRequest.fromBlock).toBe(request.fromBlock); | ||
expect(deserializedRequest.toBlock).toBe(request.toBlock); | ||
expect(deserializedRequest.cursor).toBe(request.cursor); | ||
expect(deserializedRequest.limit).toBe(request.limit); | ||
expect((deserializedRequest.contractAddresses as EvmAddress[]).map((address) => address.checksum)).toStrictEqual( | ||
request.contractAddresses, | ||
); | ||
expect((deserializedRequest.excludeContracts as EvmAddress[]).map((address) => address.checksum)).toStrictEqual( | ||
request.excludeContracts, | ||
); | ||
expect((deserializedRequest.walletAddresses as EvmAddress[]).map((address) => address.checksum)).toStrictEqual( | ||
request.walletAddresses, | ||
); | ||
expect((deserializedRequest.excludeWallets as EvmAddress[]).map((address) => address.checksum)).toStrictEqual( | ||
request.excludeWallets, | ||
); | ||
}); | ||
}); |
141 changes: 141 additions & 0 deletions
141
packages/common/evmUtils/src/operations/token/getErc20TransfersOperation.ts
This file contains 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,141 @@ | ||
import { | ||
Core, | ||
Camelize, | ||
PaginatedOperation, | ||
BigNumber, | ||
PaginatedResponseAdapter, | ||
maybe, | ||
toCamelCase, | ||
} from '@moralisweb3/common-core'; | ||
import { EvmChain, EvmChainish, EvmAddress, EvmAddressish, Erc20Transfer } from '../../dataTypes'; | ||
import { EvmChainResolver } from '../../EvmChainResolver'; | ||
import { operations } from '../openapi'; | ||
|
||
type OperationId = 'getErc20Transfers'; | ||
|
||
type QueryParams = operations[OperationId]['parameters']['query']; | ||
type RequestParams = QueryParams; | ||
|
||
// TODO: remove this overwrite when the swagger is fixxed | ||
// type SuccessResponse = operations[OperationId]['responses']['200']['content']['application/json']; | ||
type SuccessResponse = { | ||
cursor?: string | undefined; | ||
result: operations[OperationId]['responses']['200']['content']['application/json'][]; | ||
}; | ||
|
||
// Exports | ||
|
||
export interface GetErc20TransfersRequest | ||
extends Camelize< | ||
Omit<RequestParams, 'chain' | 'contract_addresses' | 'exclude_contracts' | 'wallet_addresses' | 'exclude_wallets'> | ||
> { | ||
chain?: EvmChainish; | ||
contractAddresses?: EvmAddressish[]; | ||
excludeContracts?: EvmAddressish[]; | ||
walletAddresses?: EvmAddressish[]; | ||
excludeWallets?: EvmAddressish[]; | ||
} | ||
|
||
export type GetErc20TransfersJSONRequest = ReturnType<typeof serializeRequest>; | ||
|
||
export type GetErc20TransfersJSONResponse = SuccessResponse; | ||
|
||
export type GetErc20TransfersResponse = ReturnType<typeof deserializeResponse>; | ||
|
||
export interface GetErc20TransfersResponseAdapter | ||
extends PaginatedResponseAdapter<GetErc20TransfersResponse, GetErc20TransfersJSONResponse["result"]> {} | ||
|
||
/** Get the amount which the spender is allowed to withdraw on behalf of the owner. */ | ||
export const getErc20TransfersOperation: PaginatedOperation< | ||
GetErc20TransfersRequest, | ||
GetErc20TransfersJSONRequest, | ||
GetErc20TransfersResponse, | ||
GetErc20TransfersJSONResponse['result'] | ||
> = { | ||
method: 'GET', | ||
name: 'getErc20Transfers', | ||
id: 'getErc20Transfers', | ||
groupName: 'token', | ||
urlPathPattern: '/erc20/transfers', | ||
urlSearchParamNames: [ | ||
'chain', | ||
'fromBlock', | ||
'toBlock', | ||
'limit', | ||
'contractAddresses', | ||
'excludeContracts', | ||
'walletAddresses', | ||
'excludeWallets', | ||
], | ||
firstPageIndex: 0, | ||
|
||
getRequestUrlParams, | ||
serializeRequest, | ||
deserializeRequest, | ||
deserializeResponse, | ||
}; | ||
|
||
// Methods | ||
|
||
function getRequestUrlParams(request: GetErc20TransfersRequest, core: Core) { | ||
return { | ||
chain: EvmChainResolver.resolve(request.chain, core).apiHex, | ||
from_block: maybe(request.fromBlock, String), | ||
to_block: maybe(request.toBlock, String), | ||
limit: maybe(request.limit, String), | ||
cursor: request.cursor, | ||
|
||
contract_addresses: request.contractAddresses?.map((address) => EvmAddress.create(address, core).lowercase), | ||
exclude_contracts: request.excludeContracts?.map((address) => EvmAddress.create(address, core).lowercase), | ||
wallet_addresses: request.walletAddresses?.map((address) => EvmAddress.create(address, core).lowercase), | ||
exclude_wallets: request.excludeWallets?.map((address) => EvmAddress.create(address, core).lowercase), | ||
}; | ||
} | ||
|
||
function deserializeResponse( | ||
jsonResponse: GetErc20TransfersJSONResponse, | ||
request: GetErc20TransfersRequest, | ||
core: Core, | ||
) { | ||
return (jsonResponse.result ?? []).map((transfer) => | ||
Erc20Transfer.create({ | ||
...toCamelCase(transfer), | ||
chain: EvmChainResolver.resolve(request.chain, core), | ||
address: EvmAddress.create(transfer.contract_address, core), | ||
toAddress: EvmAddress.create(transfer.to_wallet, core), | ||
fromAddress: EvmAddress.create(transfer.from_wallet, core), | ||
value: BigNumber.create(transfer.value), | ||
blockTimestamp: new Date(transfer.block_timestamp), | ||
}), | ||
); | ||
} | ||
|
||
function serializeRequest(request: GetErc20TransfersRequest, core: Core) { | ||
return { | ||
chain: EvmChainResolver.resolve(request.chain, core).apiHex, | ||
limit: request.limit, | ||
cursor: request.cursor, | ||
fromBlock: request.fromBlock, | ||
toBlock: request.toBlock, | ||
|
||
contractAddresses: request.contractAddresses?.map((address) => EvmAddress.create(address, core).lowercase), | ||
excludeContracts: request.excludeContracts?.map((address) => EvmAddress.create(address, core).lowercase), | ||
walletAddresses: request.walletAddresses?.map((address) => EvmAddress.create(address, core).lowercase), | ||
excludeWallets: request.excludeWallets?.map((address) => EvmAddress.create(address, core).lowercase), | ||
}; | ||
} | ||
|
||
function deserializeRequest(jsonRequest: GetErc20TransfersJSONRequest, core: Core): GetErc20TransfersRequest { | ||
return { | ||
chain: EvmChain.create(jsonRequest.chain, core), | ||
limit: jsonRequest.limit, | ||
cursor: jsonRequest.cursor, | ||
fromBlock: jsonRequest.fromBlock, | ||
toBlock: jsonRequest.toBlock, | ||
|
||
contractAddresses: jsonRequest.contractAddresses?.map((address) => EvmAddress.create(address, core)), | ||
excludeContracts: jsonRequest.excludeContracts?.map((address) => EvmAddress.create(address, core)), | ||
walletAddresses: jsonRequest.walletAddresses?.map((address) => EvmAddress.create(address, core)), | ||
excludeWallets: jsonRequest.excludeWallets?.map((address) => EvmAddress.create(address, core)), | ||
}; | ||
} |
This file contains 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
Oops, something went wrong.