-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.ts
60 lines (45 loc) · 1.67 KB
/
main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import BN from 'bn.js';
import { Query, queryProviders, Contract, toHex, TQuantity, randomAddress } from 'eth-sdk';
import fetch from 'node-fetch';
const abiItems = require('./abi.json');
interface IContractMethods {
balanceOf(address: string): Contract.IMethodExecute<{
balance: BN
}>;
transfer(to: string, value: TQuantity): Contract.IMethodExecute;
}
interface IContractEvents {
Transfer: {
from: string;
to: string;
value: BN;
}
}
async function main(): Promise<void> {
const provider = new queryProviders
.HttpProvider('https://mainnet.infura.io/', {
fetch,
});
const query = new Query(provider);
// see: https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7
const contract = new Contract<IContractMethods, IContractEvents>(abiItems, '0xdac17f958d2ee523a2206206994597c13d831ec7', query);
const holder = '0xbe0eb53f46cd790cd13851d5eff43d12404d33e8'; // some random token holder
console.log('contract.address:', contract.address);
const balanceOf = contract.methods.balanceOf(holder);
const output = await balanceOf.call();
console.log('methods.balanceOf.data:', balanceOf.data);
console.log('methods.balanceOf.output.balance:', toHex(output.balance));
const transfer = contract.methods.transfer(randomAddress(), 100);
const gas = await transfer.estimate({
from: holder,
});
console.log('methods.transfer.data:', transfer.data);
console.log('methods.transfer.estimate:', gas);
const eventLogs = await contract.events.Transfer.getLogs({
fromBlock: 8798207,
toBlock: 8798207,
});
console.log('events.Transfer.getLogs[0]:', JSON.stringify(eventLogs[0], null, 2));
}
main()
.catch(console.log);