Skip to content

Commit

Permalink
Keep only universal app examples (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Sep 24, 2024
1 parent da3d00a commit 8421cef
Show file tree
Hide file tree
Showing 157 changed files with 1,428 additions and 93,839 deletions.
24 changes: 0 additions & 24 deletions .github/workflows/integration.yml

This file was deleted.

28 changes: 9 additions & 19 deletions .github/workflows/slither.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ on:
- main
pull_request:
branches:
- '*'
- "*"
types:
- synchronize
- opened
Expand All @@ -19,22 +19,12 @@ jobs:
strategy:
matrix:
include:
- project: 'messaging/erc20'
file: 'm-erc20.sarif'
- project: 'messaging/message'
file: 'm-message.sarif'
- project: 'messaging/nft'
file: 'm-nft.sarif'
- project: 'messaging/zeta'
file: 'm-zeta.sarif'
- project: 'omnichain/multioutput'
file: 'o-multioutput.sarif'
- project: 'omnichain/nft'
file: 'o-nft.sarif'
- project: 'omnichain/staking'
file: 'o-staking.sarif'
- project: 'omnichain/swap'
file: 'o-swap.sarif'
- project: "examples/hello"
file: "hello.sarif"
- project: "examples/swap"
file: "swap.sarif"
- project: "omnichain/swap"
file: "omnichain-swap.sarif"
permissions:
contents: read
security-events: write
Expand All @@ -46,7 +36,7 @@ jobs:
- name: Install Node.js
uses: actions/setup-node@v2
with:
node-version: '18'
node-version: "18"

- name: Install Dependencies
run: yarn install
Expand All @@ -64,7 +54,7 @@ jobs:
with:
ignore-compile: true
sarif: ${{ matrix.file}}
node-version: '18'
node-version: "18"
target: ${{ matrix.project}}
fail-on: none

Expand Down
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import {RevertContext, RevertOptions} from "@zetachain/protocol-contracts/contracts/Revert.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/UniversalContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IGatewayZEVM.sol";
import "@zetachain/protocol-contracts/contracts/zevm/GatewayZEVM.sol";

contract Hello is UniversalContract {
GatewayZEVM public gateway;

event HelloEvent(string, string);
event ContextDataRevert(RevertContext);
event RevertEvent(string, RevertContext);

address constant gatewayAddress =
0x610178dA211FEF7D417bC0e6FeD39F05609AD788;
constructor(address payable gatewayAddress) {
gateway = GatewayZEVM(gatewayAddress);
}

function onCrossChainCall(
zContext calldata context,
address zrc20,
uint256 amount,
bytes calldata message
) external override {
string memory decodedMessage;
if (message.length > 0) {
decodedMessage = abi.decode(message, (string));
}
emit HelloEvent("Hello from a universal app", decodedMessage);
string memory name = abi.decode(message, (string));
emit HelloEvent("Hello on ZetaChain", name);
}

function onRevert(RevertContext calldata revertContext) external override {
emit ContextDataRevert(revertContext);
emit RevertEvent("Revert on ZetaChain", revertContext);
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import {RevertContext} from "@zetachain/protocol-contracts/contracts/Revert.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/UniversalContract.sol";

contract ReceiverContract {
event RevertEvent(string);
event HelloEvent(string);
contract Revert {
event RevertEvent(string, RevertContext);
event HelloEvent(string, string);

function hello(string memory message) external {
emit HelloEvent(message);
emit HelloEvent("Hello on EVM", message);
}

function onRevert(RevertContext calldata revertContext) external {
emit RevertEvent("Event from RevertContract!");
emit RevertEvent("Revert on EVM", revertContext);
}

receive() external payable {}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import "./tasks/deploy";
import "./tasks/deployRevert";
import "./tasks/solana/interact";
import "@zetachain/localnet/tasks";
import "@nomicfoundation/hardhat-toolbox";
Expand Down
7 changes: 3 additions & 4 deletions universal/swap/package.json → examples/hello/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"test": "echo \"Error: no test specified\" && exit 1",
"lint:fix": "npx eslint . --ext .js,.ts --fix",
"lint": "npx eslint . --ext .js,.ts",
"deploy": "npx hardhat compile --force && npx hardhat deploy --network localhost"
"deploy": "npx hardhat compile --force && npx hardhat deploy --network localhost && npx hardhat deploy-revert --network localhost"
},
"keywords": [],
"author": "",
Expand All @@ -28,7 +28,7 @@
"@types/node": ">=12.0.0",
"@typescript-eslint/eslint-plugin": "^5.59.9",
"@typescript-eslint/parser": "^5.59.9",
"@zetachain/localnet": "3.0.2",
"@zetachain/localnet": "3.0.3",
"@zetachain/toolkit": "13.0.0-rc2",
"axios": "^1.3.6",
"chai": "^4.2.0",
Expand All @@ -54,10 +54,9 @@
"packageManager": "[email protected]+sha1.1959a18351b811cdeedbd484a8f86c3cc3bbaf72",
"dependencies": {
"@coral-xyz/anchor": "0.30.0",
"@openzeppelin/contracts": "^4.9.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"
}
}
}
36 changes: 36 additions & 0 deletions examples/hello/tasks/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { task, types } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const network = hre.network.name;

const [signer] = await hre.ethers.getSigners();
if (signer === undefined) {
throw new Error(
`Wallet not found. Please, run "npx hardhat account --save" or set PRIVATE_KEY env variable (for example, in a .env file)`
);
}

const factory = await hre.ethers.getContractFactory(args.name);
const contract = await (factory as any).deploy(args.gatewayZetaChain);
await contract.deployed();

if (args.json) {
console.log(JSON.stringify(contract));
} else {
console.log(`🔑 Using account: ${signer.address}
🚀 Successfully deployed "${args.name}" contract on ${network}.
📜 Contract address: ${contract.address}
`);
}
};

task("deploy", "Deploy the contract", main)
.addFlag("json", "Output in JSON")
.addOptionalParam("name", "Contract to deploy", "Hello")
.addOptionalParam(
"gatewayZetaChain",
"Gateway address",
"0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0"
);
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { task } from "hardhat/config";
import { task, types } from "hardhat/config";
import { HardhatRuntimeEnvironment } from "hardhat/types";

const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
const network = hre.network.name as ParamChainName;
const network = hre.network.name;

const [signer] = await hre.ethers.getSigners();
if (signer === undefined) {
Expand All @@ -12,20 +12,20 @@ const main = async (args: any, hre: HardhatRuntimeEnvironment) => {
}

const factory = await hre.ethers.getContractFactory(args.name);
const contract = await factory.deploy();
const contract = await (factory as any).deploy();
await contract.deployed();

if (args.json) {
console.log(JSON.stringify(contract));
} else {
console.log(`🔑 Using account: ${signer.address}
🚀 Successfully deployed contract on ${network}.
🚀 Successfully deployed "${args.name}" contract on ${network}.
📜 Contract address: ${contract.address}
`);
}
};

task("deploy", "Deploy the contract", main)
task("deploy-revert", "Deploy the contract", main)
.addFlag("json", "Output in JSON")
.addOptionalParam("name", "Contract to deploy", "Hello");
.addOptionalParam("name", "Contract to deploy", "Revert");
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions universal/hello/yarn.lock → examples/hello/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2407,10 +2407,10 @@
typescript "5.5.4"
zod "3.22.4"

"@zetachain/[email protected].2":
version "3.0.2"
resolved "https://registry.yarnpkg.com/@zetachain/localnet/-/localnet-3.0.2.tgz#4b9af86f8b0810a9a528073611c0e3a5ac49c28a"
integrity sha512-nNBAOWWUuebUSuRMTwVnsF9V3W5HcWZ+vTiLSINMXBagbKyOJ4wQAK7nFBBB1UZBTnAcGPyMQVP+ymoEvEpn6g==
"@zetachain/[email protected].3":
version "3.0.3"
resolved "https://registry.yarnpkg.com/@zetachain/localnet/-/localnet-3.0.3.tgz#2d822791c1814690971c35efc43be6b3743045e7"
integrity sha512-s/DGVFzGwURwqw6krkU9jlMCUBYbwgOEQPpYbGwVqBHrMR4YB/EFur8FHz1Sc5VPMDV1VarbSsC253oI99Tulw==
dependencies:
"@inquirer/prompts" "^5.5.0"
"@uniswap/v2-core" "^1.0.1"
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;

import "./shared/SystemContract.sol";
import "./shared/SwapHelperLib.sol";
import "./shared/BytesHelperLib.sol";
import {SystemContract, IZRC20} from "@zetachain/toolkit/contracts/SystemContract.sol";
import {SwapHelperLib} from "@zetachain/toolkit/contracts/SwapHelperLib.sol";
import {BytesHelperLib} from "@zetachain/toolkit/contracts/BytesHelperLib.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "./shared/libraries/UniswapV2Library.sol";

import {RevertContext, RevertOptions} from "@zetachain/protocol-contracts/contracts/Revert.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/UniversalContract.sol";
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/IGatewayZEVM.sol";
import {GatewayZEVM} from "@zetachain/protocol-contracts/contracts/zevm/GatewayZEVM.sol";

contract Swap is UniversalContract {
SystemContract public systemContract;
GatewayZEVM public gateway;
uint256 constant BITCOIN = 18332;
address constant gatewayAddress =
0xA51c1fc2f0D1a1b8494Ed1FE312d7C3a78Ed91C0;

constructor(address systemContractAddress) {
constructor(address systemContractAddress, address payable gatewayAddress) {
systemContract = SystemContract(systemContractAddress);
gateway = GatewayZEVM(gatewayAddress);
}

struct Params {
Expand Down Expand Up @@ -47,19 +47,28 @@ contract Swap is UniversalContract {
params.to = recipient;
}

swapAndWithdraw(zrc20, amount, params.target, params.to);
}

function swapAndWithdraw(
address inputToken,
uint256 amount,
address targetToken,
bytes memory recipient
) internal {
uint256 inputForGas;
address gasZRC20;
uint256 gasFee;
uint256 swapAmount;

(gasZRC20, gasFee) = IZRC20(params.target).withdrawGasFee();
(gasZRC20, gasFee) = IZRC20(targetToken).withdrawGasFee();

if (gasZRC20 == zrc20) {
if (gasZRC20 == inputToken) {
swapAmount = amount - gasFee;
} else {
inputForGas = SwapHelperLib.swapTokensForExactTokens(
systemContract,
zrc20,
inputToken,
gasFee,
gasZRC20,
amount
Expand All @@ -69,22 +78,23 @@ contract Swap is UniversalContract {

uint256 outputAmount = SwapHelperLib.swapExactTokensForTokens(
systemContract,
zrc20,
inputToken,
swapAmount,
params.target,
targetToken,
0
);

if (gasZRC20 == params.target) {
IZRC20(gasZRC20).approve(gatewayAddress, outputAmount + gasFee);
if (gasZRC20 == targetToken) {
IZRC20(gasZRC20).approve(address(gateway), outputAmount + gasFee);
} else {
IZRC20(gasZRC20).approve(gatewayAddress, gasFee);
IZRC20(params.target).approve(gatewayAddress, outputAmount);
IZRC20(gasZRC20).approve(address(gateway), gasFee);
IZRC20(targetToken).approve(address(gateway), outputAmount);
}
IGatewayZEVM(gatewayAddress).withdraw(
params.to,

gateway.withdraw(
recipient,
outputAmount,
params.target,
targetToken,
RevertOptions({
revertAddress: address(0),
callOnRevert: false,
Expand Down
Loading

0 comments on commit 8421cef

Please sign in to comment.