Skip to content

Commit

Permalink
NFT example (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
fadeev authored Feb 5, 2024
1 parent 291d6fc commit 23bbea5
Show file tree
Hide file tree
Showing 17 changed files with 7,265 additions and 1 deletion.
43 changes: 43 additions & 0 deletions .github/workflows/publish-npm.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Publish to NPM

on:
release:
types: [published]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout Repository
uses: actions/checkout@v3

- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: "16"
registry-url: "https://registry.npmjs.org"

- name: Install Dependencies
run: yarn install

- name: Build
run: yarn build

- name: Determine NPM Tag
id: determine-npm-tag
run: |
VERSION_TAG=${GITHUB_REF#refs/tags/v}
if [[ $VERSION_TAG == *"-"* ]]; then
echo ::set-output name=NPM_TAG::${VERSION_TAG#*-}
else
echo ::set-output name=NPM_TAG::latest
fi
env:
GITHUB_REF: ${{ github.ref }}

- name: Publish to NPM
run: yarn publish --new-version ${GITHUB_REF#refs/tags/v} --tag ${{ steps.determine-npm-tag.outputs.NPM_TAG }} --no-git-tag-version
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
GITHUB_REF: ${{ github.ref }}
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ typechain-types
# Hardhat files
cache
artifacts

abi
6 changes: 6 additions & 0 deletions omnichain/nft/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.yarn
artifacts
cache
coverage
node_modules
typechain-types
47 changes: 47 additions & 0 deletions omnichain/nft/.eslintrc.js
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"),
},
},
},
};
12 changes: 12 additions & 0 deletions omnichain/nft/.gitignore
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
21 changes: 21 additions & 0 deletions omnichain/nft/LICENSE
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.
30 changes: 30 additions & 0 deletions omnichain/nft/README.md
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/).
78 changes: 78 additions & 0 deletions omnichain/nft/contracts/NFT.sol
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 "@zetachain/toolkit/contracts/BytesHelperLib.sol";

contract NFT is zContract, ERC721 {
uint256 constant BITCOIN = 18332;

SystemContract public immutable systemContract;
mapping(uint256 => uint256) public tokenAmounts;
mapping(uint256 => uint256) public tokenChains;

uint256 private _nextTokenId;

constructor(address systemContractAddress) ERC721("MyNFT", "MNFT") {
systemContract = SystemContract(systemContractAddress);
_nextTokenId = 0;
}

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 = _nextTokenId;
_safeMint(recipient, tokenId);
tokenChains[tokenId] = chainId;
tokenAmounts[tokenId] = amount;
_nextTokenId++;
}

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];
}
}
20 changes: 20 additions & 0 deletions omnichain/nft/goldsky.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"version": "1",
"name": "NFT",
"abis": {
"NFT": {
"path": "artifacts/contracts/NFT.sol/NFT.json"
}
},
"chains": [
"zetachain-testnet"
],
"instances": [
{
"abi": "NFT",
"address": "0x936b0724C849090907f9bDDB21b70Ff570E7e358",
"chain": "zetachain-testnet",
"startBlock": 3325386
}
]
}
16 changes: 16 additions & 0 deletions omnichain/nft/hardhat.config.ts
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;
51 changes: 51 additions & 0 deletions omnichain/nft/package.json
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"
}
}
37 changes: 37 additions & 0 deletions omnichain/nft/tasks/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
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("NFT");
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");
Loading

0 comments on commit 23bbea5

Please sign in to comment.