@energi/web3-ext
v0.3.0
Published
Energi Cryptocurrency - Energi3 Web3 Extension
Downloads
16
Readme
Energi Web3js extension
Give web3.js features to interact with Energi's testnet or mainnet. Use whatever version of web3.js you like.
This module comes with three features:
- web3.js extensions:
web3.nrg
,web3.energi
andweb3.masternode
EnergiTxCommon
, to be used along withrequire('ethereumjs-tx').Transaction
, enabling signed transaction to the Energi testnet and mainnet- Energi unit maps, so you can use web3.utils.toWei('1', 'nrg')
Note that Energi Unit Maps, like web3.utils.toWei('1', 'nrg')
, can only be used on a web3
instance, not directly with Web3
.
Networks to connect to (providers):
testnet: https://nodeapi.test.energi.network
mainnet: https://nodeapi.energi.network
Or you can launch your own node and connect on either port 39797 (mainnet) or 49797 (testnet)
How to use:
const Web3 = require('web3');
const web3Extension = require('@energi/web3-ext');
// use 'https://nodeapi.test.energi.network' to connect to the testnet
// use 'https://nodeapi.energi.network' to connect to the mainnet
const web3 = new Web3('https://nodeapi.test.energi.network');
// extend the features of web3, so that you have Energi's full public api:
web3Extension.extend(web3);
// use web3 for json-rpc requests:
const showBalance = async address => {
let balance, balanceWei, balanceNrg;
try {
balance = await web3.nrg.getBalance(address);
// balance is a BigNumber. To show, use .toString():
balanceWei = balance.toString();
balanceNrg = web3.utils.fromWei(balanceWei, 'nrg');
console.log('Balance:', balanceNrg, 'NRG');
}
catch (err) {
console.error(err);
}
};
// example Energi address:
const myAddress = '0x2066640b59210bbbC84a36cA2eB64C6D2096636f';
showBalance(myAddress);
More examples
Actual masternode rewards:
const Web3 = require('web3');
const web3Extension = require('@energi/web3-ext');
const web3 = new Web3('https://nodeapi.energi.network');
// extend the features of web3, so that you have Energi's full public api:
web3Extension.extend(web3);
// use web3 for json-rpc requests:
const showRewards = async () => {
let activeCollateral,
payoutCycleTimeMin,
annualMasternodeRewardsFractional,
annualMasternodeRewardsPercent,
energiPublishedReward,
stats;
const REWARDED_COLLATERAL_PER_BLOCK = 10000, // each block, 10,000 collaterized NRG get a reward
MN_REWARD_1000_NRG = 0.914, // every payout cycle time, every 1000 NRG collateral gets 0.914 NRG rewarded
MN_REWARD_NRG = MN_REWARD_1000_NRG / 1000,
MIN_PER_YEAR = 365 * 1440;
try {
stats = await web3.masternode.stats();
activeCollateral = stats.activeCollateral.toString();
// activeCollateral was in Wei -> transfer to NRG:
activeCollateral = web3.utils.fromWei(activeCollateral, 'nrg');
payoutCycleTimeMin = Math.round(activeCollateral / REWARDED_COLLATERAL_PER_BLOCK);
annualMasternodeRewardsFractional = (MIN_PER_YEAR / payoutCycleTimeMin) * MN_REWARD_NRG;
annualMasternodeRewardsPercent = 100 * annualMasternodeRewardsFractional;
// Energi publishes 10% below the actual reward, because rewards may change due to fluctiations of activeCollateral:
energiPublishedReward = Math.round(0.9 * annualMasternodeRewardsPercent);
console.log(energiPublishedReward);
}
catch (err) {
console.error(err);
}
};
showRewards();
Send NRG coins:
const Web3 = require('web3');
const Tx = require('ethereumjs-tx').Transaction;
const web3Extension = require('@energi/web3-ext');
const web3 = new Web3('https://nodeapi.test.energi.network');
// extend the features of web3, so that you have Energi's full public api:
web3Extension.extend(web3);
const sendTx = async (fromPrivateKey, fromAddress, toAddress, amountWei) => {
let txCount,
privateKeyBuffer,
tx,
rawTx,
serializedTx,
txResult,
energiTxCommon;
try {
energiTxCommon = await web3Extension.getTxCommon(web3);
if (fromPrivateKey.startsWith('0x')) {
fromPrivateKey = fromPrivateKey.substr(2);
}
txCount = await web3.nrg.getTransactionCount(fromAddress);
rawTx = {
nonce: web3.utils.toHex(txCount),
value: web3.utils.toHex(amountWei),
gasPrice: web3.utils.toHex(web3.utils.toWei('10', 'gwei')),
gasLimit: web3.utils.toHex(21000),
to: toAddress
};
tx = new Tx(rawTx, {common: energiTxCommon});
privateKeyBuffer = Buffer.from(fromPrivateKey, 'hex');
tx.sign(privateKeyBuffer);
serializedTx = tx.serialize();
txResult = await web3.nrg.sendSignedTransaction('0x' + serializedTx.toString('hex'));
console.log(txResult);
}
catch (err) {
console.error(err);
}
};
const amountNrg = '1',
amounNrgWei = web3.utils.toWei(amountNrg, 'nrg'),
privateKeyFrom = '0x...', // use a valid NRG "from" privateKey
addressFrom = '0x...', // use a valid NRG "from" address that belongs to the "from" privateKey above
addressTo = '0x...'; // use a valid NRG "to" address
sendTx(privateKeyFrom, addressFrom, addressTo, amounNrgWei);
API
web3.nrg
Same API as web3.eth, see Web3.js API
web3.energi
web3.energi.blacklistInfo()
returns the blacklistweb3.energi.budgetInfo()
returns a list of proposals and the available balanceweb3.energi.compensationInfo()
returns a list of compensations and available balanceweb3.energi.searchGen2Coins(gen2CoinList {Array}, showEmpty {boolean})
searches for Gen2 coin infoweb3.energi.searchGen3DestinationByGen2Address(gen2Address {String})
searches for the Gen3 destination address for a given Gen2 addressweb3.energi.searchRawGen2Coins(gen2RawOwnerAddressList {Array}, showEmpty {Boolean})
searches for Gen2 coin infoweb3.energi.upgradeInfo()
returns the upgrade info
web3.masternode
web3.masternode.collateralBalance(address {String})
finds the amount collateralized NRG of the specified address, also returns the last block on which the NRG coins were collateralizedweb3.masternode.listMasternodes()
returns a list with all announced masternodesweb3.masternode.masternodeInfo(masternodeOrOwnerAddress {String})
finds detailed information about a masternodeweb3.masternode.stats()
retrieves masternode statistics: active, activeCollateral {BN}, maxOfAllTimes {BN}, total, totalCollateral {BN}
Unitmaps
web3.utils.fromWei
- web3.utils.fromWei(value, 'femtonrg')
- web3.utils.fromWei(value, 'piconrg')
- web3.utils.fromWei(value, 'nanonrg')
- web3.utils.fromWei(value, 'micronrg')
- web3.utils.fromWei(value, 'millinrg')
- web3.utils.fromWei(value, 'nrg')
- web3.utils.fromWei(value, 'knrg')
- web3.utils.fromWei(value, 'mnrg')
- web3.utils.fromWei(value, 'gnrg')
- web3.utils.fromWei(value, 'tnrg')
web3.utils.toWei
- web3.utils.toWei(value, 'nonrg')
- web3.utils.toWei(value, 'femtonrg')
- web3.utils.toWei(value, 'piconrg')
- web3.utils.toWei(value, 'nanonrg')
- web3.utils.toWei(value, 'micronrg')
- web3.utils.toWei(value, 'millinrg')
- web3.utils.toWei(value, 'nrg')
- web3.utils.toWei(value, 'knrg')
- web3.utils.toWei(value, 'mnrg')
- web3.utils.toWei(value, 'gnrg')
- web3.utils.toWei(value, 'tnrg')
LICENCE
All code is licensed under the New BSD License.
© 2020 Energi Cryptocurrency. All rights reserved.