Skip to content

Commit

Permalink
batch to batch-call (#1245)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmosites authored Jan 5, 2024
1 parent caa2473 commit 5d47a9f
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 82 deletions.
4 changes: 2 additions & 2 deletions source/batch/README.md → source/batch-call/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Batch
# BatchCall

[AirSwap](https://www.airswap.io/) is a peer-to-peer trading network for Ethereum tokens. This package contains source code and tests for a basic ERC20 balance and allowance aggregator.

Expand All @@ -15,7 +15,7 @@

## Usage

:warning: This package is under active development. The [Batch](./contracts/Batch.sol) contract is deployed; see [deploys.js](./deploys.js) for latest. For all AirSwap contract deployments see [Deployed Contracts](https://docs.airswap.io/system/contract-deployments).
:warning: This package is under active development. The [BatchCall](./contracts/BatchCall.sol) contract is deployed; see [deploys.js](./deploys.js) for latest. For all AirSwap contract deployments see [Deployed Contracts](https://docs.airswap.io/system/contract-deployments).

## Commands

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;
pragma solidity 0.8.23;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
Expand All @@ -9,20 +9,12 @@ import "@airswap/swap/contracts/interfaces/ISwap.sol";
import "@airswap/swap-erc20/contracts/interfaces/ISwapERC20.sol";

/**
* @title Batching: Batch balance, allowance, order validity check calls
* @title BatchCalling: BatchCall balance, allowance, order validity check calls
*/
contract Batch is Ownable {
contract BatchCall is Ownable {
using SafeERC20 for IERC20;
using Address for address;

ISwap public Swap;
ISwapERC20 public SwapERC20;

constructor(address _swapContractAddress, address _swapERC20ContractAddress) {
Swap = ISwap(_swapContractAddress);
SwapERC20 = ISwapERC20(_swapERC20ContractAddress);
}

/**
* @notice Check the token balances of a wallet for multiple tokens
* @dev return array and will fail if large token arrays are inputted
Expand Down Expand Up @@ -137,36 +129,6 @@ contract Batch is Ownable {
return balances;
}

/**
* @notice Self-destruct contract for clean-up
*/
function destruct(address payable recipientAddress) public onlyOwner {
selfdestruct(recipientAddress);
}

/**
* @notice Allow owner to withdraw ether from contract
*/
function withdraw() public onlyOwner {
(bool success, ) = address(owner()).call{ value: address(this).balance }(
""
);
require(success, "ETH_WITHDRAW_FAILED");
}

/**
* @notice Allow owner to withdraw stuck tokens from contract
* @param tokenAddress address
* @param amount uint256
*/
function withdrawToken(
address tokenAddress,
uint256 amount
) public onlyOwner {
require(tokenAddress != address(0x0)); //use withdraw for ETH
IERC20(tokenAddress).safeTransfer(msg.sender, amount);
}

/**
* @notice Check the token allowance of a wallet in a token contract
* @dev return 0 on returns 0 on invalid spender contract or non-contract address
Expand Down Expand Up @@ -233,28 +195,30 @@ contract Batch is Ownable {

function checkOrders(
address senderWallet,
ISwap.Order[] calldata orders
ISwap.Order[] calldata orders,
ISwap swapContract
) external view returns (bool[] memory) {
require(orders.length > 0);
bool[] memory orderValidity = new bool[](orders.length);

for (uint256 i = 0; i < orders.length; i++) {
(, uint256 errorCount) = Swap.check(senderWallet, orders[i]);
(, uint256 errorCount) = swapContract.check(senderWallet, orders[i]);
orderValidity[i] = errorCount == 0 ? true : false;
}
return orderValidity;
}

function checkOrdersERC20(
address senderWallet,
ISwapERC20.OrderERC20[] calldata ordersERC20
ISwapERC20.OrderERC20[] calldata ordersERC20,
ISwapERC20 swapERC20Contract
) external view returns (bool[] memory) {
require(ordersERC20.length > 0);
bool[] memory orderValidity = new bool[](ordersERC20.length);

for (uint256 i = 0; i < ordersERC20.length; i++) {
ISwapERC20.OrderERC20 memory order = ordersERC20[i];
(uint256 errorCount, ) = SwapERC20.check(
(uint256 errorCount, ) = swapERC20Contract.check(
senderWallet,
order.nonce,
order.expiry,
Expand All @@ -271,4 +235,27 @@ contract Batch is Ownable {
}
return orderValidity;
}

/**
* @notice Allow owner to withdraw ether from contract
*/
function withdraw() public onlyOwner {
(bool success, ) = address(owner()).call{ value: address(this).balance }(
""
);
require(success, "ETH_WITHDRAW_FAILED");
}

/**
* @notice Allow owner to withdraw stuck tokens from contract
* @param tokenAddress address
* @param amount uint256
*/
function withdrawToken(
address tokenAddress,
uint256 amount
) public onlyOwner {
require(tokenAddress != address(0x0)); //use withdraw for ETH
IERC20(tokenAddress).safeTransfer(msg.sender, amount);
}
}
File renamed without changes.
1 change: 1 addition & 0 deletions source/batch-call/deploys.js.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@airswap/batch-call/deploys.js'
File renamed without changes.
8 changes: 5 additions & 3 deletions source/batch/package.json → source/batch-call/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@airswap/batch",
"name": "@airswap/batch-call",
"version": "4.1.0",
"description": "Batch balance, allowance, order validity check calls",
"description": "BatchCall balance, allowance, order validity check calls",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -26,7 +26,9 @@
"owners": "hardhat run ./scripts/owner.js"
},
"dependencies": {
"@openzeppelin/contracts": "^4.8.3"
"@openzeppelin/contracts": "^4.8.3",
"@airswap/swap": "4.1.2",
"@airswap/swap-erc20": "4.1.6"
},
"devDependencies": {
"@airswap/constants": "4.1.8",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const Confirm = require('prompt-confirm')
const { ethers, run } = require('hardhat')
const { ChainIds, chainNames, chainLabels } = require('@airswap/constants')
const { getReceiptUrl } = require('@airswap/utils')
const batchDeploys = require('../deploys.js')
const batchCallDeploys = require('../deploys.js')

async function main() {
await run('compile')
Expand All @@ -21,19 +21,19 @@ async function main() {

const prompt = new Confirm('Proceed to deploy?')
if (await prompt.run()) {
const batchFactory = await ethers.getContractFactory('Batch')
const batchContract = await batchFactory.deploy()
const batchFactory = await ethers.getContractFactory('BatchCall')
const batchCallContract = await batchFactory.deploy()
console.log(
'Deploying...',
getReceiptUrl(chainId, batchContract.deployTransaction.hash)
getReceiptUrl(chainId, batchCallContract.deployTransaction.hash)
)
await batchContract.deployed()
console.log(`Deployed: ${batchContract.address}`)
await batchCallContract.deployed()
console.log(`Deployed: ${batchCallContract.address}`)

batchDeploys[chainId] = batchContract.address
batchCallDeploys[chainId] = batchCallContract.address
fs.writeFileSync(
'./deploys.js',
`module.exports = ${JSON.stringify(batchDeploys, null, '\t')}`
`module.exports = ${JSON.stringify(batchCallDeploys, null, '\t')}`
)
console.log('Updated deploys.js')

Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* eslint-disable no-console */
const { ethers, run } = require('hardhat')
const { chainNames } = require('@airswap/constants')
const batchDeploys = require('../deploys.js')
const batchCallDeploys = require('../deploys.js')

async function main() {
await run('compile')
Expand All @@ -12,7 +12,7 @@ async function main() {

console.log(`Verifying on ${chainNames[chainId].toUpperCase()}`)
await run('verify:verify', {
address: batchDeploys[chainId],
address: batchCallDeploys[chainId],
constructorArguments: [],
})
}
Expand Down
14 changes: 6 additions & 8 deletions source/batch/test/Batch.js → source/batch-call/test/Batch.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ async function setUpBalances(senderAmount, signerAmount) {
await erc20token.mock.balanceOf.withArgs(signer.address).returns(signerAmount)
}

describe('BatchChecks Integration', () => {
describe('BatchCallChecks Integration', () => {
beforeEach(async () => {
snapshotId = await ethers.provider.send('evm_snapshot')
})
Expand Down Expand Up @@ -164,9 +164,7 @@ describe('BatchChecks Integration', () => {
)
await swapERC20.deployed()

batch = await (
await ethers.getContractFactory('Batch')
).deploy(swap.address, swapERC20.address)
batch = await (await ethers.getContractFactory('BatchCall')).deploy()
await batch.deployed()
})

Expand All @@ -181,7 +179,7 @@ describe('BatchChecks Integration', () => {
]
const orderValidities = await batch
.connect(sender)
.checkOrders(sender.address, orders)
.checkOrders(sender.address, orders, swap.address)
expect(orderValidities.toString()).to.equal([true, true, true].toString())
})

Expand All @@ -195,7 +193,7 @@ describe('BatchChecks Integration', () => {
]
const orderValidities = await batch
.connect(sender)
.checkOrdersERC20(sender.address, ERC20orders)
.checkOrdersERC20(sender.address, ERC20orders, swapERC20.address)
expect(orderValidities.toString()).to.equal([true, true, true].toString())
})

Expand All @@ -209,7 +207,7 @@ describe('BatchChecks Integration', () => {
]
const orderValidities = await batch
.connect(sender)
.checkOrders(sender.address, orders)
.checkOrders(sender.address, orders, swap.address)
expect(orderValidities.toString()).to.equal(
[false, false, false].toString()
)
Expand All @@ -225,7 +223,7 @@ describe('BatchChecks Integration', () => {
]
const orderValidities = await batch
.connect(sender)
.checkOrdersERC20(sender.address, ERC20orders)
.checkOrdersERC20(sender.address, ERC20orders, swapERC20.address)
expect(orderValidities.toString()).to.equal(
[false, false, false].toString()
)
Expand Down
File renamed without changes.
1 change: 0 additions & 1 deletion source/batch/deploys.js.d.ts

This file was deleted.

10 changes: 5 additions & 5 deletions source/pool/scripts/balances.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const { ethers } = require('hardhat')
const { getKnownTokens } = require('@airswap/metadata')
const { chainNames, ChainIds } = require('@airswap/constants')
const Batch = require('@airswap/batch/build/contracts/Batch.sol/Batch.json')
const batchDeploys = require('@airswap/batch/deploys.js')
const BatchCall = require('@airswap/batch-call/build/contracts/BatchCall.sol/BatchCall.json')
const batchCallDeploys = require('@airswap/batch-call/deploys.js')
const poolDeploys = require('../deploys.js')

async function main() {
Expand All @@ -16,7 +16,7 @@ async function main() {
console.log('Network:', chainNames[chainId].toUpperCase())
console.log('\nPool:', poolDeploys[chainId])

if (!batchDeploys[chainId]) {
if (!batchCallDeploys[chainId]) {
throw new Error('Unable to check balances on this chain.')
}

Expand All @@ -33,8 +33,8 @@ async function main() {
console.log(`\nScanning non-zero balances for ${tokens.length} tokens...\n`)

const balancesContract = new ethers.Contract(
batchDeploys[chainId],
Batch.abi,
batchCallDeploys[chainId],
BatchCall.abi,
account.provider
)

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.17",
"version": "4.1.18",
"description": "AirSwap: Libraries for Developers",
"repository": {
"type": "git",
Expand All @@ -26,7 +26,7 @@
"test:ci": "yarn test"
},
"dependencies": {
"@airswap/batch": "4.1.0",
"@airswap/batch-call": "4.1.0",
"@airswap/constants": "4.1.8",
"@airswap/jsonrpc-client-websocket": "0.0.1",
"@airswap/registry": "4.1.3",
Expand Down
12 changes: 6 additions & 6 deletions tools/libraries/src/Contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ import swapERC20Blocks from '@airswap/swap-erc20/deploys-blocks.js'
import wrapperBlocks from '@airswap/wrapper/deploys-blocks.js'
import wethBlocks from '@airswap/wrapper/deploys-blocks-weth.js'

import BatchContract from '@airswap/batch/build/contracts/Batch.sol/Batch.json'
import batchDeploys from '@airswap/batch/deploys.js'
import BatchCallContract from '@airswap/batch-call/build/contracts/BatchCall.sol/BatchCall.json'
import batchCallDeploys from '@airswap/batch-call/deploys.js'
const batchInterface = new ethers.utils.Interface(
JSON.stringify(BatchContract.abi)
JSON.stringify(BatchCallContract.abi)
)

export class Contract {
Expand Down Expand Up @@ -82,13 +82,13 @@ export const WETH = new Contract(
wethBlocks,
WETH9__factory
)
export const Batch = new Contract('Batch', batchDeploys)
Batch.getContract = (
export const BatchCall = new Contract('BatchCall', batchCallDeploys)
BatchCall.getContract = (
providerOrSigner: ethers.providers.Provider | ethers.Signer,
chainId: number
): ethers.Contract => {
return new ethers.Contract(
batchDeploys[chainId],
batchCallDeploys[chainId],
batchInterface,
providerOrSigner
)
Expand Down

0 comments on commit 5d47a9f

Please sign in to comment.