diff --git a/README.md b/README.md index 61c4f92..f525458 100644 --- a/README.md +++ b/README.md @@ -84,6 +84,21 @@ const txResponse = await signer.sendTransaction({ }) ``` +## Getting transaction history with CeloscanProvider + +You can also rely on EthersProviders functionality, such as getting an account's transaction history, using our alternative CeloscanProvider + +```js +import { CeloscanProvider } from '@celo-tools/celo-ethers-wrapper' + +// You can use 'celo', 'alfajores' or 'baklava'. +// Default is 'celo' (mainnet) +const scanProvider = new CeloscanProvider('alfajores'); + +const history = await provider.getHistory(YOUR_ACCOUNT); +console.info("History:", history); +``` + ## Examples See the tests under `/test` for more detailed examples. diff --git a/package.json b/package.json index a52ffcf..e2800dd 100644 --- a/package.json +++ b/package.json @@ -18,7 +18,8 @@ "build:main": "tsc -p tsconfig.json", "build:module": "tsc -p tsconfig.module.json", "test:contract": "ts-node test/useContract.ts", - "test:deploy": "ts-node test/deployContract.ts" + "test:deploy": "ts-node test/deployContract.ts", + "test:history": "ts-node test/getHistory.ts" }, "engines": { "node": ">=10" diff --git a/src/index.ts b/src/index.ts index 5a2aab3..67a5427 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,4 +1,5 @@ export * from "./lib/CeloProvider"; export * from "./lib/CeloWallet"; export * from "./lib/StaticCeloProvider"; +export * from "./lib/CeloscanProvider"; export * from "./lib/transactions"; diff --git a/src/lib/CeloscanProvider.ts b/src/lib/CeloscanProvider.ts new file mode 100644 index 0000000..1ae03d5 --- /dev/null +++ b/src/lib/CeloscanProvider.ts @@ -0,0 +1,37 @@ +import { logger, providers, utils } from "ethers"; +import { getNetwork } from "./networks"; + +export class CeloscanProvider extends providers.EtherscanProvider { + constructor( + networkish: providers.Networkish = 'celo', + apiKey?: string + ) { + const network = getNetwork(networkish); + if (network == null) { + return logger.throwError( + `unknown network: ${JSON.stringify(network)}`, + utils.Logger.errors.UNSUPPORTED_OPERATION, + { + operation: 'getNetwork', + value: networkish, + }, + ); + } + super(network, apiKey); + } + + getBaseUrl(): string { + switch (this.network ? this.network.name : "invalid") { + case "celo": + return "https://api.celoscan.io"; + case "alfajores": + return "https://alfajores.celoscan.io"; + case "baklava": + // baklava is currently not supported by celoscan.io, so we use Blockscout + return "https://explorer.celo.org/baklava"; + default: + }; + + return logger.throwArgumentError("unsupported network", "network", this.network.name); + } +} diff --git a/test/getHistory.ts b/test/getHistory.ts new file mode 100644 index 0000000..bcea029 --- /dev/null +++ b/test/getHistory.ts @@ -0,0 +1,16 @@ +import { CeloscanProvider } from "../src/lib/CeloscanProvider"; + +async function main() { + const account = process.env.ACCOUNT; + if (!account) throw new Error("No ACCOUNT provided in env"); + const network = process.env.NETWORK?.toLocaleLowerCase() || "celo"; + console.info("Using CeloscanProvider with", network.toUpperCase(), "network"); + const provider = new CeloscanProvider(network); + const history = await provider.getHistory(account); + console.info("Account", account, "history:"); + console.info(history); +} + +main() + .then(() => console.info("Get history complete")) + .catch(console.error);