Skip to content

Commit

Permalink
Update contract
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev committed Sep 28, 2023
1 parent c0322e0 commit 6cdc802
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 19 deletions.
11 changes: 10 additions & 1 deletion lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,18 @@ const prepareData = (args: any) => {
return [n, `args.${p[0]}`];
});

const feesNative = args.fees === "native";

return {
args,
arguments: { casts, names, pairs, pairsWithDataLocation, types },
arguments: {
casts,
names,
pairs,
pairsWithDataLocation,
types,
feesNative,

Check failure on line 98 in lib/index.ts

View workflow job for this annotation

GitHub Actions / build

Expected object keys to be in ascending order. 'feesNative' should be before 'types'
},
contractName,
contractNameUnderscore: camelToUnderscoreUpper(contractName),
};
Expand Down
8 changes: 7 additions & 1 deletion tasks/messaging.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from "fs";
import { task } from "hardhat/config";
import { task, types } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";
import * as path from "path";

Expand Down Expand Up @@ -28,6 +28,12 @@ export const messagingTask = task(
main
)
.addPositionalParam("name", "Name of the contract")
.addOptionalParam(
"fees",
"Use ZETA or native gas tokens for cross-chain fees",
"native",
types.string
)
.addOptionalVariadicPositionalParam(
"arguments",
"Arguments for a crosschain call (e.g. dest:address to:bytes32 output:uint256)"
Expand Down
37 changes: 21 additions & 16 deletions templates/messaging/contracts/{{contractName}}.sol.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,43 +5,48 @@ import "@openzeppelin/contracts/interfaces/IERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "@zetachain/protocol-contracts/contracts/evm/tools/ZetaInteractor.sol";
import "@zetachain/protocol-contracts/contracts/evm/interfaces/ZetaInterfaces.sol";
{{#unless arguments.feesNative}}
import "@zetachain/protocol-contracts/contracts/evm/Zeta.eth.sol";
{{/unless}}

interface {{contractName}}Errors {
contract {{contractName}} is ZetaInteractor, ZetaReceiver {
error InvalidMessageType();
}

contract {{contractName}} is
ZetaInteractor,
ZetaReceiver,
{{contractName}}Errors
{
bytes32 public constant {{contractNameUnderscore}}_MESSAGE_TYPE =
keccak256("CROSS_CHAIN_{{contractNameUnderscore}}");
{{#unless arguments.feesNative}}
error ErrorTransferringZeta();
{{/unless}}

event {{contractName}}Event({{#each arguments.pairs}}{{#if @index}}, {{/if}}{{this.[1]}}{{/each}});
event {{contractName}}RevertedEvent({{#each arguments.pairs}}{{#if @index}}, {{/if}}{{this.[1]}}{{/each}});

bytes32 public constant {{contractNameUnderscore}}_MESSAGE_TYPE =
keccak256("CROSS_CHAIN_{{contractNameUnderscore}}");
{{#if arguments.feesNative}}
ZetaTokenConsumer private immutable _zetaConsumer;
{{/if}}
IERC20 internal immutable _zetaToken;

constructor(
address connectorAddress,
address zetaTokenAddress,
address zetaConsumerAddress
) ZetaInteractor(connectorAddress) {
constructor(address connectorAddress, address zetaTokenAddress{{#if arguments.feesNative}}, address zetaConsumerAddress{{/if}}) ZetaInteractor(connectorAddress) {
_zetaToken = IERC20(zetaTokenAddress);
{{#if arguments.feesNative}}
_zetaConsumer = ZetaTokenConsumer(zetaConsumerAddress);
{{/if}}
}

function sendMessage(uint256 destinationChainId{{#if arguments.pairsWithDataLocation}}, {{#each arguments.pairsWithDataLocation}}{{#if @index}}, {{/if}}{{this.[1]}} {{this.[0]}}{{/each}}{{/if}}) external payable {
function sendMessage(uint256 destinationChainId{{#unless arguments.feesNative}}, uint256 zetaValueAndGas{{/unless}}{{#if arguments.pairsWithDataLocation}}, {{#each arguments.pairsWithDataLocation}}{{#if @index}}, {{/if}}{{this.[1]}} {{this.[0]}}{{/each}}{{/if}}) external payable {
if (!_isValidChainId(destinationChainId))
revert InvalidDestinationChainId();

{{#if arguments.feesNative}}
uint256 crossChainGas = 2 * (10 ** 18);
uint256 zetaValueAndGas = _zetaConsumer.getZetaFromEth{
value: msg.value
}(address(this), crossChainGas);
_zetaToken.approve(address(connector), zetaValueAndGas);
{{else}}
bool success1 = _zetaToken.approve(address(connector), zetaValueAndGas);
bool success2 = _zetaToken.transferFrom(msg.sender, address(this), zetaValueAndGas);
if (!(success1 && success2)) revert ErrorTransferringZeta();
{{/if}}

connector.send(
ZetaInterfaces.SendInput({
Expand Down
2 changes: 1 addition & 1 deletion typechain-types/factories/contracts/TestZRC20__factory.ts

Large diffs are not rendered by default.

0 comments on commit 6cdc802

Please sign in to comment.