import { Injectable , Inject , PLATFORM_ID } from '@angular/core' ; import { Web3WalletService } from '../web3-wallet.service' ; import { TokenContractService } from './token-contract.service' ; import { isPlatformBrowser } from '@angular/common' ; import { BigNumber } from 'ethers' ; @ Injectable ( ) export class BoostContractService { protected instance : any ; constructor ( protected web3Wallet , protected tokenContract , @ Inject ( PLATFORM_ID ) platformId ) { if ( isPlatformBrowser ( platformId ) ) { this . load ( ) ; } } async load ( ) { this . instance = this . web3Wallet . getContract ( this . web3Wallet . config . boost . address , this . web3Wallet . config . boost . abi ) ; this . boost ( ) ; } async boost ( gasPriceGwei = this . web3Wallet . config . default_gas_price || 1 ) { if ( ! this . instance ) { throw new Error ( 'No boost 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 create ( guid , amount , checksum , message = '' ) { const checksumInt = BigNumber . from ( '0x' + checksum ) . toString ( ) ; return await this . web3Wallet . sendSignedContractMethod ( await this . tokenContract . token ( ) , 'approveAndCall' , [ this . instance . address , this . tokenContract . tokenToUnit ( amount ) , this . tokenContract . encodeParams ( [ { type : 'address' , value : this . web3Wallet . config . boost_wallet_address , } , { type : 'uint256' , value : guid , } , { type : 'uint256' , value : checksumInt , } , ] ) , ] , `Network Boost for ${ amount } Tokens. ${ message }` . trim ( ) ) ; } async createPeer ( receiver , guid , amount , checksum , message = '' ) { return await this . web3Wallet . sendSignedContractMethod ( await this . tokenContract . token ( ) , 'approveAndCall' , [ this . instance . address , this . tokenContract . tokenToUnit ( amount ) , this . tokenContract . encodeParams ( [ { type : 'address' , value : receiver , } , { type : 'uint256' , value : guid , } , { type : 'uint256' , value : checksum , } , ] ) , ] , `Channel Boost for ${ amount } Tokens to ${ receiver }. ${ message }` . trim ( ) ) ; } async accept ( guid , message = '' ) { return await this . web3Wallet . sendSignedContractMethod ( await this . boost ( ) , 'accept' , [ guid ] , `Accept a Channel Boost. ${ message }` . trim ( ) ) ; } async reject ( guid , message = '' ) { return await this . web3Wallet . sendSignedContractMethod ( await this . boost ( ) , 'reject' , [ guid ] , `Reject a Channel Boost. ${ message }` . trim ( ) ) ; } async revoke ( guid , message = '' ) { return await this . web3Wallet . sendSignedContractMethod ( await this . boost ( ) , 'revoke' , [ guid ] , `Revoke a Boost. ${ message }` . trim ( ) ) ; } }