Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Track blocks and relevant txs + account history
Closes #126
- Loading branch information
Showing
7 changed files
with
507 additions
and
72 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
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,82 @@ | ||
import { | ||
AlchemyProvider, | ||
AlchemyWebSocketProvider, | ||
} from "@ethersproject/providers" | ||
import { BigNumber, utils } from "ethers" | ||
|
||
export interface AlchemyAssetTransfer { | ||
hash: string | ||
blockHeight: number | ||
category: "token" | "internal" | "external" | ||
from: string | null | ||
to: string | null | ||
rawContract?: { | ||
address: string | ||
decimals: number | ||
value: BigInt | ||
} | ||
value: number | ||
erc721TokenId: string | null | ||
} | ||
|
||
/* | ||
* Use Alchemy's getAssetTransfers call to get historical transfers for an | ||
* account. | ||
* | ||
* Note that pagination isn't supported, so any responses after 1k transfers | ||
* will be dropped. | ||
* | ||
* More information https://docs.alchemy.com/alchemy/documentation/apis/enhanced-apis/transfers-api#alchemy_getassettransfers | ||
* @param provider - an Alchemy ethers provider | ||
* @param account - the account whose transfer history we're fetching | ||
* @param fromBlock - the block height specifying how far in the past we want | ||
* to look. | ||
*/ | ||
export async function getAssetTransfers( | ||
provider: AlchemyProvider | AlchemyWebSocketProvider, | ||
account: string, | ||
fromBlock: number | ||
): Promise<AlchemyAssetTransfer[]> { | ||
const params = { | ||
fromBlock: utils.hexValue(fromBlock), | ||
toBlock: "latest", | ||
// excludeZeroValue: false, | ||
} | ||
const rpcResponses = await Promise.all([ | ||
provider.send("alchemy_getAssetTransfers", [ | ||
{ | ||
...params, | ||
fromAddress: account, | ||
}, | ||
]), | ||
provider.send("alchemy_getAssetTransfers", [ | ||
{ | ||
...params, | ||
toAddress: account, | ||
}, | ||
]), | ||
]) | ||
|
||
return rpcResponses[0].transfers | ||
.concat(rpcResponses[1].transfers) | ||
.map((json) => { | ||
const formattedTransfer: AlchemyAssetTransfer = { | ||
hash: json.hash, | ||
blockHeight: BigNumber.from(json.blockNum).toNumber(), | ||
category: json.category, | ||
from: json.from, | ||
to: json.to, | ||
value: json.value, | ||
erc721TokenId: json.erc721TokenId, | ||
} | ||
// TODO parse rawContract properly | ||
if (json.rawContract) { | ||
formattedTransfer.rawContract = { | ||
address: json.rawContract.address, | ||
value: BigNumber.from(json.rawContract.value).toBigInt(), | ||
decimals: Number(json.rawContract.decimal), | ||
} | ||
} | ||
return formattedTransfer | ||
}) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,19 @@ | ||
import PreferenceService from "../preferences/service" | ||
import ChainService from "./service" | ||
|
||
export { default as ChainService } from "./service" | ||
|
||
const SCHEDULES = { | ||
queuedTransactions: { | ||
delayInMinutes: 1, | ||
periodInMinutes: 5, | ||
}, | ||
} | ||
|
||
export async function startService( | ||
preferenceService: Promise<PreferenceService> | ||
): Promise<ChainService> { | ||
const service = new ChainService(preferenceService) | ||
const service = new ChainService(SCHEDULES, preferenceService) | ||
await service.startService() | ||
return service | ||
} |
Oops, something went wrong.