1.1.9 • Published 3 years ago

@dattabot-blockchain/geneth-tx v1.1.9

Weekly downloads
12
License
ISC
Repository
-
Last release
3 years ago

Javascript SDK to interact with smart contract that implements EIP-137

How To

1. Install Package

npm install generalized-sdk

2. Set Environment Variables(optional):

You need to set environment variables: ENS_ADDRESS, and RPC.

For example:

export ENS_ADDRESS=my_ens_address
export RPC=my_rpc

Default Environment Variables:

This is the default setting if you do not set it

ENS_ADDRESS=0x6833934d94fb4bebb9a780c5af3848a3de0c1f0e
RPC=https://testnet.haratoken.app

Functions

Constructor

You can set which web3 you want to use when initiating new instance.

let SDK = new GeneralizeSDK(my_ens_address, my_web3);

Parameters

  1. my_ens_address(optional) : Ens address
  2. my_web3(optional) : Your web3

setRPC

Set RPC to interact with

SDK.setRPC(_yourRPC);

Will return a new Web3 instance if succeded and boolean 'false' if failed. Currently listed chain ID for transaction are : 1. mainnet 2. morden 3. ropsten 4. rinkeby 5. goerli 6. kovan 7. haratoken

You can add your own customize chain ID here at SDK/chainID.json

Fetch

Get transaction hash

SDK.fetch(obj);

Parameters

  1. Object : Object you have to define to interact with SDK
    • uri: ENS name
    • fn: which function you want call/send
    • addr: address used to call the contract (resolved using current web3 if not defined)
    • callback : an object that has following functions:
      • sign(tx): function to sign transaction. Have to return raw of the signedTransaction
      • getAddress(): function to get sender of the transaction. Have to return the sender address
    • params: array of parameters

Returns

You will get an Object with following attribute:

  • txHash : Transaction hash if you send transaction to blockchain

    or

  • result : Data from smart contract if you pass call function

Example

let objSend =  {
    uri : 'attesttest.hara.ethnet',
    fn : 'attest',
    callback: {
        sign: async (tx)=>{
            let pk = web3.utils.sha3(seed +  process.env['TOKEN_SALT']);
            let acc = web3.eth.accounts.privateKeyToAccount(pk);
            tx.fromAddress = acc.address;
            let signedTx = await acc.signTransaction(tx);
            return signedTx.rawTransaction;
        },
        getAddress: async ()=>{
            let pk = web3.utils.sha3(seed + process.env['TOKEN_SALT']);
            return web3.eth.accounts.privateKeyToAccount(pk).address;
        }
    },
    params : [
        initVersion,
        initItemAddress,
        initTopic,
        initValue,
        initExpiredTime
    ]
};

Object for call

let objCall =  {
    uri : 'attesttest.hara.ethnet',
    fn : 'getValue',
    callback: {
        sign: async (tx)=>{
            let pk = web3.utils.sha3(seed +  process.env['TOKEN_SALT']);
            let acc = web3.eth.accounts.privateKeyToAccount(pk);
            tx.fromAddress = acc.address;
            let signedTx = await acc.signTransaction(tx);
            return signedTx.rawTransaction;
        },
        getAddress: async ()=>{
            let pk = web3.utils.sha3(seed + process.env['TOKEN_SALT']);
            return web3.eth.accounts.privateKeyToAccount(pk).address;
        }
    },
    params : [
        initVersion,
        initItemAddress,
        initTopic,
        attestorAddress
    ]
};

Get Transaction Hash

Remember you can still get transaction hash even though the transaction has not mined yet.

There two ways to get transaction hash from send transaction object:

.then

SDK.fetch(obj).then(result=>{
    console.log(result.txHash)
}).catch(err=>{
    ...
});

async/await

try{
    let result = await SDK.fetch(obj);
    txHash = result.txHash;
}catch(err){
    ...
}

getTransactionReceipt

You need to wait several seconds for transaction to be mined/confirmed

SDK.getTransactionReceipt(txHash);

Parameters

  • txHash : Transaction hash

Returns

  • receipt : Full receipt of the transaction hash

getPastEvents(Not Tested Yet)

To get past events that are emitted from a smart contract from block 0 to latest

SDK.getPastEvents(uri, eventName, filter);

Parameters

  • uri : ENS or HNS name
  • eventName : Event name
  • filter : an Object that needs to follow these rules:
    1. The key in the object is the event variable name followed by its value
    2. The value of the element is in array form

Returns

Promise returns

  • Array : an array consists of event objects

Example

events = await SDK.getPastEvents(
    'attest.hara.ethnet',//uri
    'AttestLog', //eventName
    {itemAddress:[0xfaC4316f68389F1b1ec40Cb39113Ea8F8BFb8DdD], //filter
     version:['0x000000c.....']} //the each value is in an array
    );

Testing

You need to define MNEMONIC in environment variable

Rename test.example.sh to test.sh

test.sh will contain:

    export ENS_ADDRESS=0xmy_ens_address..
    export RPC=my_rpc
    export MNEMONIC=my_mnemonic_words
    npm run test

then execute test.sh in terminal

. test.sh