Skip to content

Commit

Permalink
add toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
andresaiello committed Mar 13, 2024
1 parent ccd87af commit f871058
Show file tree
Hide file tree
Showing 7 changed files with 1,632 additions and 89 deletions.
2 changes: 1 addition & 1 deletion packages/zeta-app-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@
"@zetachain/protocol-contracts": "^4.0.1",
"ethers": "5.6.8"
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
pragma solidity 0.8.7;

import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol";
import "@zetachain/toolkit/contracts/SwapHelperLib.sol";

// import "@zetachain/toolkit/contracts/SwapHelperLib.sol";

contract WithdrawERC20 {
SystemContract public immutable systemContract;
Expand All @@ -16,17 +17,17 @@ contract WithdrawERC20 {

(address gasZRC20, uint256 gasFee) = IZRC20(zrc20).withdrawGasFee();

uint256 inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract.wZetaContractAddress(),
systemContract.uniswapv2FactoryAddress(),
systemContract.uniswapv2Router02Address(),
zrc20,
gasFee,
gasZRC20,
amount
);

IZRC20(gasZRC20).approve(zrc20, gasFee);
IZRC20(zrc20).withdraw(to, amount - inputForGas);
// uint256 inputForGas = SwapHelperLib.swapTokensForExactTokens(
// systemContract.wZetaContractAddress(),
// systemContract.uniswapv2FactoryAddress(),
// systemContract.uniswapv2Router02Address(),
// zrc20,
// gasFee,
// gasZRC20,
// amount
// );

// IZRC20(gasZRC20).approve(zrc20, gasFee);
// IZRC20(zrc20).withdraw(to, amount - inputForGas);
}
}
1 change: 1 addition & 0 deletions packages/zevm-app-contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"@uniswap/v2-periphery": "1.1.0-beta.0",
"@zetachain/networks": "^4.0.0",
"@zetachain/protocol-contracts": "^4.0.1",
"@zetachain/toolkit": "^5.0.0",
"ethers": "5.6.8"
}
}
20 changes: 5 additions & 15 deletions packages/zevm-app-contracts/scripts/withdrawERC20/deploy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { isProtocolNetworkName } from "@zetachain/protocol-contracts";
import { ethers, network } from "hardhat";

import { ERC20__factory, WithdrawERC20__factory } from "../../typechain-types";
import { WithdrawERC20__factory } from "../../typechain-types";
import { saveAddress } from "../address.helpers";

const networkName = network.name;
Expand All @@ -11,21 +11,11 @@ async function main() {

const WithdrawERC20Factory = (await ethers.getContractFactory("WithdrawERC20")) as WithdrawERC20__factory;

// const withdrawERC20 = await WithdrawERC20Factory.deploy();
// await withdrawERC20.deployed();
// console.log("WithdrawERC20 deployed to:", withdrawERC20.address);
// saveAddress("withdrawERC20", withdrawERC20.address);
const withdrawERC20 = await WithdrawERC20Factory.deploy();
await withdrawERC20.deployed();

const WithdrawERC20 = WithdrawERC20Factory.attach("0x16a790cf206bbcd30cb2e36cc7b1f890d260b80c");
const ERC20Factory = (await ethers.getContractFactory("ERC20")) as ERC20__factory;
const erc20 = ERC20Factory.attach("0x0cbe0dF132a6c6B4a2974Fa1b7Fb953CF0Cc798a");

await erc20.approve(WithdrawERC20.address, ethers.utils.parseUnits("5", 6));
await WithdrawERC20.withdraw(
erc20.address,
ethers.utils.parseUnits("5", 6),
"0x19caCb4c0A7fC25598CC44564ED0eCA01249fc31"
);
console.log("WithdrawERC20 deployed to:", withdrawERC20.address);
saveAddress("withdrawERC20", withdrawERC20.address, networkName);
}

main().catch((error) => {
Expand Down
34 changes: 34 additions & 0 deletions packages/zevm-app-contracts/scripts/withdrawERC20/withdraw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { isProtocolNetworkName } from "@zetachain/protocol-contracts";
import { ethers, network } from "hardhat";

import { ERC20__factory, WithdrawERC20__factory } from "../../typechain-types";
import { saveAddress } from "../address.helpers";

const networkName = network.name;

async function main() {
if (!isProtocolNetworkName(networkName)) throw new Error("Invalid network name");

const WithdrawERC20Factory = (await ethers.getContractFactory("WithdrawERC20")) as WithdrawERC20__factory;

// const withdrawERC20 = await WithdrawERC20Factory.deploy();
// await withdrawERC20.deployed();
// console.log("WithdrawERC20 deployed to:", withdrawERC20.address);
// saveAddress("withdrawERC20", withdrawERC20.address);

const WithdrawERC20 = WithdrawERC20Factory.attach("0x16a790cf206bbcd30cb2e36cc7b1f890d260b80c");
const ERC20Factory = (await ethers.getContractFactory("ERC20")) as ERC20__factory;
const erc20 = ERC20Factory.attach("0x0cbe0dF132a6c6B4a2974Fa1b7Fb953CF0Cc798a");

await erc20.approve(WithdrawERC20.address, ethers.utils.parseUnits("5", 6));
await WithdrawERC20.withdraw(
erc20.address,
ethers.utils.parseUnits("5", 6),
"0x19caCb4c0A7fC25598CC44564ED0eCA01249fc31"
);
}

main().catch((error) => {
console.error(error);
process.exit(1);
});
105 changes: 105 additions & 0 deletions packages/zevm-app-contracts/test/withdrawERC20/withdrawERC20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import { parseUnits } from "@ethersproject/units";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { expect } from "chai";
import { parseEther } from "ethers/lib/utils";
import { ethers, network } from "hardhat";

import { Disperse, Disperse__factory, MockZRC20, MockZRC20__factory } from "../typechain-types";

describe("WithdrawERC20 tests", () => {
let withdrawERC20Contract: WithdrawERC20;

let accounts: SignerWithAddress[];
let deployer: SignerWithAddress;

beforeEach(async () => {
[deployer, ...accounts] = await ethers.getSigners();

await network.provider.send("hardhat_setBalance", [deployer.address, parseUnits("1000000").toHexString()]);

const DisperseFactory = (await ethers.getContractFactory("Disperse")) as Disperse__factory;
withdrawERC20Contract = (await DisperseFactory.deploy()) as Disperse;
await withdrawERC20Contract.deployed();
});

describe("Disperse", () => {
it("Should disperse ETH", async () => {
const count = 500;
const amount = parseEther("0.01");
const balance0 = await ethers.provider.getBalance(accounts[0].address);
const balance1 = await ethers.provider.getBalance(accounts[1].address);

const bigArrayAddress = new Array(count).fill(accounts[0].address);
const bigArrayAmount = new Array(count).fill(amount);

await withdrawERC20Contract.disperseEther(bigArrayAddress, bigArrayAmount, {
value: amount.mul(count),
});

const balance0After = await ethers.provider.getBalance(accounts[0].address);
const balance1After = await ethers.provider.getBalance(accounts[1].address);

expect(balance0After.sub(balance0)).to.be.eq(amount.mul(count));
expect(balance1After.sub(balance1)).to.be.eq(0);
});

it("Should disperse ETH with surplus", async () => {
const amount = parseUnits("10");
const balance0 = await ethers.provider.getBalance(accounts[0].address);
const balance1 = await ethers.provider.getBalance(accounts[1].address);
await withdrawERC20Contract.disperseEther([accounts[0].address, accounts[1].address], [amount, amount.mul(2)], {
value: amount.mul(4),
});

const balance0After = await ethers.provider.getBalance(accounts[0].address);
const balance1After = await ethers.provider.getBalance(accounts[1].address);

expect(balance0After.sub(balance0)).to.be.eq(amount);
expect(balance1After.sub(balance1)).to.be.eq(amount.mul(2));
});

it("Should disperse token", async () => {
const MockTokenFactory = (await ethers.getContractFactory("MockZRC20")) as MockZRC20__factory;
const mockTokenContract = (await MockTokenFactory.deploy(1_000_000, "MOCK", "MOCK")) as MockZRC20;
await mockTokenContract.deployed();
await mockTokenContract.approve(withdrawERC20Contract.address, parseUnits("1000000"));

const amount = parseUnits("10");
const balance0 = await mockTokenContract.balanceOf(accounts[0].address);
const balance1 = await mockTokenContract.balanceOf(accounts[1].address);
await withdrawERC20Contract.disperseToken(
mockTokenContract.address,
[accounts[0].address, accounts[1].address],
[amount, amount.mul(2)]
);

const balance0After = await mockTokenContract.balanceOf(accounts[0].address);
const balance1After = await mockTokenContract.balanceOf(accounts[1].address);

expect(balance0After.sub(balance0)).to.be.eq(amount);
expect(balance1After.sub(balance1)).to.be.eq(amount.mul(2));
});

it("Should disperse token simple", async () => {
const MockTokenFactory = (await ethers.getContractFactory("MockZRC20")) as MockZRC20__factory;
const mockTokenContract = (await MockTokenFactory.deploy(1_000_000, "MOCK", "MOCK")) as MockZRC20;
await mockTokenContract.deployed();
await mockTokenContract.approve(withdrawERC20Contract.address, parseUnits("1000000"));

const amount = parseUnits("10");
const balance0 = await mockTokenContract.balanceOf(accounts[0].address);
const balance1 = await mockTokenContract.balanceOf(accounts[1].address);
await withdrawERC20Contract.disperseTokenSimple(
mockTokenContract.address,
[accounts[0].address, accounts[1].address],
[amount, amount.mul(2)]
);

const balance0After = await mockTokenContract.balanceOf(accounts[0].address);
const balance1After = await mockTokenContract.balanceOf(accounts[1].address);

expect(balance0After.sub(balance0)).to.be.eq(amount);
expect(balance1After.sub(balance1)).to.be.eq(amount.mul(2));
});
});
});
Loading

0 comments on commit f871058

Please sign in to comment.