Skip to content

sun-blockchain/harmony-pet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

34 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Welcome to Harmony pet 👋

Description

  • Structures : Each pet is equivalent to 1 smart contract Feature to detemine the shape of pet : Amount token + Number of days to raise pet In which:

    • The amount of tokens determines the size
    • The number of raising day determines the pet's effects
  • Interact with pet :

    • Feed : Buy food in proportion to the amount you want to deposit into the smart contract
    • Withdraw: Withdraw tokens from smart contract (pet) and transfer to the math wallet

Setup ( Testnet version )

  • Custom mathWallet:

  • Blockchain (smart contracts):

    • Load packages :
    cd deploy_contract
    yarn install
    • Setup .env file:
    TESTNET_PRIVATE_KEY=
    TESTNET_MNEMONIC=
    
    TESTNET_0_URL=
    TESTNET_1_URL=
    
    GAS_LIMIT=3321900
    GAS_PRICE=1000000000
    • Deploy contract:
    yarn truffle migrate --network testnet

    Contract abi will be located in client/src/contracts

  • Frontend ( Reactjs ):

    • Load packages:
    cd client
    yarn install
    • Setup .env file:
    NODE_PATH=src/
    • Run client server:
    yarn start

Usage

Create a Harmony instance connecting to testnet

import { ChainID, ChainType, fromWei, hexToNumber, Units } from '@harmony-js/utils';

const hmy = new Harmony('https://api.s0.b.hmny.io', {
  chainType: ChainType.Harmony,
  chainId: ChainID.HmyTestnet
});

Sign in with mathWallet

let isMathWallet = window.harmony && window.harmony.isMathWallet;
if (isMathWallet) {
  let mathwallet = window.harmony;
  mathwallet.getAccount().then(async (account) => {});
}

Init factory harmony contract:

import Factory from 'contracts/PetWalletFactory.json';
let factoryAddress = Factory.networks[2].address;
let factory = hmy.contracts.createContract(Factory.abi, factoryAddress);

Config gas && gaslinit:

const GAS_LIMIT = 6721900;
const GAS_PRICE = 1000000000;
const options = {
  gasPrice: GAS_PRICE,
  gasLimit: GAS_LIMIT
};

Call function:

let petArray = await factory.methods
  .getAllPetAddressOf(hmy.crypto.getAddress(account.address).checksum)
  .call(options);

Create transaction with mathWallet:

return new Promise(async (resolve, reject) => {
  try {
    // Add signer to contract instance
    factory.wallet.defaultSigner = hmy.crypto.getAddress(account.address).checksum;
    factory.wallet.signTransaction = async (tx) => {
      try {
        tx.from = hmy.crypto.getAddress(account.address).checksum;
        const signTx = await window.harmony.signTransaction(tx);
        return signTx;
      } catch (e) {
        console.error(e);
        reject(e);
      }
    };
    // Send transaction
    const res = await factory.methods
      .create(petId, targetFund, duration, purpose)
      .send(options)
      .then(() => {
        window.location.href = `/mypets/${pets.length}`;
      })
      .catch((e) => {
        console.log('Create pet action error', e);
      });
    resolve(res);
  } catch (e) {
    console.error(e);
    reject(e);
  }
});

Show your support

Give a ⭐️ if this project helped you!


This README was generated with ❤️ by readme-md-generator

Releases

No releases published

Packages

 
 
 

Contributors