Skip to content

feat: update Gateway contract for order settlement logic #65

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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

This file was deleted.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion artifacts/contracts/Gateway.sol/Gateway.dbg.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/b92a18bd36be0e82f59e420b9f0d4c50.json"
"buildInfo": "../../build-info/d61b59df6bfad998198e6e3c456345ab.json"
}
4 changes: 2 additions & 2 deletions artifacts/contracts/Gateway.sol/Gateway.json

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions artifacts/hardhat/console.sol/console.dbg.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/c06096a1ebd484f760f1a9f4fc67718a.json"
}
10 changes: 10 additions & 0 deletions artifacts/hardhat/console.sol/console.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"_format": "hh-sol-artifact-1",
"contractName": "console",
"sourceName": "hardhat/console.sol",
"abi": [],
"bytecode": "0x60566050600b82828239805160001a6073146043577f4e487b7100000000000000000000000000000000000000000000000000000000600052600060045260246000fd5b30600052607381538281f3fe73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220470fcfd58def13a889bbd652f04e000f5a30bded8ac7e3420af9916b59c60abd64736f6c63430008120033",
"deployedBytecode": "0x73000000000000000000000000000000000000000030146080604052600080fdfea2646970667358221220470fcfd58def13a889bbd652f04e000f5a30bded8ac7e3420af9916b59c60abd64736f6c63430008120033",
"linkReferences": {},
"deployedLinkReferences": {}
}
3 changes: 2 additions & 1 deletion contracts/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
address token = order[_orderId].token;

// subtract sum of amount based on the input _settlePercent
uint256 currentOrderBPS = order[_orderId].currentBPS;
order[_orderId].currentBPS -= _settlePercent;

if (order[_orderId].currentBPS == 0) {
Expand All @@ -179,7 +180,7 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
}

// transfer to liquidity provider
uint256 liquidityProviderAmount = (order[_orderId].amount * _settlePercent) / MAX_BPS;
uint256 liquidityProviderAmount = (order[_orderId].amount * _settlePercent) / currentOrderBPS;
order[_orderId].amount -= liquidityProviderAmount;

uint256 protocolFee = (liquidityProviderAmount * protocolFeePercent) / MAX_BPS;
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@
"repository": "https://github.com/paycrest/contracts",
"scripts": {
"test": "npx hardhat test",
"ownership": "npx hardhat test test/paycrest/paycrest.ownable.test.js",
"settleOrder": "npx hardhat test test/paycrest/paycrest.settleOrder.test.js",
"createOrder": "npx hardhat test test/paycrest/paycrest.createorder.test.js",
"ownership": "npx hardhat test test/gateway/gateway.ownable.test.js",
"settleOrder": "npx hardhat test test/gateway/gateway.settleOrder.test.js",
"createOrder": "npx hardhat test test/gateway/gateway.createorder.test.js",
"compile": "npx hardhat compile",
"lint:sol": "prettier --loglevel warn --ignore-path .gitignore 'contracts/**/*.sol' --check && solhint 'contracts/**/*.sol'",
"lint:sol:fix": "prettier --loglevel warn --ignore-path .gitignore 'contracts/**/*.sol' --write"
},
"keywords": [],
"author": "https://onahprosperity.github.io/",
"author": "https://onahprosper.github.io/",
"license": "MIT",
"devDependencies": {
"@daochild/tronweb-typescript": "^1.1.2",
Expand Down
141 changes: 138 additions & 3 deletions test/gateway/gateway.settleOrder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ describe("Gateway settle order", function () {
this.alice,
this.bob,
this.liquidityProvider,
this.liquidityProvider2,
this.sender,
this.hacker,
...this.accounts
Expand All @@ -37,17 +38,17 @@ describe("Gateway settle order", function () {
//

// charge 0.1% as protocol fee
const protocolFeePercent = BigNumber.from(100);
this.protocolFeePercent = BigNumber.from(100);

this.protocolFee = ethers.utils.parseEther("27000")

this.liquidityProviderAmount = this.orderAmount.sub(this.protocolFee);

await expect(
gateway.connect(this.deployer).updateProtocolFee(protocolFeePercent)
gateway.connect(this.deployer).updateProtocolFee(this.protocolFeePercent)
)
.to.emit(gateway, Events.Gateway.ProtocolFeeUpdated)
.withArgs(protocolFeePercent);
.withArgs(this.protocolFeePercent);

await mockUSDT.connect(this.alice).mint(this.mintAmount);

Expand Down Expand Up @@ -211,6 +212,140 @@ describe("Gateway settle order", function () {
expect(await mockUSDT.balanceOf(gateway.address)).to.eq(ZERO_AMOUNT);
});

it("Should be able to create order by the sender and split the order", async function () {
const ret = await getSupportedInstitutions();

await mockUSDT
.connect(this.sender)
.approve(gateway.address, this.mintAmount);

expect(
await mockUSDT.allowance(this.sender.address, gateway.address)
).to.equal(this.mintAmount);

const rate = 750;
const data = [
{ bank_account: "09090990901" },
{ bank_name: "ACCESS BANK" },
{ account_name: "Jeff Dean" },
{ institution_code: ret.accessBank.code },
];
const password = "123";

const cipher = CryptoJS.AES.encrypt(
JSON.stringify(data),
password
).toString();

const messageHash = "0x" + cipher;

const argOrderID = [this.sender.address, 1];

const encoded = ethers.utils.defaultAbiCoder.encode(
["address", "uint256"],
argOrderID
);
const orderId = ethers.utils.solidityKeccak256(["bytes"], [encoded]);

await expect(
gateway
.connect(this.sender)
.createOrder(
mockUSDT.address,
this.orderAmount,
rate,
this.sender.address,
this.senderFee,
this.alice.address,
messageHash.toString()
)
)
.to.emit(gateway, Events.Gateway.OrderCreated)
.withArgs(
this.alice.address,
mockUSDT.address,
this.orderAmount,
this.protocolFee,
orderId,
rate,
messageHash.toString()
);

[
this.seller,
this.token,
this.senderRecipient,
this.senderFee,
this.protocolFee,
this.isFulfilled,
this.isRefunded,
this.refundAddress,
this.currentBPS,
this.amount,
] = await gateway.getOrderInfo(orderId);

expect(this.seller).to.eq(this.sender.address);
expect(this.token).to.eq(mockUSDT.address);
expect(this.senderRecipient).to.eq(this.sender.address);
expect(this.senderFee).to.eq(this.senderFee);
expect(this.isFulfilled).to.eq(false);
expect(this.isRefunded).to.eq(false);
expect(this.refundAddress).to.eq(this.alice.address);
expect(this.currentBPS).to.eq(MAX_BPS);
expect(this.amount).to.eq(BigNumber.from(this.orderAmount));

expect(await mockUSDT.balanceOf(this.alice.address)).to.eq(ZERO_AMOUNT);

expect(
await mockUSDT.allowance(this.alice.address, gateway.address)
).to.equal(ZERO_AMOUNT);

// =================== Create Order ===================
const splitOrderpercent = 50_000; // 50% split
const encodedSplitOrder = ethers.utils.defaultAbiCoder.encode(
["uint256"],
[splitOrderpercent]
);
const splitOrderId = ethers.utils.solidityKeccak256(["bytes"], [encodedSplitOrder]);
// provider balance before
console.log(await mockUSDT.balanceOf(this.liquidityProvider.address))

expect(
await gateway
.connect(this.aggregator)
.settle(splitOrderId, orderId, this.liquidityProvider.address, splitOrderpercent)
).to.emit(gateway, Events.Gateway.OrderSettled)
.withArgs(splitOrderId, orderId, this.liquidityProvider.address, splitOrderpercent);

const splitLiquidityProviderAmount = this.orderAmount.mul(splitOrderpercent).div(MAX_BPS);
const splitProtocolFee = splitLiquidityProviderAmount.mul(this.protocolFeePercent).div(MAX_BPS);

expect(await mockUSDT.balanceOf(this.liquidityProvider.address)).to.eq(
(splitLiquidityProviderAmount.sub(splitProtocolFee))
);
expect(await mockUSDT.balanceOf(this.treasuryAddress.address)).to.eq(
splitProtocolFee
);
expect(await mockUSDT.balanceOf(gateway.address)).to.eq(splitLiquidityProviderAmount);

expect(
await gateway
.connect(this.aggregator)
.settle(splitOrderId, orderId, this.liquidityProvider2.address, splitOrderpercent)
).to.emit(gateway, Events.Gateway.OrderSettled)
.withArgs(splitOrderId, orderId, this.liquidityProvider2.address, splitOrderpercent);

const splitProtocolFeeSplitedOrder = splitLiquidityProviderAmount.mul(this.protocolFeePercent).div(MAX_BPS);

expect(await mockUSDT.balanceOf(this.liquidityProvider2.address)).to.eq(
(splitLiquidityProviderAmount.sub(splitProtocolFeeSplitedOrder))
);
expect(await mockUSDT.balanceOf(this.treasuryAddress.address)).to.eq(
splitProtocolFee.add(splitProtocolFeeSplitedOrder)
);
expect(await mockUSDT.balanceOf(gateway.address)).to.eq(0);
});

it("Should revert when trying to settle an already fulfilled order", async function () {
const ret = await getSupportedInstitutions();

Expand Down