Skip to content

Commit

Permalink
fix: should check and approve the fungible token when wrapping
Browse files Browse the repository at this point in the history
  • Loading branch information
AtelyPham committed Jul 31, 2023
1 parent 7d6e594 commit ad56b05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
7 changes: 6 additions & 1 deletion libs/web3-api-provider/src/VAnchor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,14 +492,19 @@ class VAnchor {
account: Account
) {
const userAddress = account.address;

const tokenInstance = getContract({
address: tokenAddress,
abi: ERC20__factory.abi,
publicClient: this.publicClient,
});

// When wrapping, we need to check allowance of the fungible token
// as the fungible token is the contract that transfers the token
// from the user to the contract
const tokenAllowance = await tokenInstance.read.allowance([
userAddress,
this.contract.address,
this.fungibleToken,
]);

if (tokenAllowance < depositAmount) {
Expand Down
16 changes: 9 additions & 7 deletions libs/web3-api-provider/src/webb-provider/vanchor-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -879,8 +879,7 @@ export class Web3VAnchorActions extends VAnchorActions<
this.inner.publicClient
);

const spenderAddress = srcVAnchor.contract.address;
const currentWebbToken = srcVAnchor.getWebbToken();
const currentFungibleToken = srcVAnchor.getWebbToken();

const amountBI = BigInt(amount);

Expand All @@ -890,7 +889,7 @@ export class Web3VAnchorActions extends VAnchorActions<

// If the `wrapUnwrapToken` is different from the `currentWebbToken` address,
// we are wrapping / unwrapping. otherwise, we are depositing / withdrawing.
const isWrapOrUnwrap = wrapUnwrapToken !== currentWebbToken.address;
const isWrapOrUnwrap = wrapUnwrapToken !== currentFungibleToken.address;

// Only non-native tokens require approval
if (!isNative) {
Expand Down Expand Up @@ -920,8 +919,10 @@ export class Web3VAnchorActions extends VAnchorActions<
publicClient: this.inner.publicClient,
});

// On the wrap case, we need to approve the tokenWrapper contract
// to spend the token on behalf of the user to wrap it.
const { request } = await tokenContract.simulate.approve(
[spenderAddress, approvalValue],
[currentFungibleToken.address, approvalValue],
{
gas: BigInt('0x5B8D80'),
account,
Expand All @@ -930,9 +931,10 @@ export class Web3VAnchorActions extends VAnchorActions<

approvalHash = await this.inner.walletClient.writeContract(request);
} else {
// approve the token
const { request } = await currentWebbToken.simulate.approve(
[spenderAddress, amountBI],
// If we are depositing (without wrapping), we need to approve the
// VAnchor contract to spend the user's fungible token.
const { request } = await currentFungibleToken.simulate.approve(
[srcVAnchor.contract.address, amountBI],
{
gas: BigInt('0x5B8D80'),
account,
Expand Down

0 comments on commit ad56b05

Please sign in to comment.