|
| 1 | +import { Overrides } from "ethers"; |
| 2 | +import { Provider } from "@ethersproject/providers"; |
| 3 | +import { BigNumberish, BigNumber } from "@ethersproject/bignumber"; |
| 4 | +import { CallOverrides, PopulatedTransaction } from "@ethersproject/contracts"; |
| 5 | + |
| 6 | +import { ERC20 } from "../typechain-types/@openzeppelin/contracts/token/ERC20"; |
| 7 | +import { ERC20__factory } from "../typechain-types/factories/@openzeppelin/contracts/token/ERC20/ERC20__factory"; |
| 8 | +import { PromiseOrValue } from "../typechain-types/common"; |
| 9 | + |
| 10 | +export class ERC20Client { |
| 11 | + private readonly contract: ERC20; |
| 12 | + |
| 13 | + constructor(contractAddress: string) { |
| 14 | + const factory = new ERC20__factory(); |
| 15 | + this.contract = factory.attach(contractAddress); |
| 16 | + } |
| 17 | + |
| 18 | + /** |
| 19 | + * @returns a promise that resolves with a BigNumber that represents the amount of tokens in existence |
| 20 | + */ |
| 21 | + public async totalSupply(provider: Provider, overrides: CallOverrides = {}): Promise<BigNumber> { |
| 22 | + return this.contract.connect(provider).totalSupply(overrides); |
| 23 | + } |
| 24 | + |
| 25 | + /** |
| 26 | + * @returns a promise that resolves with a BigNumber that represents the amount of tokens owned by account |
| 27 | + */ |
| 28 | + public async balanceOf( |
| 29 | + provider: Provider, |
| 30 | + account: PromiseOrValue<string>, |
| 31 | + overrides: CallOverrides = {} |
| 32 | + ): Promise<BigNumber> { |
| 33 | + return this.contract.connect(provider).balanceOf(account, overrides); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @returns a promise that resolves with a BigNumber that represents the remaining number of tokens that spender will be allowed to spend on behalf of owner through transferFrom |
| 38 | + */ |
| 39 | + public async allowance( |
| 40 | + provider: Provider, |
| 41 | + owner: PromiseOrValue<string>, |
| 42 | + spender: PromiseOrValue<string>, |
| 43 | + overrides: CallOverrides = {} |
| 44 | + ): Promise<BigNumber> { |
| 45 | + return this.contract.connect(provider).allowance(owner, spender, overrides); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @returns a promise that resolves with a populated transaction |
| 50 | + */ |
| 51 | + public async populateTransfer( |
| 52 | + to: PromiseOrValue<string>, |
| 53 | + amount: PromiseOrValue<BigNumberish>, |
| 54 | + overrides?: Overrides & { from?: PromiseOrValue<string> } |
| 55 | + ): Promise<PopulatedTransaction> { |
| 56 | + return this.contract.populateTransaction.transfer(to, amount, overrides); |
| 57 | + } |
| 58 | + |
| 59 | + /** |
| 60 | + * @returns a promise that resolves with a populated transaction |
| 61 | + */ |
| 62 | + public async populateApprove( |
| 63 | + spender: PromiseOrValue<string>, |
| 64 | + amount: PromiseOrValue<BigNumberish>, |
| 65 | + overrides: Overrides & { from?: PromiseOrValue<string> } = {} |
| 66 | + ): Promise<PopulatedTransaction> { |
| 67 | + return this.contract.populateTransaction.approve(spender, amount, overrides); |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @returns a promise that resolves with a populated transaction |
| 72 | + */ |
| 73 | + public async populateTransferFrom( |
| 74 | + from: PromiseOrValue<string>, |
| 75 | + to: PromiseOrValue<string>, |
| 76 | + amount: PromiseOrValue<BigNumberish>, |
| 77 | + overrides: Overrides & { from?: PromiseOrValue<string> } = {} |
| 78 | + ): Promise<PopulatedTransaction> { |
| 79 | + return this.contract.populateTransaction.transferFrom(from, to, amount, overrides); |
| 80 | + } |
| 81 | +} |
0 commit comments