Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:airswap/airswap-protocols into beta
Browse files Browse the repository at this point in the history
  • Loading branch information
dmosites committed Aug 28, 2024
2 parents 87e8971 + 4317242 commit 009f7f5
Show file tree
Hide file tree
Showing 9 changed files with 321 additions and 71 deletions.
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
1 change: 1 addition & 0 deletions tools/libraries/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './src/Contracts'
export * from './src/Registry'
export * from './src/RegistryV3'
export * from './src/Server'
Loading

0 comments on commit 009f7f5

Please sign in to comment.