1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-15 21:08:57 +00:00
trezor-wallet/src/actions/ripple/BlockchainActions.js

128 lines
4.1 KiB
JavaScript
Raw Normal View History

/* @flow */
import TrezorConnect from 'trezor-connect';
2018-12-28 15:15:18 +00:00
import * as BLOCKCHAIN from 'actions/constants/blockchain';
import * as PENDING from 'actions/constants/pendingTx';
import * as AccountsActions from 'actions/AccountsActions';
import { mergeAccount, enhanceTransaction } from 'utils/accountUtils';
import { observeChanges } from 'reducers/utils';
import type { BlockchainNotification } from 'trezor-connect';
import type {
Dispatch,
GetState,
PromiseAction,
2018-12-28 15:15:18 +00:00
PayloadAction,
Network,
BlockchainFeeLevel,
} from 'flowtype';
2019-03-04 12:33:02 +00:00
export const subscribe = (network: string): PromiseAction<void> => async (
dispatch: Dispatch,
getState: GetState
) => {
const accounts = getState()
2019-03-04 12:33:02 +00:00
.accounts.filter(a => a.network === network)
.map(a => ({ descriptor: a.descriptor }));
await TrezorConnect.blockchainSubscribe({
accounts,
coin: network,
});
};
2018-12-28 15:15:18 +00:00
// Get current known fee
// Use default values from appConfig.json if it wasn't downloaded from blockchain yet
// update them later, after onBlockMined event
2019-03-04 12:33:02 +00:00
export const getFeeLevels = (network: Network): PayloadAction<Array<BlockchainFeeLevel>> => (
dispatch: Dispatch,
getState: GetState
): Array<BlockchainFeeLevel> => {
2018-12-28 15:15:18 +00:00
const blockchain = getState().blockchain.find(b => b.shortcut === network.shortcut);
if (!blockchain || blockchain.feeLevels.length < 1) {
return network.fee.levels.map(level => ({
name: level.name,
value: level.value,
}));
}
return blockchain.feeLevels;
};
export const onBlockMined = (network: Network): PromiseAction<void> => async (
2019-03-04 12:33:02 +00:00
dispatch: Dispatch,
getState: GetState
): Promise<void> => {
const blockchain = getState().blockchain.find(b => b.shortcut === network.shortcut);
2018-12-28 15:15:18 +00:00
if (!blockchain) return; // flowtype fallback
// if last update was more than 5 minutes ago
const now = new Date().getTime();
if (blockchain.feeTimestamp < now - 300000) {
const feeRequest = await TrezorConnect.blockchainEstimateFee({
request: {
feeLevels: 'smart',
},
coin: network.shortcut,
2018-12-28 15:15:18 +00:00
});
if (feeRequest.success && observeChanges(blockchain.feeLevels, feeRequest.payload)) {
// check if downloaded fee levels are different
2018-12-28 15:15:18 +00:00
dispatch({
type: BLOCKCHAIN.UPDATE_FEE,
shortcut: network.shortcut,
feeLevels: feeRequest.payload.levels.map(l => ({
name: 'Normal',
value: l.feePerUnit,
})),
2018-12-28 15:15:18 +00:00
});
}
}
// TODO: check for blockchain rollbacks here!
const accounts = getState().accounts.filter(a => a.network === network.shortcut);
2019-05-17 15:41:00 +00:00
if (accounts.length === 0) return;
const bundle = accounts.map(a => ({ descriptor: a.descriptor, coin: network.shortcut }));
const response = await TrezorConnect.getAccountInfo({ bundle });
if (!response.success) return;
response.payload.forEach((info, i) => {
dispatch(
AccountsActions.update(mergeAccount(info, accounts[i], network, blockchain.block))
);
2019-05-17 15:41:00 +00:00
});
};
2019-03-04 12:33:02 +00:00
export const onNotification = (
payload: BlockchainNotification,
network: Network
2019-03-04 12:33:02 +00:00
): PromiseAction<void> => async (dispatch: Dispatch, getState: GetState): Promise<void> => {
const { descriptor, tx } = payload.notification;
const account = getState().accounts.find(a => a.descriptor === descriptor);
const blockchain = getState().blockchain.find(b => b.shortcut === network.shortcut);
if (!account || !blockchain) return;
if (!tx.blockHeight) {
dispatch({
type: PENDING.ADD,
payload: enhanceTransaction(account, tx, network),
});
2019-01-04 12:58:34 +00:00
} else {
dispatch({
type: PENDING.TX_RESOLVED,
hash: tx.txid,
});
}
const response = await TrezorConnect.getAccountInfo({
descriptor: account.descriptor,
coin: account.network,
});
if (!response.success) return;
dispatch(
AccountsActions.update(mergeAccount(response.payload, account, network, blockchain.block))
);
};