项目作者: manifoldfinance

项目描述 :
Software Engineering Kit
高级语言: TypeScript
项目地址: git://github.com/manifoldfinance/typed-sdk.git
创建时间: 2020-11-14T14:52:45Z
项目社区:https://github.com/manifoldfinance/typed-sdk

开源协议:MIT License

下载



title: manifold finance sdk
description: a well typed sdk for web3

version: 0.1.1

manifold-sdk

nodejs

a human web3 interface


SDK Overview:

  • Keep your private keys in your client, safe and sound
  • Import and export JSON wallets
  • Import and export BIP 39 mnemonic phrases (12-word backup phrases) and HD wallets (English, Italian, Japanese, Korean, Simplified Chinese, Traditional Chinese)
  • Connect to Ethereum nodes over JSON-RPC (both public and private).
  • Aliases names are first-class citizens; it can be used to log in any Manifold Finance services
  • Complete functionality for all your Manifold Finance needs
  • Large collection of test cases which are maintained and added to
  • Fully TypeScript-ready, with definition files and full TypeScript source

usage

make build

npm install --save manifold-sdk

importing

JavaScript (ES3)

  1. var manifold = require('manifold-sdk');

JavaScript (ES5 or ES6)

  1. const manifold = require('manifold-sdk');

JavaScript (ES6) / TypeScript

  1. import { manifold } from 'manifold-sdk';

example

  1. const Web3 = require("web3");
  2. const BN = Web3.utils.BN;
  3. const ManifoldProvider = require("@manifold/provider");
  4. class GasEVO {
  5. constructor(options) {
  6. this.GasEVO = null;
  7. this.pollingInterval = null;
  8. this.account = null;
  9. this.unlocked = false;
  10. this.balanceWei = 0;
  11. this.balance = 0;
  12. this.address = "REPLACE_WITH_CONTRACT_ADDRESS";
  13. this.genesisBlock = 0;
  14. this.loading = false;
  15. this.options = {
  16. readonlyRpcURL: "https://mainnet.infura.io",
  17. autoInit: true,
  18. getPastEvents: false,
  19. watchFutureEvents: false,
  20. connectionRetries: 3,
  21. };
  22. Object.assign(this.options, options);
  23. if (this.options.autoInit) this.initWeb3();
  24. }
  25. /*
  26. * Connect
  27. */
  28. initWeb3() {
  29. return new Promise((resolve, reject) => {
  30. let web3Provider = false;
  31. // check for metamask
  32. if (global.web3) {
  33. web3Provider = global.web3.currentProvider;
  34. // attempt to try again if no web3Provider
  35. } else if (this.options.connectionRetries > 0) {
  36. this.options.connectionRetries -= 1;
  37. setTimeout(() => {
  38. this.initWeb3()
  39. .then(resolve)
  40. .catch((error) => {
  41. reject(new Error(error));
  42. });
  43. }, 1000);
  44. // revert to a read only version using infura endpoint
  45. } else {
  46. this.readOnly = true;
  47. web3Provider = ZeroClientProvider({
  48. getAccounts: function () {},
  49. rpcUrl: this.options.readonlyRpcURL,
  50. });
  51. }
  52. if (web3Provider) {
  53. global.web3 = new Web3(web3Provider);
  54. this.startChecking();
  55. if (this.options.getPastEvents) this.getPastEvents();
  56. if (this.options.watchFutureEvents) this.watchFutureEvents();
  57. }
  58. });
  59. }
  60. /*
  61. * Check every second for switching network or switching wallet
  62. */
  63. startChecking() {
  64. if (this.pollingInterval) clearInterval(this.pollingInterval);
  65. this.getGenesisBlock()
  66. .then(() => {
  67. this.pollingInterval = setInterval(this.check.bind(this), 1000);
  68. })
  69. .catch((err) => {
  70. throw new Error(err);
  71. });
  72. }
  73. check() {
  74. this.checkNetwork()
  75. .then(this.checkAccount.bind(this))
  76. .catch((error) => {
  77. console.error(error);
  78. throw new Error(error);
  79. });
  80. }
  81. checkNetwork() {
  82. return global.web3.eth.net.getId((err, netId) => {
  83. if (err) console.error(err);
  84. if (!err && this.network !== netId) {
  85. this.network = netId;
  86. return this.deployContract();
  87. }
  88. });
  89. }
  90. checkAccount() {
  91. return global.web3.eth.getAccounts((error, accounts) => {
  92. if (error) throw new Error(error);
  93. if (accounts.length && this.account !== accounts[0]) {
  94. this.unlocked = true;
  95. this.account = accounts[0];
  96. } else if (!accounts.length) {
  97. this.unlocked = false;
  98. this.account = null;
  99. }
  100. });
  101. }
  102. /*
  103. *
  104. * Constant Functions
  105. *
  106. */
  107. DAY_IN_SECONDS() {
  108. return this.GasEVO.methods
  109. .DAY_IN_SECONDS()
  110. .call()
  111. .then((resp) => {
  112. return resp;
  113. })
  114. .catch((err) => {
  115. console.error(err);
  116. });
  117. }
  118. NUM_OF_RATES() {
  119. return this.GasEVO.methods
  120. .NUM_OF_RATES()
  121. .call()
  122. .then((resp) => {
  123. return resp;
  124. })
  125. .catch((err) => {
  126. console.error(err);
  127. });
  128. }
  129. allowance(owner, spender) {
  130. return this.GasEVO.methods
  131. .allowance(owner, spender)
  132. .call()
  133. .then((resp) => {
  134. return resp;
  135. })
  136. .catch((err) => {
  137. console.error(err);
  138. });
  139. }
  140. balanceOf(account) {
  141. return this.GasEVO.methods
  142. .balanceOf(account)
  143. .call()
  144. .then((resp) => {
  145. return resp;
  146. })
  147. .catch((err) => {
  148. console.error(err);
  149. });
  150. }
  151. decimals() {
  152. return this.GasEVO.methods
  153. .decimals()
  154. .call()
  155. .then((resp) => {
  156. return resp;
  157. })
  158. .catch((err) => {
  159. console.error(err);
  160. });
  161. }
  162. name() {
  163. return this.GasEVO.methods
  164. .name()
  165. .call()
  166. .then((resp) => {
  167. return resp;
  168. })
  169. .catch((err) => {
  170. console.error(err);
  171. });
  172. }
  173. symbol() {
  174. return this.GasEVO.methods
  175. .symbol()
  176. .call()
  177. .then((resp) => {
  178. return resp;
  179. })
  180. .catch((err) => {
  181. console.error(err);
  182. });
  183. }
  184. totalSupply() {
  185. return this.GasEVO.methods
  186. .totalSupply()
  187. .call()
  188. .then((resp) => {
  189. return resp;
  190. })
  191. .catch((err) => {
  192. console.error(err);
  193. });
  194. }
  195. getEthDeposit() {
  196. return this.GasEVO.methods
  197. .getEthDeposit()
  198. .call()
  199. .then((resp) => {
  200. return resp;
  201. })
  202. .catch((err) => {
  203. console.error(err);
  204. });
  205. }
  206. getTotalRate(day) {
  207. return this.GasEVO.methods
  208. .getTotalRate(new BN(day, 10))
  209. .call()
  210. .then((resp) => {
  211. return resp;
  212. })
  213. .catch((err) => {
  214. console.error(err);
  215. });
  216. }
  217. getHolderRate(holder, day) {
  218. return this.GasEVO.methods
  219. .getHolderRate(holder, new BN(day, 10))
  220. .call()
  221. .then((resp) => {
  222. return resp;
  223. })
  224. .catch((err) => {
  225. console.error(err);
  226. });
  227. }
  228. convEvo2Eth(amountEvo) {
  229. return this.GasEVO.methods
  230. .convEvo2Eth(new BN(amountEvo, 10))
  231. .call()
  232. .then((resp) => {
  233. return resp;
  234. })
  235. .catch((err) => {
  236. console.error(err);
  237. });
  238. }
  239. convEth2Evo(amountEth) {
  240. return this.GasEVO.methods
  241. .convEth2Evo(new BN(amountEth, 10))
  242. .call()
  243. .then((resp) => {
  244. return resp;
  245. })
  246. .catch((err) => {
  247. console.error(err);
  248. });
  249. }
  250. getRates(holder, _timestamp) {
  251. return this.GasEVO.methods
  252. .getRates(holder, new BN(_timestamp, 10))
  253. .call()
  254. .then((resp) => {
  255. return resp;
  256. })
  257. .catch((err) => {
  258. console.error(err);
  259. });
  260. }
  261. getEvoInterestPct(holder, amountEvo) {
  262. return this.GasEVO.methods
  263. .getEvoInterestPct(holder, new BN(amountEvo, 10))
  264. .call()
  265. .then((resp) => {
  266. return resp;
  267. })
  268. .catch((err) => {
  269. console.error(err);
  270. });
  271. }
  272. getEvoInterestPct(holder, amountEvo, _timestamp) {
  273. return this.GasEVO.methods
  274. .getEvoInterestPct(holder, new BN(amountEvo, 10), new BN(_timestamp, 10))
  275. .call()
  276. .then((resp) => {
  277. return resp;
  278. })
  279. .catch((err) => {
  280. console.error(err);
  281. });
  282. }
  283. getEvoInterest(holder, amountEvo, _timestamp) {
  284. return this.GasEVO.methods
  285. .getEvoInterest(holder, new BN(amountEvo, 10), new BN(_timestamp, 10))
  286. .call()
  287. .then((resp) => {
  288. return resp;
  289. })
  290. .catch((err) => {
  291. console.error(err);
  292. });
  293. }
  294. getEvoInterest(holder, amountEvo) {
  295. return this.GasEVO.methods
  296. .getEvoInterest(holder, new BN(amountEvo, 10))
  297. .call()
  298. .then((resp) => {
  299. return resp;
  300. })
  301. .catch((err) => {
  302. console.error(err);
  303. });
  304. }
  305. /*
  306. *
  307. * Transaction Functions
  308. *
  309. */
  310. approve(spender, amount) {
  311. if (!this.account) return Promise.reject(new Error("Unlock Account"));
  312. return this.GasEVO.methods
  313. .approve(spender, new BN(amount, 10))
  314. .send({ from: this.account })
  315. .on("transactionHash", (hash) => {
  316. console.log(hash);
  317. this.loading = true;
  318. })
  319. .then((resp) => {
  320. this.loading = false;
  321. return resp;
  322. })
  323. .catch((err) => {
  324. this.loading = false;
  325. console.error(err);
  326. });
  327. }
  328. decreaseAllowance(spender, subtractedValue) {
  329. if (!this.account) return Promise.reject(new Error("Unlock Account"));
  330. return this.GasEVO.methods
  331. .decreaseAllowance(spender, new BN(subtractedValue, 10))
  332. .send({ from: this.account })
  333. .on("transactionHash", (hash) => {
  334. console.log(hash);
  335. this.loading = true;
  336. })
  337. .then((resp) => {
  338. this.loading = false;
  339. return resp;
  340. })
  341. .catch((err) => {
  342. this.loading = false;
  343. console.error(err);
  344. });
  345. }
  346. increaseAllowance(spender, addedValue) {
  347. if (!this.account) return Promise.reject(new Error("Unlock Account"));
  348. return this.GasEVO.methods
  349. .increaseAllowance(spender, new BN(addedValue, 10))
  350. .send({ from: this.account })
  351. .on("transactionHash", (hash) => {
  352. console.log(hash);
  353. this.loading = true;
  354. })
  355. .then((resp) => {
  356. this.loading = false;
  357. return resp;
  358. })
  359. .catch((err) => {
  360. this.loading = false;
  361. console.error(err);
  362. });
  363. }
  364. withdrawEth(amountEvo) {
  365. if (!this.account) return Promise.reject(new Error("Unlock Account"));
  366. return this.GasEVO.methods
  367. .withdrawEth(new BN(amountEvo, 10))
  368. .send({ from: this.account })
  369. .on("transactionHash", (hash) => {
  370. console.log(hash);
  371. this.loading = true;
  372. })
  373. .then((resp) => {
  374. this.loading = false;
  375. return resp;
  376. })
  377. .catch((err) => {
  378. this.loading = false;
  379. console.error(err);
  380. });
  381. }
  382. transfer(_to, _value) {
  383. if (!this.account) return Promise.reject(new Error("Unlock Account"));
  384. return this.GasEVO.methods
  385. .transfer(_to, new BN(_value, 10))
  386. .send({ from: this.account })
  387. .on("transactionHash", (hash) => {
  388. console.log(hash);
  389. this.loading = true;
  390. })
  391. .then((resp) => {
  392. this.loading = false;
  393. return resp;
  394. })
  395. .catch((err) => {
  396. this.loading = false;
  397. console.error(err);
  398. });
  399. }
  400. transferFrom(_from, _to, _value) {
  401. if (!this.account) return Promise.reject(new Error("Unlock Account"));
  402. return this.GasEVO.methods
  403. .transferFrom(_from, _to, new BN(_value, 10))
  404. .send({ from: this.account })
  405. .on("transactionHash", (hash) => {
  406. console.log(hash);
  407. this.loading = true;
  408. })
  409. .then((resp) => {
  410. this.loading = false;
  411. return resp;
  412. })
  413. .catch((err) => {
  414. this.loading = false;
  415. console.error(err);
  416. });
  417. }
  418. /*
  419. *
  420. * end examples
  421. *
  422. */
  423. }

acknowledgements

portions from ethers.js with much modification

license

SPDX-License-Identifier: MIT