diff --git a/source/balances/package.json b/source/balances/package.json index 9b6bbaf78..b440c4129 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.1", + "@airswap/utils": "^4.1.2", "prompt-confirm": "^2.0.4" }, "publishConfig": { diff --git a/source/pool/package.json b/source/pool/package.json index 60843a536..5c6cc7419 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.1", + "@airswap/utils": "^4.1.2", "prompt-confirm": "^2.0.4" }, "publishConfig": { diff --git a/source/registry/package.json b/source/registry/package.json index 8050c8ec5..c665a2186 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.1", + "@airswap/utils": "^4.1.2", "prompt-confirm": "^2.0.4" } } diff --git a/source/staking/package.json b/source/staking/package.json index f535732dc..bc2b8892d 100644 --- a/source/staking/package.json +++ b/source/staking/package.json @@ -31,7 +31,7 @@ "access": "public" }, "devDependencies": { - "@airswap/utils": "^4.1.1", + "@airswap/utils": "^4.1.2", "prompt-confirm": "^2.0.4" } } diff --git a/source/swap-erc20/events.ts b/source/swap-erc20/events.ts new file mode 100644 index 000000000..5552322c9 --- /dev/null +++ b/source/swap-erc20/events.ts @@ -0,0 +1,78 @@ +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 a0eaa608b..362247107 100644 --- a/source/swap-erc20/package.json +++ b/source/swap-erc20/package.json @@ -1,6 +1,6 @@ { "name": "@airswap/swap-erc20", - "version": "4.1.0", + "version": "4.1.1", "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.1", + "@airswap/utils": "^4.1.2", "prompt-confirm": "^2.0.4" }, "publishConfig": { diff --git a/source/swap-erc20/tsconfig.json b/source/swap-erc20/tsconfig.json index ce170734c..9a33095c4 100644 --- a/source/swap-erc20/tsconfig.json +++ b/source/swap-erc20/tsconfig.json @@ -3,5 +3,5 @@ "compilerOptions": { "outDir": "./typechain" }, - "files": ["./typechain/index.ts"] + "files": ["./typechain/index.ts", "./events.ts"] } diff --git a/source/swap/package.json b/source/swap/package.json index e7f1463b4..f97312bdc 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.1", + "@airswap/utils": "^4.1.2", "@nomicfoundation/hardhat-network-helpers": "^1.0.7" }, "publishConfig": { diff --git a/source/wrapper/package.json b/source/wrapper/package.json index 453e25862..ce550006b 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.0", + "@airswap/swap-erc20": "4.1.1", "@openzeppelin/contracts": "^4.8.3" }, "devDependencies": { "@airswap/constants": "^4.1.0", - "@airswap/utils": "^4.1.1", + "@airswap/utils": "^4.1.2", "@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 3fe870796..68571e69f 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.0", + "@airswap/swap-erc20": "4.1.1", "@airswap/types": "^4.1.1", - "@airswap/utils": "^4.1.1", + "@airswap/utils": "^4.1.2", "@airswap/wrapper": "4.1.0", "browser-or-node": "^2.1.1", "ethers": "^5.6.9", diff --git a/tools/utils/index.ts b/tools/utils/index.ts index 6a2b8dcaa..90e272840 100644 --- a/tools/utils/index.ts +++ b/tools/utils/index.ts @@ -1,13 +1,6 @@ import { ethers, BigNumber as BigNumberEthers } from 'ethers' import * as url from 'url' import { explorerUrls } from '@airswap/constants' -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) export * from './src/pricing' export * from './src/swap' @@ -21,77 +14,6 @@ export function getAccountUrl(chainId: number, address: string): string { return `${explorerUrls[chainId]}/address/${address}` } -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 = [] - 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(), - } -} - export function checkResultToErrors( count: BigNumberEthers, errors: Array diff --git a/tools/utils/package.json b/tools/utils/package.json index 28a3a19f6..8da52b893 100644 --- a/tools/utils/package.json +++ b/tools/utils/package.json @@ -1,6 +1,6 @@ { "name": "@airswap/utils", - "version": "4.1.1", + "version": "4.1.2", "description": "AirSwap: Utilities for Developers", "repository": { "type": "git", @@ -22,7 +22,6 @@ }, "dependencies": { "@airswap/constants": "^4.1.0", - "@airswap/swap-erc20": "4.1.0", "@airswap/types": "^4.1.1", "@metamask/eth-sig-util": "^5.0.2", "bignumber.js": "^9.0.1",