Created
October 8, 2020 16:40
-
-
Save karzak/829919f3966c2c37dafa26466d265668 to your computer and use it in GitHub Desktop.
binance-chain javascript-sdk for local node
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { BncClient, crypto } = require('@binance-chain/javascript-sdk'); | |
(async () => { | |
// Start Binance Chain client | |
const bnbClient = await new BncClient('http://localhost:8080'); | |
bnbClient.chooseNetwork('mainnet'); | |
const privateKey = crypto.getPrivateKeyFromMnemonic( | |
'village fiscal december liquid better drink disorder unusual tent ivory cage diesel bike slab tilt spray wife neck oak science beef upper chapter blade' | |
); | |
await bnbClient.setPrivateKey(privateKey); | |
// set chainID manually so initChain doesn't complain | |
bnbClient.chainId = "Binance-Chain-Tigris" | |
await bnbClient.initChain() | |
// override the default transaction broadcast endpoint with the tendermint RPC endpoint on the binance-chain node (port 26658 in kvtool) | |
bnbClient.setBroadcastDelegate(async(signedTx) => { | |
const signedBz = signedTx.serialize() | |
console.log(signedBz) | |
const opts = { | |
params: {tx: "0x" + signedBz}, | |
headers: { | |
"content-type": "application/json", | |
}, | |
} | |
var result | |
try { | |
result = await bnbClient._httpClient.request( | |
"get", | |
`http://localhost:26658/broadcast_tx_commit`, | |
null, | |
opts | |
) | |
} catch (error) { | |
console.log(error) | |
} | |
return result | |
}) | |
// Check Bnbchain balance | |
let bnbAccbalances = await bnbClient.getBalance( | |
'bnb1gg3a488t69ueg79lqneqx0h3442x7gtu8drvjz' | |
); | |
console.log('BnbChain balance:', bnbAccbalances); | |
// transfer coins | |
const receiver = "bnb1z8ryd66lhc4d9c0mmxx9zyyq4t3cqht9mt0qz3" | |
try { | |
const res = await bnbClient.transfer(bnbClient.getClientKeyAddress(), receiver, 1.0, "BNB") | |
console.log(JSON.stringify(res)) | |
} catch (error) { | |
console.log(error) | |
} | |
})(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"name": "js-testing", | |
"version": "1.0.0", | |
"description": "", | |
"main": "index.js", | |
"scripts": { | |
"test": "echo \"Error: no test specified\" && exit 1" | |
}, | |
"author": "", | |
"license": "ISC", | |
"dependencies": { | |
"@binance-chain/javascript-sdk": "@binance-chain/javascript-sdk" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment