Source

models/BEPRO/NetworkFactory.js

  1. /* eslint-disable no-underscore-dangle */
  2. import BigNumber from 'bignumber.js';
  3. import { networkFactory } from '../../interfaces';
  4. import Numbers from '../../utils/Numbers';
  5. import IContract from '../IContract';
  6. import ERC20Contract from '../ERC20/ERC20Contract';
  7. /**
  8. * @typedef {Object} NetworkFactory~Options
  9. * @property {Boolean} test
  10. * @property {Boolean} localtest ganache local blockchain
  11. * @property {Web3Connection} [web3Connection=Web3Connection] created from params: 'test', 'localtest' and optional 'web3Connection' string and 'privateKey'
  12. * @property {string} [contractAddress]
  13. */
  14. /**
  15. * NetworkFactory Object
  16. * @class NetworkFactory
  17. * @param {NetworkFactory~Options} options
  18. */
  19. class NetworkFactory extends IContract {
  20. constructor(params) {
  21. super({ abi: networkFactory, ...params });
  22. }
  23. /**
  24. * Asserts the 2 {@link ERC20Contract} on the current address
  25. * @function
  26. * @return {Promise<void>}
  27. * @throws {Error} Contract is not deployed, first deploy it and provide a contract address
  28. */
  29. __assert = async () => {
  30. if (!this.getAddress()) {
  31. throw new Error(
  32. 'Contract is not deployed, first deploy it and provide a contract address',
  33. );
  34. }
  35. // Use ABI
  36. this.params.contract.use(networkFactory, this.getAddress());
  37. const beproAddress = await this.getSettlerTokenAddress();
  38. // Set Token Address Contract for easy access
  39. this.params.settlerToken = new ERC20Contract({
  40. web3Connection: this.web3Connection,
  41. contractAddress: beproAddress,
  42. });
  43. // Assert Token Contract
  44. await this.params.settlerToken.start();
  45. await this.params.settlerToken.__assert();
  46. };
  47. /**
  48. * Get Network By Creator Address
  49. * @param {Address} address
  50. * @returns {Adddress}
  51. */
  52. getNetworkByAddress(address) {
  53. return this.getContract()
  54. .methods.getNetworkByAddress(address)
  55. .call();
  56. }
  57. /**
  58. * Get Network By Id
  59. * @param {number} id
  60. * @returns {Adddress}
  61. */
  62. getNetworkById(id) {
  63. return this.getContract()
  64. .methods.getNetworkById(id)
  65. .call();
  66. }
  67. /**
  68. * Get Amount of Networks Forked in the Protocol
  69. * @returns {Promise<number>}
  70. */
  71. async getAmountofNetworksForked() {
  72. return Number(await this.getContract().methods.networksAmount().call());
  73. }
  74. async networksAmount() {
  75. return BigNumber(await this.getContract().methods.networksAmount().call());
  76. }
  77. /**
  78. * Get Total Amount of Tokens Locked by Operator in the Network
  79. * @param {Address} address
  80. * @returns {Promise<number>}
  81. */
  82. async getLockedStakedByAddress(address) {
  83. return Numbers.fromDecimals(
  84. await this.getContract().methods.tokensLocked(address).call(),
  85. 18,
  86. );
  87. }
  88. async getTokensLocked(address) {
  89. return Numbers.fromDecimalsToBN(
  90. await this.getContract().methods.tokensLocked(address).call(),
  91. 18,
  92. );
  93. }
  94. /**
  95. * Get Open Issues Available
  96. * @param {Address} address
  97. * @returns {Address[]}
  98. */
  99. getNetworks() {
  100. return this.getContract()
  101. .methods.networksArray()
  102. .call();
  103. }
  104. /**
  105. * Get Total Amount of Tokens Staked in the Protocol
  106. * @returns {Promise<number>}
  107. */
  108. async getBEPROLocked() {
  109. return Numbers.fromDecimals(
  110. await this.getContract().methods.tokensLockedTotal().call(),
  111. 18,
  112. );
  113. }
  114. async tokensLockedTotal() {
  115. return Numbers.fromDecimalsToBN(
  116. await this.getContract().methods.tokensLockedTotal().call(),
  117. 18,
  118. );
  119. }
  120. /**
  121. * Verify if Address is Council
  122. * @param {Object} params
  123. * @param {number} params.address
  124. * @returns {Promise<address>}
  125. */
  126. async isOperator({ address }) {
  127. return await this.getLockedStakedByAddress(address) >= await this.OPERATOR_AMOUNT();
  128. }
  129. /**
  130. * Get Settler Token Address
  131. * @returns {Promise<address>}
  132. */
  133. getSettlerTokenAddress() {
  134. return this.getContract()
  135. .methods.beproAddress()
  136. .call();
  137. }
  138. beproAddress() {
  139. return this.getContract()
  140. .methods.beproAddress()
  141. .call();
  142. }
  143. /**
  144. * Get Amount Needed for Operator
  145. * @returns {Promise<Integer>}
  146. */
  147. async OPERATOR_AMOUNT() {
  148. return Numbers.fromDecimals(
  149. await this.getContract()
  150. .methods.OPERATOR_AMOUNT()
  151. .call(),
  152. 18,
  153. );
  154. }
  155. /**
  156. * Approve ERC20 Allowance
  157. * @function
  158. * @return {Promise<number>}
  159. */
  160. approveSettlerERC20Token = async () => {
  161. const totalMaxAmount = await this.getSettlerTokenContract().totalSupply();
  162. return this.getSettlerTokenContract().approve({
  163. address: this.getAddress(),
  164. amount: totalMaxAmount,
  165. });
  166. };
  167. /**
  168. * Verify if Approved
  169. * @function
  170. * @param {Object} params
  171. * @param {number} params.amount
  172. * @param {Address} params.address
  173. * @return {Promise<number>}
  174. */
  175. isApprovedSettlerToken = ({ amount, address }) => this.getSettlerTokenContract().isApproved({
  176. address,
  177. amount,
  178. spenderAddress: this.getAddress(),
  179. });
  180. /**
  181. * lock tokens for operator use
  182. * @param {Object} params
  183. * @params params.tokenAmount {number}
  184. * @throws {Error} Tokens Amount has to be higher than 0
  185. * @throws {Error} Tokens not approve for tx, first use 'approveERC20'
  186. * @return {Promise<TransactionObject>}
  187. */
  188. lock({ tokenAmount }, options) {
  189. if (tokenAmount <= 0) {
  190. throw new Error('Token Amount has to be higher than 0');
  191. }
  192. return this.__sendTx(
  193. this.getContract().methods.lock(
  194. Numbers.toSmartContractDecimals(tokenAmount, this.getSettlerTokenContract().getDecimals()),
  195. ),
  196. options,
  197. );
  198. }
  199. /**
  200. * Unlock Tokens for oracles
  201. * @throws {Error} Tokens Amount has to be higher than 0
  202. * @return {Promise<TransactionObject>}
  203. */
  204. unlock(options) {
  205. return this.__sendTx(
  206. this.getContract().methods.unlock(),
  207. options,
  208. );
  209. }
  210. /**
  211. * Create Network
  212. * @param {Object} params
  213. * @param {Address} params.settlerToken
  214. * @param {Address} params.transactionalToken
  215. * @return {Promise<TransactionObject>}
  216. */
  217. createNetwork({ settlerToken, transactionalToken }, options) {
  218. return this.__sendTx(
  219. this.getContract()
  220. .methods.createNetwork(settlerToken, transactionalToken),
  221. options,
  222. );
  223. }
  224. /**
  225. * Deploys Contracts
  226. * @function
  227. * @param {Object} params
  228. * @param {string} params.beproAddress
  229. * @param {IContract~TxOptions} options
  230. * @return {Promise<*|undefined>}
  231. */
  232. deploy = async ({ beproAddress }, options) => {
  233. const params = [ beproAddress ];
  234. const res = await this.__deploy(params, options);
  235. this.params.contractAddress = res.contractAddress;
  236. /* Call to Backend API */
  237. await this.__assert();
  238. return res;
  239. };
  240. /**
  241. * @function
  242. * @return ERC20Contract|null
  243. */
  244. getSettlerTokenContract() {
  245. return this.params.settlerToken;
  246. }
  247. }
  248. export default NetworkFactory;