Skip to content

Commit

Permalink
add swap event helper to @airswap/utils
Browse files Browse the repository at this point in the history
  • Loading branch information
dmosites committed Oct 1, 2024
1 parent 5b2e403 commit 28156e5
Show file tree
Hide file tree
Showing 13 changed files with 65 additions and 44 deletions.
2 changes: 1 addition & 1 deletion source/batch-call/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"owners": "hardhat run ./scripts/owner.js"
},
"devDependencies": {
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"@airswap/swap": "5.0.0",
"@airswap/swap-erc20": "5.0.0",
"@openzeppelin/contracts": "^4.8.3"
Expand Down
2 changes: 1 addition & 1 deletion source/delegate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"owners": "hardhat run ./scripts/owner.js"
},
"devDependencies": {
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"@airswap/swap-erc20": "5.0.0"
},
"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 @@ -25,7 +25,7 @@
"balances": "hardhat run ./scripts/balances.js"
},
"devDependencies": {
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"@airswap/merkle": "0.0.2",
"@openzeppelin/contracts": "^4.8.3"
},
Expand Down
2 changes: 1 addition & 1 deletion source/registry/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"owners": "hardhat run ./scripts/owner.js"
},
"devDependencies": {
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"@openzeppelin/contracts": "^4.8.3"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion source/staking/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"owners": "hardhat run ./scripts/owner.js"
},
"devDependencies": {
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"@openzeppelin/contracts": "^4.8.3"
},
"publishConfig": {
Expand Down
2 changes: 1 addition & 1 deletion source/swap-erc20/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
},
"devDependencies": {
"@airswap/staking": "5.0.0",
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"solady": "0.0.173"
},
"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 @@ -26,7 +26,7 @@
"owners": "hardhat run ./scripts/owner.js"
},
"devDependencies": {
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"@nomicfoundation/hardhat-network-helpers": "^1.0.7",
"@openzeppelin/contracts": "^4.8.3"
},
Expand Down
2 changes: 1 addition & 1 deletion source/wrapper/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"owners": "hardhat run ./scripts/owner.js"
},
"devDependencies": {
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"@airswap/swap-erc20": "5.0.0",
"@openzeppelin/contracts": "^4.8.3",
"@uniswap/v2-periphery": "^1.1.0-beta.0"
Expand Down
2 changes: 1 addition & 1 deletion tools/libraries/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"@airswap/staking": "5.0.0",
"@airswap/swap": "5.0.0",
"@airswap/swap-erc20": "5.0.0",
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"@airswap/wrapper": "5.0.0",
"browser-or-node": "^2.1.1",
"ethers": "^5.7.2",
Expand Down
2 changes: 1 addition & 1 deletion tools/stores/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"test": "REDISCLOUD_URL=redis://localhost:6379 TS_NODE_COMPILER_OPTIONS='{\"strict\":false}' yarn mocha -r ts-node/esm test/*.ts"
},
"dependencies": {
"@airswap/utils": "5.0.0",
"@airswap/utils": "5.0.1",
"redis": "^4.6.13"
},
"devDependencies": {
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": "5.0.0",
"version": "5.0.1",
"description": "AirSwap: Utilities for Developers",
"repository": {
"type": "git",
Expand Down
80 changes: 47 additions & 33 deletions tools/utils/src/swap-erc20.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
DOMAIN_NAME_SWAP_ERC20,
} from './constants'

import { Signature, Settlement } from './types'
import { Signature, Settlement, Transfer } from './types'

export const EIP712SwapERC20 = {
EIP712Domain: [
Expand Down Expand Up @@ -332,7 +332,7 @@ export function decompressFullOrderERC20(str: string): FullOrderERC20 {
return paramsToFullOrderERC20(lzString.decompressFromEncodedURIComponent(str))
}

const parseTransfer = (log: any) => {
const parseTransfer = (log: any): Transfer | null => {
let parsed
let transfer
try {
Expand All @@ -351,33 +351,20 @@ const parseTransfer = (log: any) => {
return transfer
}

export const getFullSwapERC20 = async (
export const getFullSwapERC20FromTransfers = (
nonce: string,
signerWallet: string,
feeReceiver: string,
logs: ethers.providers.Log[]
): Promise<FullSwapERC20> => {
const transfers = []
let transfer: any
let length = logs.length

feeReceiver = feeReceiver.toLowerCase()
signerWallet = signerWallet.toLowerCase()

while (length--) {
if ((transfer = parseTransfer(logs[length]))) {
transfers.push(transfer)
}
}

let fee: any
let signer: any
let sender: any
transfers: Transfer[]
): FullSwapERC20 => {
let feeTransfer: Transfer | null
let signerTransfer: Transfer | null
let senderTransfer: Transfer | null

let i = transfers.length
while (i--) {
if (transfers[i].to === feeReceiver) {
fee = transfers[i]
feeTransfer = transfers[i]
} else {
let j = transfers.length
while (j--) {
Expand All @@ -386,26 +373,53 @@ export const getFullSwapERC20 = async (
transfers[i].from === transfers[j].to &&
transfers[i].to == transfers[j].from
) {
signer = transfers[i]
sender = transfers[j]
signerTransfer = transfers[i]
senderTransfer = transfers[j]
break
}
}
}
}

if (!signer || !sender) {
throw new Error('getFullSwapERC20: Swap not found')
if (!signerTransfer || !senderTransfer) {
throw new Error('getFullSwapERC20: Either signer or sender not found')
}

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(),
signerWallet: signerTransfer.from,
signerToken: signerTransfer.token,
signerAmount: signerTransfer.amount.toString(),
senderWallet: senderTransfer.from,
senderToken: senderTransfer.token,
senderAmount: senderTransfer.amount.toString(),
feeAmount: feeTransfer.amount.toString(),
}
}

export const getFullSwapERC20 = (
nonce: string,
signerWallet: string,
feeReceiver: string,
logs: ethers.providers.Log[]
): FullSwapERC20 => {
const allTransfers: Transfer[] = []
let transfer: Transfer | null
let length = logs.length

feeReceiver = feeReceiver.toLowerCase()
signerWallet = signerWallet.toLowerCase()

while (length--) {
if ((transfer = parseTransfer(logs[length]))) {
allTransfers.push(transfer)
}
}

return getFullSwapERC20FromTransfers(
nonce,
signerWallet,
feeReceiver,
allTransfers
)
}
7 changes: 7 additions & 0 deletions tools/utils/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ export type Token = {
decimals: number
}

export type Transfer = {
from: string
to: string
token: string
amount: string
}

export type { TokenInfo } from '@uniswap/token-lists'

export interface CollectionTokenAttribute {
Expand Down

0 comments on commit 28156e5

Please sign in to comment.