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

Delegate Updates #1345

Merged
merged 3 commits into from
Nov 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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: 9 additions & 10 deletions source/delegate/contracts/Delegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,21 @@ contract Delegate is IDelegate, Ownable {
bytes32 _s
) external {
Rule storage rule = rules[_senderWallet][_senderToken][_signerToken];
// Ensure the signer amount is valid
if (
_signerAmount <
(rule.signerAmount * (rule.senderAmount - rule.senderFilledAmount)) /
rule.senderAmount
) {
revert SignerAmountInvalid();
}
// Ensure the rule has not expired
if (rule.expiry < block.timestamp) revert RuleExpired();
// Ensure the expiry is not passed
if (rule.expiry <= block.timestamp) revert RuleExpiredOrDoesNotExist();

// Ensure the sender amount is valid
if (_senderAmount > (rule.senderAmount - rule.senderFilledAmount)) {
revert SenderAmountInvalid();
}

// Ensure the signer amount is valid
if (
rule.signerAmount * _senderAmount != rule.senderAmount * _signerAmount
) {
revert SignerAmountInvalid();
}

// Transfer the sender token to this contract
SafeTransferLib.safeTransferFrom(
_senderToken,
Expand Down
2 changes: 1 addition & 1 deletion source/delegate/contracts/interfaces/IDelegate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ interface IDelegate {
);

error AddressInvalid();
error RuleExpired();
error RuleExpiredOrDoesNotExist();
error SenderAmountInvalid();
error SignerAmountInvalid();
error SenderInvalid();
Expand Down
87 changes: 65 additions & 22 deletions source/delegate/test/Delegate.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
} = require('@airswap/utils')
const CHAIN_ID = 31337
const DEFAULT_BALANCE = '100000'
const DEFAULT_SENDER_AMOUNT = '10000'
const DEFAULT_SENDER_AMOUNT = '5000'
const DEFAULT_SIGNER_AMOUNT = '10000'
const PROTOCOL_FEE = '5'
const REBATE_SCALE = '10'
Expand Down Expand Up @@ -456,23 +456,51 @@ describe('Delegate Unit', () => {
.withArgs(delegate.address)
.returns(DEFAULT_SIGNER_AMOUNT)

await expect(delegate.connect(signer).swap(sender.address, ...order)).to
.be.reverted
await expect(
delegate.connect(signer).swap(sender.address, ...order)
).to.be.revertedWith('RuleExpiredOrDoesNotExist')
})

it('fails to swap with insufficient remaining sender amount on Rule', async () => {
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,
Math.round(Date.now() / 1000) - 10
)

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.be.revertedWith('RuleExpiredOrDoesNotExist')
})

it('fails to swap with sender amount above rule sender amount', async () => {
await senderToken.mock.approve
.withArgs(delegate.address, DEFAULT_SENDER_AMOUNT - 1)
.withArgs(delegate.address, DEFAULT_SENDER_AMOUNT / 2)
.returns(true)

await delegate
.connect(sender)
.setRule(
sender.address,
senderToken.address,
DEFAULT_SENDER_AMOUNT - 1,
DEFAULT_SENDER_AMOUNT / 2,
signerToken.address,
DEFAULT_SIGNER_AMOUNT,
DEFAULT_SIGNER_AMOUNT / 2,
RULE_EXPIRY
)

Expand All @@ -488,16 +516,16 @@ describe('Delegate Unit', () => {

await signerToken.mock.balanceOf
.withArgs(signer.address)
.returns(DEFAULT_SIGNER_AMOUNT - 1)
.returns(DEFAULT_SIGNER_AMOUNT)

await expect(
delegate.connect(signer).swap(sender.address, ...order)
).to.be.revertedWith('SenderAmountInvalid')
})

it('fails to swap with insufficient signer amount on Rule', async () => {
it('fails to swap with sender amount above remaining rule sender amount', async () => {
await senderToken.mock.approve
.withArgs(delegate.address, DEFAULT_SENDER_AMOUNT - 1)
.withArgs(delegate.address, DEFAULT_SENDER_AMOUNT / 2)
.returns(true)

await delegate
Expand All @@ -511,12 +539,7 @@ describe('Delegate Unit', () => {
RULE_EXPIRY
)

const order = await createSignedOrderERC20(
{
signerAmount: DEFAULT_SIGNER_AMOUNT - 1,
},
signer
)
const order = await createSignedOrderERC20({}, signer)

await setUpAllowances(
sender.address,
Expand All @@ -528,14 +551,24 @@ describe('Delegate Unit', () => {

await signerToken.mock.balanceOf
.withArgs(signer.address)
.returns(DEFAULT_SIGNER_AMOUNT - 1)
.returns(DEFAULT_SIGNER_AMOUNT)

await expect(
delegate.connect(signer).swap(sender.address, ...order)
).to.be.revertedWith('SignerAmountInvalid')
).to.emit(delegate, 'DelegateSwap')

const order2 = await createSignedOrderERC20({}, signer)

await expect(
delegate.connect(signer).swap(sender.address, ...order2)
).to.be.revertedWith('SenderAmountInvalid')
})

it('fails to swap with a rule expired', async () => {
it('fails to swap with insufficient signer amount on Rule', async () => {
await senderToken.mock.approve
.withArgs(delegate.address, DEFAULT_SENDER_AMOUNT - 1)
.returns(true)

await delegate
.connect(sender)
.setRule(
Expand All @@ -544,10 +577,16 @@ describe('Delegate Unit', () => {
DEFAULT_SENDER_AMOUNT,
signerToken.address,
DEFAULT_SIGNER_AMOUNT,
0
RULE_EXPIRY
)

const order = await createSignedOrderERC20({}, signer)
const order = await createSignedOrderERC20(
{
senderAmount: DEFAULT_SENDER_AMOUNT,
signerAmount: DEFAULT_SIGNER_AMOUNT / 4,
},
signer
)

await setUpAllowances(
sender.address,
Expand All @@ -557,9 +596,13 @@ describe('Delegate Unit', () => {
)
await setUpBalances(signer.address, sender.address)

await signerToken.mock.balanceOf
.withArgs(signer.address)
.returns(DEFAULT_SIGNER_AMOUNT - 1)

await expect(
delegate.connect(signer).swap(sender.address, ...order)
).to.revertedWith('RuleExpired')
).to.be.revertedWith('SignerAmountInvalid')
})
})
})
Loading