fortress-js
Version:
A JavaScript SDK for Binance Smart Chain and the Fortress Protocol.
330 lines (303 loc) • 21.1 kB
HTML
<html class="default no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Fortress.js - v1.0.0</title>
<meta name="description" content="Documentation for Fortress.js - v1.0.0">
<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">Fortress.js - v1.0.0</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>Fortress.js - v1.0.0</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="#fortressjs" id="fortressjs" style="color: inherit; text-decoration: none;">
<h1>Fortress.js</h1>
</a>
<p>A JavaScript SDK for Binance Smart Chain and the Fortress 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.fortress.loans/fortress-js">Fortress.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> Fortress = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fortress-js'</span>); <span class="hljs-comment">// in Node.js</span>
<span class="hljs-keyword">const</span> fUsdtAddress = Fortress.util.getAddress(Fortress.fUSDT);
(<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> Fortress.eth.read(
fUsdtAddress,
<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> Fortress.eth.trx(
toAddress,
<span class="hljs-string">'function send() external payable'</span>,
[],
{
<span class="hljs-attr">value</span>: Fortress._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> Fortress.eth.getBalance(toAddress);
})().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
<a href="#fortress-protocol" id="fortress-protocol" style="color: inherit; text-decoration: none;">
<h2>Fortress Protocol</h2>
</a>
<p>Simple methods for using the Fortress protocol.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> fortress = <span class="hljs-keyword">new</span> Fortress(<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 Fortress protocol...'</span>);
<span class="hljs-keyword">const</span> trx = <span class="hljs-keyword">await</span> fortress.supply(Fortress.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/fortress-js@latest/dist/browser/fortress.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>.Fortress; <span class="hljs-comment">// or `Fortress`</span>
</span><span class="hljs-tag"></<span class="hljs-name">script</span>></span></code></pre>
<p>Node.js</p>
<pre><code>npm <span class="hljs-keyword">install</span> fortress-js</code></pre>
<pre><code class="language-js"><span class="hljs-keyword">const</span> Fortress = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fortress-js'</span>);
<span class="hljs-comment">// or, when using ES6</span>
<span class="hljs-keyword">import</span> Fortress <span class="hljs-keyword">from</span> <span class="hljs-string">'fortress-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/jetfuelfinance/fortress-js/tree/master/examples">Node.js</a></li>
<li><a href="https://github.com/jetfuelfinance/fortress-js/examples/web/">Web Browser</a></li>
</ul>
<p><a href="https://github.com/jetfuelfinance/fortress-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> fortress = <span class="hljs-keyword">new</span> Fortress(<span class="hljs-built_in">window</span>.ethereum); <span class="hljs-comment">// web browser</span>
<span class="hljs-keyword">var</span> fortress = <span class="hljs-keyword">new</span> Fortress(<span class="hljs-string">'http://127.0.0.1:8545'</span>); <span class="hljs-comment">// HTTP provider</span>
<span class="hljs-keyword">var</span> fortress = <span class="hljs-keyword">new</span> Fortress(); <span class="hljs-comment">// Uses Ethers.js fallback mainnet (for testing only)</span>
<span class="hljs-keyword">var</span> fortress = <span class="hljs-keyword">new</span> Fortress(<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> fortress = <span class="hljs-keyword">new</span> Fortress(<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> fortress = <span class="hljs-keyword">new</span> Fortress(<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(Fortress.BUSD, Fortress.BNB, Fortress.fDAI);
<span class="hljs-comment">// BUSD, BNB, fDAI</span>
<span class="hljs-keyword">const</span> fUsdtAddress = Fortress.util.getAddress(Fortress.fUSDT);
<span class="hljs-comment">// Mainnet fUSDT 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> fortress.borrow(Fortress.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> fortress.borrow(Fortress.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 `Fortress._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 `Fortress.eth.trx` (server side)</span>
mnemonic, <span class="hljs-comment">// String, meant to be used with `Fortress.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.fortress.loans/api">Fortress API</a> is accessible from Fortress.js. The corresponding services are defined in the <code>api</code> namespace on the class.</p>
<ul>
<li><code>Fortress.api.account</code></li>
<li><code>Fortress.api.fToken</code></li>
<li><code>Fortress.api.marketHistory</code></li>
<li><code>Fortress.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.fortress.loans/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> Fortress.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> daiBorrowBalance = <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 === Fortress.fDAI) {
daiBorrowBalance = +tok.borrow_balance_underlying.value;
}
});
});
}
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">'daiBorrowBalance'</span>, daiBorrowBalance);
}
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:jetfuelfinance/fortress-js.git</span>
<span class="hljs-attr">cd</span> <span class="hljs-string">fortress-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/fortress.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/fortress-js@latest/dist/browser/fortress.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> Fortress = <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> Fortress = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fortress-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/_ftoken_.html">"f<wbr>Token"</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>
</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>