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 all 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
19 changes: 19 additions & 0 deletions source/delegate/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Copyright 2024 AirSwap

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 @@
# Delegate

[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.
176 changes: 176 additions & 0 deletions source/delegate/contracts/Delegate.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.23;

import "./interfaces/IDelegate.sol";
import "@airswap/swap-erc20/contracts/interfaces/ISwapERC20.sol";
import { ERC20 } from "solady/src/tokens/ERC20.sol";
import { Ownable } from "solady/src/auth/Ownable.sol";
import { SafeTransferLib } from "solady/src/utils/SafeTransferLib.sol";

/**
* @title Delegate: Deployable Trading Rules for the AirSwap Network
* @notice Supports fungible tokens (ERC-20)
* @dev inherits IDelegate, Ownable and uses SafeTransferLib
*/
contract Delegate is IDelegate, Ownable {
// The Swap contract to be used to settle trades
ISwapERC20 public swapERC20;

// Mapping of sender to senderToken to to signerToken to Rule
mapping(address => mapping(address => mapping(address => Rule))) public rules;

// Mapping of signer to authorized signatory
mapping(address => address) public authorized;

/**
* @notice Contract Constructor
*/
constructor(ISwapERC20 _swapERC20) {
_initializeOwner(msg.sender);
swapERC20 = _swapERC20;
smartcontrart marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @notice Set a Trading Rule
* @param _senderToken address Address of an ERC-20 token the consumer would send
* @param _senderAmount uint256 Maximum amount of ERC-20 token the sender wants to swap
* @param _signerToken address Address of an ERC-20 token the delegate would recieve
* @param _signerAmount uint256 Minimum amount of ERC-20 token the delegate would recieve
*/
function setRule(
address _senderWallet,
address _senderToken,
uint256 _senderAmount,
address _signerToken,
uint256 _signerAmount
) external {
if (authorized[_senderWallet] != address(0)) {
if (authorized[_senderWallet] != msg.sender) revert SenderInvalid();
} else {
if (_senderWallet != msg.sender) revert SenderInvalid();
}

rules[_senderWallet][_senderToken][_signerToken] = Rule(
_senderWallet,
_senderToken,
_senderAmount,
_signerToken,
_signerAmount
);

emit SetRule(
_senderWallet,
_senderToken,
_senderAmount,
_signerToken,
_signerAmount
);
}

/**
* @notice Unset a Trading Rule
* @dev only callable by the owner of the contract, removes from a mapping
* @param _senderToken address Address of an ERC-20 token the sender would send
* @param _signerToken address Address of an ERC-20 token the delegate would receive
*/
function unsetRule(
address _senderWallet,
address _senderToken,
address _signerToken
) external {
if (authorized[_senderWallet] != address(0)) {
if (authorized[_senderWallet] != msg.sender) revert SenderInvalid();
} else {
if (_senderWallet != msg.sender) revert SenderInvalid();
}
Rule storage rule = rules[_senderWallet][_senderToken][_signerToken];
delete rules[_senderWallet][_senderToken][_signerToken];

emit UnsetRule(_senderWallet, _senderToken, _signerToken);
}

function swap(
address _senderWallet,
uint256 _nonce,
uint256 _expiry,
address _signerWallet,
address _signerToken,
uint256 _signerAmount,
address _senderToken,
uint256 _senderAmount,
uint8 _v,
bytes32 _r,
bytes32 _s
) external {
Rule storage rule = rules[_senderWallet][_senderToken][_signerToken];

if (
_senderAmount > (_signerAmount * rule.senderAmount) / rule.signerAmount
) {
revert InvalidSignerAmount();
}

if (rule.senderAmount < _senderAmount) {
revert InvalidSenderAmount();
}

SafeTransferLib.safeTransferFrom(
_senderToken,
_senderWallet,
address(this),
_senderAmount
);

ERC20(_senderToken).approve(address(swapERC20), _senderAmount);

swapERC20.swapLight(
_nonce,
_expiry,
_signerWallet,
_signerToken,
_signerAmount,
_senderToken,
_senderAmount,
_v,
_r,
_s
);

SafeTransferLib.safeTransfer(_signerToken, _senderWallet, _signerAmount);

rules[_senderWallet][_senderToken][_signerToken]
.senderAmount -= _senderAmount;
emit DelegateSwap(_nonce, _signerWallet);
}

/**
* @notice Authorize a wallet to manage rules
* @param _manager address Wallet of the signatory to authorize
* @dev Emits an Authorize event
*/
function authorize(address _manager) external {
if (_manager == address(0)) revert ManagerInvalid();
authorized[msg.sender] = _manager;
emit Authorize(_manager, msg.sender);
}

/**
* @notice Revoke a manager
* @dev Emits a Revoke event
*/
function revoke() external {
address _tmp = authorized[msg.sender];
delete authorized[msg.sender];
emit Revoke(_tmp, msg.sender);
}

/**
* @notice Sets the SwapERC20 contract
* @param _swapERC20 ISwapERC20 The SwapERC20 contract
*/
function setSwapERC20Contract(ISwapERC20 _swapERC20) external onlyOwner {
if (address(_swapERC20) == address(0)) revert InvalidAddress();
swapERC20 = _swapERC20;
}
}
64 changes: 64 additions & 0 deletions source/delegate/contracts/interfaces/IDelegate.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: MIT

import "@airswap/swap-erc20/contracts/interfaces/ISwapERC20.sol";

pragma solidity 0.8.23;

interface IDelegate {
struct Rule {
address sender;
address senderToken;
uint256 senderAmount;
address signerToken;
uint256 signerAmount;
}

error InvalidAddress();
error InvalidSenderAmount();
error InvalidSignerAmount();
error ManagerInvalid();
error SenderInvalid();
error TransferFromFailed();

event Authorize(address _signatory, address _signer);
event DelegateSwap(uint256 _nonce, address _signerWallet);
event Revoke(address _tmp, address _signer);

event SetRule(
address _senderWallet,
address _senderToken,
uint256 _senderAmount,
address _signerToken,
uint256 _signerAmount
);

event UnsetRule(address _signer, address _signerToken, address _senderToken);

function setRule(
address _sender,
address _senderToken,
uint256 _senderAmount,
address _signerToken,
uint256 _signerAmount
) external;

function swap(
address _senderWallet,
uint256 _nonce,
uint256 _expiry,
address _signerWallet,
address _signerToken,
uint256 _signerAmount,
address _senderToken,
uint256 _senderAmount,
uint8 _v,
bytes32 _r,
bytes32 _s
) external;

function unsetRule(
address _sender,
address _signerToken,
address _senderToken
) external;
}
3 changes: 3 additions & 0 deletions source/delegate/deploys-blocks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
11155111: 5860563,
}
3 changes: 3 additions & 0 deletions source/delegate/deploys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
11155111: '0x4f4F5517Fd344A4abe84C91D8F80111Fb821B531',
}
2 changes: 2 additions & 0 deletions source/delegate/deploys.js.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
declare module '@airswap/delegate/deploys.js'
declare module '@airswap/delegate/deploys-blocks.js'
6 changes: 6 additions & 0 deletions source/delegate/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = {
typechain: {
outDir: 'typechain',
},
...require('../../hardhat.config.js'),
}
38 changes: 38 additions & 0 deletions source/delegate/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"name": "@airswap/delegate",
"version": "4.3.0-beta.0",
"description": "AirSwap: Delegate On-chain Trading Rules",
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/airswap/airswap-protocols"
},
"files": [
"./build",
"./typechain",
"./deploys.js",
"./deploys-blocks.js",
"./deploys.js.d.ts"
],
"scripts": {
"clean": "rm -rf ./cache && rm -rf ./build && rm -rf ./typechain",
"compile": "hardhat compile; yarn typechain",
"typechain": "tsc -b",
"coverage": "hardhat coverage",
"test": "hardhat test",
"test:ci": "hardhat test",
"deploy": "hardhat run ./scripts/deploy.js",
"verify": "hardhat run ./scripts/verify.js",
"owners": "hardhat run ./scripts/owner.js",
"migrate": "hardhat run ./scripts/migrate.js",
"balances": "hardhat run ./scripts/balances.js"
},
"devDependencies": {
"@airswap/utils": "4.3.2",
"@airswap/swap-erc20": "4.3.1",
"prompt-confirm": "^2.0.4"
},
"publishConfig": {
"access": "public"
}
}
Loading
Loading