Skip to content

Commit

Permalink
Delegates (#1309)
Browse files Browse the repository at this point in the history
* Imported Delegate and Indexer contracts and Fixed Delegate code to allow compilation

* Updated Indexer code to compile with Solidity 0.8.17

* Updated licenses for Delegates and Indexer contracts

* Removed constructor visibility and updated Delegate/Indexer interactions to ensure both contracts compile simultaneously

* Removed the indexer boilerplate and adjusted the delegate placeholder contract accordingly. Removed the delegateFactory contract

* Removed the indexer boilerplate and adjusted the delegate placeholder contract accordingly. Removed the delegateFactory contract

* Removed IDelegateFactoryContract

* Created basics of the swap function for the delegate contract

* Fixed token transfers

* Removed unecessary variable

* Removed unused imports in Delegate tests

* Removed unused imports in Delegate tests

* Fixed transfer amounts

* Updated recipient to delegator in swapERC20

* Renamed signer to delegator and sender to taker

* Renamed signer and sender to delegator and taker for clarity and added tracking of remaining rule allowance

* Created delegate integration test

* Removed unused variables

* updated delegates contract to solady library

* delegates: cleanup deps; remove redundants

* name updates; test quickfix

* prettier

* Renamed _maxDelegatorAmount to _maxSenderAmount for consistency

* Added test to check delegate allowance

* Added internal rule tests

* Implemented Delegate price check

* Separated setting and unsetting rules tests

* Specified DEFAULT_SIGNER_AMOUNT and DEFAULT_SENDER_AMOUNT in tests. Added a test case when SENDER_AMOUNT is above requirements

* Removed test to check if sender can send more token that defined in the rule

* Implemented signatory logic allowing sender to delegate rule setting on their behalf

* Wrote delegate deploy script

* Removed unecessary scripts

* updated deploy.js.d.ts

* Fixed chain name import

* Deployed to Sepolia

* Renamed Signatory to Manager, adjusted tests accordingly

* Deployed to Sepolia

* prettier

* restore abis

* Updated copyright date

* PR comments, added swapERC20 setter and fixed ownership initialization

* zeroed out all fields in the rule when unsetting

* Refactored rule reset on unset

---------

Co-authored-by: Don Mosites <mosites@gmail.com>
  • Loading branch information
smartcontrart and dmosites authored May 27, 2024
1 parent 54cc4ab commit da05cb9
Show file tree
Hide file tree
Showing 15 changed files with 1,046 additions and 0 deletions.
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;
}

/**
* @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

0 comments on commit da05cb9

Please sign in to comment.