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

move SwapERC20 helper to utils to avoid typechain confusion #1208

Merged
merged 1 commit into from
Oct 3, 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
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.2",
"@airswap/utils": "^4.1.3",
"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.2",
"@airswap/utils": "^4.1.3",
"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.2",
"@airswap/utils": "^4.1.3",
"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.2",
"@airswap/utils": "^4.1.3",
"prompt-confirm": "^2.0.4"
}
}
78 changes: 0 additions & 78 deletions source/swap-erc20/events.ts

This file was deleted.

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.1",
"version": "4.1.2",
"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.2",
"@airswap/utils": "^4.1.3",
"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", "./events.ts"]
"files": ["./typechain/index.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.2",
"@airswap/utils": "^4.1.3",
"@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.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"
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.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",
Expand Down
76 changes: 75 additions & 1 deletion tools/utils/index.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<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
2 changes: 1 addition & 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.2",
"version": "4.1.3",
"description": "AirSwap: Utilities for Developers",
"repository": {
"type": "git",
Expand Down
Loading