Skip to content

Commit

Permalink
Authenticated call support (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Nov 26, 2024
1 parent be6e0c0 commit 28b837e
Show file tree
Hide file tree
Showing 14 changed files with 1,233 additions and 328 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,11 @@
"@inquirer/prompts": "^5.5.0",
"@uniswap/v2-core": "^1.0.1",
"@uniswap/v2-periphery": "^1.1.0-beta.0",
"@zetachain/protocol-contracts": "10.0.0-rc11",
"@zetachain/protocol-contracts": "11.0.0-rc3",
"ansis": "^3.3.2",
"concurrently": "^8.2.2",
"ethers": "^6.13.2",
"hardhat": "^2.22.8",
"wait-on": "^7.2.0"
}
}
}
70 changes: 36 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 All @@ -115,6 +109,14 @@ export const createToken = async ({
deployOpts
);

await (zrc20 as any)
.connect(deployer)
.transfer(
fungibleModuleSigner.getAddress(),
ethers.parseUnits("100", await (zrc20 as any).decimals()),
deployOpts
);

await (wzeta as any)
.connect(deployer)
.deposit({ value: ethers.parseEther("1000"), ...deployOpts });
Expand Down
30 changes: 20 additions & 10 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 receiver = args[1];
const message = args[2];

(deployer as NonceManager).reset();
const context = {
origin: protocolContracts.gatewayZEVM.target,
sender: await fungibleModuleSigner.getAddress(),
chainID: 1,
origin: ethers.ZeroAddress,
sender,
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,
});
}
};
57 changes: 19 additions & 38 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 @@ -44,41 +52,11 @@ export const handleOnEVMDeposited = async ({

const zrc20 = foreignCoin.zrc20_contract_address;
try {
const context = {
origin: protocolContracts.gatewayZEVM.target,
sender: await fungibleModuleSigner.getAddress(),
chainID: 1,
};

// If message is not empty, execute depositAndCall
if (message !== "0x") {
log(
"ZetaChain",
`Universal contract ${receiver} executing onCall (context: ${JSON.stringify(
context
)}), zrc20: ${zrc20}, amount: ${amount}, message: ${message})`
);

const tx = await protocolContracts.gatewayZEVM
.connect(fungibleModuleSigner)
.depositAndCall(context, zrc20, amount, receiver, message, deployOpts);

await tx.wait();
const logs = await provider.getLogs({
address: receiver,
fromBlock: "latest",
});

logs.forEach((data) => {
log("ZetaChain", `Event from onCall: ${JSON.stringify(data)}`);
});
} else {
const tx = await protocolContracts.gatewayZEVM
.connect(fungibleModuleSigner)
.deposit(zrc20, amount, receiver, deployOpts);
await tx.wait();
log("ZetaChain", `Deposited ${amount} of ${zrc20} tokens to ${receiver}`);
}
const tx = await protocolContracts.gatewayZEVM
.connect(fungibleModuleSigner)
.deposit(zrc20, amount, receiver, deployOpts);
await tx.wait();
log("ZetaChain", `Deposited ${amount} of ${zrc20} tokens to ${receiver}`);
} catch (err) {
logErr("ZetaChain", `Error depositing: ${err}`);
const revertOptions = args[5];
Expand Down Expand Up @@ -118,8 +96,11 @@ export const handleOnEVMDeposited = async ({
isGas,
token,
provider,
protocolContracts,
exitOnError,
chain,
gatewayEVM,
custody,
sender,
});
}
};
Expand Down
Loading

0 comments on commit 28b837e

Please sign in to comment.