-
Notifications
You must be signed in to change notification settings - Fork 6
/
XDaiWeb3Client.ts
56 lines (47 loc) · 1.73 KB
/
XDaiWeb3Client.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
import { $config } from '@dequanto/utils/$config';
import { TPlatform } from '@dequanto/models/TPlatform';
import { Web3Client } from '@dequanto/clients/Web3Client';
import { IWeb3EndpointOptions } from '@dequanto/clients/interfaces/IWeb3EndpointOptions';
import { ClientEndpoints } from '@dequanto/clients/utils/ClientEndpoints';
import memd from 'memd';
import { $bigint } from '@dequanto/utils/$bigint';
import { $http } from '@dequanto/utils/$http';
export class XDaiWeb3Client extends Web3Client {
platform: TPlatform = 'xdai'
chainId: number = this.options.chainId ?? 100
chainToken = 'XDAI';
defaultGasLimit = 500_000
defaultTxType: 1 | 2 = 1;
constructor (opts?: IWeb3EndpointOptions) {
super({
...(opts ?? {}),
endpoints: ClientEndpoints.filterEndpoints($config.get('web3.xdai.endpoints'), opts)
});
}
async getGasPrice() {
let gasPrice:bigint;
try {
gasPrice = await this.loadGasPrice();
} catch (err) {
let { price } = await super.getGasPrice();
gasPrice = price;
}
// MIN 20gwei, max: 80gwei
const MAX = $bigint.toWeiFromGwei(80);
const MIN = $bigint.toWeiFromGwei(20);
if (gasPrice < MIN) gasPrice = MIN;
if (gasPrice > MAX) gasPrice = MAX;
return {
price: gasPrice
};
}
@memd.deco.memoize({ maxAge: 10 })
private async loadGasPrice () {
let resp = await $http.get<{ average: number }>(`https://blockscout.com/xdai/mainnet/api/v1/gas-price-oracle`);
let avg = resp.data?.average;
if (avg) {
return $bigint.toWeiFromGwei(avg);
}
throw new Error(`Field is missing`);
}
}