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

feat: add withdraw function to Gateway #51

Merged
merged 5 commits into from
Oct 14, 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
14 changes: 14 additions & 0 deletions contracts/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,20 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
emit OrderSettledIn(_provider, _sender, _amount, _token, _orderId);
}

/** @dev See {withdrawFrom-IGateway} */
function withdrawFrom(address provider, address recipient, uint256 _amount, address _token, bytes memory _signature) external isValidAmount(_amount) onlyAggregator returns (bool) {
bytes32 messageHash = keccak256(abi.encodePacked(provider, recipient, _amount, _token));
bytes32 ethSignedMessageHash = messageHash.toEthSignedMessageHash();
address signer = ethSignedMessageHash.recover(_signature);
require(signer == provider, 'InvalidSignature');

require(balance[_token][provider] >= _amount, 'InsufficientBalance');
balance[_token][provider] -= _amount;
IERC20(_token).transfer(recipient, _amount);
emit Withdrawn(provider, recipient, _amount, _token);
return true;
}

/* ##################################################################
VIEW CALLS
################################################################## */
Expand Down
23 changes: 23 additions & 0 deletions contracts/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,16 @@ interface IGateway {
* @param orderId The ID of the order.
*/
event OrderSettledIn(address indexed provider, address indexed senderAddress, uint256 indexed amount, address token, bytes32 orderId);

/**
* @notice Emitted when a withdrawal is made by a provider.
* @param provider The address of the provider.
* @param sender The address of the sender.
* @param amount The amount of the withdrawal.
* @param token The address of the withdrawn token.
*/
event Withdrawn(address indexed provider, address indexed sender, uint256 amount, address indexed token);

/* ##################################################################
STRUCTS
################################################################## */
Expand Down Expand Up @@ -205,6 +215,19 @@ interface IGateway {
uint256 _amount
) external;

/**
* @notice Withdraws an asset from Gateway.
* @dev Requirements:
* - The provider must have enough balance.
* @param _provider The address of the provider.
* @param _recipient The address of the recipient.
* @param _amount The amount to be withdrawn.
* @param _token The address of the asset.
* @param _signature The signature of the provider.
* @return bool The withdrawal is successful.
*/
function withdrawFrom(address _provider, address _recipient, uint256 _amount, address _token, bytes memory _signature) external returns (bool);

/**
* @notice Checks if a token is supported by Gateway.
* @param _token The address of the token to check.
Expand Down
4 changes: 2 additions & 2 deletions test/gateway/gateway.settleOrderOut.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ describe("Gateway offramp order", function () {
)
.to.emit(gateway, Events.Gateway.OrderCreated)
.withArgs(
this.sender.address,
this.alice.address,
mockUSDT.address,
this.orderAmount,
this.protocolFee,
Expand Down Expand Up @@ -264,7 +264,7 @@ describe("Gateway offramp order", function () {
)
.to.emit(gateway, Events.Gateway.OrderCreated)
.withArgs(
this.sender.address,
this.alice.address,
mockUSDT.address,
this.orderAmount,
this.protocolFee,
Expand Down
198 changes: 198 additions & 0 deletions test/gateway/gateway.withdraw.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
const { ethers } = require("hardhat");
const { BigNumber } = require("@ethersproject/bignumber");

const { gatewayFixture } = require("../fixtures/gateway.js");

const {
deployContract,
ZERO_AMOUNT,
Errors,
Events,
mockMintDeposit,
assertBalance,
assertDepositBalance,
} = require("../utils/utils.manager.js");
const { expect } = require("chai");

describe("Gateway Provider withdraw", function () {
beforeEach(async function () {
[
this.deployer,
this.treasuryAddress,
this.aggregator,
this.alice,
this.bob,
this.Eve,
this.hacker,
...this.accounts
] = await ethers.getSigners();

({ gateway, mockUSDT } = await gatewayFixture());

this.mockDAI = await deployContract("MockUSDT");

this.mockUSDT = mockUSDT;
this.gateway = gateway;

this.depositAmount = ethers.utils.parseEther("1000000");

await mockMintDeposit(gateway, this.alice, mockUSDT, this.depositAmount);
await mockMintDeposit(gateway, this.bob, mockUSDT, this.depositAmount);
await mockMintDeposit(gateway, this.Eve, mockUSDT, this.depositAmount);
await mockMintDeposit(
gateway,
this.alice,
this.mockDAI,
this.depositAmount
);
await mockMintDeposit(gateway, this.bob, this.mockDAI, this.depositAmount);
await mockMintDeposit(gateway, this.Eve, this.mockDAI, this.depositAmount);

await assertBalance(
this.mockUSDT,
this.mockDAI,
this.alice.address,
this.depositAmount
);
await assertBalance(
this.mockUSDT,
this.mockDAI,
this.bob.address,
this.depositAmount
);
await assertBalance(
this.mockUSDT,
this.mockDAI,
this.Eve.address,
this.depositAmount
);

const token = ethers.utils.formatBytes32String("token");

await expect(
this.gateway
.connect(this.deployer)
.settingManagerBool(token, this.mockUSDT.address, BigNumber.from(1))
)
.to.emit(this.gateway, Events.Gateway.SettingManagerBool)
.withArgs(token, this.mockUSDT.address, BigNumber.from(1));

const aggregator = ethers.utils.formatBytes32String("aggregator");

await expect(
gateway
.connect(this.deployer)
.updateProtocolAddress(aggregator, this.aggregator.address)
).to.emit(gateway, Events.Gateway.ProtocolAddressUpdated);
});

it("Should withdraw successfully with valid signature", async function () {
const amount = ethers.utils.parseEther("1000");
const messageHash = ethers.utils.solidityKeccak256(
["address", "address", "uint256", "address",],
[this.alice.address, this.bob.address, amount, this.mockUSDT.address]
);
const signature = await this.alice.signMessage(ethers.utils.arrayify(messageHash));

await assertDepositBalance(
this.gateway,
this.mockUSDT.address,
this.alice.address,
ZERO_AMOUNT
);

await expect(
this.gateway
.connect(this.alice)
.deposit(this.mockUSDT.address, this.depositAmount)
)
.to.emit(this.gateway, Events.Gateway.Deposit)
.withArgs(
this.alice.address,
this.mockUSDT.address,
BigNumber.from(this.depositAmount)
);

await assertDepositBalance(
this.gateway,
this.mockUSDT.address,
this.alice.address,
this.depositAmount
);

await expect(
this.gateway
.connect(this.aggregator)
.withdrawFrom(this.alice.address, this.bob.address, amount, this.mockUSDT.address, signature)
)
.to.emit(this.gateway, Events.Gateway.Withdrawn)
.withArgs(this.alice.address, this.bob.address, amount, this.mockUSDT.address);

await assertDepositBalance(
this.gateway,
this.mockUSDT.address,
this.alice.address,
this.depositAmount.sub(amount)
);

});

it("Should fail with invalid signature", async function () {
const amount = ethers.utils.parseEther("1000");
const messageHash = ethers.utils.solidityKeccak256(
["address", "address", "uint256", "address"],
[this.alice.address, this.bob.address, this.mockUSDT.address, amount]
);
const ethSignedMessageHash = ethers.utils.hashMessage(messageHash);
const invalidSignature = await this.bob.signMessage(ethers.utils.arrayify(ethSignedMessageHash));

await expect(
this.gateway
.connect(this.alice)
.deposit(this.mockUSDT.address, this.depositAmount)
)
.to.emit(this.gateway, Events.Gateway.Deposit)
.withArgs(
this.alice.address,
this.mockUSDT.address,
BigNumber.from(this.depositAmount)
);

await expect(
this.gateway
.connect(this.aggregator)
.withdrawFrom( this.alice.address, this.bob.address, amount, this.mockUSDT.address, invalidSignature)
).to.be.revertedWith("InvalidSignature");
});

it("Should fail with insufficient balance", async function () {
const amount = ethers.utils.parseEther("1000");
const messageHash = ethers.utils.solidityKeccak256(
["address", "address", "uint256", "address"],
[this.alice.address, this.bob.address, amount, this.mockUSDT.address]
);
const signature = await this.alice.signMessage(ethers.utils.arrayify(messageHash));

await expect(
this.gateway
.connect(this.aggregator)
.withdrawFrom(this.alice.address, this.bob.address, amount, this.mockUSDT.address, signature)
).to.be.revertedWith("InsufficientBalance");
});

it("Should fail when called by non-aggregator", async function () {
const amount = ethers.utils.parseEther("1000");
const messageHash = ethers.utils.solidityKeccak256(
["address", "address", "address", "uint256"],
[this.alice.address, this.bob.address, this.mockUSDT.address, amount]
);
const ethSignedMessageHash = ethers.utils.hashMessage(messageHash);
const signature = await this.alice.signMessage(ethers.utils.arrayify(ethSignedMessageHash));

await expect(
this.gateway
.connect(this.alice)
.withdrawFrom(this.alice.address, this.bob.address, amount, this.mockUSDT.address, signature)
).to.be.revertedWith("OnlyAggregator");
});
});
1 change: 1 addition & 0 deletions test/utils/utils.manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const Events = {
ProtocolAddressUpdated: "ProtocolAddressUpdated",
Deposit: "Deposit",
OrderSettledIn: "OrderSettledIn",
Withdrawn: "Withdrawn",
},
};

Expand Down
Loading