This commit is contained in:
saplf
2019-09-05 23:24:49 +08:00
parent df25ff4244
commit c8f919ce5a
19 changed files with 658 additions and 37 deletions

55
src/pages/draw/service.ts Normal file
View File

@@ -0,0 +1,55 @@
import http from '@/utils/http';
import _ from 'lodash';
import BigNumber from 'bignumber.js';
import { CoinDrawn, FundBasic } from '@/types/common';
import { loadDrawCoins, storeDrawCoins } from '@/utils/storage';
export async function getCoins(): Promise<CoinDrawn[]> {
const coinsCached = loadDrawCoins();
if (coinsCached && coinsCached.length) {
return coinsCached;
}
const { ok, msg, data } = await http.get<CoinDrawn[]>('/fund/withdrawCoins');
if (!ok || !data) {
throw Error(msg);
}
storeDrawCoins(data);
return data;
}
export async function withdraw(
coin: CoinDrawn,
targetAddress: string,
amount: string,
password: string,
): Promise<void> {
const { ok, msg } = await http.post('/fund/withdraw', {
coinId: coin.id,
targetAddress,
amount,
password,
});
if (ok) return
throw Error(msg);
}
export function retrieveBalance(basic?: FundBasic, coin?: CoinDrawn): string {
if (!basic || !coin) return '';
return (basic as any)[coin.label.toLocaleLowerCase()]
}
export function calcFee(coin: CoinDrawn, input: string): { fee: string, actual: string } {
const fail = { fee: '', actual: '' };
if (!coin || !input) return fail;
const i = new BigNumber(input);
const f = new BigNumber(coin.fee);
if (i.isNaN() || f.isNaN()) return fail;
const fee = i.times(f);
return {
fee: fee.decimalPlaces(2).toString(),
actual: i.minus(fee).decimalPlaces(2).toString(),
};
}