-
Notifications
You must be signed in to change notification settings - Fork 42
/
assert-has-balance.tsx
38 lines (33 loc) · 1.06 KB
/
assert-has-balance.tsx
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
import { CeloTokenContract, ContractKit } from '@celo/contractkit';
import { MiniContractKit } from '@celo/contractkit/lib/mini-kit';
import Web3 from 'web3';
import { feeTokenMap } from '../../utils';
export async function assertHasBalance(
address: string,
kit: ContractKit | MiniContractKit,
feeCurrency: CeloTokenContract
): Promise<void> {
let convertedBalance;
try {
const totalBalance = await kit.getTotalBalance(address);
const token = feeTokenMap[feeCurrency];
const tokenBalance = totalBalance[token];
if (!tokenBalance) {
throw new Error(`Balance did not include any ${token}`);
}
convertedBalance = Number(Web3.utils.fromWei(tokenBalance.toFixed()));
} catch (error) {
let message;
if (error instanceof Error) {
message = error.message;
} else {
message = JSON.stringify(error);
}
throw new Error(`Got an error when trying to check balance: ${message}`);
}
if (convertedBalance < 0.1) {
throw new Error(
'Your wallet does not have enough funds for the transaction'
);
}
}