1import Tx from 'ethereumjs-tx';
2import * as azimuth from 'azimuth-js';
3import * as tank from './tank';
5import { hexify } from './txn';
7 deriveNetworkSeedFromUrbitWallet,
11import { addHexPrefix } from './wallet';
12import { sendAndAwaitAll } from './txn';
13import { getSuggestedGasPrice } from './useSuggestedGasPrice';
14import { GAS_LIMITS } from './constants';
15import { toWei } from 'web3-utils';
17// the initial network key revision is always 1
18const INITIAL_NETWORK_KEY_REVISION = 1;
20export const TRANSACTION_PROGRESS = {
29export async function reticketPointBetweenWallets({
38 nextRevision = INITIAL_NETWORK_KEY_REVISION,
40 const askForFunding = (address, minBalance, balance) =>
43 value: { address, minBalance, balance },
46 const gotFunding = () => onUpdate({ type: 'gotFunding' });
47 const progress = state => onUpdate({ type: 'progress', state });
50 // generate transactions
53 progress(TRANSACTION_PROGRESS.GENERATING);
55 // transfer to invite wallet, so it can configure
56 //NOTE doing this in the invite wallet instead of the final wallet makes
57 // failure cases "solvable" by just telling the user to fully retry.
58 // in other words, the invite flow is effectively idempotent.
60 //TODO no harm done if we already owned it, but should still get a bool arg
61 // for skipping this, if it isn't too big a burden for callers
62 const transferTmpTx = azimuth.ecliptic.transferPoint(
68 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
70 // configure networking public keys
71 const seed = await deriveNetworkSeedFromUrbitWallet(toWallet, nextRevision);
73 const networkSeed = seed.matchWith({
75 throw new Error('network seed not derived');
80 const networkKeys = deriveNetworkKeys(networkSeed);
82 const keysTx = azimuth.ecliptic.configureKeys(
85 addHexPrefix(networkKeys.crypt.public),
86 addHexPrefix(networkKeys.auth.public),
88 /* discontinuous */ false
90 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
92 // configure management proxy
94 const managementTx = azimuth.ecliptic.setManagementProxy(
97 toWallet.management.keys.address
99 managementTx.gas = GAS_LIMITS.SET_PROXY;
101 // set spawn & voting proxies situationally
103 let txs = [transferTmpTx, keysTx, managementTx];
105 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
107 const spawnTx = azimuth.ecliptic.setSpawnProxy(
110 toWallet.spawn.keys.address
112 spawnTx.gas = GAS_LIMITS.SET_PROXY;
116 azimuth.azimuth.getPointSize(point) === azimuth.azimuth.PointSize.Galaxy
118 const votingTx = azimuth.ecliptic.setVotingProxy(
121 toWallet.voting.keys.address
123 votingTx.gas = GAS_LIMITS.SET_PROXY;
128 // transfer configured point to user's new wallet
130 const transferFinalTx = azimuth.ecliptic.transferPoint(
133 toWallet.ownership.keys.address,
136 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
137 txs.push(transferFinalTx);
140 // finalizing & signing transactions
143 progress(TRANSACTION_PROGRESS.SIGNING);
145 const suggestedGasPrice = await getSuggestedGasPrice(networkType);
146 const gasPrice = parseInt(toWei(suggestedGasPrice.toFixed(), 'gwei'), 10);
148 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
149 txs = txs.map(tx => {
150 tx.from = fromWallet.address;
151 tx.nonce = inviteNonce++;
152 tx.gasPrice = gasPrice;
153 totalCost = totalCost + tx.gasPrice * tx.gas;
157 let txPairs = txs.map(tx => {
158 let stx = new Tx(tx);
159 stx.sign(Buffer.from(fromWallet.privateKey, 'hex'));
161 raw: hexify(stx.serialize()),
167 // ensuring funding for transactions
170 progress(TRANSACTION_PROGRESS.FUNDING);
172 const usedTank = await tank.ensureFundsFor(
177 txPairs.map(p => p.raw),
183 // sending and awaiting transactions
186 progress(TRANSACTION_PROGRESS.TRANSFERRING);
188 await sendAndAwaitAll(web3, txPairs.map(p => p.signed), usedTank);
194 progress(TRANSACTION_PROGRESS.CLEANING);
196 // if non-trivial eth left in invite wallet, transfer to new ownership
197 let balance = await web3.eth.getBalance(fromWallet.address);
198 const gasLimit = GAS_LIMITS.SEND_ETH;
199 const sendEthCost = gasPrice * gasLimit;
200 if (transferEth && balance > sendEthCost) {
201 const value = balance - sendEthCost;
203 to: toWallet.ownership.keys.address,
207 nonce: inviteNonce++,
209 let stx = new Tx(tx);
210 stx.sign(fromWallet.privateKey);
211 const rawTx = hexify(stx.serialize());
212 web3.eth.sendSignedTransaction(rawTx).catch(err => {
213 console.log('error sending value tx, who cares', err);
217 progress(TRANSACTION_PROGRESS.DONE);
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list