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 approve transaction #355

Merged
merged 3 commits into from
Mar 6, 2024
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
2 changes: 2 additions & 0 deletions config/consts/addresses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export const GRAVITY_BRIDGE_ETH_ADDRESS =
"0xa4108aA1Ec4967F8b52220a4f7e94A8201F2D906";
export const WETH_MAINNET_ADDRESS =
"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2";
export const USDT_ETH_MAINNET_ADDRESS =
"0xdac17f958d2ee523a2206206994597c13d831ec7";
export const PUB_KEY_BOT_ADDRESS =
"canto1efrhdukv096tmjs7r80m8pqkr3udp9g0uadjfv";

Expand Down
3 changes: 1 addition & 2 deletions transactions/erc20/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
} from "../interfaces";
import { MAX_UINT256 } from "@/config/consts/addresses";


export const _approveTx = (
chainId: number,
ethAccount: string,
Expand All @@ -23,6 +22,6 @@ export const _approveTx = (
target: tokenAddress,
abi: ERC20_ABI,
method: "approve",
params: [spender, MAX_UINT256],
params: [spender, amount === "0" ? "0" : MAX_UINT256],
value: "0",
});
23 changes: 22 additions & 1 deletion transactions/erc20/transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { NEW_ERROR, NO_ERROR, PromiseWithError } from "@/config/interfaces";
import { checkTokenAllowance } from "@/utils/tokens";
import { Transaction, TX_DESCRIPTIONS } from "../interfaces";
import { _approveTx } from ".";
import { ETH_MAINNET } from "@/config/networks";
import { USDT_ETH_MAINNET_ADDRESS } from "@/config/consts/addresses";
import { areEqualAddresses } from "@/utils/address";
import BigNumber from "bignumber.js";

/**
* @notice creates a transaction to approve a token
Expand Down Expand Up @@ -45,7 +49,24 @@ export async function createApprovalTxs(
}
// create tx for each token that needs approval
allowanceChecks.forEach((check, index) => {
if (!check.data) {
if (!check.data.hasEnoughAllowance) {
if (
chainId === ETH_MAINNET.chainId &&
areEqualAddresses(tokens[index].address, USDT_ETH_MAINNET_ADDRESS) &&
!BigNumber(check.data.allowance).isZero()
) {
// usdt requires approval to be reset to 0 before setting a new allowance
txList.push(
_approveTx(
chainId,
ethAccount,
tokens[index].address,
spender.address,
"0",
TX_DESCRIPTIONS.APPROVE_TOKEN(tokens[index].symbol, spender.name)
)
);
}
txList.push(
_approveTx(
chainId,
Expand Down
11 changes: 7 additions & 4 deletions utils/tokens/erc20.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ export async function checkTokenAllowance(
account: string,
spender: string,
amount: string
): PromiseWithError<boolean> {
): PromiseWithError<{ hasEnoughAllowance: boolean; allowance: string }> {
try {
const { data: tokenContract, error } = newContractInstance<
typeof ERC20_ABI
Expand All @@ -112,9 +112,12 @@ export async function checkTokenAllowance(
const allowance = await tokenContract.methods
.allowance(account, spender)
.call();
return NO_ERROR(
new BigNumber(allowance as string).isGreaterThanOrEqualTo(amount)
);
return NO_ERROR({
hasEnoughAllowance: new BigNumber(
allowance as string
).isGreaterThanOrEqualTo(amount),
allowance: allowance as string,
});
} catch (err) {
return NEW_ERROR("checkTokenAllowance", err);
}
Expand Down
Loading