From d95b6f971e5b817508742a801fe8fad4064351ed Mon Sep 17 00:00:00 2001 From: anarkrypto Date: Mon, 20 Feb 2023 20:01:57 -0300 Subject: [PATCH 1/5] Add CeloscanProvider --- src/index.ts | 1 + src/lib/CeloscanProvider.ts | 36 ++++++++++++++++++++++++++++++++++++ test/getHistory.ts | 17 +++++++++++++++++ 3 files changed, 54 insertions(+) create mode 100644 src/lib/CeloscanProvider.ts create mode 100644 test/getHistory.ts 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..f6ca3a7 --- /dev/null +++ b/src/lib/CeloscanProvider.ts @@ -0,0 +1,36 @@ +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:/\/explorer.celo.org/mainnet"; + case "alfajores": + return "https:/\/explorer.celo.org/alfajores"; + case "baklava": + 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..5629f5b --- /dev/null +++ b/test/getHistory.ts @@ -0,0 +1,17 @@ +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"; + if (network !== "celo" && network !== "alfajores" && network !== "baklava") throw new Error("Invalid NETWORK provided in env. Use celo, alfajores, or baklava"); + console.info("Using CeloscanProvider with", network.toUpperCase(), "network"); + const provider = new CeloscanProvider(); + const history = await provider.getHistory(account); + console.info("Account", account, "history:"); + console.info(history); +} + +main() + .then(() => console.info("Get history complete")) + .catch(console.error); From 17d13a6b5e0612415e57c71efdf56c6c6b80a355 Mon Sep 17 00:00:00 2001 From: anarkrypto Date: Mon, 20 Feb 2023 20:02:53 -0300 Subject: [PATCH 2/5] Add unit test to CeloscanProvider (getHistory) --- package.json | 3 ++- test/getHistory.ts | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) 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/test/getHistory.ts b/test/getHistory.ts index 5629f5b..94ba6fc 100644 --- a/test/getHistory.ts +++ b/test/getHistory.ts @@ -6,7 +6,7 @@ async function main() { const network = process.env.NETWORK?.toLocaleLowerCase() || "celo"; if (network !== "celo" && network !== "alfajores" && network !== "baklava") throw new Error("Invalid NETWORK provided in env. Use celo, alfajores, or baklava"); console.info("Using CeloscanProvider with", network.toUpperCase(), "network"); - const provider = new CeloscanProvider(); + const provider = new CeloscanProvider(network); const history = await provider.getHistory(account); console.info("Account", account, "history:"); console.info(history); From fe3ff005fe64525f8e15e9b590528e57c07cb5d7 Mon Sep 17 00:00:00 2001 From: anarkrypto Date: Mon, 20 Feb 2023 20:13:28 -0300 Subject: [PATCH 3/5] Add CeloscanProvider example in README --- README.md | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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. From 449624a8b54cc4c3129fa6fbb6880fab6ecb5e78 Mon Sep 17 00:00:00 2001 From: anarkrypto Date: Wed, 22 Feb 2023 06:45:51 -0300 Subject: [PATCH 4/5] Remove env network check - getBaseUrl can handle it --- test/getHistory.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/test/getHistory.ts b/test/getHistory.ts index 94ba6fc..bcea029 100644 --- a/test/getHistory.ts +++ b/test/getHistory.ts @@ -4,7 +4,6 @@ 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"; - if (network !== "celo" && network !== "alfajores" && network !== "baklava") throw new Error("Invalid NETWORK provided in env. Use celo, alfajores, or baklava"); console.info("Using CeloscanProvider with", network.toUpperCase(), "network"); const provider = new CeloscanProvider(network); const history = await provider.getHistory(account); From a396fb2c08181c7c3cfc431cf212443918e2c28b Mon Sep 17 00:00:00 2001 From: anarkrypto Date: Wed, 22 Feb 2023 17:09:16 -0300 Subject: [PATCH 5/5] Use celoscan api in mainnet and alfajores - remove \ escape --- src/lib/CeloscanProvider.ts | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/lib/CeloscanProvider.ts b/src/lib/CeloscanProvider.ts index f6ca3a7..1ae03d5 100644 --- a/src/lib/CeloscanProvider.ts +++ b/src/lib/CeloscanProvider.ts @@ -23,11 +23,12 @@ export class CeloscanProvider extends providers.EtherscanProvider { getBaseUrl(): string { switch (this.network ? this.network.name : "invalid") { case "celo": - return "https:/\/explorer.celo.org/mainnet"; + return "https://api.celoscan.io"; case "alfajores": - return "https:/\/explorer.celo.org/alfajores"; + return "https://alfajores.celoscan.io"; case "baklava": - return "https:/\/explorer.celo.org/baklava"; + // baklava is currently not supported by celoscan.io, so we use Blockscout + return "https://explorer.celo.org/baklava"; default: };