Skip to content

Commit

Permalink
move getFullSwapERC20 to swap-erc20 to fix circular dep (#1207)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmosites authored Oct 3, 2023
1 parent 2c3f543 commit f0bda15
Show file tree
Hide file tree
Showing 12 changed files with 91 additions and 92 deletions.
2 changes: 1 addition & 1 deletion source/balances/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion source/pool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion source/registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
2 changes: 1 addition & 1 deletion source/staking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"access": "public"
},
"devDependencies": {
"@airswap/utils": "^4.1.1",
"@airswap/utils": "^4.1.2",
"prompt-confirm": "^2.0.4"
}
}
78 changes: 78 additions & 0 deletions source/swap-erc20/events.ts
Original file line number Diff line number Diff line change
@@ -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<FullSwapERC20> => {
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(),
}
}
4 changes: 2 additions & 2 deletions source/swap-erc20/package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down Expand Up @@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion source/swap-erc20/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
"compilerOptions": {
"outDir": "./typechain"
},
"files": ["./typechain/index.ts"]
"files": ["./typechain/index.ts", "./events.ts"]
}
2 changes: 1 addition & 1 deletion source/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
4 changes: 2 additions & 2 deletions source/wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 2 additions & 2 deletions tools/libraries/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
78 changes: 0 additions & 78 deletions tools/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<FullSwapERC20> => {
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<string>
Expand Down
3 changes: 1 addition & 2 deletions tools/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airswap/utils",
"version": "4.1.1",
"version": "4.1.2",
"description": "AirSwap: Utilities for Developers",
"repository": {
"type": "git",
Expand All @@ -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",
Expand Down

0 comments on commit f0bda15

Please sign in to comment.