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

feat: custom approval amounts for ERC20 tokens in V3 #110

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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 src/sdk/v3/INftSwapV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import type { TransactionReceipt } from '@ethersproject/providers';
import type { ContractTransaction } from '@ethersproject/contracts';
import type { Signer } from '@ethersproject/abstract-signer';
import type {
BigNumberish,
Order,
OrderInfoV3,
OrderStatusV3,
Expand Down Expand Up @@ -119,6 +120,7 @@ export interface ApprovalOverrides {
exchangeProxyContractAddressForAsset: string;
chainId: number;
gasAmountBufferMultiple: number | null;
approvalAmount: BigNumberish
}

export interface FillOrderOverrides {
Expand Down
6 changes: 4 additions & 2 deletions src/sdk/v3/NftSwapV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,8 @@ class NftSwapV3 implements INftSwapV3 {

public loadApprovalStatus = async (
asset: SwappableAsset,
walletAddress: string
walletAddress: string,
approvalOverrides?: Partial<ApprovalOverrides> | undefined
) => {
// TODO(johnrjj) - Fix this...
const exchangeProxyAddressForAsset = getProxyAddressForErcType(
Expand All @@ -297,7 +298,8 @@ class NftSwapV3 implements INftSwapV3 {
walletAddress,
exchangeProxyAddressForAsset,
assetInternalFmt,
this.provider
this.provider,
approvalOverrides?.approvalAmount
);
};

Expand Down
21 changes: 11 additions & 10 deletions src/sdk/v3/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from '@ethersproject/bytes';
import { verifyTypedData } from '@ethersproject/wallet';
import { _TypedDataEncoder } from '@ethersproject/hash';
import { BigNumber } from '@ethersproject/bignumber';
import { BigNumber, BigNumberish } from '@ethersproject/bignumber';
import { Interface } from '@ethersproject/abi';
import type { Signer, TypedDataSigner } from '@ethersproject/abstract-signer';
import {
Expand Down Expand Up @@ -420,7 +420,8 @@ export const getApprovalStatus = async (
walletAddress: string,
exchangeProxyAddressForAsset: string,
asset: InterallySupportedAssetFormat,
provider: BaseProvider
provider: BaseProvider,
approvalAmount: BigNumberish = MAX_APPROVAL_WITH_BUFFER
): Promise<ApprovalStatus> => {
switch (asset.type) {
case 'ERC20':
Expand All @@ -429,15 +430,10 @@ export const getApprovalStatus = async (
walletAddress,
exchangeProxyAddressForAsset
);
// Weird issue with BigNumber and approvals...need to look into it, adding buffer.
const MAX_APPROVAL_WITH_BUFFER = BigNumber.from(
MAX_APPROVAL.toString()
).sub('100000000000000000');
const approvedForMax = erc20AllowanceBigNumber.gte(
MAX_APPROVAL_WITH_BUFFER
);

const hasEnoughApproval = erc20AllowanceBigNumber.gte(approvalAmount);
return {
contractApproved: approvedForMax,
contractApproved: hasEnoughApproval,
};
case 'ERC721':
const erc721 = ERC721__factory.connect(asset.tokenAddress, provider);
Expand Down Expand Up @@ -478,6 +474,11 @@ export const getApprovalStatus = async (
// TODO(johnrjj) - Support custom ERC20 approval amounts
export const MAX_APPROVAL = BigNumber.from(2).pow(118);

// Weird issue with BigNumber and approvals...need to look into it, adding buffer.
const MAX_APPROVAL_WITH_BUFFER = BigNumber.from(MAX_APPROVAL.toString()).sub(
'100000000000000000'
);

/**
* @param exchangeProxyAddressForAsset Exchange Proxy address specific to the ERC type (e.g. use the 0x ERC721 Proxy if you're using a 721 asset). This is the address that will need approval & does the spending/swap.
* @param asset
Expand Down