Skip to content

Commit

Permalink
chore: expose get allowance method
Browse files Browse the repository at this point in the history
  • Loading branch information
leoslr committed Jul 5, 2023
1 parent e0e9151 commit 94be5b1
Showing 1 changed file with 26 additions and 4 deletions.
30 changes: 26 additions & 4 deletions packages/payment-processor/src/payment/erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,12 +86,30 @@ export async function hasErc20Approval(
);
}

/**
* Retrieves the allowance given by an ERC20 token owner to a spender.
* @param ownerAddress address of the owner
* @param spenderAddress address of the spender
* @param signerOprovider the web3 provider
* @param tokenAddress Address of the ERC20 token
*/
export async function getErc20Allowance(
ownerAddress: string,
spenderAddress: string,
signerOrProvider: providers.Provider | Signer,
tokenAddress: string,
): Promise<BigNumber> {
const erc20Contract = ERC20__factory.connect(tokenAddress, signerOrProvider);
return await erc20Contract.allowance(ownerAddress, spenderAddress);
}

/**
* Checks if a spender has enough allowance from an ERC20 token owner to pay an amount.
* @param ownerAddress address of the owner
* @param spenderAddress address of the spender
* @param provider the web3 provider. Defaults to Etherscan.
* @param paymentCurrency ERC20 currency
* @param signerOrProvider the web3 provider.
* @param tokenAddress Address of the ERC20 currency
* @param amount The amount to check the allowance for
*/
export async function checkErc20Allowance(
ownerAddress: string,
Expand All @@ -100,8 +118,12 @@ export async function checkErc20Allowance(
tokenAddress: string,
amount: BigNumberish,
): Promise<boolean> {
const erc20Contract = ERC20__factory.connect(tokenAddress, signerOrProvider);
const allowance = await erc20Contract.allowance(ownerAddress, spenderAddress);
const allowance = await getErc20Allowance(
ownerAddress,
spenderAddress,
signerOrProvider,
tokenAddress,
);
return allowance.gte(amount);
}

Expand Down

0 comments on commit 94be5b1

Please sign in to comment.