forked from compound-finance/compound-js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
326 lines (301 loc) · 20.5 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
<!doctype html>
<html class="default no-js">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Compound.js - v0.1.2</title>
<meta name="description" content="Documentation for Compound.js - v0.1.2">
<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">Compound.js - v0.1.2</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>Compound.js - v0.1.2</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="#compoundjs" id="compoundjs" style="color: inherit; text-decoration: none;">
<h1>Compound.js</h1>
</a>
<p>A JavaScript SDK for Ethereum and the Compound 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://compound-finance.github.io/compound-js/docs/">Compound.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="#ethereum-read-amp-write" id="ethereum-read-amp-write" style="color: inherit; text-decoration: none;">
<h2>Ethereum Read & Write</h2>
</a>
<p>JSON RPC based Ethereum <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> Compound = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@compound-finance/compound-js'</span>); <span class="hljs-comment">// in Node.js</span>
<span class="hljs-keyword">const</span> cUsdtAddress = Compound.util.getAddress(Compound.cUSDT);
(<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> Compound.eth.read(
cUsdtAddress,
<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> Compound.eth.trx(
toAddress,
<span class="hljs-string">'function send() external payable'</span>,
[],
{
<span class="hljs-attr">value</span>: Compound._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> Compound.eth.getBalance(toAddress);
})().catch(<span class="hljs-built_in">console</span>.error);</code></pre>
<a href="#compound-protocol" id="compound-protocol" style="color: inherit; text-decoration: none;">
<h2>Compound Protocol</h2>
</a>
<p>Simple methods for using the Compound protocol.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> compound = <span class="hljs-keyword">new</span> Compound(<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 Compound protocol...'</span>);
<span class="hljs-keyword">const</span> trx = <span class="hljs-keyword">await</span> compound.supply(Compound.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/@compound-finance/compound-js@latest/dist/browser/compound.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>.Compound; <span class="hljs-comment">// or `Compound`</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> @<span class="hljs-keyword">compound</span>-finance/<span class="hljs-keyword">compound</span>-js</code></pre>
<pre><code class="language-js"><span class="hljs-keyword">const</span> Compound = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@compound-finance/compound-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/compound-finance/compound-js/tree/master/examples">Node.js</a></li>
<li><a href="https://compound-finance.github.io/compound-js/examples/web/">Web Browser</a></li>
</ul>
<p><a href="https://github.com/compound-finance/compound-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 Ethereum providers for initialization of the SDK.</p>
<pre><code class="language-js"><span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-built_in">window</span>.ethereum); <span class="hljs-comment">// web browser</span>
<span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-string">'http://127.0.0.1:8545'</span>); <span class="hljs-comment">// HTTP provider</span>
<span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(); <span class="hljs-comment">// Uses Ethers.js fallback mainnet (for testing only)</span>
<span class="hljs-keyword">var</span> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-string">'ropsten'</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> compound = <span class="hljs-keyword">new</span> Compound(<span class="hljs-string">'https://mainnet.infura.io/v3/_your_project_id_'</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> compound = <span class="hljs-keyword">new</span> Compound(<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(Compound.DAI, Compound.ETH, Compound.cETH);
<span class="hljs-comment">// DAI, ETH, cETH</span>
<span class="hljs-keyword">const</span> cUsdtAddress = Compound.util.getAddress(Compound.cUSDT);
<span class="hljs-comment">// Mainnet cUSDT address. Second parameter can be a network like 'ropsten'.</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 Dai</span>
<span class="hljs-keyword">await</span> compound.borrow(Compound.DAI, <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> compound.borrow(Compound.DAI, <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 Ethereum transaction is send from</span>
gasPrice, <span class="hljs-comment">// Ethers.js override `Compound_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 `Compound.eth.trx` (server side)</span>
mnemonic, <span class="hljs-comment">// String, meant to be used with `Compound.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://compound.finance/docs/api">Compound API</a> is accessible from Compound.js. The corresponding services are defined in the <code>api</code> namespace on the class.</p>
<ul>
<li><code>Compound.api.account</code></li>
<li><code>Compound.api.cToken</code></li>
<li><code>Compound.api.marketHistory</code></li>
<li><code>Compound.api.governance</code></li>
</ul>
<p>The governance method requires a second parameter (string) for the corresponding endpoint shown in the <a href="https://compound.finance/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> Compound.api.account({
<span class="hljs-string">"addresses"</span>: <span class="hljs-string">"0xB61C5971d9c0472befceFfbE662555B78284c307"</span>,
<span class="hljs-string">"network"</span>: <span class="hljs-string">"ropsten"</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 === Compound.cDAI) {
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 [email protected]:compound-finance/compound-js.git</span>
<span class="hljs-attr">cd</span> <span class="hljs-string">compound-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/compound.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/@compound-finance/compound-js@latest/dist/browser/compound.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> Compound = <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> Compound = <span class="hljs-built_in">require</span>(<span class="hljs-string">'@compound-finance/compound-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/_ctoken_.html">"c<wbr>Token"</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>
</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>