-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
7,200 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
.yarn | ||
artifacts | ||
cache | ||
coverage | ||
node_modules | ||
typechain-types |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
const path = require("path"); | ||
|
||
/** | ||
* @type {import("eslint").Linter.Config} | ||
*/ | ||
module.exports = { | ||
env: { | ||
browser: false, | ||
es2021: true, | ||
mocha: true, | ||
node: true, | ||
}, | ||
extends: ["plugin:prettier/recommended"], | ||
parser: "@typescript-eslint/parser", | ||
parserOptions: { | ||
ecmaVersion: 12, | ||
}, | ||
plugins: [ | ||
"@typescript-eslint", | ||
"prettier", | ||
"simple-import-sort", | ||
"sort-keys-fix", | ||
"typescript-sort-keys", | ||
], | ||
rules: { | ||
"@typescript-eslint/sort-type-union-intersection-members": "error", | ||
camelcase: "off", | ||
"simple-import-sort/exports": "error", | ||
"simple-import-sort/imports": "error", | ||
"sort-keys-fix/sort-keys-fix": "error", | ||
"typescript-sort-keys/interface": "error", | ||
"typescript-sort-keys/string-enum": "error", | ||
}, | ||
settings: { | ||
"import/parsers": { | ||
"@typescript-eslint/parser": [".js", ".jsx", ".ts", ".tsx", ".d.ts"], | ||
}, | ||
"import/resolver": { | ||
node: { | ||
extensions: [".js", ".jsx", ".ts", ".tsx", ".d.ts"], | ||
}, | ||
typescript: { | ||
project: path.join(__dirname, "tsconfig.json"), | ||
}, | ||
}, | ||
}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
node_modules | ||
.env | ||
coverage | ||
coverage.json | ||
typechain | ||
typechain-types | ||
|
||
# Hardhat files | ||
cache | ||
artifacts | ||
|
||
access_token |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 ZetaChain | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
# Hardhat Template for ZetaChain | ||
|
||
This is a simple Hardhat template that provides a starting point for developing | ||
smart contract applications on ZetaChain. | ||
|
||
## Prerequisites | ||
|
||
Before getting started, ensure that you have | ||
[Node.js](https://nodejs.org/en/download) (version 18 or above) and | ||
[Yarn](https://yarnpkg.com/) installed on your system. | ||
|
||
## Getting Started | ||
|
||
To get started, install the necessary dependencies: | ||
|
||
``` | ||
yarn | ||
``` | ||
|
||
## Hardhat Tasks | ||
|
||
This template includes Hardhat tasks that streamline smart contract development. | ||
Learn more about the template and the functionality it provides | ||
[in the docs](https://www.zetachain.com/docs/developers/template/). | ||
|
||
## Next Steps | ||
|
||
To learn more about building decentralized apps on ZetaChain, follow the | ||
tutorials available in | ||
[the introduction to ZetaChain](https://www.zetachain.com/docs/developers/overview/). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.7; | ||
|
||
import "@zetachain/protocol-contracts/contracts/zevm/SystemContract.sol"; | ||
import "@zetachain/protocol-contracts/contracts/zevm/interfaces/zContract.sol"; | ||
import "@openzeppelin/contracts/token/ERC721/ERC721.sol"; | ||
import "@openzeppelin/contracts/utils/Counters.sol"; | ||
import "@zetachain/toolkit/contracts/BytesHelperLib.sol"; | ||
|
||
contract MyContract is zContract, ERC721 { | ||
uint256 constant BITCOIN = 18332; | ||
using Counters for Counters.Counter; | ||
Counters.Counter private _tokenIdCounter; | ||
|
||
SystemContract public immutable systemContract; | ||
mapping(uint256 => uint256) public tokenAmounts; | ||
mapping(uint256 => uint256) public tokenChains; | ||
|
||
constructor(address systemContractAddress) ERC721("MyNFT", "MNFT") { | ||
systemContract = SystemContract(systemContractAddress); | ||
} | ||
|
||
modifier onlySystem() { | ||
require( | ||
msg.sender == address(systemContract), | ||
"Only system contract can call this function" | ||
); | ||
_; | ||
} | ||
|
||
function onCrossChainCall( | ||
zContext calldata context, | ||
address zrc20, | ||
uint256 amount, | ||
bytes calldata message | ||
) external override onlySystem { | ||
address recipient; | ||
|
||
if (context.chainID == BITCOIN) { | ||
recipient = BytesHelperLib.bytesToAddress(message, 0); | ||
} else { | ||
recipient = abi.decode(message, (address)); | ||
} | ||
|
||
_mintNFT(recipient, context.chainID, amount); | ||
} | ||
|
||
function _mintNFT( | ||
address recipient, | ||
uint256 chainId, | ||
uint256 amount | ||
) private { | ||
uint256 tokenId = _tokenIdCounter.current(); | ||
_safeMint(recipient, tokenId); | ||
tokenChains[tokenId] = chainId; | ||
tokenAmounts[tokenId] = amount; | ||
_tokenIdCounter.increment(); | ||
} | ||
|
||
function burnNFT(uint256 tokenId, bytes memory recipient) public { | ||
require( | ||
_isApprovedOrOwner(_msgSender(), tokenId), | ||
"Caller is not owner nor approved" | ||
); | ||
address zrc20 = systemContract.gasCoinZRC20ByChainId( | ||
tokenChains[tokenId] | ||
); | ||
|
||
(, uint256 gasFee) = IZRC20(zrc20).withdrawGasFee(); | ||
|
||
IZRC20(zrc20).approve(zrc20, gasFee); | ||
IZRC20(zrc20).withdraw(recipient, tokenAmounts[tokenId] - gasFee); | ||
|
||
_burn(tokenId); | ||
delete tokenAmounts[tokenId]; | ||
delete tokenChains[tokenId]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"version": "1", | ||
"name": "MyContract", | ||
"abis": { | ||
"MyContract": { | ||
"path": "artifacts/contracts/MyContract.sol/MyContract.json" | ||
} | ||
}, | ||
"chains": [ | ||
"zetachain-testnet" | ||
], | ||
"instances": [ | ||
{ | ||
"abi": "MyContract", | ||
"address": "0x3d026bE559797557Fa215f9F7EB4e8BE89fD7d6f", | ||
"chain": "zetachain-testnet", | ||
"startBlock": 3175521 | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import "./tasks/interact"; | ||
import "./tasks/deploy"; | ||
import "@nomicfoundation/hardhat-toolbox"; | ||
import "@zetachain/toolkit/tasks"; | ||
|
||
import { getHardhatConfigNetworks } from "@zetachain/networks"; | ||
import { HardhatUserConfig } from "hardhat/config"; | ||
|
||
const config: HardhatUserConfig = { | ||
networks: { | ||
...getHardhatConfigNetworks(), | ||
}, | ||
solidity: "0.8.7", | ||
}; | ||
|
||
export default config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
{ | ||
"name": "example-template", | ||
"version": "1.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"lint:fix": "npx eslint . --ext .js,.ts --fix", | ||
"lint": "npx eslint . --ext .js,.ts" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"devDependencies": { | ||
"@ethersproject/abi": "^5.4.7", | ||
"@ethersproject/providers": "^5.4.7", | ||
"@nomicfoundation/hardhat-chai-matchers": "^1.0.0", | ||
"@nomicfoundation/hardhat-network-helpers": "^1.0.0", | ||
"@nomicfoundation/hardhat-toolbox": "^2.0.0", | ||
"@nomiclabs/hardhat-ethers": "^2.0.0", | ||
"@nomiclabs/hardhat-etherscan": "^3.0.0", | ||
"@typechain/ethers-v5": "^10.1.0", | ||
"@typechain/hardhat": "^6.1.2", | ||
"@types/chai": "^4.2.0", | ||
"@types/mocha": ">=9.1.0", | ||
"@types/node": ">=12.0.0", | ||
"@typescript-eslint/eslint-plugin": "^5.59.9", | ||
"@typescript-eslint/parser": "^5.59.9", | ||
"@zetachain/toolkit": "5.0.0", | ||
"axios": "^1.3.6", | ||
"chai": "^4.2.0", | ||
"dotenv": "^16.0.3", | ||
"envfile": "^6.18.0", | ||
"eslint": "^8.42.0", | ||
"eslint-config-prettier": "^8.8.0", | ||
"eslint-import-resolver-typescript": "^3.5.5", | ||
"eslint-plugin-import": "^2.27.5", | ||
"eslint-plugin-prettier": "^4.2.1", | ||
"eslint-plugin-simple-import-sort": "^10.0.0", | ||
"eslint-plugin-sort-keys-fix": "^1.1.2", | ||
"eslint-plugin-typescript-sort-keys": "^2.3.0", | ||
"ethers": "^5.4.7", | ||
"hardhat": "^2.14.0", | ||
"hardhat-gas-reporter": "^1.0.8", | ||
"prettier": "^2.8.8", | ||
"solidity-coverage": "^0.8.0", | ||
"ts-node": ">=8.0.0", | ||
"typechain": "^8.1.0", | ||
"typescript": ">=4.5.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { getAddress } from "@zetachain/protocol-contracts"; | ||
import { task } from "hardhat/config"; | ||
import { HardhatRuntimeEnvironment } from "hardhat/types"; | ||
|
||
const main = async (args: any, hre: HardhatRuntimeEnvironment) => { | ||
if (hre.network.name !== "zeta_testnet") { | ||
throw new Error( | ||
'🚨 Please use the "zeta_testnet" network to deploy to ZetaChain.' | ||
); | ||
} | ||
|
||
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 systemContract = getAddress("systemContract", "zeta_testnet"); | ||
|
||
const factory = await hre.ethers.getContractFactory("MyContract"); | ||
const contract = await factory.deploy(systemContract); | ||
await contract.deployed(); | ||
|
||
if (args.json) { | ||
console.log(JSON.stringify(contract)); | ||
} else { | ||
console.log(`🔑 Using account: ${signer.address} | ||
🚀 Successfully deployed contract on ZetaChain. | ||
📜 Contract address: ${contract.address} | ||
🌍 Explorer: https://athens3.explorer.zetachain.com/address/${contract.address} | ||
`); | ||
} | ||
}; | ||
|
||
task("deploy", "Deploy the contract", main).addFlag( | ||
"json", | ||
"Output in JSON" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import { task } from "hardhat/config"; | ||
import { HardhatRuntimeEnvironment } from "hardhat/types"; | ||
import { parseUnits } from "@ethersproject/units"; | ||
import { getAddress } from "@zetachain/protocol-contracts"; | ||
import ERC20Custody from "@zetachain/protocol-contracts/abi/evm/ERC20Custody.sol/ERC20Custody.json"; | ||
import { prepareData } from "@zetachain/toolkit/helpers"; | ||
import { utils, ethers } from "ethers"; | ||
import ERC20 from "@openzeppelin/contracts/build/contracts/ERC20.json"; | ||
|
||
const main = async (args: any, hre: HardhatRuntimeEnvironment) => { | ||
const [signer] = await hre.ethers.getSigners(); | ||
|
||
const data = prepareData(args.contract, ["address"], [args.recipient]); | ||
|
||
let tx; | ||
|
||
if (args.token) { | ||
const custodyAddress = getAddress("erc20Custody", hre.network.name as any); | ||
const custodyContract = new ethers.Contract( | ||
custodyAddress, | ||
ERC20Custody.abi, | ||
signer | ||
); | ||
const tokenContract = new ethers.Contract(args.token, ERC20.abi, signer); | ||
const decimals = await tokenContract.decimals(); | ||
const value = parseUnits(args.amount, decimals); | ||
const approve = await tokenContract.approve(custodyAddress, value); | ||
await approve.wait(); | ||
|
||
tx = await custodyContract.deposit(signer.address, args.token, value, data); | ||
tx.wait(); | ||
} else { | ||
const value = parseUnits(args.amount, 18); | ||
const to = getAddress("tss", hre.network.name as any); | ||
tx = await signer.sendTransaction({ data, to, value }); | ||
} | ||
|
||
if (args.json) { | ||
console.log(JSON.stringify(tx, null, 2)); | ||
} else { | ||
console.log(`🔑 Using account: ${signer.address}\n`); | ||
|
||
console.log(`🚀 Successfully broadcasted a token transfer transaction on ${hre.network.name} network. | ||
📝 Transaction hash: ${tx.hash} | ||
`); | ||
} | ||
}; | ||
|
||
task("interact", "Interact with the contract", main) | ||
.addParam("contract", "The address of the withdraw contract on ZetaChain") | ||
.addParam("amount", "Amount of tokens to send") | ||
.addParam("recipient", "The address of the recipient") | ||
.addOptionalParam("token", "The address of the token to send") | ||
.addFlag("json", "Output in JSON"); |
Oops, something went wrong.