This repository has been archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathethereum.ts
106 lines (91 loc) · 2.49 KB
/
ethereum.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import {
decodeAbiNumber,
decodeAbiString,
ERC20Client,
GethClient,
HttpClient,
IClientConfig,
IEthBlockSimple,
IEthBlockVerbose,
IMessage,
ParityClient,
} from "wallet-rpc";
{
// use default client: new HttpClient({ url: 'http://127.0.0.1:8545' })
const EthClient = new GethClient();
EthClient.client.setUrl("https://etherscan.io");
EthClient.getChainId();
}
{
const DefaultEthRpcConf: IClientConfig = {
url: "http://127.0.0.1:8545",
keepAlive: true,
timeout: 10 * 1000,
};
const client = new HttpClient(DefaultEthRpcConf);
const EthClient = new GethClient(client);
const Parity = new ParityClient(client);
const erc20 = new ERC20Client(EthClient);
const token = "0x1";
const address = "0x0";
async function exmaple() {
const txid =
"0xc4f1806717dd22e89d158ae1466f9ebc0124d2169843a7bcc65975c8a8327854";
// In production you should use Promise.all
await EthClient.getTrx(txid);
await EthClient.getTrxReceipt(txid);
// include all transaction detail
const blockVerbose: IMessage<IEthBlockVerbose> = await EthClient.getBlock(
100,
true,
);
// default and only txid returns
const blockSimple: IMessage<IEthBlockSimple> = await EthClient.getBlock(
1,
false,
);
console.log(blockVerbose, blockSimple);
await EthClient.getBalance(address, "latest");
await EthClient.getBalance(address, "pending");
await EthClient.getBalance(address, 100);
// Concurrency
await Promise.all([
EthClient.getBalance(address, "latest"),
EthClient.getBalance(address, "pending"),
]);
{
// get latest balanceOf ERC20 token
// Maybe throw
const balance: bigint = await erc20.balanceOf(token, address);
// or get pending
// await erc20.balanceOf(token, address, "pending");
console.log(balance);
}
// get name
{
const name: string = await erc20.name(token).catch(() => "Maybe throw");
console.log(name);
}
{
const symbol: string = await erc20
.symbol(token)
.catch(() => "Maybe throw");
console.log(symbol);
}
// get decimals
{
const decimals: bigint = await erc20.decimals(token);
console.log(decimals);
}
// get totalSupply
{
// Maybe throw
const totalSupply: bigint = await erc20.totalSupply(token);
console.log(totalSupply);
}
// parity trace transaction
Parity.traceTrx(txid);
Parity.traceBlock(100);
}
exmaple();
}