import { Injectable } from '@angular/core' ; import { BigNumber } from 'ethers' ; import { Web3WalletService } from '../web3-wallet.service' ; @ Injectable ( ) export class WithdrawContractService { protected instance : any ; constructor ( protected web3Wallet ) { this . load ( ) ; } async load ( ) { this . instance = this . web3Wallet . getContract ( this . web3Wallet . config . withdraw . address , this . web3Wallet . config . withdraw . abi ) ; this . contract ( ) ; } async contract ( gasPriceGwei = this . web3Wallet . config . default_gas_price || 1 ) { if ( ! this . instance ) { throw new Error ( 'No withdraw instance' ) ; } if ( ! this . instance . defaultTxObject ) { this . instance . defaultTxObject = { } ; } const wallet = await this . web3Wallet . getCurrentWallet ( ) ; if ( wallet ) { this . instance . defaultTxObject . from = await this . web3Wallet . getCurrentWallet ( ) ; this . instance . defaultTxObject . gasPrice = this . web3Wallet . toWei ( gasPriceGwei , 'gwei' ) ; } return this . instance ; } async request ( guid , amount , message = '' ) { await this . contract ( ) ; const tokens = this . web3Wallet . fromWei ( BigNumber . from ( amount ) , 'ether' ) ; const gasLimit = 67839 ; const gas = BigNumber . from ( this . instance . defaultTxObject . gasPrice ) . mul ( BigNumber . from ( gasLimit ) ) ; const gasEther = this . web3Wallet . fromWei ( gas , 'ether' ) ; let tx = await this . web3Wallet . sendSignedContractMethodWithValue ( await this . contract ( ) , 'request' , [ guid , amount . toString ( ) ] , gas . toString ( ) , `Request a withdrawal of ${ tokens } Minds Tokens. ~${ gasEther . toString ( ) } ETH will be transferred to cover the gas fee. If you send a low amount of gas fee, your withdrawal may fail. ${ message }` . trim ( ) ) ; return { address : ( await this . contract ( ) ) . defaultTxObject . from , guid , amount : amount . toString ( ) , gas : gas . toString ( ) , tx , } ; } static _ ( web3Wallet ) { return new WithdrawContractService ( web3Wallet ) ; } }