android APIs for withdrawal and exchange listing

This commit is contained in:
Florian Dold 2020-07-09 18:56:18 +05:30
parent 2efe743950
commit 63ebe1b2e2
No known key found for this signature in database
GPG Key ID: D2E4F00F29D02A4B
8 changed files with 110 additions and 8 deletions

View File

@ -184,6 +184,19 @@ class AndroidWalletMessageHandler {
const wallet = await this.wp.promise;
return await wallet.getPendingOperations();
}
case "listExchanges": {
const wallet = await this.wp.promise;
return await wallet.getExchanges();
}
case "addExchange": {
const wallet = await this.wp.promise;
await wallet.updateExchangeFromUrl(args.exchangeBaseUrl);
return {};
}
case "getWithdrawalDetailsForAmount": {
const wallet = await this.wp.promise;
return await wallet.getWithdrawDetailsForAmount(args.exchangeBaseUrl, args.amount);
}
case "withdrawTestkudos": {
const wallet = await this.wp.promise;
try {

View File

@ -7,7 +7,7 @@ import { openDatabase, Database, Store, Index } from "./util/query";
* with each major change. When incrementing the major version,
* the wallet should import data from the previous version.
*/
const TALER_DB_NAME = "taler-walletdb-v4";
const TALER_DB_NAME = "taler-walletdb-v5";
/**
* Current database minor version, should be incremented
@ -16,7 +16,7 @@ const TALER_DB_NAME = "taler-walletdb-v4";
* backwards-compatible way or object stores and indices
* are added.
*/
export const WALLET_DB_VERSION = 1;
export const WALLET_DB_MINOR_VERSION = 1;
/**
* Return a promise that resolves
@ -54,7 +54,7 @@ export function openTalerDatabase(
return openDatabase(
idbFactory,
TALER_DB_NAME,
WALLET_DB_VERSION,
WALLET_DB_MINOR_VERSION,
onVersionChange,
onUpgradeNeeded,
);

View File

@ -337,7 +337,7 @@ exchangesCli
console.log("Listing exchanges ...");
await withWallet(args, async (wallet) => {
const exchanges = await wallet.getExchanges();
console.log("exchanges", exchanges);
console.log(JSON.stringify(exchanges, undefined, 2));
});
});
@ -358,11 +358,42 @@ exchangesCli
});
});
exchangesCli
.subcommand("exchangesAddCmd", "add", {
help: "Add an exchange by base URL.",
})
.requiredArgument("url", clk.STRING, {
help: "Base URL of the exchange.",
})
.action(async (args) => {
await withWallet(args, async (wallet) => {
await wallet.updateExchangeFromUrl(
args.exchangesAddCmd.url,
);
});
});
const advancedCli = walletCli.subcommand("advancedArgs", "advanced", {
help:
"Subcommands for advanced operations (only use if you know what you're doing!).",
});
advancedCli
.subcommand("manualWithdrawalDetails", "manual-withdrawal-details", {
help: "Query withdrawal fees.",
})
.requiredArgument("exchange", clk.STRING)
.requiredArgument("amount", clk.STRING)
.action(async (args) => {
await withWallet(args, async (wallet) => {
const details = await wallet.getWithdrawDetailsForAmount(
args.manualWithdrawalDetails.exchange,
Amounts.parseOrThrow(args.manualWithdrawalDetails.amount),
);
console.log(JSON.stringify(details, undefined, 2));
});
});
advancedCli
.subcommand("decode", "decode", {
help: "Decode base32-crockford.",

View File

@ -301,6 +301,7 @@ async function updateExchangeFinalize(
if (r.updateStatus != ExchangeUpdateStatus.FinalizeUpdate) {
return;
}
r.addComplete = true;
r.updateStatus = ExchangeUpdateStatus.Finished;
await tx.put(Stores.exchanges, r);
const updateEvent: ExchangeUpdatedEventRecord = {
@ -485,6 +486,8 @@ async function updateExchangeFromUrlImpl(
if (!r) {
const newExchangeRecord: ExchangeRecord = {
builtIn: false,
addComplete: false,
permanent: true,
baseUrl: baseUrl,
details: undefined,
wireInfo: undefined,

View File

@ -555,6 +555,16 @@ export interface ExchangeRecord {
*/
baseUrl: string;
/**
* Did we finish adding the exchange?
*/
addComplete: boolean;
/**
* Is this a permanent or temporary exchange record?
*/
permanent: boolean;
/**
* Was the exchange added as a built-in exchange?
*/
@ -601,6 +611,9 @@ export interface ExchangeRecord {
*/
updateStarted: Timestamp | undefined;
/**
* Status of updating the info about the exchange.
*/
updateStatus: ExchangeUpdateStatus;
updateReason?: ExchangeUpdateReason;

View File

@ -479,3 +479,13 @@ export interface DepositInfo {
export interface ExtendedPermissionsResponse {
newValue: boolean;
}
export interface ExchangesListRespose {
exchanges: ExchangeListItem[];
}
export interface ExchangeListItem {
exchangeBaseUrl: string;
currency: string;
paytoUris: string[];
}

View File

@ -69,6 +69,8 @@ import {
PurchaseDetails,
ExchangeWithdrawDetails,
RefreshReason,
ExchangeListItem,
ExchangesListRespose,
} from "./types/walletTypes";
import { Logger } from "./util/logging";
@ -549,10 +551,40 @@ export class Wallet {
return denoms;
}
async getExchanges(): Promise<ExchangeRecord[]> {
/**
* Get all exchanges known to the exchange.
*
* @deprecated Use getExchanges instead
*/
async getExchangeRecords(): Promise<ExchangeRecord[]> {
return await this.db.iter(Stores.exchanges).toArray();
}
async getExchanges(): Promise<ExchangesListRespose> {
const exchanges: (ExchangeListItem | undefined)[] = await this.db
.iter(Stores.exchanges)
.map((x) => {
const details = x.details;
if (!details) {
return undefined;
}
if (!x.addComplete) {
return undefined;
}
if (!x.wireInfo) {
return undefined;
}
return {
exchangeBaseUrl: x.baseUrl,
currency: details.currency,
paytoUris: x.wireInfo.accounts.map(x => x.payto_uri),
};
});
return {
exchanges: exchanges.filter((x) => !!x) as ExchangeListItem[],
};
}
async getCurrencies(): Promise<CurrencyRecord[]> {
return await this.db.iter(Stores.currencies).toArray();
}

View File

@ -27,7 +27,7 @@ import { BrowserCryptoWorkerFactory } from "../crypto/workers/cryptoApi";
import {
deleteTalerDatabase,
openTalerDatabase,
WALLET_DB_VERSION,
WALLET_DB_MINOR_VERSION,
} from "../db";
import {
ReturnCoinsRequest,
@ -151,7 +151,7 @@ async function handleMessage(
return needsWallet().getHistory();
}
case "get-exchanges": {
return needsWallet().getExchanges();
return needsWallet().getExchangeRecords();
}
case "get-currencies": {
return needsWallet().getCurrencies();
@ -201,7 +201,7 @@ async function handleMessage(
dbResetRequired = true;
}
const resp: wxApi.UpgradeResponse = {
currentDbVersion: WALLET_DB_VERSION.toString(),
currentDbVersion: WALLET_DB_MINOR_VERSION.toString(),
dbResetRequired,
oldDbVersion: (outdatedDbVersion || "unknown").toString(),
};