import {Reflaunt} from "./Reflaunt"; import {ReflauntInstanceShim} from "../models/ReflauntInstanceShim"; import {Web3Util} from "./utils/Web3Util"; import TransactionDetails = Truffle.TransactionDetails; import {ContractUtil} from "./utils/ContractUtil"; /** * Contain all actions can be performed after someone logged in */ export class ReflauntContractInstanceOverride { main: Reflaunt; instance: ReflauntInstanceShim; constructor(reflaunt: Reflaunt, reflauntInstance: ReflauntInstanceShim) { this.instance = reflauntInstance; this.main = reflaunt; } /** * Get current logged in user reflaunt token balance */ async getMyBalance() { const balance = await this.instance.balanceOf.call( this.instance.wallet.address ); return balance.toNumber(); } /** * Get current logged in user main currency balance */ async getMyEtherBalance() { const balance = await this.instance.web3.eth.getBalance(this.instance.wallet.address); return Web3Util.convertWeiToEther(balance); } /** * Get current user transaction count * useful to set a unique nonce when performing transaction */ async getMyTransactionCount() { return this.instance.web3.eth.getTransactionCount(this.instance.wallet.address); } /** * Send blockchain main currency to someone * @param toUserId * @param etherAmount */ async sendEther(toUserId: number, etherAmount: number) { return this.instance.web3.eth.sendTransaction({ from: this.instance.wallet.address, to: this.main.getWallet(toUserId).address, value: Web3Util.convertEtherToWei(etherAmount) }); } /** * Create a reflaunt token for someone with random metadata url * @note the current logged in userId must be a product creator or the blockchain owner * @param receiverId * @param productId * @param options */ async createProduct(receiverId: number, productId: number, options?: TransactionDetails) { let randomUrl = 'random_url_' + Math.ceil((Math.random() * 100000)); return this.instance .mintWithTokenURI.apply( this, [ this.main.getWallet(receiverId).address, productId, randomUrl, ...ContractUtil.wrapOptionIntoArray(options) ] ) } /** * Transfer reflaunt token to someone * @param fromUserId => must be an userid of the approved token spender or the token owner * @param toUserId * @param productId * @param options */ async transferFrom(fromUserId: number, toUserId: number, productId: number, options?: TransactionDetails) { return this.instance .transferFrom.apply( this, [ this.main.getWallet(fromUserId).address, this.main.getWallet(toUserId).address, productId, ...ContractUtil.wrapOptionIntoArray(options) ] ); } /** * Promote an user id to be product creator * @param granteeUserId * @param options */ async addProductCreator(granteeUserId: number, options?: TransactionDetails) { return this.instance .addMinter.apply( this, [ this.main.getWallet(granteeUserId).address, ...ContractUtil.wrapOptionIntoArray(options) ] ); } }