Skip to content

Instantly share code, notes, and snippets.

describe('products', async () => {
let transaction, productCount;
before(async () => {
const bytes32Key = web3.utils.fromAscii('sportBikeModelX01');
const wei = web3.utils.toWei('1.4', 'Ether');
transaction = await contractInstance.createPurchaseContract(bytes32Key, 'Sport bike', 'ipfsHash001', {
from: seller,
value: wei
@Injectable({
providedIn: P2pBazaarAnchorModule
})
export class ProductsLoadedGuard implements CanActivate {
constructor(private store: Store<fromStore.AppState>) {}
canActivate(): Observable<boolean> {
return this.waitForProductsToLoad().pipe(
switchMap(() => of(true)),
catchError(() => of(false))
loadProducts$ = createEffect(() =>
this.actions$.pipe(
ofType(PurchaseContractActions.loadProducts),
switchMap(() =>
this.fleaSrv.getPurchaseContractList().pipe(
tap(products => console.log('purchase contracts:', products)),
map(products => PurchaseContractActions.loadProductsSuccess({ products })),
catchError((err: Error) =>
of(ErrorActions.errorMessage({ errorMsg: err.message }),
private widgetObservable = (id: number): Observable<PurchaseWidgetModel> =>
from(this.contractToken.getContractKeyAtIndex(id)).pipe(
switchMap(key => from(this.contractToken.getContractByKey(key)).pipe(
map(address => {
const widget: PurchaseWidgetModel = {
productKey: utils.parseBytes32String(key as ethers.utils.Arrayish),
contractAddress: address as string
};
ngAfterViewInit() {
const products$ = this.store$.pipe(select(fromStore.getAllProducts));
const filter$ = fromEvent(this.contractKey.nativeElement, 'keyup').pipe(
map(event => this.contractKey.nativeElement.value),
startWith(''),
debounceTime(150),
distinctUntilChanged());
this.filteredProducts$ = combineLatest([products$, filter$]).pipe(
this.selectedPurchaseContract$ = this.store$.pipe(
select(fromStore.getSelectedProduct),
filter(product => !!product),
tap(product => this.store$.dispatch(PurchaseContractActions.loadPurchaseContract({ address: product.contractAddress }))),
// we switch from one observable to another
switchMap(() => this.store$.select(fromStore.getSelectedPurchaseContract)),
filter(contract => !!contract),
);
const routes: Routes = [
{
path: '',
redirectTo: 'products',
pathMatch: 'full',
},
{
path: 'products',
component: fromComponents.MarketplaceHomeComponent,
loadPurchaseContract$ = createEffect(
() =>
this.actions$.pipe(
ofType(PurchaseContractActions.loadPurchaseContract),
map(action => action.address),
switchMap(address => {
return this.purchaseSrv.loadPurchaseContract(address).pipe(
map(contract =>
PurchaseContractActions.loadPurchaseContractSuccess({ contract })),
public loadPurchaseContract(contractAddress: string): Observable<PurchaseContractModel> {
const contract: Contract = new ethers.Contract(contractAddress, this.abi, this.provider.getSigner());
return zip(
from(contract.key()),
from(contract.seller()),
from(contract.buyer()),
from(contract.price()),
from(contract.balanceOf()),
from(contract.title()),
this.image$ = this.store$.pipe(
select(fromStore.getSelectedPurchaseContract),
filter(contract => !!contract),
tap(contract =>
this.store$.dispatch(IpfsImageActions.download_image({ ipfsHash: contract.ipfsHash }))
),
// we switch to another observable
switchMap(() => this.store$.select(fromStore.getImageBlob)),
filter(image => !!image)
);