Skip to content

Commit

Permalink
Merge pull request #13 from ava-labs/port-to-typescript
Browse files Browse the repository at this point in the history
v0.1.1-rc
  • Loading branch information
cgcardona authored May 26, 2021
2 parents 0627fcc + 230df1f commit 8cd0c77
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 140 deletions.
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ The goal of this guide is to lay out a best-practices regarding writing, testing

### NodeJS and Yarn

First install the LTS of [nodejs](https://nodejs.org/en) which is `14.15.4` at the time of writing. NodeJS bundles `npm`.
First install the LTS of [nodejs](https://nodejs.org/en) which is `14.17.0` at the time of writing. NodeJS bundles `npm`.

Next install [yarn](https://yarnpkg.com):

Expand Down Expand Up @@ -54,14 +54,14 @@ Run `yarn compile` to make sure your project compiles.

## Prepare to Deploy

Edit the deployment script in `scripts/deploy.js`
Edit the deployment script in `scripts/deploy.ts`

## Deploy

Hardhat enables deploying to multiple environments. In [`package.json`](./package.json) there is a script for deploying.

```json
"deploy": "npx hardhat run scripts/deploy.js",
"deploy": "npx hardhat run scripts/deploy.ts",
```

You can chose which environment that you want to deploy to by passing in the `--network` flag with `avash`, `fuji`, or `mainnet` for each respective environment. If you don't pass in `--network` then it will default to the hardhat network. For example, if you want to deploy to mainnet
Expand All @@ -74,7 +74,7 @@ When you deploy to `avash`, `fuji` or `mainnet` you can add your private key(s)

## Hardhat Tasks

You can define custom hardhat tasks in [hardhat.config.js](./hardhat.config.js). There are two tasks included as examples—`accounts` and `balances` both of which have scripts in [package.json](./package.json).
You can define custom hardhat tasks in [hardhat.config.ts](./hardhat.config.ts). There are two tasks included as examples—`accounts` and `balances` both of which have scripts in [package.json](./package.json).

```json
"accounts": "npx hardhat accounts",
Expand Down Expand Up @@ -206,7 +206,7 @@ RunScript: Running scripts/five_node_staking.lua
RunScript: Successfully ran scripts/five_node_staking.lua
```

Now you have a local avalanche network with 5 staking nodes. Next transfer 1000 AVAX from the X-Chain to each of the 10 avash accounts in `hardhat.config.js`.
Now you have a local avalanche network with 5 staking nodes. Next transfer 1000 AVAX from the X-Chain to each of the 10 avash accounts in `hardhat.config.ts`.

```zsh
cd /path/to/avalanche-smart-contract-quickstart
Expand Down Expand Up @@ -284,12 +284,12 @@ Deploy the contract to the `avash` local network
```zsh
yarn deploy --network avash
yarn run v1.22.4
$ npx hardhat run scripts/deploy.js --network avash
$ npx hardhat run scripts/deploy.ts --network avash
Coin deployed to: 0x789a5FDac2b37FCD290fb2924382297A6AE65860
✨ Done in 1.28s.
```

We now have a token deployed at `0x789a5FDac2b37FCD290fb2924382297A6AE65860`. The `constructor` function of [this token](contracts/Coin.sol#L15) `mint`s the `TOTAL_BALANCE` to the account which called the contract which is the first address in the `avash` `accounts` array in [hardhat.config.js](./hardhat.config.js).
We now have a token deployed at `0x789a5FDac2b37FCD290fb2924382297A6AE65860`. The `constructor` function of [this token](contracts/Coin.sol#L15) `mint`s the `TOTAL_BALANCE` to the account which called the contract which is the first address in the `avash` `accounts` array in [hardhat.config.ts](./hardhat.config.ts).

```json
"0x56289e99c94b6912bfc12adc093c9b51124f0dc54ac7a766b2bc5ccf558d8027",
Expand Down
17 changes: 0 additions & 17 deletions contracts/Coin.sol

This file was deleted.

24 changes: 24 additions & 0 deletions contracts/ERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//SPDX-License-Identifier: mit
pragma solidity >= 0.6.2 < 0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract ExampleERC20 is ERC20, Ownable {
string private TOKEN_NAME = "Example ERC20 Token";
string private TOKEN_SYMBOL = "XMPL";

uint private constant TOTAL_SUPPLY = 123456789;

constructor() ERC20(TOKEN_NAME, TOKEN_SYMBOL) {
_mint(msg.sender, TOTAL_SUPPLY);
}

function mint(address to, uint256 amount) public onlyOwner() {
_mint(to, amount);
}

function burn(address from, uint256 amount) public onlyOwner() {
_burn(from, amount);
}
}
24 changes: 11 additions & 13 deletions contracts/ERC721.sol
Original file line number Diff line number Diff line change
@@ -1,26 +1,24 @@
// contracts/ERC721.sol
// spdx-license-identifier: mit

pragma solidity >= 0.6.2;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/utils/Counters.sol";

contract GameItem is ERC721 {
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;
using Counters for Counters.Counter;
Counters.Counter private _tokenIds;

constructor() ERC721("GameItem", "ITM") {}
constructor() ERC721("GameItem", "ITM") {}

function awardItem(address player, string memory tokenURI)
public
returns (uint256)
{
_tokenIds.increment();
function awardItem(address player, string memory tokenURI) public returns (uint256) {
_tokenIds.increment();

uint256 newItemId = _tokenIds.current();
_mint(player, newItemId);
_setTokenURI(newItemId, tokenURI);
uint256 newItemId = _tokenIds.current();
_mint(player, newItemId);
// _setTokenURI(newItemId, tokenURI);

return newItemId;
}
return newItemId;
}
}
14 changes: 14 additions & 0 deletions contracts/Storage.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
//SPDX-License-Identifier: mit
pragma solidity ^0.8.0;

contract Storage {
uint256 number;

function store(uint256 num) public {
number = num;
}

function retrieve() public view returns (uint256) {
return number;
}
}
48 changes: 21 additions & 27 deletions hardhat.config.js → hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,38 @@
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-ethers");

import { task } from "hardhat/config"
import { SignerWithAddress } from "@nomiclabs/hardhat-ethers/signers"
import { BigNumber } from "ethers"
import "@nomiclabs/hardhat-waffle"

// When using the hardhat network, you may choose to fork Fuji or Avalanche Mainnet
// This will allow you to debug contracts using the hardhat network while keeping the current network state
// To enable forking, turn one of these booleans on, and then run your tasks/scripts using ``--network hardhat``
// For more information go to the hardhat guide
// https://hardhat.org/hardhat-network/
// https://hardhat.org/guides/mainnet-forking.html
const FORK_FUJI = false;
const FORK_MAINNET = false;
const FORK_FUJI = false
const FORK_MAINNET = false
const forkingData = FORK_FUJI ? {
url: 'https://api.avax-test.network/ext/bc/C/rpc',
} : FORK_MAINNET ? {
url: 'https://api.avax.network/ext/bc/C/rpc'
} : undefined

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async () => {
const accounts = await ethers.getSigners();

for (const account of accounts) {
console.log(account.address);
}
});
task("accounts", "Prints the list of accounts", async (args, hre): Promise<void> => {
const accounts: SignerWithAddress[] = await hre.ethers.getSigners()
accounts.forEach((account: SignerWithAddress): void => {
console.log(account.address)
})
})

task("balances", "Prints the list of AVAX account balances", async () => {
const accounts = await ethers.getSigners();

for (const account of accounts) {
balance = await ethers.provider.getBalance(account.address);
console.log(account.address, "has balance", balance.toString());
}
});
task("balances", "Prints the list of AVAX account balances", async (args, hre): Promise<void> => {
const accounts: SignerWithAddress[] = await hre.ethers.getSigners()
accounts.forEach(async (account: SignerWithAddress): Promise<void> => {
const balance: BigNumber = await hre.ethers.provider.getBalance(account.address)
console.log(`${account.address} has balance ${balance.toString()}`)
})
})

/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
export default {
solidity: {
compilers: [
{
Expand Down Expand Up @@ -94,4 +88,4 @@ module.exports = {
accounts: []
}
}
};
}
23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
"@nomiclabs/hardhat-ethers": "^2.0.1",
"@nomiclabs/hardhat-waffle": "^2.0.1",
"@openzeppelin/contracts": "^3.4.0",
"@types/chai": "^4.2.18",
"@types/mocha": "^8.2.2",
"@types/node": "^15.6.0",
"avalanche": "3.2.0",
"chai": "^4.2.0",
"chai": "^4.3.4",
"ethereum-waffle": "^3.2.1",
"ethereumjs-tx": "^2.1.2",
"ethers": "^5.0.24",
"hardhat": "^2.2.0",
"hardhat": "2.3.0",
"ts-node": "^9.1.1",
"web3": "^1.3.1"
},
"version": "1.0.0",
Expand All @@ -27,13 +31,22 @@
"console": "npx hardhat console",
"pretest": "yarn compile",
"test": "npx hardhat test",
"deploy": "npx hardhat run scripts/deploy.js",
"send-avax": "npx hardhat run scripts/sendAvax.js",
"deploy": "npx hardhat run scripts/deploy.ts",
"erc20": "npx hardhat run scripts/erc20.ts",
"storage": "npx hardhat run scripts/storage.ts",
"send-avax-wallet-signer": "npx hardhat run scripts/sendAvaxWalletSigner.ts",
"send-avax-json-provider": "npx hardhat run scripts/sendAvaxJSONProvider.ts",
"lint": "prettier ./test/**/*.ts --check",
"prepublishOnly": "yarn test",
"hardhat": "npx hardhat",
"accounts": "npx hardhat accounts",
"balances": "npx hardhat balances",
"fund-cchain-addresses": "npx hardhat run scripts/fund-cchain-addresses.js"
},
"dependencies": {
"typescript": "^4.2.4"
},
"engines": {
"node": ">=14.17.0"
}
}
}
33 changes: 0 additions & 33 deletions scripts/deploy.js

This file was deleted.

20 changes: 20 additions & 0 deletions scripts/deploy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
Contract,
ContractFactory
} from "ethers"
import { ethers } from "hardhat"

const main = async(): Promise<any> => {
const Coin: ContractFactory = await ethers.getContractFactory("ExampleERC20")
const coin: Contract = await Coin.deploy()

await coin.deployed()
console.log(`Coin deployed to: ${coin.address}`)
}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})
57 changes: 57 additions & 0 deletions scripts/erc20.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
BigNumber,
Contract
} from "ethers"
import { ethers } from "hardhat"

const coinName: string = ""
const coinAddr: string = ""
const walletAddress: string = ""

const main = async (): Promise<any> => {
const contract: Contract = await ethers.getContractAt(coinName, coinAddr)
const contractAddress: string = contract.address
console.log(`Address: ${contractAddress}`)

const name: string = await contract.name()
console.log(`Name: ${name}`)

const symbol: string = await contract.symbol()
console.log(`Symbol: ${symbol}`)

const decimals: string = await contract.decimals()
console.log(`Decimals: ${decimals}`)

const balance: BigNumber = await contract.balanceOf(walletAddress)
console.log(`Balance of ${walletAddress}: ${balance.toString()}`)

let totalSupply: BigNumber = await contract.totalSupply()
console.log(`Total supply: ${totalSupply.toString()}`)

console.log(`-----MINTING-----`)
await contract.mint(walletAddress, totalSupply)

totalSupply = await contract.totalSupply()
console.log(`Total supply: ${totalSupply.toString()}`)

console.log(`-----BURNING-----`)
await contract.burn(walletAddress, totalSupply)

totalSupply = await contract.totalSupply()
console.log(`Total supply: ${totalSupply.toString()}`)

const tx = await contract.transfer(walletAddress, balance)
console.log("--TX--")
console.log(tx)

const txReceipt = await tx.wait()
console.log("--TX RECEIPT--")
console.log(txReceipt)
}

main()
.then(() => process.exit(0))
.catch(error => {
console.error(error)
process.exit(1)
})
Loading

0 comments on commit 8cd0c77

Please sign in to comment.