Blockchain Utilities
A suite of utility functions for interacting with the blockchain. Functions are separated into Pledgecamp-specific and general blockchain helpers. The library provides a consolidated package of basic blockchain operations for use across all projects that interact with the blockchain. It contains functions such as the following:
- Manage connection to Web3
- Calculate and manage gas prices
- Loading contract objects for use within projects
- View balances and wallet addresses
- Sign and transfer transactions
- Handle events coming back from the blockchain
- Updating and setting basic blockchain settings
Common Directory
contracts-setup script
Mainly in charge of setting up tokens and assigning them to appropriate contracts after deployment.
Also creates connections between contracts for future inter-contract interactions. Currently supports the following connections:
- Administrator to CampShareManager
- AccountStorage to AccountManager
- AccountManager to CampShareManager
- AccountManager to Administrator
- CampShareManager to Administrator
- CampShareManager to Moderator
- CampShareStorage to CampShareManager
derivePrivateKey
Derives the private key given by BIP index and the coin index
bipIdx[integer]: BIP indexcoin[integer]: Coin indexchangeIndex[integer]: 0 = External chain (default), 1 = Internal chain- @returns [string] Private key
Ethereum Directory
checkAddress
Checks if address is a valid Ethereum address
address[string]: The target wallet address to check- @returns [boolean]: True or false depending on result
getBalance
Gets token/ETH balance
address[string]: Changes the BIP index involved in the operationtoken[string]: Name of the token balance to check (Currently only 'eth' and 'plg' available)accountId[string]: Id of token accounttokenContractObject[string]: The token web3 object (Only for tokens other than 'eth')- @returns [BigNumber] Balance in BigNumber format
getAccountFromBipIndex
Get address from bip wallet index
bipIdx[integer]: Bip wallet IndexchangeIndex[integer]: 0 = External chain (default), 1 = Internal chaincoinIdx[integer]: Registered coin type code (Default 60 = ETH)- @returns [string] Derived wallet address
updateCommunalWalletTransactionIndex
Sets the communal wallet index to the 0 index address
- @returns [string] Nonce
getAmountHex
Convert string representation of a numerical number into hex
valString[string]: String representation of number- @returns [string] Hex representation of number
getConfirmationTime
Get block timestamp of a transaction in the ETH timestamp format
txHash[string]: Transaction hash- @returns [BigNumber] Block timestamp of transaction
getCurrentBlockNumber
Get current block number on the blockchain
- @returns [BigNumber] Block number in BigNumber format
getTransactionReceipt
Get transaction receipt object from blockchain
txHash[string]: Transaction hash- @returns [object] Transaction receipt object
getTransactionCount
Get number of transactions (nonce) from this address
address[string]: Target address to check transaction count- @returns [string] Transaction count in string format
getTransactionList
Get transaction list of a certain wallet address
walletAddress[string]: Target wallet address to get transaction list fromstartCondition[integer]: Transaction number to start counting from (Default 0)etherscanPath[string]: Route for Etherscan APIetherscanApiKey[string]: API Key for Etherscan- @returns [map] Map of transactions
setConfirmationWaitTime
Set current settings for time needed to confirm transaction on blockchain
confirmationNum[integer]: Number of blocks to wait for a finalized transactiontime[integer]: The time needed for one block to be confirmed
getConfirmationWaitTime
Get current settings for time needed to confirm transaction on blockchain
- @returns [BigNumber] Total estimated confirmation time in BigNumber format
loadContract
Get contract reference
contractReference[string]: The filename of the contract json file (ie. PLGToken if PLGToken.json)path[string]: The local path of the contract's json file- @returns [object] Contract web3 reference object
loadLiveContract
Gets contract web3 object of a previously deployed contract
contractReference[string]: The filename of the contract json file (ie. PLGToken if PLGToken.json)address[string]: The deployed contract addresspath[string]: The local path of the contract's json file- @returns [object] Contract web3 reference object
loadContractByteCode
Gets contract bytecode
contractReference[string]: The filename of the contract json file (ie. PLGToken if PLGToken.json)path[string]: The local path of the contract's json file- @returns [string] Contract bytecode
transfer
Perform a transfer from an account that we control
txParam[object]:transactionType[string]: Type of transactionamount[integer]: Amount to transfertoAddress[string]: Target addressbipIndex[integer]: bipIndex valuechangeIndex[integer]: 0 = External chain (default), 1 = Internal chaincoinIndex[integer]: 60 = EthereumabiData[string]: Encoded contract abi dataabiType[integer]: Value used for gasEstimate() - 0 = raw, 1 = bareTransactioncallbackURL[string]: Callback URLgasParams[object]:priorityFactor[integer]: Priority factor to allocate more gas for urgent operationsopsFactor[integer]: Ops factor of transaction to allocate more gas for gas intense operationsbaseGasIndex[integer]: Base gas level to use for operationsisDeploy[integer]: TRUE = deploy contract operation, FALSE = all other operationsgasPriceLevel[integer]: Level of speed required for transaction (Range from 1 - FASTEST to 4 - SLOWEST)- @returns [object] Transaction hash\
setProvider
Gets web3 client object
rpcProviderMethod[string]: Method of web3 connection (HTTP, IPC, Websocket)rpcProviderAddress[string]: Route for connection- @returns [object] Web3 object
getClient
Get web3 client object
- @returns [object] Web3 object
eventHandler
Decodes and returns event logs from the blockchain
logs[object]: Transaction log objecttransactionType[string]: Type or category of transaction- @returns [array] Array of contract event contents
transactionSign
Signs transaction and returns serialized transaction
transactionParams[object]: Transaction parameters to initialize transactionbipIndex[integer]: Bip wallet IndexchangeIndex[integer]: 0 = External chain (default), 1 = Internal chaincoinIdx[integer]: Registered coin type code (Default 60 = ETH)chainName[string]: Name of network- @returns [string] Serialized signed transaction
getSerializedTrans
Gets serialized transaction
gasParams[object]:priorityFactor[integer]: Priority factor to allocate more gas for urgent operationsopsFactor[integer]: Ops factor of transaction to allocate more gas for gas intense operationsbaseGasIndex[integer]: Base gas level to use for operationsisDeploy[integer]: TRUE = deploy contract operation, FALSE = all other operationsgasPriceLevel[integer]: Level of speed required for transaction (Range from 1 - FASTEST to 4 - SLOWEST)- [integer]
activityIdActivity ID associated with transaction - [object]
txParamtransactionType[string]: Type of transactiontoAddress[string]: Target addressbipIndex[integer]: bipIndex valuechangeIndex[integer]: 0 = External chain (default), 1 = Internal chaincoinIndex[integer]: 60 = EthereumabiData[string]: Encoded contract abi dataabiType[integer]: Value used for gasEstimate() - 0 = raw, 1 = bareTransactioncallbackURL[string]: Callback URL - @returns [object] Transaction data object
pullGasPrice
Gets latest Gas Prices from ETH Gas Station
ethGasAPI[string]: ETH Gas Station API route and API key- @returns [object] Latest priceData object
getGasPrice
Gets and calculates gas price to use based on various priority factors
gasLevel[integer]: Level of speed required for transaction (Range from 1 - FASTEST to 4 - SLOWEST)priorityFactor[integer]: Priority factor to allocate more gas for urgent operationsopsFactor[integer]: Ops factor of transaction to allocate more gas for gas intense operations- @returns [BigNumber] Final gas price
setAdminWalletAddress
Sets wallet address as default admin wallet
walletAddress[string]: Address of target wallet
getAdminWalletAddress
Gets default admin wallet address
- @returns [string] Current default admin wallet address
calcGas
Calculates gas estimate including priority and ops factors
isDeploy[boolean]: Whether or not transaction involves contract deploymentabiData[string]: Abi data from contractpriorityFactor[integer]: Priority factor to allocate more gas for urgent operationsopsFactor[integer]: Ops factor of transaction to allocate more gas for gas intense operationsbaseGas[integer]: Minimum base gas level for transactiongasLevel[integer]: Level of speed required for transaction (Range from 1 - FASTEST to 4 - SLOWEST)toAddress[string]: Target address of transaction- @returns [BigNumber] Total estimated gas price
setPrivateKey
Sets private key of account
key[string]: Wallet private key- @returns [string] Set private key
setMnemonic
Sets list of mnemonic phrases
mnemonicPhrases[string]: List of mnemonic phrases- @returns [string] Set of mnemonicPhrases
setMnemonicPassword
Sets list of mnemonic password
mnemonicPassword[string]: Mnemonic password- @returns [string] The mnemonic password