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

Delegates #1309

Merged
merged 50 commits into from
May 27, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
50 commits
Select commit Hold shift + click to select a range
749cd0c
Imported Delegate and Indexer contracts and Fixed Delegate code to al…
smartcontrart Dec 14, 2023
13bd57b
Updated Indexer code to compile with Solidity 0.8.17
smartcontrart Dec 14, 2023
2de8b8c
Updated licenses for Delegates and Indexer contracts
smartcontrart Dec 14, 2023
e3a4552
Removed constructor visibility and updated Delegate/Indexer interacti…
smartcontrart Dec 14, 2023
7cdb24d
Removed the indexer boilerplate and adjusted the delegate placeholder…
smartcontrart Jan 9, 2024
c388135
Removed the indexer boilerplate and adjusted the delegate placeholder…
smartcontrart Jan 9, 2024
b00c2ad
Merge branch 'develop' into delegates
smartcontrart Feb 1, 2024
d27ad6f
Removed IDelegateFactoryContract
smartcontrart Feb 1, 2024
67b6d43
Created basics of the swap function for the delegate contract
smartcontrart Feb 29, 2024
4674419
Fixed token transfers
smartcontrart Mar 10, 2024
469508b
Removed unecessary variable
smartcontrart Mar 10, 2024
e439848
Removed unused imports in Delegate tests
smartcontrart Mar 10, 2024
1e32701
Removed unused imports in Delegate tests
smartcontrart Mar 10, 2024
6eb837e
Fixed transfer amounts
smartcontrart Mar 12, 2024
03994bf
Updated recipient to delegator in swapERC20
smartcontrart Mar 12, 2024
97fa0b8
Renamed signer to delegator and sender to taker
smartcontrart Mar 12, 2024
e5ee912
Renamed signer and sender to delegator and taker for clarity and adde…
smartcontrart Mar 13, 2024
4017966
Created delegate integration test
smartcontrart Mar 13, 2024
809590b
Removed unused variables
smartcontrart Mar 13, 2024
b3a7643
Merge branch 'develop' into delegates
smartcontrart Mar 19, 2024
53def14
updated delegates contract to solady library
smartcontrart Mar 20, 2024
a87c3ea
Merge branch 'main' of github.com:airswap/airswap-protocols into dele…
dmosites Mar 21, 2024
282bcaf
Merge branch 'develop' of github.com:airswap/airswap-protocols into d…
dmosites Mar 21, 2024
a996532
delegates: cleanup deps; remove redundants
dmosites Mar 21, 2024
274f770
name updates; test quickfix
dmosites Mar 21, 2024
4b1a993
prettier
dmosites Mar 21, 2024
10b0548
Renamed _maxDelegatorAmount to _maxSenderAmount for consistency
smartcontrart Mar 29, 2024
868dfd7
Added test to check delegate allowance
smartcontrart Mar 29, 2024
b27a3b1
Added internal rule tests
smartcontrart Mar 29, 2024
8f1c372
Implemented Delegate price check
smartcontrart Apr 4, 2024
c505ce5
Separated setting and unsetting rules tests
smartcontrart Apr 5, 2024
7b976cc
Specified DEFAULT_SIGNER_AMOUNT and DEFAULT_SENDER_AMOUNT in tests. A…
smartcontrart Apr 10, 2024
3872788
Removed test to check if sender can send more token that defined in t…
smartcontrart Apr 16, 2024
07076a3
Implemented signatory logic allowing sender to delegate rule setting …
smartcontrart Apr 22, 2024
8784a44
Wrote delegate deploy script
smartcontrart May 7, 2024
822f346
Removed unecessary scripts
smartcontrart May 7, 2024
d79aa13
updated deploy.js.d.ts
smartcontrart May 7, 2024
e953079
Fixed chain name import
smartcontrart May 8, 2024
7bf3577
Deployed to Sepolia
smartcontrart May 8, 2024
b08cce0
Renamed Signatory to Manager, adjusted tests accordingly
smartcontrart May 8, 2024
5b76c88
Deployed to Sepolia
smartcontrart May 8, 2024
1076bbd
Merge branch 'develop' of github.com:airswap/airswap-protocols into d…
dmosites May 10, 2024
c20ee77
prettier
smartcontrart May 10, 2024
c548a68
Merge branch 'delegates' of github.com:airswap/airswap-protocols into…
dmosites May 15, 2024
a703ec1
Merge branch 'develop' of github.com:airswap/airswap-protocols into d…
dmosites May 15, 2024
a017d41
restore abis
dmosites May 15, 2024
620e34e
Updated copyright date
smartcontrart May 16, 2024
f0033c8
PR comments, added swapERC20 setter and fixed ownership initialization
smartcontrart May 16, 2024
becd52d
zeroed out all fields in the rule when unsetting
smartcontrart May 16, 2024
97d3d82
Refactored rule reset on unset
smartcontrart May 18, 2024
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
31 changes: 26 additions & 5 deletions source/batch-call/contracts/BatchCall.sol
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.23;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import { ERC20 } from "solady/src/tokens/ERC20.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "@airswap/swap/contracts/interfaces/ISwap.sol";
import "@airswap/swap-erc20/contracts/interfaces/ISwapERC20.sol";
import "@airswap/registry/contracts/interfaces/IRegistry.sol";

/**
* @title BatchCall: Batch balance, allowance, order validity checks, nonce usage check
*/
contract BatchCall {
using SafeERC20 for IERC20;
using Address for address;

error ArgumentInvalid();
Expand All @@ -28,7 +27,7 @@ contract BatchCall {
address tokenAddress
) public view returns (uint256) {
if (tokenAddress.isContract()) {
IERC20 token = IERC20(tokenAddress);
ERC20 token = ERC20(tokenAddress);
// Check if balanceOf succeeds.
(bool success, ) = address(token).staticcall(
abi.encodeWithSelector(token.balanceOf.selector, userAddress)
Expand Down Expand Up @@ -119,7 +118,7 @@ contract BatchCall {
address tokenAddress
) public view returns (uint256) {
if (tokenAddress.isContract()) {
IERC20 token = IERC20(tokenAddress);
ERC20 token = ERC20(tokenAddress);
// Check if allowance succeeds as a call else returns 0.
(bool success, ) = address(token).staticcall(
abi.encodeWithSelector(
Expand Down Expand Up @@ -290,4 +289,26 @@ contract BatchCall {
}
return nonceUsed;
}

/**
* @notice provides the tokens supported by multiple Stakers
* @param stakers address[] list of stakers to be checked
* @param registryContract IRegistry Registry contract to call
* @return bool[] true indicates the nonce is used
*/
function getTokensForStakers(
address[] calldata stakers,
IRegistry registryContract
) external view returns (address[][] memory) {
if (stakers.length == 0) revert ArgumentInvalid();
address[][] memory tokensSupported = new address[][](stakers.length);

for (uint256 i; i < stakers.length; ) {
tokensSupported[i] = registryContract.getTokensForStaker(stakers[i]);
unchecked {
++i;
}
}
return tokensSupported;
}
}
95 changes: 93 additions & 2 deletions source/batch-call/test/BatchCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const { ethers, waffle } = require('hardhat')
const { deployMockContract } = waffle
const IERC20 = require('@openzeppelin/contracts/build/contracts/IERC20.json')
const IERC721 = require('@openzeppelin/contracts/build/contracts/ERC721Royalty.json')
const REGISTRY = require('@airswap/registry/build/contracts/Registry.sol/Registry.json')
const SWAP = require('@airswap/swap/build/contracts/Swap.sol/Swap.json')
const SWAP_ERC20 = require('@airswap/swap-erc20/build/contracts/SwapERC20.sol/SwapERC20.json')
const ERC20_ADAPTER = require('@airswap/swap/build/contracts/adapters/ERC20Adapter.sol/ERC20Adapter.json')
Expand All @@ -22,6 +23,8 @@ const DEFAULT_AMOUNT = '1000'
const DEFAULT_BALANCE = '100000'
const BONUS_SCALE = '10'
const BONUS_MAX = '100'
const STAKING_COST = '1000000000'
const SUPPORT_COST = '1000000'

let snapshotId
let deployer
Expand All @@ -31,6 +34,7 @@ let erc20token
let erc20adapter
let erc721token
let erc721adapter
let registry
let swap
let swapERC20
let batchCall
Expand Down Expand Up @@ -107,6 +111,15 @@ async function setUpAllowances(senderAmount, signerAmount) {
async function setUpBalances(senderAmount, signerAmount) {
await erc20token.mock.balanceOf.withArgs(sender.address).returns(senderAmount)
await erc20token.mock.balanceOf.withArgs(signer.address).returns(signerAmount)
await erc20token.mock.balanceOf
.withArgs(staker1.address)
.returns(STAKING_COST)
await erc20token.mock.balanceOf
.withArgs(staker2.address)
.returns(STAKING_COST)
await erc20token.mock.balanceOf
.withArgs(staker3.address)
.returns(STAKING_COST)
}

describe('BatchCall Integration', () => {
Expand All @@ -119,8 +132,17 @@ describe('BatchCall Integration', () => {
})

before('deploy adapter and swap', async () => {
;[deployer, sender, signer, affiliate, protocolFeeWallet, anyone] =
await ethers.getSigners()
;[
deployer,
sender,
signer,
affiliate,
protocolFeeWallet,
anyone,
staker1,
staker2,
staker3,
] = await ethers.getSigners()
erc20token = await deployMockContract(deployer, IERC20.abi)
await erc20token.mock.allowance.returns(DEFAULT_AMOUNT)
await erc20token.mock.balanceOf.returns(DEFAULT_AMOUNT)
Expand All @@ -143,6 +165,12 @@ describe('BatchCall Integration', () => {
)
).deploy()
await erc721adapter.deployed()

registry = await (
await ethers.getContractFactory(REGISTRY.abi, REGISTRY.bytecode)
).deploy(erc20token.address, STAKING_COST, SUPPORT_COST)
await registry.deployed()

swap = await (
await ethers.getContractFactory(SWAP.abi, SWAP.bytecode)
).deploy(
Expand Down Expand Up @@ -329,4 +357,67 @@ describe('BatchCall Integration', () => {
)
})
})

describe('returns tokensSupported for mutiple stakers', () => {
it('returns all tokens supported by multiple stakers', async () => {
const tokensSupported = []
for (let i = 0; i < 10; i++) {
tokensSupported[i] = await deployMockContract(deployer, IERC20.abi)
}
registry
.connect(staker1)
.addTokens([
tokensSupported[0].address,
tokensSupported[1].address,
tokensSupported[2].address,
])
registry
.connect(staker2)
.addTokens([
tokensSupported[3].address,
tokensSupported[4].address,
tokensSupported[5].address,
])
registry
.connect(staker3)
.addTokens([
tokensSupported[6].address,
tokensSupported[7].address,
tokensSupported[8].address,
tokensSupported[9].address,
])
const expectedTokensSupported = await batchCall
.connect(deployer)
.getTokensForStakers(
[staker1.address, staker2.address, staker3.address],
registry.address
)
expect(expectedTokensSupported.toString()).to.equal(
[
[
tokensSupported[0].address,
tokensSupported[1].address,
tokensSupported[2].address,
],
[
tokensSupported[3].address,
tokensSupported[4].address,
tokensSupported[5].address,
],
[
tokensSupported[6].address,
tokensSupported[7].address,
tokensSupported[8].address,
tokensSupported[9].address,
],
].toString()
)
})

it('reverts if a wrong argument is passed', async () => {
await expect(
batchCall.getTokensForStakers([], registry.address)
).to.be.revertedWith('ArgumentInvalid')
})
})
})
19 changes: 19 additions & 0 deletions source/delegate/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2023 AirSwap
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2024


Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
37 changes: 37 additions & 0 deletions source/delegate/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Pool
smartcontrart marked this conversation as resolved.
Show resolved Hide resolved

[AirSwap](https://www.airswap.io/) is an open-source peer-to-peer trading network.

[![Discord](https://img.shields.io/discord/590643190281928738.svg)](https://discord.gg/ecQbV7H)
[![License](https://img.shields.io/badge/License-MIT-blue)](https://opensource.org/licenses/MIT)
![Twitter Follow](https://img.shields.io/twitter/follow/airswap?style=social)

## Resources

- About → https://about.airswap.io/
- Website → https://www.airswap.io/
- Twitter → https://twitter.com/airswap
- Chat → https://chat.airswap.io/

## Usage

:warning: This package may contain unaudited code. For all AirSwap contract deployments see [Deployed Contracts](https://about.airswap.io/technology/deployments).

## Commands

Environment variables are set in an `.env` file in the repository root.

| Command | Description |
| :--------------- | :--------------------------------------- |
| `yarn` | Install dependencies |
| `yarn clean` | Delete the contract `build` folder |
| `yarn compile` | Compile all contracts to `build` folder |
| `yarn coverage` | Report test coverage |
| `yarn test` | Run all tests in `test` folder |
| `yarn test:ci` | Run CI tests in `test` folder |
| `yarn deploy` | Deploy on a network using --network flag |
| `yarn verify` | Verify on a network using --network flag |

## Running Tests

:bulb: Prior to testing locally, run `yarn compile` in the `airswap-protocols` project root to build required artifacts.
Loading
Loading