Skip to content

Commit

Permalink
add support for multiple evm chains
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Oct 25, 2024
1 parent af74cba commit a2db10c
Show file tree
Hide file tree
Showing 11 changed files with 275 additions and 205 deletions.
62 changes: 28 additions & 34 deletions packages/localnet/src/createToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,27 @@ import * as ZRC20 from "@zetachain/protocol-contracts/abi/ZRC20.sol/ZRC20.json";
import { deployOpts } from "./deployOpts";
import * as TestERC20 from "@zetachain/protocol-contracts/abi/TestERC20.sol/TestERC20.json";

export const createToken = async ({
fungibleModuleSigner,
deployer,
systemContract,
gatewayZEVM,
foreignCoins,
custody,
tss,
uniswapFactoryInstance,
wzeta,
uniswapRouterInstance,
symbol,
isGasToken = false,
}: {
fungibleModuleSigner: any;
deployer: ethers.Signer;
systemContract: any;
gatewayZEVM: any;
foreignCoins: any[];
custody: ethers.BaseContract;
tss: ethers.Signer;
uniswapFactoryInstance: ethers.BaseContract;
wzeta: ethers.BaseContract;
uniswapRouterInstance: ethers.BaseContract;
symbol: string;
isGasToken: boolean;
}) => {
export const createToken = async (
addresses: any,
custody: any,
symbol: string,
isGasToken: boolean,
chainID: string
) => {
let erc20;

const {
fungibleModuleSigner,
deployer,
foreignCoins,
tss,
systemContract,
gatewayZEVM,
uniswapFactoryInstance,
uniswapRouterInstance,
wzeta,
} = addresses;

const zrc20Factory = new ethers.ContractFactory(
ZRC20.abi,
ZRC20.bytecode,
Expand All @@ -40,10 +32,10 @@ export const createToken = async ({
const zrc20 = await zrc20Factory
.connect(fungibleModuleSigner)
.deploy(
`ZRC-20 ${symbol}`,
`ZRC-20 ${symbol} on ${chainID}`,
`ZRC20${symbol}`,
18,
1,
chainID,
isGasToken ? 1 : 2,
1,
systemContract.target,
Expand All @@ -56,8 +48,10 @@ export const createToken = async ({
if (isGasToken) {
(systemContract as any)
.connect(fungibleModuleSigner)
.setGasCoinZRC20(1, zrc20.target);
(systemContract as any).connect(fungibleModuleSigner).setGasPrice(1, 1);
.setGasCoinZRC20(chainID, zrc20.target);
(systemContract as any)
.connect(fungibleModuleSigner)
.setGasPrice(chainID, 1);
} else {
const erc20Factory = new ethers.ContractFactory(
TestERC20.abi,
Expand Down Expand Up @@ -99,10 +93,10 @@ export const createToken = async ({
foreignCoins.push({
zrc20_contract_address: zrc20.target,
asset: isGasToken ? "" : (erc20 as any).target,
foreign_chain_id: "1",
foreign_chain_id: chainID,
decimals: 18,
name: `ZetaChain ZRC-20 ${symbol}`,
symbol: `${symbol}.ETH`,
name: `ZRC-20 ${symbol} on ${chainID}`,
symbol: `${symbol}`,
coin_type: isGasToken ? "Gas" : "ERC20",
gas_limit: null,
paused: null,
Expand Down
26 changes: 18 additions & 8 deletions packages/localnet/src/handleOnEVMCalled.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { handleOnRevertEVM } from "./handleOnRevertEVM";
import { log, logErr } from "./log";
import { deployOpts } from "./deployOpts";

// event Called(address indexed sender, address indexed receiver, bytes payload, RevertOptions revertOptions);
export const handleOnEVMCalled = async ({
tss,
provider,
Expand All @@ -13,6 +12,10 @@ export const handleOnEVMCalled = async ({
fungibleModuleSigner,
foreignCoins,
exitOnError = false,
chainID,
chain,
gatewayEVM,
custody,
}: {
tss: any;
provider: ethers.JsonRpcProvider;
Expand All @@ -22,20 +25,24 @@ export const handleOnEVMCalled = async ({
fungibleModuleSigner: any;
foreignCoins: any[];
exitOnError: boolean;
chainID: string;
chain: string;
gatewayEVM: any;
custody: any;
}) => {
log("EVM", "Gateway: 'Called' event emitted");
log(chain, "Gateway: 'Called' event emitted");
const sender = args[0];
const receiver = args[1];
const message = args[2];
try {
const sender = args[0];
const receiver = args[1];
const message = args[2];
(deployer as NonceManager).reset();
const context = {
origin: sender,
sender: await fungibleModuleSigner.getAddress(),
chainID: 1,
chainID,
};
const zrc20 = foreignCoins.find(
(coin) => coin.foreign_chain_id === "1" && coin.coin_type === "Gas"
(coin) => coin.foreign_chain_id === chainID && coin.coin_type === "Gas"
)?.zrc20_contract_address;

log(
Expand Down Expand Up @@ -68,8 +75,11 @@ export const handleOnEVMCalled = async ({
isGas: true,
token: "",
provider,
protocolContracts,
exitOnError,
chain,
gatewayEVM,
custody,
sender,
});
}
};
19 changes: 15 additions & 4 deletions packages/localnet/src/handleOnEVMDeposited.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import { deployOpts } from "./deployOpts";
import * as ZRC20 from "@zetachain/protocol-contracts/abi/ZRC20.sol/ZRC20.json";
import * as UniswapV2Router02 from "@uniswap/v2-periphery/build/UniswapV2Router02.json";

// event Deposited(address indexed sender, address indexed receiver, uint256 amount, address asset, bytes payload, RevertOptions revertOptions);
export const handleOnEVMDeposited = async ({
tss,
provider,
Expand All @@ -15,6 +14,10 @@ export const handleOnEVMDeposited = async ({
fungibleModuleSigner,
foreignCoins,
exitOnError = false,
chainID,
chain,
gatewayEVM,
custody,
}: {
tss: any;
provider: ethers.JsonRpcProvider;
Expand All @@ -24,8 +27,13 @@ export const handleOnEVMDeposited = async ({
fungibleModuleSigner: any;
foreignCoins: any[];
exitOnError: boolean;
chainID: string;
chain: string;
gatewayEVM: any;
custody: any;
}) => {
log("EVM", "Gateway: 'Deposited' event emitted");
log(chain, "Gateway: 'Deposited' event emitted");
const sender = args[0];
const receiver = args[1];
const amount = args[2];
const asset = args[3];
Expand All @@ -47,7 +55,7 @@ export const handleOnEVMDeposited = async ({
const context = {
origin: protocolContracts.gatewayZEVM.target,
sender: await fungibleModuleSigner.getAddress(),
chainID: 1,
chainID: chainID,
};

// If message is not empty, execute depositAndCall
Expand Down Expand Up @@ -121,8 +129,11 @@ export const handleOnEVMDeposited = async ({
isGas,
token,
provider,
protocolContracts,
exitOnError,
chain,
gatewayEVM,
custody,
sender,
});
}
};
Expand Down
30 changes: 16 additions & 14 deletions packages/localnet/src/handleOnRevertEVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,49 +9,51 @@ export const handleOnRevertEVM = async ({
err,
provider,
tss,
protocolContracts,
isGas,
token,
exitOnError = false,
chain,
gatewayEVM,
custody,
sender,
}: {
revertOptions: any;
err: any;
asset: any;
amount: any;
provider: any;
tss: any;
protocolContracts: any;
isGas: boolean;
token: string;
exitOnError: boolean;
chain: string;
gatewayEVM: any;
custody: any;
sender: string;
}) => {
const callOnRevert = revertOptions[1];
const revertAddress = revertOptions[0];
const revertMessage = revertOptions[3];
const revertContext = {
asset,
amount,
revertMessage,
};
const revertContext = { asset, amount, revertMessage, sender };
if (callOnRevert) {
try {
log(
"EVM",
chain,
`Contract ${revertAddress} executing onRevert (context: ${JSON.stringify(
revertContext
)})`
);
(tss as NonceManager).reset();
let tx;
if (isGas) {
tx = await protocolContracts.gatewayEVM
tx = await gatewayEVM
.connect(tss)
.executeRevert(revertAddress, "0x", revertContext, {
value: amount,
deployOpts,
});
} else {
tx = await protocolContracts.custody
tx = await custody
.connect(tss)
.withdrawAndRevert(
revertAddress,
Expand All @@ -63,22 +65,22 @@ export const handleOnRevertEVM = async ({
);
}
await tx.wait();
log("EVM", "Gateway: successfully called onRevert");
log(chain, "Gateway: successfully called onRevert");
const logs = await provider.getLogs({
address: revertAddress,
fromBlock: "latest",
});

logs.forEach((data: any) => {
log("EVM", `Event from onRevert: ${JSON.stringify(data)}`);
log(chain, `Event from onRevert: ${JSON.stringify(data)}`);
});
} catch (err: any) {
logErr("EVM", `Gateway: Call onRevert failed`, err);
logErr(chain, `Gateway: Call onRevert failed`, err);
if (exitOnError) throw new Error(err);
}
} else {
const error = `Tx reverted without callOnRevert: ${err}`;
logErr("EVM", error);
logErr(chain, error);
if (exitOnError) throw new Error(error);
}
};
11 changes: 7 additions & 4 deletions packages/localnet/src/handleOnRevertZEVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@ export const handleOnRevertZEVM = async ({
tss,
log,
fungibleModuleSigner,
protocolContracts,
gatewayZEVM,
deployOpts,
exitOnError = false,
sender,
}: {
revertOptions: any;
err: any;
Expand All @@ -21,10 +22,11 @@ export const handleOnRevertZEVM = async ({
provider: any;
fungibleModuleSigner: any;
tss: NonceManager;
log: (chain: "EVM" | "ZetaChain", ...messages: string[]) => void;
protocolContracts: any;
log: (chain: string, ...messages: string[]) => void;
gatewayZEVM: any;
deployOpts: any;
exitOnError: boolean;
sender: string;
}) => {
const callOnRevert = revertOptions[1];
const revertAddress = revertOptions[0];
Expand All @@ -33,13 +35,14 @@ export const handleOnRevertZEVM = async ({
asset,
amount,
revertMessage,
sender,
};

if (callOnRevert) {
log("ZetaChain", "Gateway: calling executeRevert");
try {
tss.reset();
const tx = await protocolContracts.gatewayZEVM
const tx = await gatewayZEVM
.connect(fungibleModuleSigner)
.executeRevert(revertAddress, revertContext, deployOpts);
await tx.wait();
Expand Down
Loading

0 comments on commit a2db10c

Please sign in to comment.