-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsb-manager.service.ts
67 lines (58 loc) · 1.71 KB
/
csb-manager.service.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
import { Inject, Injectable, Logger } from '@nestjs/common';
import { BigNumber, ethers } from 'ethers';
import { CrossbellContractService } from '@/module/contract/contract.service';
import { CsbManagerOptions, CSB_MANAGER_OPTIONS } from './csb-manager.type';
import retry from 'async-retry';
@Injectable()
export class CsbManagerService {
private readonly logger = new Logger(CsbManagerService.name);
private options: CsbManagerOptions;
constructor(
@Inject(CSB_MANAGER_OPTIONS) private _options: Partial<CsbManagerOptions>,
private readonly contractService: CrossbellContractService,
) {
this.options = {
defaultCsb: '0.02',
...this._options,
};
}
private contract = this.contractService.createContractV1('readonly');
get publicClient() {
return this.contract.publicClient;
}
/**
* @returns 0.02 CSB in gwei string
*/
getDefaultCsb(): string {
return ethers.utils.parseUnits(this.options.defaultCsb, 'ether').toString();
}
/**
* In gwei
*/
async getGasOfTx(txHash: string): Promise<string> {
return await retry(
async () => {
const [gasPrice, txReceipt] = await Promise.all([
this.publicClient.getGasPrice(),
this.publicClient.getTransactionReceipt({
hash: txHash as `0x${string}`,
}),
]);
return (txReceipt.gasUsed * gasPrice).toString();
},
{ retries: 5 },
);
}
/**
* In gwei
*/
subtractCsb(csb: string, amount: string): string {
return BigNumber.from(csb).sub(BigNumber.from(amount)).toString();
}
/**
* In gwei
*/
addCsb(csb: string, amount: string): string {
return BigNumber.from(csb).add(BigNumber.from(amount)).toString();
}
}