Skip to content

Authenticated call support #205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions examples/hello/contracts/Echo.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ contract Echo {

event RevertEvent(string, RevertContext);
event HelloEvent(string, string);
event ReceivedOnCall(address sender, bytes message);

constructor(address payable gatewayAddress) {
gateway = GatewayEVM(gatewayAddress);
Expand All @@ -30,6 +31,14 @@ contract Echo {
gateway.call(receiver, message, revertOptions);
}

function onCall(
MessageContext calldata messageContext,
bytes calldata message
) external payable returns (bytes memory) {
emit ReceivedOnCall(messageContext.sender, message);
return "";
}

receive() external payable {}

fallback() external payable {}
Expand Down
20 changes: 11 additions & 9 deletions examples/hello/contracts/Hello.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ contract Hello is UniversalContract {
gateway = GatewayZEVM(gatewayAddress);
}

function onCrossChainCall(
zContext calldata context,
function onCall(
MessageContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
Expand All @@ -27,34 +27,36 @@ contract Hello is UniversalContract {
emit HelloEvent("Hello on ZetaChain", name);
}

function onRevert(RevertContext calldata revertContext) external override {
function onRevert(RevertContext calldata revertContext) external {
emit RevertEvent("Revert on ZetaChain", revertContext);
}

function call(
bytes memory receiver,
address zrc20,
bytes calldata message,
uint256 gasLimit,
CallOptions memory callOptions,
RevertOptions memory revertOptions
) external {
(, uint256 gasFee) = IZRC20(zrc20).withdrawGasFeeWithGasLimit(gasLimit);
(, uint256 gasFee) = IZRC20(zrc20).withdrawGasFeeWithGasLimit(
callOptions.gasLimit
);
if (!IZRC20(zrc20).transferFrom(msg.sender, address(this), gasFee))
revert TransferFailed();
IZRC20(zrc20).approve(address(gateway), gasFee);
gateway.call(receiver, zrc20, message, gasLimit, revertOptions);
gateway.call(receiver, zrc20, message, callOptions, revertOptions);
}

function withdrawAndCall(
bytes memory receiver,
uint256 amount,
address zrc20,
bytes calldata message,
uint256 gasLimit,
CallOptions memory callOptions,
RevertOptions memory revertOptions
) external {
(address gasZRC20, uint256 gasFee) = IZRC20(zrc20)
.withdrawGasFeeWithGasLimit(gasLimit);
.withdrawGasFeeWithGasLimit(callOptions.gasLimit);
uint256 target = zrc20 == gasZRC20 ? amount + gasFee : amount;
if (!IZRC20(zrc20).transferFrom(msg.sender, address(this), target))
revert TransferFailed();
Expand All @@ -74,7 +76,7 @@ contract Hello is UniversalContract {
amount,
zrc20,
message,
gasLimit,
callOptions,
revertOptions
);
}
Expand Down
4 changes: 2 additions & 2 deletions examples/hello/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,6 @@
"@solana-developers/helpers": "^2.4.0",
"@solana/spl-memo": "^0.2.5",
"@solana/web3.js": "^1.95.2",
"@zetachain/protocol-contracts": "10.0.0-rc10"
"@zetachain/protocol-contracts": "11.0.0-rc3"
}
}
}
2 changes: 1 addition & 1 deletion examples/hello/tasks/deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ task("deploy", "Deploy the contract", main)
.addOptionalParam(
"gateway",
"Gateway address (default: ZetaChain Gateway)",
"0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0"
"0x9A676e781A523b5d0C0e43731313A708CB607508"
);
1 change: 0 additions & 1 deletion examples/hello/tasks/echoCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {

console.log(`Transaction hash: ${tx.hash}`);
await tx.wait();
console.log("gatewayCall executed successfully");
};

task("echo-call", "Calls the gateway on a contract on EVM", main)
Expand Down
26 changes: 19 additions & 7 deletions examples/hello/tasks/helloCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
),
};

const functionSignature = ethers.utils.id(args.function).slice(0, 10);
const callOptions = {
gasLimit: args.txOptionsGasLimit,
isArbitraryCall: args.isArbitraryCall,
};

const types = JSON.parse(args.types);

Expand All @@ -46,14 +49,23 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
return value;
}
});

const encodedParameters = ethers.utils.defaultAbiCoder.encode(
types,
valuesArray
);

const message = ethers.utils.hexlify(
ethers.utils.concat([functionSignature, encodedParameters])
);
let message: string;

if (args.isArbitraryCall) {
const functionSignature = ethers.utils.id(args.function).slice(0, 10);

message = ethers.utils.hexlify(
ethers.utils.concat([functionSignature, encodedParameters])
);
} else {
message = encodedParameters;
}

const gasLimit = hre.ethers.BigNumber.from(args.txOptionsGasLimit);
const zrc20 = new ethers.Contract(args.zrc20, ZRC20ABI.abi, signer);
Expand All @@ -69,14 +81,13 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
ethers.utils.hexlify(args.receiver),
args.zrc20,
message,
gasLimit,
callOptions,
revertOptions,
txOptions
);

console.log(`Transaction hash: ${tx.hash}`);
await tx.wait();
console.log("gatewayCall executed successfully");
};

task(
Expand Down Expand Up @@ -115,7 +126,8 @@ task(
7000000,
types.int
)
.addParam("function", `Function to call (example: "hello(string)")`)
.addParam("name", "The name of the contract", "Hello")
.addOptionalParam("function", `Function to call (example: "hello(string)")`)
.addFlag("isArbitraryCall", "Whether the call is arbitrary")
.addParam("types", `The types of the parameters (example: '["string"]')`)
.addVariadicPositionalParam("values", "The values of the parameters");
26 changes: 19 additions & 7 deletions examples/hello/tasks/helloWithdrawAndCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
),
};

const functionSignature = ethers.utils.id(args.function).slice(0, 10);
const callOptions = {
gasLimit: args.txOptionsGasLimit,
isArbitraryCall: args.isArbitraryCall,
};

const types = JSON.parse(args.types);

Expand All @@ -46,14 +49,23 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
return value;
}
});

const encodedParameters = ethers.utils.defaultAbiCoder.encode(
types,
valuesArray
);

const message = ethers.utils.hexlify(
ethers.utils.concat([functionSignature, encodedParameters])
);
let message: string;

if (args.isArbitraryCall) {
const functionSignature = ethers.utils.id(args.function).slice(0, 10);

message = ethers.utils.hexlify(
ethers.utils.concat([functionSignature, encodedParameters])
);
} else {
message = encodedParameters;
}

const gasLimit = hre.ethers.BigNumber.from(args.txOptionsGasLimit);

Expand Down Expand Up @@ -86,14 +98,13 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
amount,
args.zrc20,
message,
gasLimit,
callOptions,
revertOptions,
txOptions
);

console.log(`Transaction hash: ${tx.hash}`);
await tx.wait();
console.log("gatewayCall executed successfully");
};

task(
Expand Down Expand Up @@ -132,8 +143,9 @@ task(
7000000,
types.int
)
.addParam("function", `Function to call (example: "hello(string)")`)
.addOptionalParam("function", `Function to call (example: "hello(string)")`)
.addParam("name", "The name of the contract", "Hello")
.addFlag("isArbitraryCall", "Whether the call is arbitrary")
.addParam("amount", "Amount of ZRC-20 to withdraw")
.addParam("types", `The types of the parameters (example: '["string"]')`)
.addVariadicPositionalParam("values", "The values of the parameters");
Loading
Loading