Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1179 - fixed "after a deal is funded, on page refresh, an error occurs" #1199

Open
wants to merge 1 commit into
base: refac/deals-au2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/entities/DealTokenSwap.ts
Original file line number Diff line number Diff line change
@@ -22,6 +22,7 @@ import TransactionsService, { TransactionReceipt } from "services/TransactionsSe
import { toBigNumberJs } from "services/BigNumberService";
import { AureliaHelperService } from "services/AureliaHelperService";
import { DealService } from "services/DealService";
import { BehaviorSubject } from "rxjs";

// interface ITokenSwapInfo {
// // the participating DAOs
@@ -104,6 +105,7 @@ export class DealTokenSwap implements IDeal {
private now: Date;

public id: IDealIdType;
public loaded$ = new BehaviorSubject(false);
public dealInitialized: boolean;
public totalPrice?: number;
public initializing = true;
@@ -537,6 +539,7 @@ export class DealTokenSwap implements IDeal {
}
})
.finally(() => {
this.loaded$.next(true);
this.initializing = false;
});
}
42 changes: 29 additions & 13 deletions src/services/DealService.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { skip } from "rxjs/operators";
import { combineLatest, combineLatestWith, filter, skip, take } from "rxjs/operators";
import { SortOrder, SortService } from "services/SortService";
import { IDealRegistrationTokenSwap } from "entities/DealRegistrationTokenSwap";
import { Address, EthereumService, IBlockInfoNative, IEthereumService, Networks } from "./EthereumService";
@@ -10,7 +10,7 @@ import { IDataSourceDeals, IDealIdType } from "services/DataSourceDealsTypes";
import { ContractNames, ContractsService, IStandardEvent } from "services/ContractsService";
import { IDealTokenSwapDocument } from "entities/IDealTypes";
import { EventConfigException } from "services/GeneralEvents";
import { Subscription } from "rxjs";
import { BehaviorSubject, Subject, Subscription } from "rxjs";
import { Utils } from "services/utils";
import { parseBytes32String } from "ethers/lib/utils";
import { BigNumber } from "ethers";
@@ -64,6 +64,8 @@ export class DealService {
public deals: Map<IDealIdType, DealTokenSwap> = new Map<IDealIdType, DealTokenSwap>();
private executedDealIds: Map<string, IExecutedDeal> = new Map();
private fundedDealIds: Map<string, IFundedDeal> = new Map();
private contractsLoaded$ = new Subject<{module: Address, contractDealId: number, metadata:string}>();
private deals$ = new BehaviorSubject(new Map<IDealIdType, DealTokenSwap>());

public static getDealFee(amount: BigNumber): BigNumber {
return BigNumber.from(toBigNumberJs(amount).multipliedBy(.003).toString());
@@ -148,7 +150,7 @@ export class DealService {
});

await Promise.all([this.getDealExecuted(), this.getDealFundedInfo()]);
return this.loadDeals();
await this.loadDeals();
}

public async loadDeals(): Promise<void> {
@@ -199,6 +201,8 @@ export class DealService {
this.deals.delete(key);
}
});

this.deals$.next(this.deals);
})
.catch((error) => {
this.deals = new Map();
@@ -274,17 +278,29 @@ export class DealService {
}

private async observeDealFundedInfo(): Promise<void> {
const moduleContract = await this.contractsService.getContractFor(ContractNames.TOKENSWAPMODULE);
const filter = moduleContract.filters.TokenSwapCreated();
this.contractsLoaded$.pipe(
combineLatestWith(
this.ethereumService.lastBlock$,
this.deals$.pipe(filter(deals => Boolean(deals.size))),
),
)
.pipe(take(1))
.subscribe(([contractData, lastBlock, deals]) => {
const {contractDealId, metadata} = contractData;

const dealId = parseBytes32String(metadata);
const fundedAt = new Date(lastBlock.timestamp * 1000);
this.fundedDealIds.set(dealId, {fundedAt});

const deal = deals.get(dealId);
deal.fundingStartedAt = fundedAt;
deal.contractDealId = contractDealId;
});

moduleContract.on(filter, async (_module: Address, contractDealId: number, metadata: string) => {
const dealId = parseBytes32String(metadata);
const fundedAt = new Date(this.ethereumService.lastBlock.timestamp * 1000);
this.fundedDealIds.set(dealId, { fundedAt });

const deal = this.deals.get(dealId);
deal.fundingStartedAt = fundedAt;
deal.contractDealId = contractDealId;
const moduleContract = await this.contractsService.getContractFor(ContractNames.TOKENSWAPMODULE);
const contractFilter = moduleContract.filters.TokenSwapCreated();
moduleContract.on(contractFilter, async (module: Address, contractDealId: number, metadata: string) => {
this.contractsLoaded$.next({module, contractDealId, metadata});
});
}

3 changes: 3 additions & 0 deletions src/services/EthereumService.ts
Original file line number Diff line number Diff line change
@@ -9,6 +9,7 @@ import { formatUnits, getAddress, parseUnits } from "ethers/lib/utils";
import { Utils } from "./utils";
import { DI, IEventAggregator, inject } from "aurelia";
import { DisclaimerService } from "./DisclaimerService";
import { Subject } from "rxjs";

interface IEIP1193 {
on(eventName: "accountsChanged", handler: (accounts: Array<Address>) => void);
@@ -110,6 +111,7 @@ export class EthereumService {
public walletProvider: Web3Provider;
public defaultAccountAddress: Address;
public lastBlock: IBlockInfo;
public lastBlock$ = new Subject<IBlockInfo>();
private blockSubscribed: boolean;
private web3Modal: Web3Modal;
/**
@@ -358,6 +360,7 @@ export class EthereumService {
private handleNewBlock = async (blockNumber: number): Promise<void> => {
const block = await this.getBlock(blockNumber);
this.lastBlock = block;
this.lastBlock$.next(block);
this.eventAggregator.publish("Network.NewBlock", block);
};