diff --git a/source/balances/package.json b/source/balances/package.json index b440c4129..2801dbf4b 100644 --- a/source/balances/package.json +++ b/source/balances/package.json @@ -29,7 +29,7 @@ }, "devDependencies": { "@airswap/constants": "^4.1.0", - "@airswap/utils": "^4.1.2", + "@airswap/utils": "^4.1.3", "prompt-confirm": "^2.0.4" }, "publishConfig": { diff --git a/source/pool/package.json b/source/pool/package.json index 5c6cc7419..e3f46444c 100644 --- a/source/pool/package.json +++ b/source/pool/package.json @@ -30,7 +30,7 @@ "devDependencies": { "@airswap/constants": "^4.1.0", "@airswap/types": "^4.1.1", - "@airswap/utils": "^4.1.2", + "@airswap/utils": "^4.1.3", "prompt-confirm": "^2.0.4" }, "publishConfig": { diff --git a/source/registry/package.json b/source/registry/package.json index c665a2186..c2d32f333 100644 --- a/source/registry/package.json +++ b/source/registry/package.json @@ -32,7 +32,7 @@ }, "devDependencies": { "@airswap/constants": "^4.1.0", - "@airswap/utils": "^4.1.2", + "@airswap/utils": "^4.1.3", "prompt-confirm": "^2.0.4" } } diff --git a/source/staking/package.json b/source/staking/package.json index bc2b8892d..d6aa89f0e 100644 --- a/source/staking/package.json +++ b/source/staking/package.json @@ -31,7 +31,7 @@ "access": "public" }, "devDependencies": { - "@airswap/utils": "^4.1.2", + "@airswap/utils": "^4.1.3", "prompt-confirm": "^2.0.4" } } diff --git a/source/swap-erc20/events.ts b/source/swap-erc20/events.ts deleted file mode 100644 index 5552322c9..000000000 --- a/source/swap-erc20/events.ts +++ /dev/null @@ -1,78 +0,0 @@ -import { ethers } from 'ethers' -import { SwapERC20, FullSwapERC20 } from '@airswap/types' -import { SwapERC20__factory } from '@airswap/swap-erc20/typechain/factories/contracts' -import { abi as ERC20_ABI } from '@openzeppelin/contracts/build/contracts/ERC20.json' - -const swapInterface = new ethers.utils.Interface(SwapERC20__factory.abi) -const erc20Interface = new ethers.utils.Interface(ERC20_ABI) - -const parseTransfer = (log: any) => { - let parsed - let transfer - try { - parsed = erc20Interface.parseLog(log) - if (parsed.name === 'Transfer') - transfer = { - token: log.address, - from: parsed.args[0], - to: parsed.args[1], - amount: ethers.BigNumber.from(parsed.args[2]), - } - } catch (e) { - return null - } - return transfer -} - -export const getFullSwapERC20 = async ( - event: SwapERC20, - tx: ethers.providers.TransactionResponse -): Promise => { - const receipt = await tx.wait() - const transfers: any = [] - for (let i = 0; i < receipt.logs.length; i++) { - let parsed: ethers.utils.LogDescription - try { - parsed = swapInterface.parseLog(receipt.logs[i]) - } catch (e) { - continue - } - if (parsed && parsed.name === 'SwapERC20') { - let transfer: any - while (i--) { - if ((transfer = parseTransfer(receipt.logs[i]))) { - transfers.push(transfer) - } - } - break - } - } - - const [fee, signer, sender] = transfers - if (fee.from !== event.signerWallet) - throw new Error( - 'unable to get SwapERC20 params: found incorrect fee transfer (wrong signerWallet)' - ) - if (signer.from !== event.signerWallet) - throw new Error( - 'unable to get SwapERC20 params: found incorrect signer transfer (wrong signerWallet)' - ) - if (signer.from !== sender.to) - throw new Error( - 'unable to get SwapERC20 params: signer transfer mismatched sender transfer' - ) - if (sender.from !== signer.to) - throw new Error( - 'unable to get SwapERC20 params: sender transfer mismatched signer transfer' - ) - - return { - ...event, - signerToken: signer.token, - signerAmount: signer.amount.toString(), - senderWallet: sender.from, - senderToken: sender.token, - senderAmount: sender.amount.toString(), - feeAmount: fee.amount.toString(), - } -} diff --git a/source/swap-erc20/package.json b/source/swap-erc20/package.json index 362247107..f1940b4e8 100644 --- a/source/swap-erc20/package.json +++ b/source/swap-erc20/package.json @@ -1,6 +1,6 @@ { "name": "@airswap/swap-erc20", - "version": "4.1.1", + "version": "4.1.2", "description": "AirSwap: Atomic ERC20 Token Swap", "license": "MIT", "repository": { @@ -31,7 +31,7 @@ "@airswap/constants": "^4.1.0", "@airswap/staking": "4.0.3", "@airswap/types": "^4.1.1", - "@airswap/utils": "^4.1.2", + "@airswap/utils": "^4.1.3", "prompt-confirm": "^2.0.4" }, "publishConfig": { diff --git a/source/swap-erc20/tsconfig.json b/source/swap-erc20/tsconfig.json index 9a33095c4..ce170734c 100644 --- a/source/swap-erc20/tsconfig.json +++ b/source/swap-erc20/tsconfig.json @@ -3,5 +3,5 @@ "compilerOptions": { "outDir": "./typechain" }, - "files": ["./typechain/index.ts", "./events.ts"] + "files": ["./typechain/index.ts"] } diff --git a/source/swap/package.json b/source/swap/package.json index f97312bdc..b92f7e9c0 100644 --- a/source/swap/package.json +++ b/source/swap/package.json @@ -34,7 +34,7 @@ "devDependencies": { "@airswap/constants": "^4.1.0", "@airswap/types": "^4.1.1", - "@airswap/utils": "^4.1.2", + "@airswap/utils": "^4.1.3", "@nomicfoundation/hardhat-network-helpers": "^1.0.7" }, "publishConfig": { diff --git a/source/wrapper/package.json b/source/wrapper/package.json index 843e06503..20b89810c 100644 --- a/source/wrapper/package.json +++ b/source/wrapper/package.json @@ -27,12 +27,12 @@ "verify": "hardhat run ./scripts/verify.js" }, "dependencies": { - "@airswap/swap-erc20": "4.1.1", + "@airswap/swap-erc20": "4.1.2", "@openzeppelin/contracts": "^4.8.3" }, "devDependencies": { "@airswap/constants": "^4.1.0", - "@airswap/utils": "^4.1.2", + "@airswap/utils": "^4.1.3", "@airswap/types": "^4.1.1", "@uniswap/v2-periphery": "^1.1.0-beta.0", "prompt-confirm": "^2.0.4" diff --git a/tools/libraries/package.json b/tools/libraries/package.json index c8ff34519..b56575099 100644 --- a/tools/libraries/package.json +++ b/tools/libraries/package.json @@ -34,9 +34,9 @@ "@airswap/pool": "4.1.0", "@airswap/staking": "4.0.3", "@airswap/swap": "4.0.4", - "@airswap/swap-erc20": "4.1.1", + "@airswap/swap-erc20": "4.1.2", "@airswap/types": "^4.1.1", - "@airswap/utils": "^4.1.2", + "@airswap/utils": "^4.1.3", "@airswap/wrapper": "4.1.1", "browser-or-node": "^2.1.1", "ethers": "^5.6.9", diff --git a/tools/utils/index.ts b/tools/utils/index.ts index 90e272840..cabf132a1 100644 --- a/tools/utils/index.ts +++ b/tools/utils/index.ts @@ -1,6 +1,7 @@ -import { ethers, BigNumber as BigNumberEthers } from 'ethers' import * as url from 'url' +import { ethers, BigNumber as BigNumberEthers } from 'ethers' import { explorerUrls } from '@airswap/constants' +import { SwapERC20, FullSwapERC20 } from '@airswap/types' export * from './src/pricing' export * from './src/swap' @@ -14,6 +15,79 @@ export function getAccountUrl(chainId: number, address: string): string { return `${explorerUrls[chainId]}/address/${address}` } +const parseTransfer = (log: any, tokenInterface: ethers.utils.Interface) => { + let parsed + let transfer + try { + parsed = tokenInterface.parseLog(log) + if (parsed.name === 'Transfer') + transfer = { + token: log.address, + from: parsed.args[0], + to: parsed.args[1], + amount: ethers.BigNumber.from(parsed.args[2]), + } + } catch (e) { + return null + } + return transfer +} + +export const getFullSwapERC20 = async ( + swapInterface: ethers.utils.Interface, + tokenInterface: ethers.utils.Interface, + tx: ethers.providers.TransactionResponse, + event: SwapERC20 +): Promise => { + const receipt = await tx.wait() + const transfers: any = [] + for (let i = 0; i < receipt.logs.length; i++) { + let parsed: ethers.utils.LogDescription + try { + parsed = swapInterface.parseLog(receipt.logs[i]) + } catch (e) { + continue + } + if (parsed && parsed.name === 'SwapERC20') { + let transfer: any + while (i--) { + if ((transfer = parseTransfer(receipt.logs[i], tokenInterface))) { + transfers.push(transfer) + } + } + break + } + } + + const [fee, signer, sender] = transfers + if (fee.from !== event.signerWallet) + throw new Error( + 'unable to get SwapERC20 params: found incorrect fee transfer (wrong signerWallet)' + ) + if (signer.from !== event.signerWallet) + throw new Error( + 'unable to get SwapERC20 params: found incorrect signer transfer (wrong signerWallet)' + ) + if (signer.from !== sender.to) + throw new Error( + 'unable to get SwapERC20 params: signer transfer mismatched sender transfer' + ) + if (sender.from !== signer.to) + throw new Error( + 'unable to get SwapERC20 params: sender transfer mismatched signer transfer' + ) + + return { + ...event, + signerToken: signer.token, + signerAmount: signer.amount.toString(), + senderWallet: sender.from, + senderToken: sender.token, + senderAmount: sender.amount.toString(), + feeAmount: fee.amount.toString(), + } +} + export function checkResultToErrors( count: BigNumberEthers, errors: Array diff --git a/tools/utils/package.json b/tools/utils/package.json index 8da52b893..0426b9a2c 100644 --- a/tools/utils/package.json +++ b/tools/utils/package.json @@ -1,6 +1,6 @@ { "name": "@airswap/utils", - "version": "4.1.2", + "version": "4.1.3", "description": "AirSwap: Utilities for Developers", "repository": { "type": "git",