Newer
Older
// Copyright 2015-2018 Parity Technologies (UK) Ltd.
// This file is part of Parity.
//
import { blockNumber$, post$, postRaw$ } from '@parity/light.js';
import {
contractForToken,
txForErc20,
txForEth,
transactionToRlp,
signTransactionWithSignature
} from '../utils/transaction';
import Debug from '../utils/debug';
import parityStore from './parityStore';
@observable
blockNumber; // Current block number, used to calculate tx confirmations.
tx = {}; // The actual tx we are sending. No need to be observable.
@observable
txStatus; // Status of the tx, see wiki for details.
listenForConfirmations = () => {
this.subscription = blockNumber$().subscribe(this.setBlockNumber);
};
// Since we accepted this request, we also start to listen to blockNumber,
// to calculate the number of confirmations
return parityStore.api.signer.confirmRequest(requestId, null, password);
/**
* Back to defaults.
*/
clear = () => {
if (this.subscription) {
this.subscription.unsubscribe();
}
};
/**
* Get the number of confirmations our transaction has.
*/
if (!this.txStatus || !this.txStatus.confirmed) {
return -1;
}
return this.blockNumber - +this.txStatus.confirmed.blockNumber;
}
send = password => {
const { token } = this.tx;
token.address === 'ETH' ? txForEth(this.tx) : txForErc20(this.tx, token);
: contractForToken(token.address).transfer$(...tx.args, tx.options);
return new Promise((resolve, reject) => {
send$.subscribe(txStatus => {
this.setTxStatus(txStatus);
// When we arrive to the `requested` stage, we accept the request
if (txStatus.requested) {
this.acceptRequest(txStatus.requested, password)
.then(resolve)
.catch(reject);
}
/**
* Send a raw, signed transaction.
*/
sendRaw = () => {
debug('Sending raw tx.', this.tx.rawSigned);
return new Promise((resolve, reject) => {
postRaw$(this.tx.rawSigned).subscribe(txStatus => {
this.setTxStatus(txStatus);
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
if (txStatus.signed) {
this.listenForConfirmations();
resolve();
}
debug('Tx status updated.', txStatus);
}, reject);
});
};
/**
* Get the RLP of an (unsigned) transaction.
*/
getRlp = () => {
return transactionToRlp(this.tx);
};
/**
* Sign the transaction with the given signature.
*/
@action
signRaw = signature => {
const signed = signTransactionWithSignature(this.tx, signature);
this.tx.rawSigned = '0x' + signed.toString('hex');
};
setBlockNumber = blockNumber => {
this.blockNumber = blockNumber;
@action
setTxStatus = txStatus => {
this.txStatus = txStatus;
};
}
export default new SendStore();