Skip to content

Instantly share code, notes, and snippets.

@alexytiger
Last active February 18, 2020 00:14
Show Gist options
  • Save alexytiger/e751b3382773510a65aefc9db28f062d to your computer and use it in GitHub Desktop.
Save alexytiger/e751b3382773510a65aefc9db28f062d to your computer and use it in GitHub Desktop.
e-book
import { Injectable } from '@angular/core';
import { MarketPlaceAnchorModule } from '../market-place-anchor.module';
import { FleaMarketContractToken } from './tokens/flea-market-contract-token';
import { Observable, from, of, forkJoin } from 'rxjs';
import { map, tap, switchMap, mergeMap, exhaustMap } from 'rxjs/operators';
import { ethers, utils } from 'ethers';
import { PurchaseWidgetModel } from '../models';
@Injectable({ providedIn: MarketPlaceAnchorModule })
export class FleaMarketContractService {
constructor(private contractToken: FleaMarketContractToken) {
}
public createPurchaseContract(product: any): Observable<string> {
const commission = Math.floor(parseFloat(product.commission) * 100);
const bytes32Key = utils.formatBytes32String(product.productKey);
const wei = utils.parseEther(product.etherValue);
// based on https://docs.ethers.io/ethers.js/html/cookbook-contracts.html
// Call the contract method, getting back the transaction tx
const token =
this.contractToken.createPurchaseContract(bytes32Key, product.description,
product.ipfsHash, commission, {
value: wei
});
return from(token)
.pipe(
switchMap((tx: any) => {
console.log('Transaction', tx);
// Wait for transaction to be mined
// Returned a Promise which would resolve to the TransactionReceipt
// once it is mined.
return from(tx.wait()).pipe(
tap((txReceipt: any) => console.log('TransactionReceipt: ', txReceipt)),
// The receipt will have an "events" Array, which will have
// the emitted
// 'event LogCreatePurchaseContract(address sender, address contractAddress)'.
map(txReceipt => txReceipt.events.pop()),
map(txEvent => txEvent.args.contractAddress),
tap(address => console.log('address: ', address)));
}));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment