GithubHelp home page GithubHelp logo

hrc's Introduction

HRC

Harmony ERC20

Overview

This sample project can be used to deploy an ERC20 token on Harmony's Testnet

Pre-requisites

Please read the guideline for Smart Contract Development using Truffle

Install

#install truffle
$npm install -g [email protected]

#clone this project
$git clone https://github.com/harmony-one/HRC.git
$cd HRC
$cp .envSample .env

#install modules
$npm install

Compile smart contract

$truffle compile

Deploy smart contract to Harmony's testnet

$truffle migrate --network testnet --reset

List the smart contract on testnet

$truffle networks

Network: development (id: 2)
  No contracts deployed.

Network: testnet (id: 2)
  HarmonyERC20: 0xf2c3b75dAB0e45652Bf0f9BD9e08d48b03c3926E
  Migrations: 0x7b5B72fD8A1A4B923Fb12fF1f50b5C84F920278d

Get the event logs of a transaction

LOCAL=http://localhost:9500
SHARD0=https://api.s0.b.hmny.io
SHARD1=https://api.s1.b.hmny.io
SHARD2=https://api.s2.b.hmny.io
#your params
SHARD=LOCAL
#example is HRC20 mint and transfer
TXID=0x039d2f87e6bdb81220e5a7490dc783ea835443f57f4e12d16d90dd0b3aa1f5af
#curl
curl -X POST $SHARD -H 'Accept: */*'   -H 'Accept-Encoding: gzip, deflate'   -H 'Cache-Control: no-cache'   -H 'Connection: keep-alive'   -H 'Content-Length: 162'   -H 'Content-Type: application/json'   -H 'Host: api.s0.b.hmny.io'   -H 'Postman-Token: d5415117-657a-49f9-9100-a5b7ebc70daf,cc2f3cb9-2d10-408c-a003-d6e0822ec985'   -H 'User-Agent: PostmanRuntime/7.19.0'   -H 'cache-control: no-cache'   -d '{
    "jsonrpc":"2.0",
    "method":"hmy_getTransactionReceipt",
    "params":["'$TXID'"],
    "id":1
}'

Interact with smart contract with a custom javascript

A custom javascript mint_transfer.js is used to mint and transfer token

var HarmonyERC20 = artifacts.require("HarmonyERC20");

//mint amount address
const myAddress =   "0x3aea49553Ce2E478f1c0c5ACC304a84F5F4d1f98";

//test account address, keys under
//https://github.com/harmony-one/harmony/blob/master/.hmy/keystore/one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7.key
const testAccount = "0x7c41e0668b551f4f902cfaec05b5bdca68b124ce";

const transferAmount = 2000000;

module.exports = function() {
    async function getHarmonyERC20Information() {
        let instance = await HarmonyERC20.deployed();
        let name = await instance.name();
        let total = await instance.totalSupply();
        let decimals = await instance.decimals();
        let mybalance = await instance.balanceOf(myAddress);
        
        instance.transfer(testAccount, transferAmount);
        let testAccBalance = await instance.balanceOf(testAccount);

        console.log("HarmonyERC20 is deployed at address " + instance.address);
        console.log("Harmony ERC20 Information: Name    : " + name);
        console.log("Harmony ERC20 Information: Decimals: " + decimals);
        console.log("Harmony ERC20 Information: Total   : " + total.toString());
        console.log("my address : " + myAddress);
        console.log("test account address : " + testAccount);
        console.log("my minted    H2O balance is: " + mybalance.toString());
        console.log("test account H2O balance is: " + testAccBalance.toString());
        console.log("\ntransfered " + transferAmount.toString() + " from my address (minter) to test account");
    }
    getHarmonyERC20Information();
};

A custom javascript show_balance.js is used to show balance

var HarmonyERC20 = artifacts.require("HarmonyERC20");

//mint amount address
const myAddress =   "0x3aea49553Ce2E478f1c0c5ACC304a84F5F4d1f98";

//test account address, keys under
//https://github.com/harmony-one/harmony/blob/master/.hmy/keystore/one103q7qe5t2505lypvltkqtddaef5tzfxwsse4z7.key
const testAccount = "0x7c41e0668b551f4f902cfaec05b5bdca68b124ce";

const transferAmount = 2000000;

module.exports = function() {
    async function getHarmonyERC20Information() {
        let instance = await HarmonyERC20.deployed();
        let name = await instance.name();
        let total = await instance.totalSupply();
        let decimals = await instance.decimals();
        let mybalance = await instance.balanceOf(myAddress);
        let testAccBalance = await instance.balanceOf(testAccount);

        console.log("HarmonyERC20 is deployed at address " + instance.address);
        console.log("Harmony ERC20 Information: Name    : " + name);
        console.log("Harmony ERC20 Information: Decimals: " + decimals);
        console.log("Harmony ERC20 Information: Total   : " + total.toString());
        console.log("my address : " + myAddress);
        console.log("test account address : " + testAccount);
        console.log("my minted    H2O balance is: " + mybalance.toString());
        console.log("test account H2O balance is: " + testAccBalance.toString());

    }
    getHarmonyERC20Information();
};

Mint and send token to a test account

$ truffle exec ./mint_transfer_token.js  --network testnet
Using network 'testnet'.

HarmonyERC20 is deployed at address 0xbBE1E92631C8846ff729C09FD629F98544966c6A
Harmony ERC20 Information: Name    : HarmonyERC20
Harmony ERC20 Information: Decimals: 18
Harmony ERC20 Information: Total   : 1000000000000000000000000
my address : 0x3aea49553Ce2E478f1c0c5ACC304a84F5F4d1f98
test account address : 0x7c41e0668b551f4f902cfaec05b5bdca68b124ce
my minted    H2O balance is: 1000000000000000000000000
test account H2O balance is: 0

transfered 2000000 from my address (minter) to test account

type ctrl+C to exit

Show balance of two accounts

$ truffle exec ./show_balance.js  --network testnet
Using network 'testnet'.

HarmonyERC20 is deployed at address 0xbBE1E92631C8846ff729C09FD629F98544966c6A
Harmony ERC20 Information: Name    : HarmonyERC20
Harmony ERC20 Information: Decimals: 18
Harmony ERC20 Information: Total   : 1000000000000000000000000
my address : 0x3aea49553Ce2E478f1c0c5ACC304a84F5F4d1f98
test account address : 0x7c41e0668b551f4f902cfaec05b5bdca68b124ce
my minted    H2O balance is: 999999999999999998000000
test account H2O balance is: 2000000

type ctrl+C to exit

hrc's People

Contributors

cem-harmony avatar denniswon avatar dependabot[bot] avatar john-harmony avatar mattlockyer avatar renewwen avatar rhazberries avatar rlan35 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

hrc's Issues

Faucet Improvements

Overview

Currently the faucet frequenty times out giving a bad gateway response. Believe this is due to the inablity to process more than one funding transction per block, potentially due to a nonce issue when calling the faucet. Also the faucet has run out of funds due to many people spamming it and depleting the funds in the faucet to zero.

Proposed area for improvement

  1. Funding increase amount of funds by 10x and put a top up mechanism in place
  2. Timeouts - analyze whether we can send multiple transactions per block - nonce issue
  3. Deploy multiple(5) faucets with different end points using a tree structure
  4. Modify process to remove smart contract and sign transactions directly (if this enables multi transactions per block)
  5. Enhance further to send from multiple accounts.
  6. Evaluate using a similar approach to the funding process see fund.sh and fund.py

Cannot get logs from HRC20 transactions

This issue is tracking the lack of logs returned by Harmony Core SDK + RPC calls from the core protocol.

Specifically, the absence of logs for transactions.

See #2 for issues related directly to SDK / RPC

When calling directly via RPC by using the transaction id:

TXID=0xf6fa4d7912bc40c632627e52040b4af632f52f5c79d9b4648ca9a03cad2f4384
curl -X POST http://localhost:9500 -H 'Content-Type: application/json' -d '{
    "jsonrpc":"2.0",
    "method":"hmy_getTransactionReceipt",
    "params":["'$TXID'"],
    "id":1
}'

The response has no logs and an odd logsBloom value:

    gasUsed: 51338,
    logs: [],
    logsBloom: '0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    shardID: 0,
    ...

Will be uploading an example and some tests to HRC repo shortly.

Cannot Get Events from harmony-js/core

HarmonyERC20.sol extends from Open Zepplin contracts.

Several events are emitted to the blockchain when contracts are deployed. These are written directly into the Truffle artifacts. Other events should be retrievable by connecting to the web socket endpoint via:

    const harmony = new Harmony(`ws://localhost:9800`, {
        chainType: ChainType.Harmony, 
        chainId: ChainID.HmyTestnet,
    })

And using the following JS:

    contract.events.Transfer({
        fromBlock: 0
    }, function(error, event){ console.log(event); })

However no events are emitted and this callback method is never called (nor are the promise based .on methods)

Empty blocknumber in header coming from txReceipt

This line of code in the JS SDK:
https://github.com/harmony-one/sdk/blob/0ff928bc623ef7560c38c02c7ca34387653cafe5/packages/harmony-transaction/src/transactionBase.ts#L220

Potentially related to:
harmony-one/harmony#2079

Produces undefined for blockNumber in 2 scenarios:

A regular ONE token transfer
.on('receipt', ...)

const tx = hmy.transactions.newTx({
        to: address,
        value: new hmy.utils.Unit(amount).asEther().toWei(),
        gasLimit: '210000',
        shardID: 0,
        toShardID: 0,
        gasPrice: new hmy.utils.Unit('10').asGwei().toWei(),
    });

    const signedTX = await hmy.wallet.signTransaction(tx);
    signedTX.observed().on('transactionHash', (txHash) => {
        console.log('--- txHash ---', txHash);
    })
    .on('receipt', (receipt) => {
        console.log('--- receipt ---', receipt);
        const { active } = getState().harmonyReducer
        dispatch(getBalances(active))
        dispatch(updateProcessing(false))
    }).on('error', console.error)

And a smart contract method call
.on('receipt', ...)

const tx = contract.methods.purchase(active.address, index).send({
        from: active.address,
        value: new hmy.utils.Unit(1).asEther().toWei(),
        gasLimit: '1000000',
        gasPrice: new hmy.utils.Unit('10').asGwei().toWei(),
    }).on('transactionHash', function (hash) {
        console.log('hash', hash)
    }).on('receipt', function (receipt) {
        console.log('receipt', receipt)
    }).on('confirmation', async (confirmationNumber, receipt) => {
        console.log('confirmationNumber', confirmationNumber, receipt)
        dispatch(getInventory())
        dispatch(updateProcessing(false))
    }).on('error', console.error)

My fix so far is:

blockNumber = this.messenger.chainPrefix === 'hmy'
                                    ? data.params.result.Header.number
                                    : data.params.result.number;

                                console.log(data.params.result.Header)

                                if (!blockNumber) {
                                    blockNumber = '0x0'
                                }

But this doesn't explain why a header would come back with no blockNumber???

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.