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

Nft edge cases fix #29

Merged
merged 5 commits into from
Nov 24, 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
17 changes: 15 additions & 2 deletions src/AarcSDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
SingleTransferPermitDto,
TokenData,
TokenNftData,
TransferTokenDetails,
} from './utils/AarcTypes';
import { PERMIT2_BATCH_TRANSFER_ABI } from './utils/abis/Permit2BatchTransfer.abi';
import { PERMIT2_SINGLE_TRANSFER_ABI } from './utils/abis/Permit2SingleTransfer.abi';
Expand All @@ -32,7 +33,7 @@ import {
getGelatoTransactionStatus,
relayTransaction,
} from './helpers/GelatoHelper';
import { logError } from './helpers';
import { logError, removeDuplicateTokens } from './helpers';
import { calculateTotalGasNeeded } from './helpers/EstimatorHelper';
import { ChainId } from './utils/ChainTypes';

Expand Down Expand Up @@ -171,10 +172,15 @@ class AarcSDK {
message: 'Supplied token does not exist',
});
}
tandA.tokenAddress = tandA.tokenAddress.toLowerCase();
});

if (transferTokenDetails) {
const updatedTokens: TokenData[] = [];

const removeDuplicatesResult = removeDuplicateTokens(transferTokenDetails, response);
transferTokenDetails = removeDuplicatesResult.transferTokenDetails;

for (const tokenInfo of balancesList.data) {
const matchingToken = transferTokenDetails?.find(
(token) =>
Expand All @@ -185,6 +191,7 @@ class AarcSDK {
if (
matchingToken &&
matchingToken.amount !== undefined &&
matchingToken.tokenIds == undefined &&
BigNumber.from(matchingToken.amount).gt(0) &&
BigNumber.from(matchingToken.amount).gt(tokenInfo.balance)
) {
Expand Down Expand Up @@ -602,10 +609,10 @@ class AarcSDK {
try {
const {
senderSigner,
transferTokenDetails,
receiverAddress,
gelatoApiKey,
} = executeMigrationGaslessDto;
let { transferTokenDetails } = executeMigrationGaslessDto;
const owner = await senderSigner.getAddress();
const tokenAddresses = transferTokenDetails?.map(
(token) => token.tokenAddress,
Expand Down Expand Up @@ -642,10 +649,15 @@ class AarcSDK {
message: 'Supplied token does not exist',
});
}
tandA.tokenAddress = tandA.tokenAddress.toLowerCase();
});

if (transferTokenDetails) {
const updatedTokens: TokenData[] = [];

const removeDuplicatesResult = removeDuplicateTokens(transferTokenDetails, response);
transferTokenDetails = removeDuplicatesResult.transferTokenDetails;

for (const tokenInfo of balancesList.data) {
const matchingToken = transferTokenDetails?.find(
(token) =>
Expand All @@ -656,6 +668,7 @@ class AarcSDK {
if (
matchingToken &&
matchingToken.amount !== undefined &&
matchingToken.tokenIds == undefined &&
BigNumber.from(matchingToken.amount).gt(0) &&
BigNumber.from(matchingToken.amount).gt(tokenInfo.balance)
) {
Expand Down
27 changes: 27 additions & 0 deletions src/helpers/helper.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { MigrationResponse, TransferTokenDetails } from '../utils/AarcTypes';
import { Logger } from '../utils/Logger';

export const delay = (ms: number) => {
Expand All @@ -15,3 +16,29 @@ export const logError = (tokenInfo: any, error: any) => {
},
});
};

export const removeDuplicateTokens = (transferTokenDetails: TransferTokenDetails[], response: MigrationResponse[]): {transferTokenDetails: TransferTokenDetails[], response: MigrationResponse[]} => {
const transferTokenUniqueValues: TransferTokenDetails[] = transferTokenDetails.reduce((result: TransferTokenDetails[], current) => {
const matchingToken = result.find(item => item.tokenAddress === current.tokenAddress);
if (!matchingToken) {
result.push(current);
} else if (matchingToken && matchingToken.amount !== undefined && current.amount !== undefined){
response.push({
tokenAddress: current.tokenAddress,
amount: current?.amount,
message: 'Duplicate token address',
});
} else if (matchingToken && matchingToken.tokenIds !== undefined && current.tokenIds !== undefined){
for (const tokenId of current.tokenIds) {
response.push({
tokenAddress: current.tokenAddress,
tokenId: tokenId,
message: 'Duplicate token address',
});
}
}
return result;
}, []);

return {transferTokenDetails: transferTokenUniqueValues, response: response};
};
Loading
Loading