1.0.16 • Published 1 year ago

react-solidity-web3 v1.0.16

Weekly downloads
-
License
MIT
Repository
-
Last release
1 year ago

react-solidity-web3 Npm Package

It works like a boilerplate package, which assist developer to just use the function and integrate their contracts at ease.

1 Installation

Install react-solidity-web33 with npm

  npm install react-solidity-web3

2 IMPLEMENTATION SAMPLE

3 How it works?

const { connectWallet,createWeb3Provider,createContractInstance, executeTransaction,queryData, queryEvents } = require('react-solidity-web3');

var connectOptions = {
  rpcObj: {
    50: "https://rpc.xinfin.network",
    51: "https://rpc.apothem.network"
  },
  network: "mainnet",
  toDisableInjectedProvider: true
}

const instance = await connectWallet(connectOptions);
const {provider,signer} = await createWeb3Provider(instance);
const sample = await createContractInstance(address, abi, provider);

How to write data

Consider you have 3 parameters to pass in registerFlights function in the smart contract, then

let _flightAddress = "0xA9e6835929f32DD440290b4c81466ff554b82667";
let _careerFlightNo = "ING695";
let _serviceProviderName = "Indigo Airlines";
let response1 = await executeTransaction(sample, provider, 'registerFlights', [_flightAddress, _careerFlightNo, _serviceProviderName]);

How to Read data from Blockchain with parameters overriden

Consider you have 2 parameters to pass in flights(mapping variable) in the smart contract to read

let _flightId = "1";
let _flightAddress = "0xA9e6835929f32DD440290b4c81466ff554b82667";
let response1 = await queryData(sample, provider, 'flights', [_flightId, _flightAddress]);

How to Read data from Blockchain without params

let response1 = await queryData(sample, provider, 'readAllFlight', []);

How to Read mapping variable

let response1 = await queryData(sample, provider, 'claims', ["asdgasdgsgsd"]);

How to Read Events

let returnvalue = await queryEvents(sample, provider, "InsuranceEvents", response1.blockNumber);

Parms & it's functionalities

ParmsDescriptions
connectOptionsObject contains rpcObj, network & toDisableInjectedProvider Parms
rpcObjby default it overrides Xinfin Mainnet & Apothem, if you want to override your evem compatible RPC, override it with ChainID:rpc url
networkdefault value mainnet, you can override to Apothem, Rinkeby etc
toDisableInjectedProviderdefault value true, set to false if you want Metamask to be shown

Function & Description

FunctionsDescriptions
executeTransactionIt calls the send Transaction and writes the data onto Blockchain
queryDataIt queries the data from Blockchain for a given function, you can read mapping variable, view function, public variable using this function
queryEventsIt enables you to read event details by passing block Number
createWeb3ProviderIt connects with web3 with chosen wallet and results provider & signer details
connectWalletIt enables you to connect with your wallet (metamask, XDCPay, Torus, WalletConnect)
createContractInstanceIt enables you to create a instance for your contract
EthereumContextit enables you to setup in main app.js file and Read thoes in other components
showToastsit enables you to show toast msg after successful execution
showErrorIt enables you to show error message when there is a failure
logit enables you to print detailed log by passing three parms
convertPriceToEthIt enables you to convert price to Wei
convertPricefromEthIt enables you to convert wei to actual price
uploadIt enables you to upload image to IPFS
checkCurrencyBalanceForUserIt enables to fetch balance for user
getTransactionReceiptMinedIN PROGRESS
MetaTxnIN PROGRESS

executeTransaction

let response1 = await executeTransaction(sample, provider, 'registerFlights', [_flightAddress, _careerFlightNo, _serviceProviderName]);

Function & Description

FunctionsParams
executeTransactionIt calls the send Transaction and writes the data onto Blockchain
queryDataIt queries the data from Blockchain for a given function, you can read mapping variable, view function, public variable using this function
queryEventsIt enables you to read event details by passing block Number
createWeb3ProviderIt connects with web3 with chosen wallet and results provider & signer details

executeTransaction

let response1 = await executeTransaction(contractInstance, provider, "functionName", [parm1, Parm2...]);

executeTransaction Params

FunctionsParams
contractInstanceContract that you want to interact with
providerweb3 provider
functionNameFunction you want to interact with
Parm1, Parm2List of params that function should expect, leave this array empty if no params required to pass

queryData

let response1 = await queryData(contractInstance, provider, "functionName", [parm1, Parm2...]);

queryData Params

FunctionsParams
contractInstanceContract that you want to interact with
providerweb3 provider
functionNameFunction you want to interact with
Parm1, Parm2List of params that function should expect, leave this array empty if no params required to pass

queryEvents

let response1 = await queryEvents(contractInstance, provider, eventName,blockNumber);

queryEvents Params

FunctionsParams
contractInstanceContract that you want to interact with
providerweb3 provider
eventNameEvent you want to access
blockNumberBlockNumber(latest)

connectWallet

var connectOptions = {
  rpcObj: {
    50: "https://rpc.xinfin.network",
    51: "https://rpc.apothem.network"
  },
  network: "mainnet",
  toDisableInjectedProvider: true
}
const walletinstance = await connectWallet(connectOptions);

connectWallet Params

FunctionsParams
connectOptionsObject to override the rpcObj, network & metamask injection

createWeb3Provider

    const {provider,signer} = await createWeb3Provider(walletinstance);

createWeb3Provider Params

FunctionsParams
walletinstanceResult of connectWallet should be passed as input here

createContractInstance

 const contractInstance = await createContractInstance(contractAddress, abi, provider);

createContractInstance Params

FunctionsParams
contractAddressyour smart contract address
abiAbi of your smart contract
providerProvider you obtained from createWeb3Provider

upload

const projectId = "<YOUR PROJECT ID>";
const projectSecret = "<YOUR PROJECT SECRET>";
const authorization = "Basic " + btoa(projectId + ":" + projectSecret);
const file = files[0];
const result = await upload({file,from,authorization});

upload Params

FunctionsParams
filefile you want to upload to IPFS
fromwallet addresss who is uploading this file
authorizationauthorization is required now to upload a file in IPFS. Projectid & Secret, you can get from your IPFS Infura account

More detailed information for other functions will be updated soon..