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 #1320

Merged
merged 62 commits into from
Aug 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
62 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
f0eef52
Merge pull request #1251 from airswap/develop
dmosites Jan 11, 2024
6c1cc86
Merge pull request #1260 from airswap/develop
dmosites Jan 15, 2024
a9d08e3
Merge pull request #1266 from airswap/develop
dmosites Jan 18, 2024
da9bbc7
Merge pull request #1268 from airswap/develop
dmosites Jan 19, 2024
58f612c
publish libraries 4.2.1-beta.0
dmosites Jan 20, 2024
d9d08fb
bump libraries beta version
dmosites Jan 25, 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
96dc10c
Merge branch 'beta' into develop
smartcontrart May 31, 2024
64d8bf8
Merge branch 'develop' into delegates
smartcontrart May 31, 2024
c8195eb
Removed unused variable
smartcontrart Jun 5, 2024
13c7ae8
Added Delegate fillSenderAmount and Expiry to Rule
smartcontrart Jul 25, 2024
c08efb7
Added Rule expiry test
smartcontrart Jul 25, 2024
15814c5
Fixed expiry check bug
smartcontrart Aug 15, 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
29 changes: 18 additions & 11 deletions source/delegate/contracts/Delegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,17 @@ contract Delegate is IDelegate, Ownable {
/**
* @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 _senderRuleAmount 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,
uint256 _senderRuleAmount,
address _signerToken,
uint256 _signerAmount
uint256 _signerAmount,
uint256 _ruleExpiry
) external {
if (authorized[_senderWallet] != address(0)) {
if (authorized[_senderWallet] != msg.sender) revert SenderInvalid();
Expand All @@ -54,17 +55,21 @@ contract Delegate is IDelegate, Ownable {
rules[_senderWallet][_senderToken][_signerToken] = Rule(
_senderWallet,
_senderToken,
_senderAmount,
_senderRuleAmount,
0,
_signerToken,
_signerAmount
_signerAmount,
_ruleExpiry
);

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

Expand All @@ -84,7 +89,6 @@ contract Delegate is IDelegate, Ownable {
} else {
if (_senderWallet != msg.sender) revert SenderInvalid();
}
Rule storage rule = rules[_senderWallet][_senderToken][_signerToken];
delete rules[_senderWallet][_senderToken][_signerToken];

emit UnsetRule(_senderWallet, _senderToken, _signerToken);
Expand All @@ -106,12 +110,15 @@ contract Delegate is IDelegate, Ownable {
Rule storage rule = rules[_senderWallet][_senderToken][_signerToken];

if (
_senderAmount > (_signerAmount * rule.senderAmount) / rule.signerAmount
_signerAmount <
(rule.signerAmount * (rule.senderRuleAmount - rule.senderFilledAmount)) /
rule.senderRuleAmount
) {
revert InvalidSignerAmount();
}
if (rule.ruleExpiry < block.timestamp) revert RuleExpired();

if (rule.senderAmount < _senderAmount) {
if (_senderAmount > (rule.senderRuleAmount - rule.senderFilledAmount)) {
revert InvalidSenderAmount();
}

Expand Down Expand Up @@ -140,7 +147,7 @@ contract Delegate is IDelegate, Ownable {
SafeTransferLib.safeTransfer(_signerToken, _senderWallet, _signerAmount);

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

Expand Down
64 changes: 35 additions & 29 deletions source/delegate/contracts/interfaces/IDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,63 @@ interface IDelegate {
struct Rule {
address sender;
address senderToken;
uint256 senderAmount;
uint256 senderRuleAmount;
uint256 senderFilledAmount;
address signerToken;
uint256 signerAmount;
uint256 ruleExpiry;
}

error RuleExpired();
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 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
address senderWallet,
address senderToken,
uint256 senderRuleAmount,
uint256 senderFilledAmount,
address signerToken,
uint256 signerAmount,
uint256 ruleExpiry
);

event UnsetRule(address _signer, address _signerToken, address _senderToken);
event UnsetRule(address signer, address signerToken, address senderToken);

function setRule(
address _sender,
address _senderToken,
uint256 _senderAmount,
address _signerToken,
uint256 _signerAmount
address sender,
address senderToken,
uint256 senderRuleAmount,
address signerToken,
uint256 signerAmount,
uint256 ruleExpiry
) 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
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
address sender,
address signerToken,
address senderToken
) external;
}
69 changes: 56 additions & 13 deletions source/delegate/test/Delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const {
createOrderERC20,
orderERC20ToParams,
createOrderERC20Signature,
SECONDS_IN_DAY,
} = require('@airswap/utils')
const CHAIN_ID = 31337
const DEFAULT_BALANCE = '100000'
Expand All @@ -18,6 +19,8 @@ const PROTOCOL_FEE = '5'
const REBATE_SCALE = '10'
const REBATE_MAX = '100'
const UPDATE_SWAP_ERC20_ADDRESS = '0x0000000000000000000000000000000000001337'
const RULE_EXPIRY =
Math.round(Date.now() / 1000 + SECONDS_IN_DAY).toString() + 1

describe('Delegate Unit', () => {
let deployer
Expand Down Expand Up @@ -157,16 +160,19 @@ describe('Delegate Unit', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)
)
.to.emit(delegate, 'SetRule')
.withArgs(
sender.address,
senderToken.address,
DEFAULT_SENDER_AMOUNT,
0,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)
})

Expand All @@ -190,16 +196,19 @@ describe('Delegate Unit', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)
)
.to.emit(delegate, 'SetRule')
.withArgs(
sender.address,
senderToken.address,
DEFAULT_SENDER_AMOUNT,
0,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)
})

Expand All @@ -212,7 +221,8 @@ describe('Delegate Unit', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)

await expect(
Expand All @@ -232,7 +242,8 @@ describe('Delegate Unit', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)

const rule = await delegate.rules(
Expand All @@ -241,7 +252,7 @@ describe('Delegate Unit', () => {
signerToken.address
)

expect(rule.senderAmount.toString()).to.equal(DEFAULT_SENDER_AMOUNT)
expect(rule.senderRuleAmount.toString()).to.equal(DEFAULT_SENDER_AMOUNT)
})

it('unsetting a Rule updates the rule balance', async () => {
Expand All @@ -252,7 +263,8 @@ describe('Delegate Unit', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)

let rule = await delegate.rules(
Expand All @@ -271,7 +283,7 @@ describe('Delegate Unit', () => {
signerToken.address
)

expect(rule.senderAmount.toString()).to.equal('0')
expect(rule.senderRuleAmount.toString()).to.equal('0')
})
})

Expand Down Expand Up @@ -302,7 +314,8 @@ describe('Delegate Unit', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)

const order = await createSignedOrderERC20({}, signer)
Expand Down Expand Up @@ -330,7 +343,8 @@ describe('Delegate Unit', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)

const order = await createSignedOrderERC20({}, signer)
Expand Down Expand Up @@ -379,7 +393,8 @@ describe('Delegate Unit', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT - 1,
signerToken.address,
DEFAULT_SIGNER_AMOUNT - 1
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)

const order = await createSignedOrderERC20({}, signer)
Expand Down Expand Up @@ -413,7 +428,8 @@ describe('Delegate Unit', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)

const order = await createSignedOrderERC20(
Expand All @@ -439,5 +455,32 @@ describe('Delegate Unit', () => {
delegate.connect(signer).swap(sender.address, ...order)
).to.be.revertedWith('InvalidSignerAmount')
})

it('fails to swap with a rule expired', async () => {
await delegate
.connect(sender)
.setRule(
sender.address,
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT,
0
)

const order = await createSignedOrderERC20({}, signer)

await setUpAllowances(
sender.address,
DEFAULT_SENDER_AMOUNT,
signer.address,
DEFAULT_SIGNER_AMOUNT + PROTOCOL_FEE
)
await setUpBalances(signer.address, sender.address)

await expect(
delegate.connect(signer).swap(sender.address, ...order)
).to.revertedWith('RuleExpired')
})
})
})
6 changes: 5 additions & 1 deletion source/delegate/test/DelegateIntegration.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const {
createOrderERC20,
orderERC20ToParams,
createOrderERC20Signature,
SECONDS_IN_DAY,
} = require('@airswap/utils')
const { ethers } = require('hardhat')
const ERC20 = require('@openzeppelin/contracts/build/contracts/ERC20PresetMinterPauser.json')
Expand All @@ -24,6 +25,8 @@ describe('Delegate Integration', () => {
const DEFAULT_SENDER_AMOUNT = '10000'
const DEFAULT_SIGNER_AMOUNT = '10000'
const DEFAULT_BALANCE = '1000000'
const RULE_EXPIRY =
Math.round(Date.now() / 1000 + SECONDS_IN_DAY).toString() + 1

async function createSignedOrderERC20(params, signer) {
const unsignedOrder = createOrderERC20({
Expand Down Expand Up @@ -99,7 +102,8 @@ describe('Delegate Integration', () => {
senderToken.address,
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT
DEFAULT_SIGNER_AMOUNT,
RULE_EXPIRY
)

signerToken
Expand Down
Loading