Skip to content

Commit

Permalink
deposit and call gateway zevm
Browse files Browse the repository at this point in the history
  • Loading branch information
skosito committed Jul 10, 2024
1 parent 0e03d10 commit f3c9a0b
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 28 deletions.
18 changes: 18 additions & 0 deletions contracts/prototypes/zevm/GatewayZEVM.sol
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,22 @@ contract GatewayZEVM is IGatewayZEVMEvents, IGatewayZEVMErrors, Initializable, O
IZRC20(zrc20).deposit(target, amount);
zContract(target).onCrossChainCall(context, zrc20, amount, message);
}

// Deposit zeta and call user specified contract on ZEVM
// TODO: Finalize access control
// https://github.com/zeta-chain/protocol-contracts/issues/204
function depositAndCall(
zContext calldata context,
uint256 amount,
address target,
bytes calldata message
) external {
if (msg.sender != FUNGIBLE_MODULE_ADDRESS) revert CallerIsNotFungibleModule();
if (target == FUNGIBLE_MODULE_ADDRESS || target == address(this)) revert InvalidTarget();

if (!IWETH9(wzeta).transferFrom(msg.sender, address(this), amount)) revert WZETATransferFailed();
IWETH9(wzeta).withdraw(amount);
(bool sent, ) = target.call{value: amount}("");
zContract(target).onCrossChainCall(context, wzeta, amount, message);
}
}

Large diffs are not rendered by default.

43 changes: 32 additions & 11 deletions pkg/contracts/prototypes/zevm/gatewayzevm.sol/gatewayzevm.go

Large diffs are not rendered by default.

10 changes: 8 additions & 2 deletions test/prototypes/GatewayIntegration.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AddressZero } from "@ethersproject/constants";
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers";
import { SystemContract, ZRC20, WETH9 } from "@typechain-types";
import { SystemContract, WETH9, ZRC20 } from "@typechain-types";
import { expect } from "chai";
import { Contract } from "ethers";
import { parseEther } from "ethers/lib/utils";
Expand Down Expand Up @@ -143,7 +143,13 @@ describe("GatewayEVM GatewayZEVM integration", function () {
// Encode the function call data and call on zevm
const message = receiverEVM.interface.encodeFunctionData("receivePayable", [str, num, flag]);
const callTx = await gatewayZEVM
.connect(ownerZEVM)["withdrawAndCall(bytes,uint256,address,bytes)"](receiverEVM.address, parseEther("1"), ZRC20Contract.address, message);
.connect(ownerZEVM)
["withdrawAndCall(bytes,uint256,address,bytes)"](
receiverEVM.address,
parseEther("1"),
ZRC20Contract.address,
message
);

await expect(callTx)
.to.emit(gatewayZEVM, "Withdrawal")
Expand Down
17 changes: 13 additions & 4 deletions test/prototypes/GatewayZEVM.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("GatewayZEVM", function () {
systemContract = (await SystemContractFactory.deploy(AddressZero, AddressZero, AddressZero)) as SystemContract;
const WZETAFactory = await ethers.getContractFactory("contracts/zevm/WZETA.sol:WETH9");

const zetaTokenContract = (await WZETAFactory.deploy());
const zetaTokenContract = await WZETAFactory.deploy();
const Gateway = await ethers.getContractFactory("GatewayZEVM");
gateway = await upgrades.deployProxy(Gateway, [zetaTokenContract.address], {
initializer: "initialize",
Expand Down Expand Up @@ -69,7 +69,11 @@ describe("GatewayZEVM", function () {
it("should withdraw zrc20 and emit event", async function () {
const tx = await gateway
.connect(owner)
["withdraw(bytes,uint256,address)"](ethers.utils.arrayify(addrs[0].address), parseEther("1"), ZRC20Contract.address);
["withdraw(bytes,uint256,address)"](
ethers.utils.arrayify(addrs[0].address),
parseEther("1"),
ZRC20Contract.address
);

const balanceOfAfterWithdrawal = (await ZRC20Contract.balanceOf(owner.address)) as BigNumber;
expect(balanceOfAfterWithdrawal).to.equal(parseEther("99"));
Expand All @@ -94,7 +98,12 @@ describe("GatewayZEVM", function () {

const tx = await gateway
.connect(owner)
["withdrawAndCall(bytes,uint256,address,bytes)"](ethers.utils.arrayify(addrs[0].address), parseEther("1"), ZRC20Contract.address, message);
["withdrawAndCall(bytes,uint256,address,bytes)"](
ethers.utils.arrayify(addrs[0].address),
parseEther("1"),
ZRC20Contract.address,
message
);

const balanceOfAfterWithdrawal = (await ZRC20Contract.balanceOf(owner.address)) as BigNumber;
expect(balanceOfAfterWithdrawal).to.equal(parseEther("99"));
Expand Down Expand Up @@ -165,7 +174,7 @@ describe("GatewayZEVM", function () {
const message = ethers.utils.defaultAbiCoder.encode(["string"], ["hello"]);
const tx = await gateway
.connect(fungibleModuleSigner)
.depositAndCall(
["depositAndCall((bytes,address,uint256),address,uint256,address,bytes)"](
[gateway.address, fungibleModuleSigner.address, 1],
ZRC20Contract.address,
parseEther("1"),
Expand Down
71 changes: 63 additions & 8 deletions typechain-types/contracts/prototypes/zevm/GatewayZEVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ export interface GatewayZEVMInterface extends utils.Interface {
"FUNGIBLE_MODULE_ADDRESS()": FunctionFragment;
"call(bytes,bytes)": FunctionFragment;
"deposit(address,uint256,address)": FunctionFragment;
"depositAndCall((bytes,address,uint256),uint256,address,bytes)": FunctionFragment;
"depositAndCall((bytes,address,uint256),address,uint256,address,bytes)": FunctionFragment;
"execute((bytes,address,uint256),address,uint256,address,bytes)": FunctionFragment;
"initialize(address)": FunctionFragment;
Expand All @@ -66,7 +67,8 @@ export interface GatewayZEVMInterface extends utils.Interface {
| "FUNGIBLE_MODULE_ADDRESS"
| "call"
| "deposit"
| "depositAndCall"
| "depositAndCall((bytes,address,uint256),uint256,address,bytes)"
| "depositAndCall((bytes,address,uint256),address,uint256,address,bytes)"
| "execute"
| "initialize"
| "owner"
Expand Down Expand Up @@ -99,7 +101,16 @@ export interface GatewayZEVMInterface extends utils.Interface {
]
): string;
encodeFunctionData(
functionFragment: "depositAndCall",
functionFragment: "depositAndCall((bytes,address,uint256),uint256,address,bytes)",
values: [
ZContextStruct,
PromiseOrValue<BigNumberish>,
PromiseOrValue<string>,
PromiseOrValue<BytesLike>
]
): string;
encodeFunctionData(
functionFragment: "depositAndCall((bytes,address,uint256),address,uint256,address,bytes)",
values: [
ZContextStruct,
PromiseOrValue<string>,
Expand Down Expand Up @@ -177,7 +188,11 @@ export interface GatewayZEVMInterface extends utils.Interface {
decodeFunctionResult(functionFragment: "call", data: BytesLike): Result;
decodeFunctionResult(functionFragment: "deposit", data: BytesLike): Result;
decodeFunctionResult(
functionFragment: "depositAndCall",
functionFragment: "depositAndCall((bytes,address,uint256),uint256,address,bytes)",
data: BytesLike
): Result;
decodeFunctionResult(
functionFragment: "depositAndCall((bytes,address,uint256),address,uint256,address,bytes)",
data: BytesLike
): Result;
decodeFunctionResult(functionFragment: "execute", data: BytesLike): Result;
Expand Down Expand Up @@ -351,7 +366,15 @@ export interface GatewayZEVM extends BaseContract {
overrides?: Overrides & { from?: PromiseOrValue<string> }
): Promise<ContractTransaction>;

depositAndCall(
"depositAndCall((bytes,address,uint256),uint256,address,bytes)"(
context: ZContextStruct,
amount: PromiseOrValue<BigNumberish>,
target: PromiseOrValue<string>,
message: PromiseOrValue<BytesLike>,
overrides?: Overrides & { from?: PromiseOrValue<string> }
): Promise<ContractTransaction>;

"depositAndCall((bytes,address,uint256),address,uint256,address,bytes)"(
context: ZContextStruct,
zrc20: PromiseOrValue<string>,
amount: PromiseOrValue<BigNumberish>,
Expand Down Expand Up @@ -442,7 +465,15 @@ export interface GatewayZEVM extends BaseContract {
overrides?: Overrides & { from?: PromiseOrValue<string> }
): Promise<ContractTransaction>;

depositAndCall(
"depositAndCall((bytes,address,uint256),uint256,address,bytes)"(
context: ZContextStruct,
amount: PromiseOrValue<BigNumberish>,
target: PromiseOrValue<string>,
message: PromiseOrValue<BytesLike>,
overrides?: Overrides & { from?: PromiseOrValue<string> }
): Promise<ContractTransaction>;

"depositAndCall((bytes,address,uint256),address,uint256,address,bytes)"(
context: ZContextStruct,
zrc20: PromiseOrValue<string>,
amount: PromiseOrValue<BigNumberish>,
Expand Down Expand Up @@ -533,7 +564,15 @@ export interface GatewayZEVM extends BaseContract {
overrides?: CallOverrides
): Promise<void>;

depositAndCall(
"depositAndCall((bytes,address,uint256),uint256,address,bytes)"(
context: ZContextStruct,
amount: PromiseOrValue<BigNumberish>,
target: PromiseOrValue<string>,
message: PromiseOrValue<BytesLike>,
overrides?: CallOverrides
): Promise<void>;

"depositAndCall((bytes,address,uint256),address,uint256,address,bytes)"(
context: ZContextStruct,
zrc20: PromiseOrValue<string>,
amount: PromiseOrValue<BigNumberish>,
Expand Down Expand Up @@ -690,7 +729,15 @@ export interface GatewayZEVM extends BaseContract {
overrides?: Overrides & { from?: PromiseOrValue<string> }
): Promise<BigNumber>;

depositAndCall(
"depositAndCall((bytes,address,uint256),uint256,address,bytes)"(
context: ZContextStruct,
amount: PromiseOrValue<BigNumberish>,
target: PromiseOrValue<string>,
message: PromiseOrValue<BytesLike>,
overrides?: Overrides & { from?: PromiseOrValue<string> }
): Promise<BigNumber>;

"depositAndCall((bytes,address,uint256),address,uint256,address,bytes)"(
context: ZContextStruct,
zrc20: PromiseOrValue<string>,
amount: PromiseOrValue<BigNumberish>,
Expand Down Expand Up @@ -784,7 +831,15 @@ export interface GatewayZEVM extends BaseContract {
overrides?: Overrides & { from?: PromiseOrValue<string> }
): Promise<PopulatedTransaction>;

depositAndCall(
"depositAndCall((bytes,address,uint256),uint256,address,bytes)"(
context: ZContextStruct,
amount: PromiseOrValue<BigNumberish>,
target: PromiseOrValue<string>,
message: PromiseOrValue<BytesLike>,
overrides?: Overrides & { from?: PromiseOrValue<string> }
): Promise<PopulatedTransaction>;

"depositAndCall((bytes,address,uint256),address,uint256,address,bytes)"(
context: ZContextStruct,
zrc20: PromiseOrValue<string>,
amount: PromiseOrValue<BigNumberish>,
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit f3c9a0b

Please sign in to comment.