Venus.js
A JavaScript SDK for Binance Smart Chain and the Venus Protocol. Wraps around Ethers.js. Works in the web browser and Node.js.
Venus.js Documentation
This SDK is in open beta, and is constantly under development. USE AT YOUR OWN RISK.
Binance Smart Chain Read & Write
JSON RPC based Binance Smart Chain read and write.
Read
const Venus = require('@swipewallet/venus-js');
const vUsdtAddress = Venus.util.getAddress(Venus.vUSDT);
(async function() {
let supplyRatePerBlock = await Venus.eth.read(
vUsdtAddress,
'function supplyRatePerBlock() returns (uint)',
[],
{}
);
console.log('USDT supplyRatePerBlock:', supplyRatePerBlock.toString());
})().catch(console.error);
Write
const toAddress = '0xa0df350d2637096571F7A701CBc1C5fdE30dF76A';
(async function() {
const trx = await Venus.eth.trx(
toAddress,
'function send() external payable',
[],
{
value: Venus._ethers.utils.parseEther('1.0'),
provider: window.ethereum,
}
);
const toAddressEthBalance = await Venus.eth.getBalance(toAddress);
})().catch(console.error);
Venus Protocol
Simple methods for using the Venus protocol.
const venus = new Venus(window.ethereum);
(async function() {
console.log('Supplying ETH to the Venus protocol...');
const trx = await venus.supply(Venus.ETH, 1);
console.log('Ethers.js transaction object', trx);
})().catch(console.error);
Install / Import
Web Browser
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@swipewallet/venus-js@latest/dist/browser/venus.min.js"></script>
<script type="text/javascript">
window.Venus;
</script>
Node.js
npm install @swipewallet/venus-js
const Venus = require('@swipewallet/venus-js');
import Venus from '@swipewallet/venus-js';
More Code Examples
To run, boot Ganache fork of mainnet locally
Instance Creation
The following are valid Binance Smart Chain providers for initialization of the SDK.
var venus = new Venus(window.ethereum);
var venus = new Venus('http://127.0.0.1:8545');
var venus = new Venus();
var venus = new Venus('testnet');
var venus = new Venus('http://127.0.0.1:8545', {
privateKey: '0x_your_private_key_',
});
var venus = new Venus('mainnet' {
mnemonic: 'clutch captain shoe...',
});
Constants and Contract Addresses
Names of contracts, their addresses, ABIs, token decimals, and more can be found in /src/constants.ts
. Addresses, for all networks, can be easily fetched using the getAddress
function, combined with contract name constants.
console.log(Venus.BUSD, Venus.BNB, Venus.vSXP);
const vUsdtAddress = Venus.util.getAddress(Venus.vUSDT);
Mantissas
Parameters of number values can be plain numbers or their scaled up mantissa values. There is a transaction option boolean to tell the SDK what the developer is passing.
await venus.borrow(Venus.BUSD, '1000000000000000000', { mantissa: true });
await venus.borrow(Venus.BUSD, 1, { mantissa: false });
Transaction Options
Each method that interacts with the blockchain accepts a final optional parameter for overrides, much like Ethers.js overrides.
const trxOptions = {
mantissa,
abi,
provider,
network,
from,
gasPrice,
gasLimit,
value,
data,
chainId,
nonce,
privateKey,
mnemonic,
};
API
The Venus API is accessible from Venus.js. The corresponding services are defined in the api
namespace on the class.
Venus.api.account
Venus.api.vToken
Venus.api.marketHistory
Venus.api.governance
The governance method requires a second parameter (string) for the corresponding endpoint shown in the documentation.
proposals
voteReceipts
accounts
Here is an example for using the account
endpoint. The network
parameter in the request body is optional and defaults to mainnet
.
const main = async () => {
const account = await Venus.api.account({
"addresses": "0xB61C5971d9c0472befceFfbE662555B78284c307",
"network": "testnet"
});
let sxpBorrowBalance = 0;
if (Object.isExtensible(account) && account.accounts) {
account.accounts.forEach((acc) => {
acc.tokens.forEach((tok) => {
if (tok.symbol === Venus.vSXP) {
sxpBorrowBalance = +tok.borrow_balance_underlying.value;
}
});
});
}
console.log('sxpBorrowBalance', sxpBorrowBalance);
}
main().catch(console.error);
Build for Node.js & Web Browser
git clone git@github.com:venusprotocol/venus-js.git
cd venus-js/
npm install
npm run build
Web Browser Build
<script type="text/javascript" src="./dist/browser/venus.min.js"></script>
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/@swipewallet/venus-js@latest/dist/browser/venus.min.js"></script>
Node.js Build
const Venus = require('./dist/nodejs/index.js');
const Venus = require('@swipewallet/venus-js');