Skip to content

Commit

Permalink
fix: fix localnet issues (#265)
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Bertrand <[email protected]>
  • Loading branch information
skosito and lumtis authored Jul 30, 2024
1 parent d1746fd commit 2ea8f56
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 37 deletions.
73 changes: 47 additions & 26 deletions scripts/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,53 @@ const hre = require("hardhat");

export const FUNGIBLE_MODULE_ADDRESS = "0x735b14BB79463307AAcBED86DAf3322B1e6226aB";

const deploySystemContracts = async (tss: SignerWithAddress) => {
const deployProtocolContracts = async (
tss: SignerWithAddress,
ownerEVM: SignerWithAddress,
ownerZEVM: SignerWithAddress,
fungibleModuleSigner: SignerWithAddress
) => {
// Prepare EVM
// Deploy system contracts (gateway and custody)
const TestERC20 = await ethers.getContractFactory("TestERC20");

const zetaToken = await TestERC20.deploy("Zeta Token", "ZETA");
const GatewayEVM = await ethers.getContractFactory("GatewayEVM");
const Custody = await ethers.getContractFactory("ERC20CustodyNew");
const ZetaConnector = await ethers.getContractFactory("ZetaConnectorNonNative");

const gatewayEVM = await upgrades.deployProxy(GatewayEVM, [tss.address], {
const gatewayEVM = await upgrades.deployProxy(GatewayEVM, [tss.address, zetaToken.address], {
initializer: "initialize",
kind: "uups",
});
console.log("GatewayEVM:", gatewayEVM.address);

const custody = await Custody.deploy(gatewayEVM.address);
await gatewayEVM.setCustody(custody.address);
const custody = await Custody.deploy(gatewayEVM.address, tss.address);
await gatewayEVM.connect(tss).setCustody(custody.address);

const zetaConnector = await ZetaConnector.deploy(gatewayEVM.address, zetaToken.address, tss.address);
await gatewayEVM.connect(tss).setConnector(zetaConnector.address);

// Prepare ZEVM
// Deploy system contracts (gateway and system)
const SystemContractFactory = await ethers.getContractFactory("SystemContractMock");
const systemContract = (await SystemContractFactory.deploy(AddressZero, AddressZero, AddressZero)) as SystemContract;

const WZETAFactory = await ethers.getContractFactory("contracts/zevm/WZETA.sol:WETH9");
const wzeta = await WZETAFactory.deploy();

const GatewayZEVM = await ethers.getContractFactory("GatewayZEVM");
const gatewayZEVM = await upgrades.deployProxy(GatewayZEVM, [], {
const gatewayZEVM = await upgrades.deployProxy(GatewayZEVM, [wzeta.address], {
initializer: "initialize",
kind: "uups",
});
console.log("GatewayZEVM:", gatewayZEVM.address);

await wzeta.connect(fungibleModuleSigner).deposit({ value: ethers.utils.parseEther("10") });
await wzeta.connect(fungibleModuleSigner).approve(gatewayZEVM.address, ethers.utils.parseEther("10"));
await wzeta.connect(ownerZEVM).deposit({ value: ethers.utils.parseEther("10") });
await wzeta.connect(ownerZEVM).approve(gatewayZEVM.address, ethers.utils.parseEther("10"));

return {
custody,
gatewayEVM,
Expand All @@ -44,7 +64,7 @@ const deploySystemContracts = async (tss: SignerWithAddress) => {
};

const deployTestContracts = async (
systemContracts,
protocolContracts,
ownerEVM: SignerWithAddress,
ownerZEVM: SignerWithAddress,
fungibleModuleSigner: SignerWithAddress
Expand All @@ -56,10 +76,11 @@ const deployTestContracts = async (

const token = await TestERC20.deploy("Test Token", "TTK");
const receiverEVM = await ReceiverEVM.deploy();

await token.mint(ownerEVM.address, ethers.utils.parseEther("1000"));

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

// Prepare ZEVM
// Deploy test contracts (test zContract, zrc20, sender) and mint funds to test accounts
Expand All @@ -74,14 +95,14 @@ const deployTestContracts = async (
1,
1,
0,
systemContracts.systemContract.address,
systemContracts.gatewayZEVM.address
protocolContracts.systemContract.address,
protocolContracts.gatewayZEVM.address
)) as ZRC20;

await systemContracts.systemContract.setGasCoinZRC20(1, ZRC20Contract.address);
await systemContracts.systemContract.setGasPrice(1, ZRC20Contract.address);
await protocolContracts.systemContract.setGasCoinZRC20(1, ZRC20Contract.address);
await protocolContracts.systemContract.setGasPrice(1, ZRC20Contract.address);
await ZRC20Contract.connect(fungibleModuleSigner).deposit(ownerZEVM.address, parseEther("100"));
await ZRC20Contract.connect(ownerZEVM).approve(systemContracts.gatewayZEVM.address, parseEther("100"));
await ZRC20Contract.connect(ownerZEVM).approve(protocolContracts.gatewayZEVM.address, parseEther("100"));

// Include abi of gatewayZEVM events, so hardhat can decode them automatically
const senderArtifact = await hre.artifacts.readArtifact("SenderZEVM");
Expand All @@ -92,7 +113,7 @@ const deployTestContracts = async (
];

const SenderZEVM = new ethers.ContractFactory(senderABI, senderArtifact.bytecode, ownerZEVM);
const senderZEVM = await SenderZEVM.deploy(systemContracts.gatewayZEVM.address);
const senderZEVM = await SenderZEVM.deploy(protocolContracts.gatewayZEVM.address);
await ZRC20Contract.connect(fungibleModuleSigner).deposit(senderZEVM.address, parseEther("100"));

return {
Expand All @@ -117,28 +138,28 @@ export const startWorker = async () => {
hre.network.provider.send("hardhat_setBalance", [FUNGIBLE_MODULE_ADDRESS, parseEther("1000000").toHexString()]);

// Deploy system and test contracts
const systemContracts = await deploySystemContracts(tss);
const testContracts = await deployTestContracts(systemContracts, ownerEVM, ownerZEVM, fungibleModuleSigner);
const protocolContracts = await deployProtocolContracts(tss, ownerEVM, ownerZEVM, fungibleModuleSigner);
const testContracts = await deployTestContracts(protocolContracts, ownerEVM, ownerZEVM, fungibleModuleSigner);

// Listen to contracts events
// event Call(address indexed sender, bytes receiver, bytes message);
systemContracts.gatewayZEVM.on("Call", async (...args: Array<any>) => {
protocolContracts.gatewayZEVM.on("Call", async (...args: Array<any>) => {
console.log("Worker: Call event on GatewayZEVM.");
console.log("Worker: Calling ReceiverEVM through GatewayEVM...");
const receiver = args[1];
const message = args[2];
const executeTx = await systemContracts.gatewayEVM.execute(receiver, message, { value: 0 });
const executeTx = await protocolContracts.gatewayEVM.connect(tss).execute(receiver, message, { value: 0 });
await executeTx.wait();
});

// event Withdrawal(address indexed from, address zrc20, bytes to, uint256 value, uint256 gasfee, uint256 protocolFlatFee, bytes message);
systemContracts.gatewayZEVM.on("Withdrawal", async (...args: Array<any>) => {
protocolContracts.gatewayZEVM.on("Withdrawal", async (...args: Array<any>) => {
console.log("Worker: Withdrawal event on GatewayZEVM.");
const receiver = args[1];
const receiver = args[2];
const message = args[6];
if (message != "0x") {
console.log("Worker: Calling ReceiverEVM through GatewayEVM...");
const executeTx = await systemContracts.gatewayEVM.execute(receiver, message, { value: 0 });
const executeTx = await protocolContracts.gatewayEVM.connect(tss).execute(receiver, message, { value: 0 });
await executeTx.wait();
}
});
Expand All @@ -148,13 +169,13 @@ export const startWorker = async () => {
});

// event Call(address indexed sender, address indexed receiver, bytes payload);
systemContracts.gatewayEVM.on("Call", async (...args: Array<any>) => {
protocolContracts.gatewayEVM.on("Call", async (...args: Array<any>) => {
console.log("Worker: Call event on GatewayEVM.");
console.log("Worker: Calling TestZContract through GatewayZEVM...");
const zContract = args[1];
const payload = args[2];
const executeTx = await systemContracts.gatewayZEVM.connect(fungibleModuleSigner).execute(
[systemContracts.gatewayZEVM.address, fungibleModuleSigner.address, 1],
const executeTx = await protocolContracts.gatewayZEVM.connect(fungibleModuleSigner).execute(
[protocolContracts.gatewayZEVM.address, fungibleModuleSigner.address, 1],
// onCrosschainCall contains zrc20 and amount which is not available in Call event
testContracts.ZRC20Contract.address,
parseEther("0"),
Expand All @@ -165,17 +186,17 @@ export const startWorker = async () => {
});

// event Deposit(address indexed sender, address indexed receiver, uint256 amount, address asset, bytes payload);
systemContracts.gatewayEVM.on("Deposit", async (...args: Array<any>) => {
protocolContracts.gatewayEVM.on("Deposit", async (...args: Array<any>) => {
console.log("Worker: Deposit event on GatewayEVM.");
const receiver = args[1];
const asset = args[3];
const payload = args[4];
if (payload != "0x") {
console.log("Worker: Calling TestZContract through GatewayZEVM...");
const executeTx = await systemContracts.gatewayZEVM
const executeTx = await protocolContracts.gatewayZEVM
.connect(fungibleModuleSigner)
.execute(
[systemContracts.gatewayZEVM.address, fungibleModuleSigner.address, 1],
[protocolContracts.gatewayZEVM.address, fungibleModuleSigner.address, 1],
asset,
parseEther("0"),
receiver,
Expand Down
28 changes: 17 additions & 11 deletions tasks/localnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ declare const hre: any;
// Otherwise, provide custom addresses as parameters.

task("zevm-call", "calls evm contract from zevm account")
.addOptionalParam("gatewayZEVM", "contract address of gateway on ZEVM", "0x0165878A594ca255338adfa4d48449f69242Eb8F")
.addOptionalParam("receiverEVM", "contract address of receiver on EVM", "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6")
.addOptionalParam("gatewayZEVM", "contract address of gateway on ZEVM", "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6")
.addOptionalParam("receiverEVM", "contract address of receiver on EVM", "0x610178dA211FEF7D417bC0e6FeD39F05609AD788")
.setAction(async (taskArgs) => {
const gatewayZEVM = await hre.ethers.getContractAt("GatewayZEVM", taskArgs.gatewayZEVM);
const receiverEVM = await hre.ethers.getContractAt("ReceiverEVM", taskArgs.receiverEVM);
Expand All @@ -22,15 +22,16 @@ task("zevm-call", "calls evm contract from zevm account")
try {
const callTx = await gatewayZEVM.call(receiverEVM.address, message);
await callTx.wait();
console.log("ReceiverEVM called from ZEVM");
} catch (e) {
console.error("Error calling ReceiverEVM:", e);
}
});

task("zevm-withdraw-and-call", "withdraws zrc20 and calls evm contract from zevm account")
.addOptionalParam("gatewayZEVM", "contract address of gateway on ZEVM", "0x0165878A594ca255338adfa4d48449f69242Eb8F")
.addOptionalParam("receiverEVM", "contract address of receiver on EVM", "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6")
.addOptionalParam("zrc20", "contract address of zrc20", "0x9fd96203f7b22bCF72d9DCb40ff98302376cE09c")
.addOptionalParam("gatewayZEVM", "contract address of gateway on ZEVM", "0x2279B7A0a67DB372996a5FaB50D91eAA73d2eBe6")
.addOptionalParam("receiverEVM", "contract address of receiver on EVM", "0x610178dA211FEF7D417bC0e6FeD39F05609AD788")
.addOptionalParam("zrc20", "contract address of zrc20", "0x2ca7d64A7EFE2D62A725E2B35Cf7230D6677FfEe")
.addOptionalParam("amount", "amount to withdraw", "1")
.setAction(async (taskArgs) => {
const gatewayZEVM = await hre.ethers.getContractAt("GatewayZEVM", taskArgs.gatewayZEVM);
Expand All @@ -48,7 +49,12 @@ task("zevm-withdraw-and-call", "withdraws zrc20 and calls evm contract from zevm
try {
const callTx = await gatewayZEVM
.connect(ownerZEVM)
.withdrawAndCall(receiverEVM.address, hre.ethers.utils.parseEther(taskArgs.amount), zrc20.address, message);
["withdrawAndCall(bytes,uint256,address,bytes)"](
receiverEVM.address,
hre.ethers.utils.parseEther(taskArgs.amount),
zrc20.address,
message
);
await callTx.wait();
console.log("ReceiverEVM called from ZEVM");
} catch (e) {
Expand All @@ -57,8 +63,8 @@ task("zevm-withdraw-and-call", "withdraws zrc20 and calls evm contract from zevm
});

task("evm-call", "calls zevm zcontract from evm account")
.addOptionalParam("gatewayEVM", "contract address of gateway on EVM", "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512")
.addOptionalParam("zContract", "contract address of zContract on ZEVM", "0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e")
.addOptionalParam("gatewayEVM", "contract address of gateway on EVM", "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0")
.addOptionalParam("zContract", "contract address of zContract on ZEVM", "0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82")
.setAction(async (taskArgs) => {
const gatewayEVM = await hre.ethers.getContractAt("GatewayEVM", taskArgs.gatewayEVM);
const zContract = await hre.ethers.getContractAt("TestZContract", taskArgs.zContract);
Expand All @@ -75,9 +81,9 @@ task("evm-call", "calls zevm zcontract from evm account")
});

task("evm-deposit-and-call", "deposits erc20 and calls zevm zcontract from evm account")
.addOptionalParam("gatewayEVM", "contract address of gateway on EVM", "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512")
.addOptionalParam("zContract", "contract address of zContract on ZEVM", "0xB7f8BC63BbcaD18155201308C8f3540b07f84F5e")
.addOptionalParam("erc20", "contract address of erc20", "0xa513e6e4b8f2a923d98304ec87f64353c4d5c853")
.addOptionalParam("gatewayEVM", "contract address of gateway on EVM", "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0")
.addOptionalParam("zContract", "contract address of zContract on ZEVM", "0x0DCd1Bf9A1b36cE34237eEaFef220932846BCD82")
.addOptionalParam("erc20", "contract address of erc20", "0x8A791620dd6260079BF849Dc5567aDC3F2FdC318")
.addOptionalParam("amount", "amount to deposit", "1")
.setAction(async (taskArgs) => {
const gatewayEVM = await hre.ethers.getContractAt("GatewayEVM", taskArgs.gatewayEVM);
Expand Down

0 comments on commit 2ea8f56

Please sign in to comment.