1
0
mirror of https://github.com/trezor/trezor-wallet synced 2024-11-28 03:08:30 +00:00

before merge

This commit is contained in:
Szymon Lesisz 2018-09-12 13:25:32 +02:00
parent 26978fe984
commit a6ac8239f0
6 changed files with 102 additions and 42 deletions

55
src/actions/TxActions.js Normal file
View File

@ -0,0 +1,55 @@
/* @flow */
import EthereumjsTx from 'ethereumjs-tx';
import EthereumjsUnits from 'ethereumjs-units';
import BigNumber from 'bignumber.js';
import { toHex } from 'web3-utils';
import { initWeb3 } from './Web3Actions';
import type {
Dispatch,
GetState,
PromiseAction,
EthereumTxRequest,
EthereumPreparedTx
} from 'flowtype';
export const prepareEthereumTx = (tx: EthereumTxRequest): PromiseAction<EthereumPreparedTx> => async (dispatch: Dispatch, getState: GetState): Promise<EthereumPreparedTx> => {
const instance = await dispatch( initWeb3(tx.network) );
const token = tx.token;
let data: string = `0x${tx.data}`; // TODO: check if already prefixed
let value: string = toHex( EthereumjsUnits.convert(tx.amount, 'ether', 'wei') );
let to: string = tx.to;
if (token) {
// smart contract transaction
const contract = instance.erc20.clone();
contract.options.address = token.address;
const tokenAmount: string = new BigNumber(tx.amount).times(Math.pow(10, token.decimals)).toString(10);
data = instance.erc20.methods.transfer(to, tokenAmount).encodeABI();
value = '0x00';
to = token.address;
}
return {
to,
value,
data,
chainId: instance.chainId,
nonce: toHex(tx.nonce),
gasLimit: toHex(tx.gasLimit),
gasPrice: toHex( EthereumjsUnits.convert(tx.gasPrice, 'gwei', 'wei') ),
r: '',
s: '',
v: '',
}
};
export const serializeEthereumTx = (tx: EthereumPreparedTx): PromiseAction<string> => async (dispatch: Dispatch, getState: GetState): Promise<string> => {
const ethTx = new EthereumjsTx(tx);
console.warn("SERIALIZE 1", `0x${ ethTx.serialize().toString('hex') }`)
console.warn("SERIALIZE 2", toHex( ethTx.serialize() ))
return `0x${ ethTx.serialize().toString('hex') }`;
// return toHex( ethTx.serialize() );
}

View File

@ -7,7 +7,6 @@ import * as ACCOUNT from 'actions/constants/account';
import type { Action, TrezorDevice } from 'flowtype';
import type {
AccountCreateAction,
AccountSetBalanceAction,
AccountSetNonceAction,
} from 'actions/AccountsActions';
@ -22,6 +21,8 @@ export type Account = {
+address: string;
balance: string;
nonce: number;
block: number;
transactions: number;
}
export type State = Array<Account>;
@ -37,28 +38,14 @@ export const findDeviceAccounts = (state: State, device: TrezorDevice, network:
return state.filter(addr => addr.deviceState === device.state);
};
const createAccount = (state: State, action: AccountCreateAction): State => {
const createAccount = (state: State, account: Account): State => {
// TODO check with device_id
// check if account was created before
// const exist: ?Account = state.find(account => account.address === action.address && account.network === action.network && action.device.features && account.deviceID === action.device.features.device_id);
const exist: ?Account = state.find(account => account.address === action.address && account.network === action.network && account.deviceState === action.device.state);
const exist: ?Account = state.find(a => a.address === account.address && a.network === account.network && a.deviceState === account.deviceState);
if (exist) {
return state;
}
const account: Account = {
loaded: false,
network: action.network,
deviceID: action.device.features ? action.device.features.device_id : '0',
deviceState: action.device.state || 'undefined',
index: action.index,
addressPath: action.path,
address: action.address,
balance: '0',
nonce: 0,
};
const newState: State = [...state];
const newState: State = [ ...state ];
newState.push(account);
return newState;
};
@ -74,8 +61,16 @@ const clear = (state: State, devices: Array<TrezorDevice>): State => {
return newState;
};
const updateAccount = (state: State, account: Account): State => {
const index: number = state.findIndex(a => a.address === account.address && a.network === account.network && a.deviceState === account.deviceState);
const newState: State = [...state];
newState[index] = account;
return newState;
}
const setBalance = (state: State, action: AccountSetBalanceAction): State => {
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState);
// const index: number = state.findIndex(account => account.address === action.address && account.network === action.network && account.deviceState === action.deviceState);
const index: number = state.findIndex(account => account.address === action.address && account.network === action.network);
const newState: State = [...state];
newState[index].loaded = true;
newState[index].balance = action.balance;
@ -93,7 +88,7 @@ const setNonce = (state: State, action: AccountSetNonceAction): State => {
export default (state: State = initialState, action: Action): State => {
switch (action.type) {
case ACCOUNT.CREATE:
return createAccount(state, action);
return createAccount(state, action.payload);
case CONNECT.FORGET:
case CONNECT.FORGET_SINGLE:
@ -105,6 +100,9 @@ export default (state: State = initialState, action: Action): State => {
//case CONNECT.FORGET_SINGLE :
// return forgetAccounts(state, action);
case ACCOUNT.UPDATE :
return updateAccount(state, action.payload);
case ACCOUNT.SET_BALANCE:
return setBalance(state, action);
case ACCOUNT.SET_NONCE:

View File

@ -16,9 +16,7 @@ import type {
DiscoveryCompleteAction,
} from 'actions/DiscoveryActions';
import type {
AccountCreateAction,
} from 'actions/AccountsActions';
import type { Account } from './AccountsReducer';
export type Discovery = {
network: string;
@ -75,8 +73,8 @@ const complete = (state: State, action: DiscoveryCompleteAction): State => {
return newState;
};
const accountCreate = (state: State, action: AccountCreateAction): State => {
const index: number = findIndex(state, action.network, action.device.state || '0');
const accountCreate = (state: State, account: Account): State => {
const index: number = findIndex(state, account.network, account.deviceState);
const newState: State = [...state];
newState[index].accountIndex++;
return newState;
@ -162,7 +160,7 @@ export default function discovery(state: State = initialState, action: Action):
case DISCOVERY.START:
return start(state, action);
case ACCOUNT.CREATE:
return accountCreate(state, action);
return accountCreate(state, action.payload);
case DISCOVERY.STOP:
return stop(state, action);
case DISCOVERY.COMPLETE:

View File

@ -22,7 +22,8 @@ export type Coin = {
backends: Array<{
name: string;
urls: Array<string>;
}>
}>;
web3: Array<string>;
}
export type NetworkToken = {

View File

@ -6,6 +6,7 @@ import type { Action } from 'flowtype';
import type { SendTxAction } from 'actions/SendFormActions';
export type PendingTx = {
+type: 'send' | 'recv';
+id: string;
+network: string;
+currency: string;
@ -21,19 +22,25 @@ export type State = Array<PendingTx>;
const initialState: State = [];
const add = (state: State, action: SendTxAction): State => {
// const add01 = (state: State, action: SendTxAction): State => {
// const newState = [...state];
// newState.push({
// id: action.txid,
// network: action.account.network,
// currency: action.selectedCurrency,
// amount: action.amount,
// total: action.total,
// tx: action.tx,
// nonce: action.nonce,
// address: action.account.address,
// rejected: false,
// });
// return newState;
// };
const add = (state: State, payload: any): State => {
const newState = [...state];
newState.push({
id: action.txid,
network: action.account.network,
currency: action.selectedCurrency,
amount: action.amount,
total: action.total,
tx: action.tx,
nonce: action.nonce,
address: action.account.address,
rejected: false,
});
newState.push(payload);
return newState;
};
@ -48,9 +55,11 @@ const reject = (state: State, id: string): State => state.map((tx) => {
export default function pending(state: State = initialState, action: Action): State {
switch (action.type) {
case SEND.TX_COMPLETE:
return add(state, action);
// case SEND.TX_COMPLETE:
// return add(state, action);
case PENDING.ADD:
return add(state, action.payload);
case PENDING.TX_RESOLVED:
return remove(state, action.tx.id);
case PENDING.TX_NOT_FOUND:

View File

@ -4,7 +4,6 @@
import { LOCATION_CHANGE } from 'react-router-redux';
import { DEVICE, TRANSPORT } from 'trezor-connect';
import * as MODAL from 'actions/constants/modal';
import * as WEB3 from 'actions/constants/web3';
import * as WALLET from 'actions/constants/wallet';
import * as CONNECT from 'actions/constants/TrezorConnect';