Skip to content

Commit

Permalink
add SwapERC20 to types; add getFullSwapERC20 to utils (#1206)
Browse files Browse the repository at this point in the history
* add SwapERC20 to types; add getFullSwapERC20 to utils

* add swap-erc20 to utils
  • Loading branch information
dmosites authored Oct 2, 2023
1 parent 74cf127 commit 2c3f543
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 20 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.0",
"@airswap/utils": "^4.1.1",
"prompt-confirm": "^2.0.4"
},
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions source/pool/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
},
"devDependencies": {
"@airswap/constants": "^4.1.0",
"@airswap/types": "^4.1.0",
"@airswap/utils": "^4.1.0",
"@airswap/types": "^4.1.1",
"@airswap/utils": "^4.1.1",
"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.0",
"@airswap/utils": "^4.1.1",
"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.0",
"@airswap/utils": "^4.1.1",
"prompt-confirm": "^2.0.4"
}
}
4 changes: 2 additions & 2 deletions source/swap-erc20/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@
"devDependencies": {
"@airswap/constants": "^4.1.0",
"@airswap/staking": "4.0.3",
"@airswap/types": "^4.1.0",
"@airswap/utils": "^4.1.0",
"@airswap/types": "^4.1.1",
"@airswap/utils": "^4.1.1",
"prompt-confirm": "^2.0.4"
},
"publishConfig": {
Expand Down
4 changes: 2 additions & 2 deletions source/swap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
},
"devDependencies": {
"@airswap/constants": "^4.1.0",
"@airswap/types": "^4.1.0",
"@airswap/utils": "^4.1.0",
"@airswap/types": "^4.1.1",
"@airswap/utils": "^4.1.1",
"@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 @@ -32,8 +32,8 @@
},
"devDependencies": {
"@airswap/constants": "^4.1.0",
"@airswap/utils": "^4.1.0",
"@airswap/types": "^4.1.0",
"@airswap/utils": "^4.1.1",
"@airswap/types": "^4.1.1",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"prompt-confirm": "^2.0.4"
},
Expand Down
8 changes: 4 additions & 4 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.2",
"version": "4.1.3",
"description": "AirSwap: Libraries for Developers",
"repository": {
"type": "git",
Expand All @@ -27,16 +27,16 @@
},
"dependencies": {
"@airswap/balances": "4.0.2",
"@airswap/constants": "4.1.0",
"@airswap/constants": "^4.1.0",
"@airswap/jsonrpc-client-websocket": "0.0.1",
"@airswap/registry": "4.1.0",
"@airswap/maker-registry": "4.0.4",
"@airswap/pool": "4.1.0",
"@airswap/staking": "4.0.3",
"@airswap/swap": "4.0.4",
"@airswap/swap-erc20": "4.1.0",
"@airswap/types": "4.1.0",
"@airswap/utils": "4.1.0",
"@airswap/types": "^4.1.1",
"@airswap/utils": "^4.1.1",
"@airswap/wrapper": "4.1.0",
"browser-or-node": "^2.1.1",
"ethers": "^5.6.9",
Expand Down
4 changes: 2 additions & 2 deletions tools/metadata/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airswap/metadata",
"version": "4.1.0",
"version": "4.1.1",
"description": "AirSwap: Token Metadata for Developers",
"repository": {
"type": "git",
Expand All @@ -21,7 +21,7 @@
},
"dependencies": {
"@airswap/constants": "^4.1.0",
"@airswap/types": "^4.1.0",
"@airswap/types": "^4.1.1",
"@airswap/wrapper": "4.1.0",
"@openzeppelin/contracts": "^4.8.3",
"@uniswap/token-lists": "^1.0.0-beta.24",
Expand Down
2 changes: 1 addition & 1 deletion tools/types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@airswap/types",
"version": "4.1.0",
"version": "4.1.1",
"description": "AirSwap: Types for Developers",
"repository": {
"type": "git",
Expand Down
14 changes: 14 additions & 0 deletions tools/types/src/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ export type OrderERC20 = {

export type FullOrderERC20 = UnsignedOrderERC20 & Signature & Settlement

export type SwapERC20 = {
nonce: string
signerWallet: string
}

export type FullSwapERC20 = {
signerToken: string
signerAmount: string
senderWallet: string
senderToken: string
senderAmount: string
feeAmount: string
} & SwapERC20

export type Levels = [string, string][]

export type Formula = string
Expand Down
78 changes: 78 additions & 0 deletions tools/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
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 @@ -14,6 +21,77 @@ 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
5 changes: 3 additions & 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.0",
"version": "4.1.1",
"description": "AirSwap: Utilities for Developers",
"repository": {
"type": "git",
Expand All @@ -22,7 +22,8 @@
},
"dependencies": {
"@airswap/constants": "^4.1.0",
"@airswap/types": "^4.0.4",
"@airswap/swap-erc20": "4.1.0",
"@airswap/types": "^4.1.1",
"@metamask/eth-sig-util": "^5.0.2",
"bignumber.js": "^9.0.1",
"ethereumjs-util": "^7.1.5",
Expand Down

0 comments on commit 2c3f543

Please sign in to comment.