import "reflect-metadata" import {container} from "tsyringe"; import {ConfigurationService} from "./services/ConfigurationService"; import {ReflauntConfig} from "../models/ReflauntConfig"; import {HdWalletService} from "./services/HdWalletService"; import {ContractService} from "./services/ContractService"; import {ReflauntWallet} from "../models/ReflauntWallet"; import {ReflauntContractInstanceOverride} from "./ReflauntContractInstanceOverride"; import {ReflauntInstanceShim} from "../models/ReflauntInstanceShim"; import {EventOptions} from "web3-eth-contract"; import {BlockTransactionString} from "web3-eth"; import {Event} from "../models/Event"; import {EventEmitterService} from "./services/EventEmitterService"; import TransactionLog = Truffle.TransactionLog; export class Reflaunt { configurationService: ConfigurationService; hdWalletService: HdWalletService; contractService: ContractService; eventEmitterService: EventEmitterService; constructor(rfConfig: ReflauntConfig) { const configurationService: ConfigurationService = container.resolve(ConfigurationService); configurationService.config = rfConfig; this.configurationService = configurationService; this.hdWalletService = container.resolve(HdWalletService); this.contractService = container.resolve(ContractService); this.eventEmitterService = container.resolve(EventEmitterService); } /** * Get reflaunt wallet instance from a reflaunt user id * reflaunt wallet is tightly coupled with reflaunt user id * @param id */ getWallet(id: number): ReflauntWallet { return this.hdWalletService.getWallet(id); } /** * Login as a reflaunt user id to perform user related actions * @param id */ async loginAs(id: number): Promise { const reflauntInstance: ReflauntInstanceShim = await this.contractService.loginAs(id); return new ReflauntContractInstanceOverride(this, reflauntInstance); } /** * Get the owner of a product * @param productId */ async getProductOwner(productId: number) { const reflauntInstance: ReflauntInstanceShim = await this.contractService.getContract(); return reflauntInstance.ownerOf(productId).catch(() => null); } /** * Get the metadata of a product * @param productId */ async getProductMetadata(productId: number) { const reflauntInstance: ReflauntInstanceShim = await this.contractService.getContract(); return reflauntInstance.tokenURI(productId).catch(() => null); } async getEvent(event: TransactionLog, contract?: ReflauntInstanceShim) { if (!contract) { contract = await this.contractService.getContract(); } return contract.web3.eth.getBlock(event.blockNumber).then((block: BlockTransactionString) => { return new Event(event, block); }); } /** * Get all transfer events in reflaunt * @param options */ async getAllTransferEvents(options: EventOptions) { const contract = await this.contractService.getContract(); const events = await contract.getPastEvents('Transfer', options); return Promise.all(events.map((evt: TransactionLog) => this.getEvent(evt, contract))); } /** * Get al transfer events filtered by product id * @param productId * @param options */ async getTransferEventsOfProduct(productId: number, options: EventOptions) { return this.getAllTransferEvents({ ...options, filter: { // token id must be a string tokenId: String(productId) } }) } /** * Watch every transfer event emitted in this contract * @param onData * @param onError */ async watchTransferEvent(onData, onError) { const contract = await this.contractService.getContract(); let event = contract.Transfer(); event.on('data', (evt) => { onData(evt); }); event.on('error', function(err){ onError(err); }); this.eventEmitterService.setEvent(event); return event; } /** * clear event event watcher */ async clearAllEvents() { this.eventEmitterService.clearEvent(); } }