more async

This commit is contained in:
Florian Dold 2016-09-28 18:00:13 +02:00
parent 9b37ee6ba6
commit fab098c8e7

View File

@ -418,8 +418,6 @@ export class Wallet {
nextExchange: nextExchange:
for (let key in m) { for (let key in m) {
let coins = m[key]; let coins = m[key];
console.log("trying coins");
console.log(coins);
// Sort by ascending deposit fee // Sort by ascending deposit fee
coins.sort((o1, o2) => Amounts.cmp(o1.denom.fee_deposit, coins.sort((o1, o2) => Amounts.cmp(o1.denom.fee_deposit,
o2.denom.fee_deposit)); o2.denom.fee_deposit));
@ -459,9 +457,9 @@ export class Wallet {
* Record all information that is necessary to * Record all information that is necessary to
* pay for a contract in the wallet's database. * pay for a contract in the wallet's database.
*/ */
private recordConfirmPay(offer: Offer, private async recordConfirmPay(offer: Offer,
payCoinInfo: PayCoinInfo, payCoinInfo: PayCoinInfo,
chosenExchange: string): Promise<void> { chosenExchange: string): Promise<void> {
let payReq: any = {}; let payReq: any = {};
payReq["amount"] = offer.contract.amount; payReq["amount"] = offer.contract.amount;
payReq["coins"] = payCoinInfo.map((x) => x.sig); payReq["coins"] = payCoinInfo.map((x) => x.sig);
@ -493,14 +491,13 @@ export class Wallet {
} }
}; };
return Query(this.db) await Query(this.db)
.put("transactions", t) .put("transactions", t)
.put("history", historyEntry) .put("history", historyEntry)
.putAll("coins", payCoinInfo.map((pci) => pci.updatedCoin)) .putAll("coins", payCoinInfo.map((pci) => pci.updatedCoin))
.finish() .finish();
.then(() => {
this.notifier.notify(); this.notifier.notify();
});
} }
@ -927,64 +924,66 @@ export class Wallet {
}); });
} }
private async suspendCoins(exchangeInfo: IExchangeInfo): Promise<void> {
let suspendedCoins = await Query(this.db)
.iter("coins",
{indexName: "exchangeBaseUrl", only: exchangeInfo.baseUrl})
.reduce((coin: Coin, suspendedCoins: Coin[]) => {
if (!exchangeInfo.active_denoms.find((c) => c.denom_pub == coin.denomPub)) {
return Array.prototype.concat(suspendedCoins, [coin]);
}
return Array.prototype.concat(suspendedCoins);
}, []);
private updateExchangeFromJson(baseUrl: string, let q = Query(this.db);
exchangeKeysJson: KeysJson): Promise<IExchangeInfo> { suspendedCoins.map((c) => {
console.log("suspending coin", c);
c.suspended = true;
q.put("coins", c);
});
await q.finish();
}
private async updateExchangeFromJson(baseUrl: string,
exchangeKeysJson: KeysJson): Promise<IExchangeInfo> {
const updateTimeSec = getTalerStampSec(exchangeKeysJson.list_issue_date); const updateTimeSec = getTalerStampSec(exchangeKeysJson.list_issue_date);
if (updateTimeSec === null) { if (updateTimeSec === null) {
throw Error("invalid update time"); throw Error("invalid update time");
} }
return Query(this.db).get("exchanges", baseUrl).then((r) => { let r = await Query(this.db).get("exchanges", baseUrl);
let exchangeInfo: IExchangeInfo;
console.dir(r);
if (!r) { let exchangeInfo: IExchangeInfo;
exchangeInfo = { console.dir(r);
baseUrl,
all_denoms: [], if (!r) {
active_denoms: [], exchangeInfo = {
last_update_time: updateTimeSec, baseUrl,
masterPublicKey: exchangeKeysJson.master_public_key, all_denoms: [],
}; active_denoms: [],
console.log("making fresh exchange"); last_update_time: updateTimeSec,
} else { masterPublicKey: exchangeKeysJson.master_public_key,
if (updateTimeSec < r.last_update_time) { };
console.log("outdated /keys, not updating"); console.log("making fresh exchange");
return Promise.resolve(r); } else {
} if (updateTimeSec < r.last_update_time) {
exchangeInfo = r; console.log("outdated /keys, not updating");
console.log("updating old exchange"); return r
} }
exchangeInfo = r;
console.log("updating old exchange");
}
return this.updateExchangeInfo(exchangeInfo, exchangeKeysJson) let updatedExchangeInfo = await this.updateExchangeInfo(exchangeInfo,
.then((updatedExchangeInfo: IExchangeInfo) => { exchangeKeysJson);
let q1 = Query(this.db) await this.suspendCoins(updatedExchangeInfo);
.put("exchanges", updatedExchangeInfo)
.finish()
.then(() => updatedExchangeInfo);
let q2 = Query(this.db) await Query(this.db)
.iter("coins", .put("exchanges", updatedExchangeInfo)
{indexName: "exchangeBaseUrl", only: baseUrl}) .finish();
.reduce((coin: Coin, suspendedCoins: Coin[]) => {
if (!updatedExchangeInfo.active_denoms.find((c) => c.denom_pub == coin.denomPub)) { return updatedExchangeInfo;
return Array.prototype.concat(suspendedCoins, [coin]);
}
return Array.prototype.concat(suspendedCoins);
}, [])
.then((suspendedCoins: Coin[]) => {
let q = Query(this.db);
suspendedCoins.map((c) => {
console.log("suspending coin", c);
c.suspended = true;
q.put("coins", c);
});
return q.finish();
});
return Promise.all([q1, q2]).then(() => updatedExchangeInfo);
});
});
} }
@ -1050,7 +1049,7 @@ export class Wallet {
* Retrieve a mapping from currency name to the amount * Retrieve a mapping from currency name to the amount
* that is currenctly available for spending in the wallet. * that is currenctly available for spending in the wallet.
*/ */
getBalances(): Promise<any> { async getBalances(): Promise<any> {
function collectBalances(c: Coin, byCurrency: any) { function collectBalances(c: Coin, byCurrency: any) {
if (c.suspended) { if (c.suspended) {
return byCurrency; return byCurrency;
@ -1064,12 +1063,11 @@ export class Wallet {
return byCurrency; return byCurrency;
} }
return Query(this.db) let byCurrency = await Query(this.db)
.iter("coins") .iter("coins")
.reduce(collectBalances, {}) .reduce(collectBalances, {});
.then(byCurrency => {
return {balances: byCurrency}; return {balances: byCurrency};
});
} }