From cf0713eda7a050d356cfa21a40dc54d509bdc967 Mon Sep 17 00:00:00 2001 From: Ashwin Phatak Date: Fri, 27 Aug 2021 17:47:23 +0530 Subject: [PATCH] Tasks in erc20-watcher to deploy and transfer tokens. (#233) Co-authored-by: nabarun --- packages/erc20-watcher/.gitignore | 6 +++- packages/erc20-watcher/hardhat.config.ts | 27 +++++++++++++++ packages/erc20-watcher/package.json | 9 ++++- .../erc20-watcher/test/contracts/GLDToken.sol | 10 ++++++ .../erc20-watcher/test/tasks/block-latest.ts | 18 ++++++++++ .../erc20-watcher/test/tasks/token-deploy.ts | 19 +++++++++++ .../test/tasks/token-transfer.ts | 33 +++++++++++++++++++ packages/erc20-watcher/tsconfig.json | 3 +- yarn.lock | 5 +++ 9 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 packages/erc20-watcher/hardhat.config.ts create mode 100644 packages/erc20-watcher/test/contracts/GLDToken.sol create mode 100644 packages/erc20-watcher/test/tasks/block-latest.ts create mode 100644 packages/erc20-watcher/test/tasks/token-deploy.ts create mode 100644 packages/erc20-watcher/test/tasks/token-transfer.ts diff --git a/packages/erc20-watcher/.gitignore b/packages/erc20-watcher/.gitignore index b3ab1ae4..3ac0fb0a 100644 --- a/packages/erc20-watcher/.gitignore +++ b/packages/erc20-watcher/.gitignore @@ -3,4 +3,8 @@ node_modules/ build/ tmp/ -temp/ \ No newline at end of file +temp/ + +#Hardhat files +cache +artifacts diff --git a/packages/erc20-watcher/hardhat.config.ts b/packages/erc20-watcher/hardhat.config.ts new file mode 100644 index 00000000..b3419e70 --- /dev/null +++ b/packages/erc20-watcher/hardhat.config.ts @@ -0,0 +1,27 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import '@nomiclabs/hardhat-waffle'; + +import './test/tasks/token-deploy'; +import './test/tasks/token-transfer'; +import './test/tasks/block-latest'; + +// You need to export an object to set up your config +// Go to https://hardhat.org/config/ to learn more + +/** + * @type import('hardhat/config').HardhatUserConfig + */ +export default { + solidity: '0.8.0', + networks: { + docker: { + url: 'http://dapptools:8545' + } + }, + paths: { + sources: './test/contracts' + } +}; diff --git a/packages/erc20-watcher/package.json b/packages/erc20-watcher/package.json index b65bc26c..67078230 100644 --- a/packages/erc20-watcher/package.json +++ b/packages/erc20-watcher/package.json @@ -10,7 +10,13 @@ "test": "mocha -r ts-node/register src/**/*.spec.ts", "lint": "eslint .", "build": "tsc", - "watch:contract": "ts-node src/cli/watch-contract.ts --configFile environments/local.toml" + "watch:contract": "ts-node src/cli/watch-contract.ts --configFile environments/local.toml", + "token:deploy": "hardhat --network localhost token-deploy", + "token:deploy:docker": "hardhat --network docker token-deploy", + "token:transfer": "hardhat --network localhost token-transfer", + "token:transfer:docker": "hardhat --network docker token-transfer", + "block:latest": "hardhat --network localhost block-latest", + "block:latest:docker": "hardhat --network docker block-latest" }, "repository": { "type": "git", @@ -50,6 +56,7 @@ }, "devDependencies": { "@ethersproject/abi": "^5.3.0", + "@openzeppelin/contracts": "^4.3.1", "@types/express": "^4.17.11", "@types/fs-extra": "^9.0.11", "@types/json-bigint": "^1.0.0", diff --git a/packages/erc20-watcher/test/contracts/GLDToken.sol b/packages/erc20-watcher/test/contracts/GLDToken.sol new file mode 100644 index 00000000..1d8e68b3 --- /dev/null +++ b/packages/erc20-watcher/test/contracts/GLDToken.sol @@ -0,0 +1,10 @@ +// SPDX-License-Identifier: AGPL-3.0 +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; + +contract GLDToken is ERC20 { + constructor(uint256 initialSupply) ERC20("Gold", "GLD") { + _mint(msg.sender, initialSupply); + } +} diff --git a/packages/erc20-watcher/test/tasks/block-latest.ts b/packages/erc20-watcher/test/tasks/block-latest.ts new file mode 100644 index 00000000..a6337a26 --- /dev/null +++ b/packages/erc20-watcher/test/tasks/block-latest.ts @@ -0,0 +1,18 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { task } from 'hardhat/config'; +import '@nomiclabs/hardhat-ethers'; + +task( + 'block-latest', + 'Prints the current block info', + async (_, { ethers }) => { + const blockNumber = await ethers.provider.getBlockNumber(); + const block = await ethers.provider.getBlock(blockNumber); + + console.log('Block Number:', blockNumber); + console.log('Block Hash:', block.hash); + } +); diff --git a/packages/erc20-watcher/test/tasks/token-deploy.ts b/packages/erc20-watcher/test/tasks/token-deploy.ts new file mode 100644 index 00000000..94729e8b --- /dev/null +++ b/packages/erc20-watcher/test/tasks/token-deploy.ts @@ -0,0 +1,19 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { task, types } from 'hardhat/config'; +import '@nomiclabs/hardhat-ethers'; + +const DEFAULT_INITIAL_SUPPLY = '1000000000000000000000'; + +task('token-deploy', 'Deploys GLD token') + .addOptionalParam('initialSupply', 'Set total supply', DEFAULT_INITIAL_SUPPLY, types.string) + .setAction(async (args, hre) => { + const { initialSupply } = args; + await hre.run('compile'); + const Token = await hre.ethers.getContractFactory('GLDToken'); + const token = await Token.deploy(hre.ethers.BigNumber.from(initialSupply)); + + console.log('GLD Token deployed to:', token.address); + }); diff --git a/packages/erc20-watcher/test/tasks/token-transfer.ts b/packages/erc20-watcher/test/tasks/token-transfer.ts new file mode 100644 index 00000000..23416a09 --- /dev/null +++ b/packages/erc20-watcher/test/tasks/token-transfer.ts @@ -0,0 +1,33 @@ +// +// Copyright 2021 Vulcanize, Inc. +// + +import { task, types } from 'hardhat/config'; +import '@nomiclabs/hardhat-ethers'; +import { ContractTransaction } from 'ethers'; + +task('token-transfer', 'Move tokens to recipient') + .addParam('token', 'Token contract address', undefined, types.string) + .addParam('to', 'Transfer recipient address', undefined, types.string) + .addParam('amount', 'Token amount to transfer', undefined, types.int) + .setAction(async (args, hre) => { + const { token: tokenAddress, to, amount } = args; + await hre.run('compile'); + const Token = await hre.ethers.getContractFactory('GLDToken'); + const token = Token.attach(tokenAddress); + + const transaction: ContractTransaction = await token.transfer(to, amount); + + const receipt = await transaction.wait(); + + if (receipt.events) { + const TransferEvent = receipt.events.find(el => el.event === 'Transfer'); + + if (TransferEvent && TransferEvent.args) { + console.log('Transfer Event'); + console.log('from:', TransferEvent.args.from.toString()); + console.log('to:', TransferEvent.args.to.toString()); + console.log('value:', TransferEvent.args.value.toString()); + } + } + }); diff --git a/packages/erc20-watcher/tsconfig.json b/packages/erc20-watcher/tsconfig.json index eff67a7e..0ff86607 100644 --- a/packages/erc20-watcher/tsconfig.json +++ b/packages/erc20-watcher/tsconfig.json @@ -73,5 +73,6 @@ "resolveJsonModule": true /* Enabling the option allows importing JSON, and validating the types in that JSON file. */ }, "include": ["src"], - "exclude": ["dist"] + "exclude": ["dist"], + "files": ["./hardhat.config.ts"] } diff --git a/yarn.lock b/yarn.lock index 1f7db796..2bec5ea5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1895,6 +1895,11 @@ resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-3.4.1-solc-0.7-2.tgz#371c67ebffe50f551c3146a9eec5fe6ffe862e92" integrity sha512-tAG9LWg8+M2CMu7hIsqHPaTyG4uDzjr6mhvH96LvOpLZZj6tgzTluBt+LsCf1/QaYrlis6pITvpIaIhE+iZB+Q== +"@openzeppelin/contracts@^4.3.1": + version "4.3.1" + resolved "https://registry.yarnpkg.com/@openzeppelin/contracts/-/contracts-4.3.1.tgz#c01f791ce6c9d3989ac1a643267501dbe336b9e3" + integrity sha512-QjgbPPlmDK2clK1hzjw2ROfY8KA5q+PfhDUUxZFEBCZP9fi6d5FuNoh/Uq0oCTMEKPmue69vhX2jcl0N/tFKGw== + "@protobufjs/aspromise@^1.1.1", "@protobufjs/aspromise@^1.1.2": version "1.1.2" resolved "https://registry.yarnpkg.com/@protobufjs/aspromise/-/aspromise-1.1.2.tgz#9b8b0cc663d669a7d8f6f5d0893a14d348f30fbf"