Skip to content

Commit

Permalink
Merge pull request #8 from ecobioca/CeloscanProvider
Browse files Browse the repository at this point in the history
Add CeloscanProvider
  • Loading branch information
jmrossy authored Feb 22, 2023
2 parents f4163a5 + a396fb2 commit 3268d82
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 1 deletion.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./lib/CeloProvider";
export * from "./lib/CeloWallet";
export * from "./lib/StaticCeloProvider";
export * from "./lib/CeloscanProvider";
export * from "./lib/transactions";
37 changes: 37 additions & 0 deletions src/lib/CeloscanProvider.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
16 changes: 16 additions & 0 deletions test/getHistory.ts
Original file line number Diff line number Diff line change
@@ -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);

0 comments on commit 3268d82

Please sign in to comment.