Skip to content

Commit

Permalink
generate
Browse files Browse the repository at this point in the history
  • Loading branch information
lumtis committed Jun 19, 2024
1 parent 5b893d2 commit 776caef
Show file tree
Hide file tree
Showing 6 changed files with 2,197 additions and 242 deletions.
585 changes: 585 additions & 0 deletions pkg/contracts/prototypes/erc20custodynew.sol/erc20custodynew.go

Large diffs are not rendered by default.

308 changes: 268 additions & 40 deletions pkg/contracts/prototypes/gateway.sol/gateway.go

Large diffs are not rendered by default.

317 changes: 315 additions & 2 deletions pkg/contracts/prototypes/receiver.sol/receiver.go

Large diffs are not rendered by default.

823 changes: 823 additions & 0 deletions pkg/contracts/prototypes/testerc20.sol/testerc20.go

Large diffs are not rendered by default.

232 changes: 117 additions & 115 deletions test/prototypes/GatewayIntegration.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,118 +3,120 @@ import { Contract } from "ethers";
import { ethers } from "hardhat";

describe("Gateway and Receiver", function () {
let receiver: Contract;
let gateway: Contract;
let token: Contract;
let custody: Contract;
let owner: any, destination: any;

beforeEach(async function () {
const TestERC20 = await ethers.getContractFactory("TestERC20");
const Receiver = await ethers.getContractFactory("Receiver");
const Gateway = await ethers.getContractFactory("Gateway");
const Custody = await ethers.getContractFactory("ERC20CustodyNew");
[owner, destination] = await ethers.getSigners();

// Deploy the contracts
token = await TestERC20.deploy("Test Token", "TTK");
receiver = await Receiver.deploy();
gateway = await Gateway.deploy();
custody = await Custody.deploy(gateway.address);

gateway.setCustody(custody.address);

// Mint initial supply to the owner
await token.mint(owner.address, ethers.utils.parseEther("1000"));

// Transfer some tokens to the custody contract
await token.transfer(custody.address, ethers.utils.parseEther("500"));
});

it("should forward call to Receiver's receiveA function", async function () {
const str = "Hello, Hardhat!";
const num = 42;
const flag = true;
const value = ethers.utils.parseEther("1.0");

// Encode the function call data
const data = receiver.interface.encodeFunctionData("receiveA", [str, num, flag]);

// Call execute on the Gateway contract
const tx = await gateway.execute(receiver.address, data, { value: value });
await tx.wait();

// Listen for the event
await expect(tx).to.emit(receiver, "ReceivedA").withArgs(gateway.address, value, str, num, flag);
});

it("should forward call to Receiver's receiveB function", async function () {
const strs = ["Hello", "Hardhat"];
const nums = [1, 2, 3];
const flag = false;
const data = receiver.interface.encodeFunctionData("receiveB", [strs, nums, flag]);
const tx = await gateway.execute(receiver.address, data);
await tx.wait();
await expect(tx).to.emit(receiver, "ReceivedB").withArgs(gateway.address, strs, nums, flag);
});

it("should forward call with withdrawAndCall and give allowance to destination contract", async function () {
const amount = ethers.utils.parseEther("100");

// Encode the function call data for receiveC
const data = receiver.interface.encodeFunctionData("receiveC", [amount, token.address, destination.address]);

// Withdraw and call
const tx = await custody.withdrawAndCall(token.address, receiver.address, amount, data);
await tx.wait();

// Verify the event was emitted
await expect(tx).to.emit(receiver, "ReceivedC").withArgs(gateway.address, amount, token.address, destination.address);

// Verify that the tokens were transferred to the destination address
const receiverBalance = await token.balanceOf(destination.address);
expect(receiverBalance).to.equal(amount);

// Verify that the remaining tokens were refunded to the Custody contract
const remainingBalance = await token.balanceOf(custody.address);
expect(remainingBalance).to.equal(ethers.utils.parseEther("400"));

// Verify that the approval was reset
const allowance = await token.allowance(gateway.address, receiver.address);
expect(allowance).to.equal(0);
});

it("should forward call to Receiver's receiveD function", async function () {
const data = receiver.interface.encodeFunctionData("receiveD");

// Execute the call
const tx = await gateway.execute(receiver.address, data);
await tx.wait();

// Verify the event was emitted
await expect(tx).to.emit(receiver, "ReceivedD").withArgs(gateway.address)
});

it("should forward call to Receiver's receiveD function through withdrawAndCall and return ERC20 tokens to custody", async function () {
const amount = ethers.utils.parseEther("100");

// Encode the function call data for receiveD
const data = receiver.interface.encodeFunctionData("receiveD");

// Withdraw and call
await custody.withdrawAndCall(token.address, receiver.address, amount, data);

// Verify the event was emitted
await expect(custody.withdrawAndCall(token.address, receiver.address, amount, data))
.to.emit(receiver, "ReceivedD")
.withArgs(gateway.address);

// Verify that the remaining tokens were refunded to the Custody contract
const remainingBalance = await token.balanceOf(custody.address);
expect(remainingBalance).to.equal(ethers.utils.parseEther("500"));

// Verify that the approval was reset
const allowance = await token.allowance(gateway.address, receiver.address);
expect(allowance).to.equal(0);
});
});
let receiver: Contract;
let gateway: Contract;
let token: Contract;
let custody: Contract;
let owner: any, destination: any;

beforeEach(async function () {
const TestERC20 = await ethers.getContractFactory("TestERC20");
const Receiver = await ethers.getContractFactory("Receiver");
const Gateway = await ethers.getContractFactory("Gateway");
const Custody = await ethers.getContractFactory("ERC20CustodyNew");
[owner, destination] = await ethers.getSigners();

// Deploy the contracts
token = await TestERC20.deploy("Test Token", "TTK");
receiver = await Receiver.deploy();
gateway = await Gateway.deploy();
custody = await Custody.deploy(gateway.address);

gateway.setCustody(custody.address);

// Mint initial supply to the owner
await token.mint(owner.address, ethers.utils.parseEther("1000"));

// Transfer some tokens to the custody contract
await token.transfer(custody.address, ethers.utils.parseEther("500"));
});

it("should forward call to Receiver's receiveA function", async function () {
const str = "Hello, Hardhat!";
const num = 42;
const flag = true;
const value = ethers.utils.parseEther("1.0");

// Encode the function call data
const data = receiver.interface.encodeFunctionData("receiveA", [str, num, flag]);

// Call execute on the Gateway contract
const tx = await gateway.execute(receiver.address, data, { value: value });
await tx.wait();

// Listen for the event
await expect(tx).to.emit(receiver, "ReceivedA").withArgs(gateway.address, value, str, num, flag);
});

it("should forward call to Receiver's receiveB function", async function () {
const strs = ["Hello", "Hardhat"];
const nums = [1, 2, 3];
const flag = false;
const data = receiver.interface.encodeFunctionData("receiveB", [strs, nums, flag]);
const tx = await gateway.execute(receiver.address, data);
await tx.wait();
await expect(tx).to.emit(receiver, "ReceivedB").withArgs(gateway.address, strs, nums, flag);
});

it("should forward call with withdrawAndCall and give allowance to destination contract", async function () {
const amount = ethers.utils.parseEther("100");

// Encode the function call data for receiveC
const data = receiver.interface.encodeFunctionData("receiveC", [amount, token.address, destination.address]);

// Withdraw and call
const tx = await custody.withdrawAndCall(token.address, receiver.address, amount, data);
await tx.wait();

// Verify the event was emitted
await expect(tx)
.to.emit(receiver, "ReceivedC")
.withArgs(gateway.address, amount, token.address, destination.address);

// Verify that the tokens were transferred to the destination address
const receiverBalance = await token.balanceOf(destination.address);
expect(receiverBalance).to.equal(amount);

// Verify that the remaining tokens were refunded to the Custody contract
const remainingBalance = await token.balanceOf(custody.address);
expect(remainingBalance).to.equal(ethers.utils.parseEther("400"));

// Verify that the approval was reset
const allowance = await token.allowance(gateway.address, receiver.address);
expect(allowance).to.equal(0);
});

it("should forward call to Receiver's receiveD function", async function () {
const data = receiver.interface.encodeFunctionData("receiveD");

// Execute the call
const tx = await gateway.execute(receiver.address, data);
await tx.wait();

// Verify the event was emitted
await expect(tx).to.emit(receiver, "ReceivedD").withArgs(gateway.address);
});

it("should forward call to Receiver's receiveD function through withdrawAndCall and return ERC20 tokens to custody", async function () {
const amount = ethers.utils.parseEther("100");

// Encode the function call data for receiveD
const data = receiver.interface.encodeFunctionData("receiveD");

// Withdraw and call
await custody.withdrawAndCall(token.address, receiver.address, amount, data);

// Verify the event was emitted
await expect(custody.withdrawAndCall(token.address, receiver.address, amount, data))
.to.emit(receiver, "ReceivedD")
.withArgs(gateway.address);

// Verify that the remaining tokens were refunded to the Custody contract
const remainingBalance = await token.balanceOf(custody.address);
expect(remainingBalance).to.equal(ethers.utils.parseEther("500"));

// Verify that the approval was reset
const allowance = await token.allowance(gateway.address, receiver.address);
expect(allowance).to.equal(0);
});
});
Loading

0 comments on commit 776caef

Please sign in to comment.