2022.1.1 • Published 2 years ago
@brewchain/hardhat-provider v2022.1.1
@brewchain/hardhat-provider
Install
$ npm install @brewchain/hardhat-provider
Setting hardhat.config.js
require("@nomiclabs/hardhat-waffle");
require("@nomiclabs/hardhat-etherscan");
require("@brewchain/hardhat-provider");
// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
const accounts = {
mnemonic: process.env.MNEMONIC || "test test test test test test test test test test test junk",
// initialIndex: 18
// accountsBalance: "990000000000000000000",
}
task("accounts", "Prints the list of accounts", async () => {
const accounts = await ethers.getSigners();
for (const account of accounts) {
console.log(account.address);
}
});
// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more
/**
* @type import('hardhat/config').HardhatUserConfig
*/
module.exports = {
solidity: {
compilers: [
{
version: "0.6.12",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
{
version: "0.5.17",
settings: {
optimizer: {
enabled: true,
runs: 200,
},
},
},
],
},
spdxLicenseIdentifier: {
overwrite: false,
runOnCompile: true,
},
etherscan: {
// Your API key for Etherscan
// Obtain one at https://etherscan.io/
apiKey:"6IQTZTMD392X2U2SYZBABWDS8KB6D8UD4T"
},
defaultNetwork: "brewchain",
networks: {
brewchain:{
url:"http://c0:8000",
accounts,
gasPrice: 20*1000000000,
chainId: 168,
loggingEnabled: true,
blockGasLimit:0x280de80,
},
}
};
@brewchain/kit.js simple to use
Setting up the network,@brewchain/kit.js support network has testnet and prodnet
const brewchain=require('@brewchain/kit.js');
var rp = require('request-promise')
//set prodnet network
brewchain.config.server_base='http://rc.domain.com/fbs';
brewchain.config.net_type='prodnet'
brewchain.config.rpc_provider = rp;
Create keystore.json、keypair
const brewchain=require('@brewchain/kit.js');
const fs=require('fs');
var kp = brewchain.KeyPair.genRandomKey();
/**
* create keystore
* @param {*} kp
* @param {*} password
*/
var json = brewchain.keystore.exportJSON(kp,"000000");
let fd = fs.openSync('keystore.json', 'w+');
fs.writeSync(fd,JSON.stringify(json));
console.log('keypair',kp);
Get balance and nonce
const brewchain=require('@brewchain/kit.js');
/**
* get balance and nonce
* @param {*} address
* @result {
"retCode": 1,
"address": "0x46f1f188bca9c555464ab41daecffaa0405f177c",
"nonce": 0,//nonce
"balance": "0x21e19e0c9bab2400000",//16进制余额
"status": 0
}
*/
brewchain.rpc.getBalance('8f3aa4f0f35ff81ba487f91f6b980c0ba2562245').then(function(result){
console.log(result.account.balance)
console.log(result.account.nonce)
})
Get block info by hash
const brewchain=require('@brewchain/kit.js');
/**
* get block info by hash
* @param {*} blockhash
*/
brewchain.rpc.getBlockByHash('03a15d84e6e29d2affab1ddc680f0aefc20586bd73ea3d81dcf6505924cfb86c').then(function(result){
console.log(result)
})
Get block info by height
const brewchain=require('@brewchain/kit.js');
/**
* get block info by height
* @param {*} height
*/
brewchain.rpc.getBlockByNumber('1518059').then(function(result){
console.log(result)
})
Get the current height of the BREW blockchain
const brewchain=require('@brewchain/kit.js');
brewchain.rpc.getBlockByMax().then(function(result){
console.log(result)
})
Get transaction information through transaction hash
const brewchain=require('@brewchain/kit.js');
/**
* get transaction information
* @param {*} transactionhash
*/
brewchain.rpc.getTransaction('f9bea09140e8e2eb2956976c3373418e2a935d821732d86bce33117d17314088').then(function(result){
console.log(result)
})
Send transfer,support balance transfer,token transfer, crypto token transfer 。
const brewchain=require('@brewchain/kit.js');
/**
* get keystore by prikey
* @param {*} prikey
*/
var kp = brewchain.KeyPair.genFromPrikey(
'89611e9ed751b2bb0f2a84d1b364bd6ef97a512a7ad0b1b50241168ff3add985')
/**
* transfer
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
* @param {*} exdata "hexstring"
* @param {*} args [{"address":"066c03fcc3048863f72b051530e5a212fb9233f6","amount":""}]
* @result {*}
* {
* "retCode": 1, //1=成功 0=失败
* "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
* }
*/
kp.nonce=10;
//remove prefix
kp.hexAddress=brewchain.rpc.removeCVN(kp.hexAddress);
var from={keypair:kp};
var args=[{"address":brewchain.rpc.removeCVN("066c03fcc3048863f72b051530e5a212fb9233f6"),"amount":1}]
var exdata=""
brewchain.rpc.transfer(from,extdata,args).then(function(result){
console.log(result)
}).catch(function(error){
console.log(error);
})
sign transfer
/**
* sign transfer normal
* @param {*} from {"keypair":{"address":"","privateKey":"",nonce:10}}
* @param {*} exdata
* @param {*} args [{"address":"066c03fcc3048863f72b051530e5a212fb9233f6","amount":""}]
*/
//only get sign
var sign = brewchain.rpc.signTransfer(from,extdata,args);
Create a token transaction
/**
* create token
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"", "nonce": 0}}
* @param {*} exdata "hexstring"
* @param {*} args {"tos":["",""], "values":["",""],"name":"","symbol":"","decimals":18}
* @result {*}
* {
* "retCode": 1, //1=成功 0=失败
* "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
* }
*/
brewchain.rpc.createToken(from,exdata args).then(function(result){
console.log(result)
}).catch(function(error){
console.log(error);
})
sign create token
/**
* create token
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"", "nonce": 0}}
* @param {*} exdata "hexstring"
* @param {*} args {"tos":["",""], "values":["",""],"name":"","symbol":"","decimals":18}
* @result {"tx":tx}
*/
//only get sign
var sign = brew.rpc.signCreateToken(from,extdata,args);
Transfer token transaction
/**
* transfer token
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
* @param {*} token
* @param {*} args {"tos":["",""], "values":["",""]}
* @result {*}
* {
* "retCode": 1, //1=成功 0=失败
* "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
* }
*/
brewchain.rpc.transferToken(from,token,args).then(function(result){
console.log(result)
}).catch(function(error){
console.log(error);
})
sign transfer token
/**
* transfer token
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
* @param {*} token
* @param {*} args {"tos":["",""], "values":["",""]}
* @result {"tx":tx}
*/
var sign = brewchain.rpc.signTransferToken(from,extdata,args);
Create contract
/**
* create contract
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
* @param {*} exdata "hexstring"
* @param {*} args {"data":"hexstring"}
* @result {*}
* {
* "retCode": 1, //1=成功 0=失败
* "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
* "contractHash":""//合约地址 ,其他交易无此参数
* }
*/
brewchain.rpc.createContract(from,exdata,args).then(function(result){
console.log(result)
}).catch(function(error){
console.log(error);
})
sign Create contract
/**
* create contract
* @param {*} from {"keypair":{"hexAddress":"","privateKey":"",nonce:10}}
* @param {*} exdata "hexstring"
* @param {*} args {"data":"hexstring"}
* @result {"tx":tx}
*/
var sign = brewchain.rpc.signCreateContract(from,extdata,args);
Call contract
/**
* call contract
* @param {*} from
* @param {*} exdata
* @param {*} args {"contract":"", "data":"hexstring", "amount":""}
* @result {*}
* {
* "retCode": 1, //1=成功 0=失败
* "hash": "70d994369495f2139102a8391e463418d0f6d62e4a2e1444949f3eba2e1ebf74b7"//交易hash
* }
*/
brewchain.rpc.callContract(from,exdata,args).then(function(result){
console.log(result)
}).catch(function(error){
console.log(error);
})
sign Call contract
/**
* call contract
* @param {*} from
* @param {*} exdata
* @param {*} args {"contract":"", "data":"hexstring", "amount":""}
* @result {"tx":tx}
*/
var sign = brewchain.rpc.signCallContract(from,extdata,args);
License
MIT