@swipewallet/venus-js
Version:
A JavaScript SDK for Ethereum and the Venus Protocol.
330 lines (303 loc) • 20.9 kB
HTML
<html class="default no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Venus.js - v0.0.4</title>
<meta name="description" content="Documentation for Venus.js - v0.0.4">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="assets/css/main.css">
</head>
<body>
<header>
<div class="tsd-page-toolbar">
<div class="container">
<div class="table-wrap">
<div class="table-cell" id="tsd-search" data-index="assets/js/search.json" data-base=".">
<div class="field">
<label for="tsd-search-field" class="tsd-widget search no-caption">Search</label>
<input id="tsd-search-field" type="text" />
</div>
<ul class="results">
<li class="state loading">Preparing search index...</li>
<li class="state failure">The search index is not available</li>
</ul>
<a href="index.html" class="title">Venus.js - v0.0.4</a>
</div>
<div class="table-cell" id="tsd-widgets">
<div id="tsd-filter">
<a href="#" class="tsd-widget options no-caption" data-toggle="options">Options</a>
<div class="tsd-filter-group">
<div class="tsd-select" id="tsd-filter-visibility">
<span class="tsd-select-label">All</span>
<ul class="tsd-select-list">
<li data-value="public">Public</li>
<li data-value="protected">Public/Protected</li>
<li data-value="private" class="selected">All</li>
</ul>
</div>
<input type="checkbox" id="tsd-filter-inherited" checked />
<label class="tsd-widget" for="tsd-filter-inherited">Inherited</label>
<input type="checkbox" id="tsd-filter-only-exported" />
<label class="tsd-widget" for="tsd-filter-only-exported">Only exported</label>
</div>
</div>
<a href="#" class="tsd-widget menu no-caption" data-toggle="menu">Menu</a>
</div>
</div>
</div>
</div>
<div class="tsd-page-title">
<div class="container">
<ul class="tsd-breadcrumb">
<li>
<a href="globals.html">Globals</a>
</li>
</ul>
<h1>Venus.js - v0.0.4</h1>
</div>
</div>
</header>
<div class="container container-main">
<div class="row">
<div class="col-8 col-content">
<div class="tsd-panel tsd-typography">
<a href="#venusjs" id="venusjs" style="color: inherit; text-decoration: none;">
<h1>Venus.js</h1>
</a>
<p>A JavaScript SDK for Binance Smart Chain and the Venus Protocol. Wraps around <a href="https://github.com/ethers-io/ethers.js/">Ethers.js</a>. Works in the <strong>web browser</strong> and <strong>Node.js</strong>.</p>
<p><a href="https://docs.venus.io/docs/venus-js">Venus.js Documentation</a></p>
<p>This SDK is in <strong>open beta</strong>, and is constantly under development. <strong>USE AT YOUR OWN RISK</strong>.</p>
<a href="#binance-smart-chain-read-amp-write" id="binance-smart-chain-read-amp-write" style="color: inherit; text-decoration: none;">
<h2>Binance Smart Chain Read & Write</h2>
</a>
<p>JSON RPC based Binance Smart Chain <strong>read</strong> and <strong>write</strong>.</p>
<a href="#read" id="read" style="color: inherit; text-decoration: none;">
<h3>Read</h3>
</a>
<pre><code class="language-js"><span class="hljs-keyword">const</span> Venus = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@swipewallet/venus-js'</span>); <span class="hljs-comment">// in Node.js</span>
<span class="hljs-keyword">const</span> vUsdtAddress = Venus.util.getAddress(Venus.vUSDT);
(<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">let</span> supplyRatePerBlock = <span class="hljs-keyword">await</span> Venus.eth.read(
vUsdtAddress,
<span class="hljs-string">'function supplyRatePerBlock() returns (uint)'</span>,
[], <span class="hljs-comment">// [optional] parameters</span>
{} <span class="hljs-comment">// [optional] call options, provider, network, ethers.js "overrides"</span>
);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'USDT supplyRatePerBlock:'</span>, supplyRatePerBlock.toString());
})().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
<a href="#write" id="write" style="color: inherit; text-decoration: none;">
<h3>Write</h3>
</a>
<pre><code class="language-js"><span class="hljs-keyword">const</span> toAddress = <span class="hljs-string">'0xa0df350d2637096571F7A701CBc1C5fdE30dF76A'</span>;
(<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">const</span> trx = <span class="hljs-keyword">await</span> Venus.eth.trx(
toAddress,
<span class="hljs-string">'function send() external payable'</span>,
[],
{
<span class="hljs-attr">value</span>: Venus._ethers.utils.parseEther(<span class="hljs-string">'1.0'</span>), <span class="hljs-comment">// 1 ETH</span>
<span class="hljs-attr">provider</span>: <span class="hljs-built_in">window</span>.ethereum, <span class="hljs-comment">// in a web browser</span>
}
);
<span class="hljs-keyword">const</span> toAddressEthBalance = <span class="hljs-keyword">await</span> Venus.eth.getBalance(toAddress);
})().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
<a href="#venus-protocol" id="venus-protocol" style="color: inherit; text-decoration: none;">
<h2>Venus Protocol</h2>
</a>
<p>Simple methods for using the Venus protocol.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> venus = <span class="hljs-keyword">new</span> Venus(<span class="hljs-built_in">window</span>.ethereum); <span class="hljs-comment">// in a web browser</span>
<span class="hljs-comment">// Ethers.js overrides are an optional 3rd parameter for `supply`</span>
<span class="hljs-comment">// const trxOptions = { gasLimit: 250000, mantissa: false };</span>
(<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Supplying ETH to the Venus protocol...'</span>);
<span class="hljs-keyword">const</span> trx = <span class="hljs-keyword">await</span> venus.supply(Venus.ETH, <span class="hljs-number">1</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'Ethers.js transaction object'</span>, trx);
})().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
<a href="#install--import" id="install--import" style="color: inherit; text-decoration: none;">
<h2>Install / Import</h2>
</a>
<p>Web Browser</p>
<pre><code class="language-html"><span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/@swipewallet/venus-js@latest/dist/browser/venus.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>
<span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span>></span><span class="javascript">
<span class="hljs-built_in">window</span>.Venus; <span class="hljs-comment">// or `Venus`</span>
</span><span class="hljs-tag"></<span class="hljs-name">script</span>></span></code></pre>
<p>Node.js</p>
<pre><code><span class="hljs-built_in">npm</span> install @swipewallet/venus-js</code></pre>
<pre><code class="language-js"><span class="hljs-keyword">const</span> Venus = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@swipewallet/venus-js'</span>);
<span class="hljs-comment">// or, when using ES6</span>
<span class="hljs-keyword">import</span> Venus <span class="hljs-keyword">from</span> <span class="hljs-string">'@swipewallet/venus-js'</span>;</code></pre>
<a href="#more-code-examples" id="more-code-examples" style="color: inherit; text-decoration: none;">
<h2>More Code Examples</h2>
</a>
<ul>
<li><a href="https://github.com/venusprotocol/venus-js/tree/master/examples">Node.js</a></li>
<li><a href="https://github.com/venusprotocol/venus-js/examples/web/">Web Browser</a></li>
</ul>
<p><a href="https://github.com/venusprotocol/venus-js/tree/master/examples">To run, boot Ganache fork of mainnet locally</a></p>
<a href="#instance-creation" id="instance-creation" style="color: inherit; text-decoration: none;">
<h2>Instance Creation</h2>
</a>
<p>The following are valid Binance Smart Chain providers for initialization of the SDK.</p>
<pre><code class="language-js"><span class="hljs-keyword">var</span> venus = <span class="hljs-keyword">new</span> Venus(<span class="hljs-built_in">window</span>.ethereum); <span class="hljs-comment">// web browser</span>
<span class="hljs-keyword">var</span> venus = <span class="hljs-keyword">new</span> Venus(<span class="hljs-string">'http://127.0.0.1:8545'</span>); <span class="hljs-comment">// HTTP provider</span>
<span class="hljs-keyword">var</span> venus = <span class="hljs-keyword">new</span> Venus(); <span class="hljs-comment">// Uses Ethers.js fallback mainnet (for testing only)</span>
<span class="hljs-keyword">var</span> venus = <span class="hljs-keyword">new</span> Venus(<span class="hljs-string">'testnet'</span>); <span class="hljs-comment">// Uses Ethers.js fallback (for testing only)</span>
<span class="hljs-comment">// Init with private key (server side)</span>
<span class="hljs-keyword">var</span> venus = <span class="hljs-keyword">new</span> Venus(<span class="hljs-string">'http://127.0.0.1:8545'</span>, {
<span class="hljs-attr">privateKey</span>: <span class="hljs-string">'0x_your_private_key_'</span>, <span class="hljs-comment">// preferably with environment variable</span>
});
<span class="hljs-comment">// Init with HD mnemonic (server side)</span>
<span class="hljs-keyword">var</span> venus = <span class="hljs-keyword">new</span> Venus(<span class="hljs-string">'mainnet'</span> {
<span class="hljs-attr">mnemonic</span>: <span class="hljs-string">'clutch captain shoe...'</span>, <span class="hljs-comment">// preferably with environment variable</span>
});</code></pre>
<a href="#constants-and-contract-addresses" id="constants-and-contract-addresses" style="color: inherit; text-decoration: none;">
<h2>Constants and Contract Addresses</h2>
</a>
<p>Names of contracts, their addresses, ABIs, token decimals, and more can be found in <code>/src/constants.ts</code>. Addresses, for all networks, can be easily fetched using the <code>getAddress</code> function, combined with contract name constants.</p>
<pre><code class="language-js"><span class="hljs-built_in">console</span>.log(Venus.BUSD, Venus.BNB, Venus.vSXP);
<span class="hljs-comment">// BUSD, BNB, vSXP</span>
<span class="hljs-keyword">const</span> vUsdtAddress = Venus.util.getAddress(Venus.vUSDT);
<span class="hljs-comment">// Mainnet vUSDT address. Second parameter can be a network like 'testnet'.</span></code></pre>
<a href="#mantissas" id="mantissas" style="color: inherit; text-decoration: none;">
<h2>Mantissas</h2>
</a>
<p>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.</p>
<pre><code class="language-js"><span class="hljs-comment">// 1 BUSD</span>
<span class="hljs-keyword">await</span> venus.borrow(Venus.BUSD, <span class="hljs-string">'1000000000000000000'</span>, { <span class="hljs-attr">mantissa</span>: <span class="hljs-literal">true</span> });
<span class="hljs-comment">// `mantissa` defaults to false if it is not specified or if an options object is not passed</span>
<span class="hljs-keyword">await</span> venus.borrow(Venus.BUSD, <span class="hljs-number">1</span>, { <span class="hljs-attr">mantissa</span>: <span class="hljs-literal">false</span> });</code></pre>
<a href="#transaction-options" id="transaction-options" style="color: inherit; text-decoration: none;">
<h2>Transaction Options</h2>
</a>
<p>Each method that interacts with the blockchain accepts a final optional parameter for overrides, much like <a href="https://docs.ethers.io/ethers.js/v5-beta/api-contract.html#overrides">Ethers.js overrides</a>.</p>
<pre><code class="language-js"><span class="hljs-comment">// The options object itself and all options are optional</span>
<span class="hljs-keyword">const</span> trxOptions = {
mantissa, <span class="hljs-comment">// Boolean, parameters array arg of 1 ETH would be '1000000000000000000' (true) vs 1 (false)</span>
abi, <span class="hljs-comment">// Definition string or an ABI array from a solc build</span>
provider, <span class="hljs-comment">// JSON RPC string, Web3 object, or Ethers.js fallback network (string)</span>
network, <span class="hljs-comment">// Ethers.js fallback network provider, "provider" has precedence over "network"</span>
<span class="hljs-keyword">from</span>, <span class="hljs-comment">// Address that the Binance Smart Chain transaction is send from</span>
gasPrice, <span class="hljs-comment">// Ethers.js override `Venus._ethers.utils.parseUnits('10.0', 'gwei')`</span>
gasLimit, <span class="hljs-comment">// Ethers.js override - see https://docs.ethers.io/ethers.js/v5-beta/api-contract.html#overrides</span>
value, <span class="hljs-comment">// Number or string</span>
data, <span class="hljs-comment">// Number or string</span>
chainId, <span class="hljs-comment">// Number</span>
nonce, <span class="hljs-comment">// Number</span>
privateKey, <span class="hljs-comment">// String, meant to be used with `Venus.eth.trx` (server side)</span>
mnemonic, <span class="hljs-comment">// String, meant to be used with `Venus.eth.trx` (server side)</span>
};</code></pre>
<a href="#api" id="api" style="color: inherit; text-decoration: none;">
<h2>API</h2>
</a>
<p>The <a href="https://docs.venus.io/docs/api">Venus API</a> is accessible from Venus.js. The corresponding services are defined in the <code>api</code> namespace on the class.</p>
<ul>
<li><code>Venus.api.account</code></li>
<li><code>Venus.api.vToken</code></li>
<li><code>Venus.api.marketHistory</code></li>
<li><code>Venus.api.governance</code></li>
</ul>
<p>The governance method requires a second parameter (string) for the corresponding endpoint shown in the <a href="https://docs.venus.io/docs/api#GovernanceService">documentation</a>.</p>
<ul>
<li><code>proposals</code></li>
<li><code>voteReceipts</code></li>
<li><code>accounts</code></li>
</ul>
<p>Here is an example for using the <code>account</code> endpoint. The <code>network</code> parameter in the request body is optional and defaults to <code>mainnet</code>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> main = <span class="hljs-keyword">async</span> () => {
<span class="hljs-keyword">const</span> account = <span class="hljs-keyword">await</span> Venus.api.account({
<span class="hljs-string">"addresses"</span>: <span class="hljs-string">"0xB61C5971d9c0472befceFfbE662555B78284c307"</span>,
<span class="hljs-string">"network"</span>: <span class="hljs-string">"testnet"</span>
});
<span class="hljs-keyword">let</span> sxpBorrowBalance = <span class="hljs-number">0</span>;
<span class="hljs-keyword">if</span> (<span class="hljs-built_in">Object</span>.isExtensible(account) && account.accounts) {
account.accounts.forEach(<span class="hljs-function">(<span class="hljs-params">acc</span>) =></span> {
acc.tokens.forEach(<span class="hljs-function">(<span class="hljs-params">tok</span>) =></span> {
<span class="hljs-keyword">if</span> (tok.symbol === Venus.vSXP) {
sxpBorrowBalance = +tok.borrow_balance_underlying.value;
}
});
});
}
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'sxpBorrowBalance'</span>, sxpBorrowBalance);
}
main().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
<a href="#build-for-nodejs-amp-web-browser" id="build-for-nodejs-amp-web-browser" style="color: inherit; text-decoration: none;">
<h2>Build for Node.js & Web Browser</h2>
</a>
<pre><code><span class="hljs-attr">git</span> <span class="hljs-string">clone git@github.com:venusprotocol/venus-js.git</span>
<span class="hljs-attr">cd</span> <span class="hljs-string">venus-js/</span>
<span class="hljs-attr">npm</span> <span class="hljs-string">install</span>
<span class="hljs-attr">npm</span> <span class="hljs-string">run build</span></code></pre>
<a href="#web-browser-build" id="web-browser-build" style="color: inherit; text-decoration: none;">
<h3>Web Browser Build</h3>
</a>
<pre><code class="language-html"><span class="hljs-comment"><!-- Local build (do `npm install` first) --></span>
<span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"./dist/browser/venus.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span>
<span class="hljs-comment"><!-- Public NPM -> jsdeliver build --></span>
<span class="hljs-tag"><<span class="hljs-name">script</span> <span class="hljs-attr">type</span>=<span class="hljs-string">"text/javascript"</span> <span class="hljs-attr">src</span>=<span class="hljs-string">"https://cdn.jsdelivr.net/npm/@swipewallet/venus-js@latest/dist/browser/venus.min.js"</span>></span><span class="hljs-tag"></<span class="hljs-name">script</span>></span></code></pre>
<a href="#nodejs-build" id="nodejs-build" style="color: inherit; text-decoration: none;">
<h3>Node.js Build</h3>
</a>
<pre><code class="language-js"><span class="hljs-comment">// Local build (do `npm install` first)</span>
<span class="hljs-keyword">const</span> Venus = <span class="hljs-built_in">require</span>(<span class="hljs-string">'./dist/nodejs/index.js'</span>);
<span class="hljs-comment">// Public NPM build</span>
<span class="hljs-keyword">const</span> Venus = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@swipewallet/venus-js'</span>);</code></pre>
</div>
</div>
<div class="col-4 col-menu menu-sticky-wrap menu-highlight">
<nav class="tsd-navigation primary">
<ul>
<li class="globals ">
<a href="globals.html"><em>Globals</em></a>
</li>
<li class=" tsd-kind-module">
<a href="modules/_api_.html">"api"</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/_comp_.html">"comp"</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/_comptroller_.html">"comptroller"</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/_eth_.html">"eth"</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/_gov_.html">"gov"</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/_index_.html">"index"</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/_pricefeed_.html">"price<wbr>Feed"</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/_util_.html">"util"</a>
</li>
<li class=" tsd-kind-module">
<a href="modules/_vtoken_.html">"v<wbr>Token"</a>
</li>
</ul>
</nav>
<nav class="tsd-navigation secondary menu-sticky">
<ul class="before-current">
</ul>
</nav>
</div>
</div>
</div>
<footer>
<div class="container">
<h2>Legend</h2>
<div class="tsd-legend-group">
<ul class="tsd-legend">
<li class="tsd-kind-function"><span class="tsd-kind-icon">Function</span></li>
</ul>
</div>
</div>
</footer>
<div class="overlay"></div>
<script src="assets/js/main.js"></script>
</body>
</html>