@compound-finance/compound-js
Advanced tools
Comparing version 0.0.16 to 0.0.17
{ | ||
"name": "@compound-finance/compound-js", | ||
"browser": "dist/browser/compound.min.js", | ||
"version": "0.0.16", | ||
"version": "0.0.17", | ||
"description": "A JavaScript SDK for Ethereum and the Compound Protocol.", | ||
@@ -43,4 +43,5 @@ "main": "dist/nodejs/index.js", | ||
"dependencies": { | ||
"@rollup/plugin-json": "^4.1.0", | ||
"ethers": "^5.0.7" | ||
} | ||
} |
@@ -168,2 +168,43 @@ # Compound.js [Alpha] | ||
## API | ||
The [Compound API](https://compound.finance/docs/api) is accessible from Compound.js. The corresponding services are defined in the `api` namespace on the class. | ||
- `Compound.api.account` | ||
- `Compound.api.cToken` | ||
- `Compound.api.marketHistory` | ||
- `Compound.api.governance` | ||
The governance method requires a second parameter (string) for the corresponding endpoint shown in the [documentation](https://compound.finance/docs/api#GovernanceService). | ||
- `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`. | ||
```js | ||
const main = async () => { | ||
const account = await Compound.api.account({ | ||
"addresses": "0xB61C5971d9c0472befceFfbE662555B78284c307", | ||
"network": "ropsten" | ||
}); | ||
let daiBorrowBalance = 0; | ||
if (Object.isExtensible(account) && account.accounts) { | ||
account.accounts.forEach((acc) => { | ||
acc.tokens.forEach((tok) => { | ||
if (tok.symbol === Compound.cDAI) { | ||
daiBorrowBalance = +tok.borrow_balance_underlying.value; | ||
} | ||
}); | ||
}); | ||
} | ||
console.log('daiBorrowBalance', daiBorrowBalance); | ||
} | ||
main().catch(console.error); | ||
``` | ||
## Build for Node.js & Web Browser | ||
@@ -170,0 +211,0 @@ |
@@ -5,6 +5,7 @@ // Rollup builds only the browser version using the Node.js build. | ||
import minify from 'rollup-plugin-babel-minify'; | ||
import json from '@rollup/plugin-json'; | ||
import pkg from './package.json'; | ||
export default [{ | ||
input: './dist/nodejs/index.js', | ||
input: './dist/nodejs/src/index.js', | ||
onwarn: (message) => { | ||
@@ -30,2 +31,3 @@ if (message.code === 'MISSING_NODE_BUILTINS') return; | ||
minify({ comments: false }), | ||
json(), | ||
], | ||
@@ -32,0 +34,0 @@ external: [ |
@@ -9,2 +9,3 @@ import { ethers } from 'ethers'; | ||
import * as gov from './gov'; | ||
import * as api from './api'; | ||
import { constants, decimals } from './constants'; | ||
@@ -50,2 +51,3 @@ | ||
Compound.eth = eth; | ||
Compound.api = api; | ||
Compound.util = util; | ||
@@ -52,0 +54,0 @@ Compound._ethers = ethers; |
151
src/util.ts
import { address } from './constants'; | ||
let _request: any; | ||
let http: any; | ||
let https: any; | ||
function _nodeJsRequest(options: any) { | ||
return new Promise<Object>((resolve, reject) => { | ||
let url = options.url || options.hostname; | ||
// Use 'https' if the protocol is not specified in 'options.hostname' | ||
if ( | ||
url.indexOf("http://") !== 0 && | ||
url.indexOf("https://") !== 0 | ||
) { | ||
url = "https://" + url; | ||
} | ||
// Choose the right module based on the protocol in 'options.hostname' | ||
const httpOrHttps = url.indexOf("http://") === 0 ? http : https; | ||
// Remove the 'http://' so the native node.js module will understand | ||
options.hostname = url.split('://')[1]; | ||
let body = ''; | ||
const req = httpOrHttps.request(options, (res: any) => { | ||
res.on("data", (bodyBuffer: any) => { | ||
body += bodyBuffer.toString(); | ||
}); | ||
res.on("end", (ending: any) => { | ||
resolve({ | ||
status: res.statusCode, | ||
statusText: res.statusMessage, | ||
body | ||
}); | ||
}); | ||
}); | ||
req.on('timeout', () => { | ||
req.abort(); | ||
return reject({ | ||
status: 408, | ||
statusText: 'Client HTTP request timeout limit reached.' | ||
}); | ||
}); | ||
req.on('error', (err: any) => { | ||
if (req.aborted) return; | ||
try { | ||
console.error(err); | ||
return reject({}); | ||
} catch (e) { | ||
console.error(err, e); | ||
return reject(); | ||
} | ||
}); | ||
if (options.body) { | ||
req.write(JSON.stringify(options.body)); | ||
} | ||
req.end(); | ||
}); | ||
}; | ||
function _webBrowserRequest(options: any) { | ||
return new Promise((resolve, reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
let contentTypeIsSet = false; | ||
options = options || {}; | ||
const method = options.method || "GET"; | ||
let url = options.url || options.hostname; | ||
url += typeof options.path === "string" ? options.path : ""; | ||
if (typeof url !== "string") { | ||
return reject("HTTP Request: Invalid URL."); | ||
} | ||
// Use 'https' if the protocol is not specified in 'options.hostname' | ||
if ( | ||
url.indexOf("http://") !== 0 && | ||
url.indexOf("https://") !== 0 | ||
) { | ||
url = "https://" + url; | ||
} | ||
xhr.open(method, url); | ||
for (let header in options.headers) { | ||
if ({}.hasOwnProperty.call(options.headers, header)) { | ||
let lcHeader = header.toLowerCase(); | ||
contentTypeIsSet = lcHeader === "content-type" ? true : contentTypeIsSet; | ||
xhr.setRequestHeader(header, options.headers[header]); | ||
} | ||
} | ||
if (!contentTypeIsSet) { | ||
xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); | ||
} | ||
xhr.onload = function() { | ||
let body; | ||
if (xhr.status >= 100 && xhr.status < 400) { | ||
try { | ||
JSON.parse(xhr.response); | ||
body = xhr.response; | ||
} catch (e) { | ||
body = xhr.statusText; | ||
} | ||
return resolve({ | ||
status: xhr.status, | ||
statusText: xhr.statusText, | ||
body | ||
}); | ||
} else { | ||
return reject({ | ||
status: xhr.status, | ||
statusText: xhr.statusText | ||
}); | ||
} | ||
}; | ||
if (method !== "GET") { | ||
xhr.send(JSON.stringify(options.body)); | ||
} else { | ||
xhr.send(); | ||
} | ||
}); | ||
}; | ||
try { | ||
window; | ||
_request = _webBrowserRequest; | ||
} catch (e) { | ||
http = require('http'); | ||
https = require('https'); | ||
_request = _nodeJsRequest; | ||
} | ||
/** | ||
* A generic HTTP request method that works in Node.js and the Web Browser. | ||
* | ||
* @param {object} options HTTP request options. See Node.js http.request | ||
* documentation for details. | ||
* | ||
* @returns {Promise} Returns a promise and eventually an HTTP response | ||
* (JavaScript object). | ||
*/ | ||
export function request(options: any) { | ||
return _request.apply(null, [ options ]); | ||
} | ||
/** | ||
* Gets the contract address of the named contract. This method supports | ||
@@ -5,0 +156,0 @@ * contracts used by the Compound protocol. |
@@ -5,2 +5,3 @@ { | ||
"lib": ["es6", "dom"], | ||
"resolveJsonModule": true, | ||
"sourceMap": true, | ||
@@ -7,0 +8,0 @@ "declaration": true |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Network access
Supply chain riskThis module accesses the network.
Found 2 instances in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1119679
63
7691
235
2
3
+ Added@rollup/plugin-json@^4.1.0
+ Added@rollup/plugin-json@4.1.0(transitive)
+ Added@rollup/pluginutils@3.1.0(transitive)
+ Added@types/estree@0.0.39(transitive)
+ Addedestree-walker@1.0.1(transitive)
+ Addedfsevents@2.3.3(transitive)
+ Addedpicomatch@2.3.1(transitive)
+ Addedrollup@2.79.2(transitive)