Skip to content

Commit

Permalink
Improve transfer parser util (#1237)
Browse files Browse the repository at this point in the history
* improve transfer event parser and add test

* package engine node 18

* bump package versions
  • Loading branch information
dmosites authored Dec 13, 2023
1 parent 773306b commit e0be97b
Show file tree
Hide file tree
Showing 14 changed files with 246 additions and 85 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@
"typescript": "^5.0.4"
},
"engines": {
"node": ">= 14"
"node": ">= 18"
}
}
2 changes: 1 addition & 1 deletion source/balances/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
},
"devDependencies": {
"@airswap/constants": "4.1.6",
"@airswap/utils": "4.1.8",
"@airswap/utils": "4.1.9",
"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 @@ -34,7 +34,7 @@
"@airswap/constants": "4.1.6",
"@airswap/metadata": "4.1.12",
"@airswap/types": "4.1.1",
"@airswap/utils": "4.1.8",
"@airswap/utils": "4.1.9",
"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 @@ -33,7 +33,7 @@
},
"devDependencies": {
"@airswap/constants": "4.1.6",
"@airswap/utils": "4.1.8",
"@airswap/utils": "4.1.9",
"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 @@ -32,7 +32,7 @@
"access": "public"
},
"devDependencies": {
"@airswap/utils": "4.1.8",
"@airswap/utils": "4.1.9",
"prompt-confirm": "^2.0.4"
}
}
2 changes: 1 addition & 1 deletion source/swap-erc20/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"@airswap/constants": "4.1.6",
"@airswap/staking": "4.0.5",
"@airswap/types": "4.1.1",
"@airswap/utils": "4.1.8",
"@airswap/utils": "4.1.9",
"prompt-confirm": "^2.0.4"
},
"publishConfig": {
Expand Down
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.6",
"@airswap/types": "4.1.1",
"@airswap/utils": "4.1.8",
"@airswap/utils": "4.1.9",
"@nomicfoundation/hardhat-network-helpers": "^1.0.7"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion source/wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
},
"devDependencies": {
"@airswap/constants": "4.1.6",
"@airswap/utils": "4.1.8",
"@airswap/utils": "4.1.9",
"@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
@@ -1,6 +1,6 @@
{
"name": "@airswap/libraries",
"version": "4.1.13",
"version": "4.1.14",
"description": "AirSwap: Libraries for Developers",
"repository": {
"type": "git",
Expand Down Expand Up @@ -35,7 +35,7 @@
"@airswap/swap": "4.1.2",
"@airswap/swap-erc20": "4.1.6",
"@airswap/types": "4.1.1",
"@airswap/utils": "4.1.8",
"@airswap/utils": "4.1.9",
"@airswap/wrapper": "4.1.5",
"browser-or-node": "^2.1.1",
"ethers": "^5.6.9",
Expand Down
74 changes: 0 additions & 74 deletions tools/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
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'
Expand All @@ -15,79 +14,6 @@ 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<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], 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<string>
Expand Down
5 changes: 4 additions & 1 deletion tools/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airswap/utils",
"version": "4.1.8",
"version": "4.1.9",
"description": "AirSwap: Utilities for Developers",
"repository": {
"type": "git",
Expand Down Expand Up @@ -29,6 +29,9 @@
"ethers": "^5.6.9",
"lz-string": "^1.5.0"
},
"devDependencies": {
"@openzeppelin/contracts": "^4.8.3"
},
"publishConfig": {
"access": "public"
}
Expand Down
74 changes: 74 additions & 0 deletions tools/utils/src/swapERC20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import {
SignTypedDataVersion,
} from '@metamask/eth-sig-util'

import { abi as ERC20_ABI } from '@openzeppelin/contracts/build/contracts/ERC20.json'
const erc20Interface = new ethers.utils.Interface(ERC20_ABI)

import {
ChainIds,
SECONDS_IN_DAY,
Expand All @@ -21,6 +24,7 @@ import {
FullOrderERC20,
Signature,
EIP712SwapERC20,
FullSwapERC20,
} from '@airswap/types'

export function createOrderERC20({
Expand Down Expand Up @@ -276,3 +280,73 @@ export function compressFullOrderERC20(order: FullOrderERC20): string {
export function decompressFullOrderERC20(str: string): FullOrderERC20 {
return paramsToFullOrderERC20(lzString.decompressFromEncodedURIComponent(str))
}

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 (
nonce: string,
signerWallet: string,
feeReceiver: string,
logs: ethers.providers.Log[]
): Promise<FullSwapERC20> => {
const transfers = []
let transfer: any
let length = logs.length
while (length--) {
if ((transfer = parseTransfer(logs[length]))) {
transfers.push(transfer)
}
}

let fee: any
let signer: any
let sender: any

let i = transfers.length
while (i--) {
if (transfers[i].to === feeReceiver) {
fee = transfers[i]
} else {
let j = transfers.length
while (j--) {
if (
transfers[i].from === signerWallet &&
transfers[i].from === transfers[j].to &&
transfers[i].to == transfers[j].from
) {
signer = transfers[i]
sender = transfers[j]
break
}
}
}
}

return {
nonce: nonce.toString(),
signerWallet: signer.from,
signerToken: signer.token,
signerAmount: signer.amount.toString(),
senderWallet: sender.from,
senderToken: sender.token,
senderAmount: sender.amount.toString(),
feeAmount: fee.amount.toString(),
}
}
Loading

0 comments on commit e0be97b

Please sign in to comment.