Skip to content

Commit

Permalink
Add Ethers-v4 example
Browse files Browse the repository at this point in the history
  • Loading branch information
krzkaczor committed Apr 28, 2020
1 parent deec5f0 commit eb79866
Show file tree
Hide file tree
Showing 9 changed files with 1,814 additions and 0 deletions.
11 changes: 11 additions & 0 deletions examples/ethers-v4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# TypeChain x Web3 v1 example

## Running

```sh
yarn # it will automatically run TypeChain types generation

# yarn generate-types to manually regenerate them

yarn start
```
277 changes: 277 additions & 0 deletions examples/ethers-v4/abi/dai.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
[

{
"inputs": [{ "internalType": "uint256", "name": "chainId_", "type": "uint256" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "internalType": "address", "name": "src", "type": "address" },
{ "indexed": true, "internalType": "address", "name": "guy", "type": "address" },
{ "indexed": false, "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "Approval",
"type": "event"
},
{
"anonymous": true,
"inputs": [
{ "indexed": true, "internalType": "bytes4", "name": "sig", "type": "bytes4" },
{ "indexed": true, "internalType": "address", "name": "usr", "type": "address" },
{ "indexed": true, "internalType": "bytes32", "name": "arg1", "type": "bytes32" },
{ "indexed": true, "internalType": "bytes32", "name": "arg2", "type": "bytes32" },
{ "indexed": false, "internalType": "bytes", "name": "data", "type": "bytes" }
],
"name": "LogNote",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{ "indexed": true, "internalType": "address", "name": "src", "type": "address" },
{ "indexed": true, "internalType": "address", "name": "dst", "type": "address" },
{ "indexed": false, "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "Transfer",
"type": "event"
},
{
"constant": true,
"inputs": [],
"name": "DOMAIN_SEPARATOR",
"outputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "PERMIT_TYPEHASH",
"outputs": [{ "internalType": "bytes32", "name": "", "type": "bytes32" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [
{ "internalType": "address", "name": "", "type": "address" },
{ "internalType": "address", "name": "", "type": "address" }
],
"name": "allowance",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "approve",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [{ "internalType": "address", "name": "", "type": "address" }],
"name": "balanceOf",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "burn",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "decimals",
"outputs": [{ "internalType": "uint8", "name": "", "type": "uint8" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [{ "internalType": "address", "name": "guy", "type": "address" }],
"name": "deny",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "mint",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "src", "type": "address" },
{ "internalType": "address", "name": "dst", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "move",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "name",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "internalType": "address", "name": "", "type": "address" }],
"name": "nonces",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "holder", "type": "address" },
{ "internalType": "address", "name": "spender", "type": "address" },
{ "internalType": "uint256", "name": "nonce", "type": "uint256" },
{ "internalType": "uint256", "name": "expiry", "type": "uint256" },
{ "internalType": "bool", "name": "allowed", "type": "bool" },
{ "internalType": "uint8", "name": "v", "type": "uint8" },
{ "internalType": "bytes32", "name": "r", "type": "bytes32" },
{ "internalType": "bytes32", "name": "s", "type": "bytes32" }
],
"name": "permit",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "pull",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "usr", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "push",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [{ "internalType": "address", "name": "guy", "type": "address" }],
"name": "rely",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "symbol",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "totalSupply",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "dst", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "transfer",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{ "internalType": "address", "name": "src", "type": "address" },
{ "internalType": "address", "name": "dst", "type": "address" },
{ "internalType": "uint256", "name": "wad", "type": "uint256" }
],
"name": "transferFrom",
"outputs": [{ "internalType": "bool", "name": "", "type": "bool" }],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "version",
"outputs": [{ "internalType": "string", "name": "", "type": "string" }],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [{ "internalType": "address", "name": "", "type": "address" }],
"name": "wards",
"outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
22 changes: 22 additions & 0 deletions examples/ethers-v4/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"name": "web3-v1",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"generate-types": "typechain --target=ethers-v4 'abi/*.json'",
"postinstall": "yarn generate-types",
"start": "ts-node ./src/index.ts",
"typecheck": "tsc --noEmit"
},
"devDependencies": {
"@typechain/ethers-v4": "^1.0.0-beta",
"@types/bn.js": "^4.11.6",
"ts-node": "^8.8.2",
"typechain": "^2.0.0-beta1",
"typescript": "^3.8.3"
},
"dependencies": {
"ethers": "^4.0.47"
}
}
24 changes: 24 additions & 0 deletions examples/ethers-v4/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import * as ethers from 'ethers'
import { Dai } from '../types/ethers-contracts/Dai'

const abi = require('../abi/dai.json')

const RPC_HOST = 'https://mainnet.infura.io/v3/6d6c70e65c77429482df5b64a4d0c943'
const DAI_ADDRESS = '0x6B175474E89094C44Da98b954EedeAC495271d0F'

async function main() {
const provider = new ethers.providers.JsonRpcProvider(RPC_HOST)
const formatEther = ethers.utils.formatEther

const dai = (new ethers.Contract(DAI_ADDRESS, abi, provider) as any) as Dai
const balance = await dai.functions.balanceOf('0x70b144972C5Ef6CB941A5379240B74239c418CD4')

console.log(`Our DAI balance is: ${formatEther(balance)}`)

dai.interface.events.Transfer
}

main().catch(e => {
console.error(e)
process.exit(1)
})
11 changes: 11 additions & 0 deletions examples/ethers-v4/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"lib": ["ES2018", "DOM"],
"module": "CommonJS",
"moduleResolution": "node",
"strict": true,
"target": "ES2018",
"sourceMap": true,
"esModuleInterop": true
}
}
Loading

0 comments on commit eb79866

Please sign in to comment.