Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Token Approval Issue #1480

Merged
merged 1 commit into from
Aug 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading