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 './getSuggestedGasPrice';
14import { GAS_LIMITS } from './constants';
15import { toBN } from 'web3-utils';
16import { safeToWei } from './lib';
18// the initial network key revision is always 1
19const INITIAL_NETWORK_KEY_REVISION = 1;
21export const TRANSACTION_PROGRESS = {
30export async function reticketPointBetweenWallets({
39 nextRevision = INITIAL_NETWORK_KEY_REVISION,
41 const askForFunding = (address, minBalance, balance) =>
44 value: { address, minBalance, balance },
47 const gotFunding = () => onUpdate({ type: 'gotFunding' });
48 const progress = state => onUpdate({ type: 'progress', state });
51 // generate transactions
54 progress(TRANSACTION_PROGRESS.GENERATING);
56 // transfer to invite wallet, so it can configure
57 //NOTE doing this in the invite wallet instead of the final wallet makes
58 // failure cases "solvable" by just telling the user to fully retry.
59 // in other words, the invite flow is effectively idempotent.
61 //TODO no harm done if we already owned it, but should still get a bool arg
62 // for skipping this, if it isn't too big a burden for callers
63 const transferTmpTx = azimuth.ecliptic.transferPoint(
69 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
71 // configure networking public keys
72 const seed = await deriveNetworkSeedFromUrbitWallet(toWallet, nextRevision);
74 const networkSeed = seed.matchWith({
76 throw new Error('network seed not derived');
81 const networkKeys = deriveNetworkKeys(networkSeed);
83 const keysTx = azimuth.ecliptic.configureKeys(
86 addHexPrefix(networkKeys.crypt.public),
87 addHexPrefix(networkKeys.auth.public),
89 /* discontinuous */ false
91 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
93 // configure management proxy
95 const managementTx = azimuth.ecliptic.setManagementProxy(
98 toWallet.management.keys.address
100 managementTx.gas = GAS_LIMITS.SET_PROXY;
102 // set spawn & voting proxies situationally
104 let txs = [transferTmpTx, keysTx, managementTx];
106 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
108 const spawnTx = azimuth.ecliptic.setSpawnProxy(
111 toWallet.spawn.keys.address
113 spawnTx.gas = GAS_LIMITS.SET_PROXY;
117 azimuth.azimuth.getPointSize(point) === azimuth.azimuth.PointSize.Galaxy
119 const votingTx = azimuth.ecliptic.setVotingProxy(
122 toWallet.voting.keys.address
124 votingTx.gas = GAS_LIMITS.SET_PROXY;
129 // transfer configured point to user's new wallet
131 const transferFinalTx = azimuth.ecliptic.transferPoint(
134 toWallet.ownership.keys.address,
137 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
138 txs.push(transferFinalTx);
141 // finalizing & signing transactions
144 progress(TRANSACTION_PROGRESS.SIGNING);
146 const suggestedGasPrice = await getSuggestedGasPrice(networkType);
147 const gasPrice = safeToWei(suggestedGasPrice.toFixed(), 'gwei');
148 const gasPriceBN = toBN(gasPrice);
149 let totalCost = toBN(0);
150 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
151 txs = txs.map(tx => {
152 tx.from = fromWallet.address;
153 tx.nonce = inviteNonce++;
154 tx.gasPrice = gasPriceBN;
155 totalCost = totalCost.add(gasPriceBN.mul(toBN(tx.gas)));
159 let txPairs = txs.map(tx => {
160 let stx = new Tx(tx);
161 stx.sign(Buffer.from(fromWallet.privateKey, 'hex'));
163 raw: hexify(stx.serialize()),
169 // ensuring funding for transactions
172 progress(TRANSACTION_PROGRESS.FUNDING);
174 const usedTank = await tank.ensureFundsFor(
179 txPairs.map(p => p.raw),
185 // sending and awaiting transactions
188 progress(TRANSACTION_PROGRESS.TRANSFERRING);
190 await sendAndAwaitAll(web3, txPairs.map(p => p.signed), usedTank);
196 progress(TRANSACTION_PROGRESS.CLEANING);
198 // if non-trivial eth left in invite wallet, transfer to new ownership
199 let balance = toBN(await web3.eth.getBalance(fromWallet.address));
200 const gasLimit = GAS_LIMITS.SEND_ETH;
201 const sendEthCost = gasPriceBN.mul(toBN(gasLimit));
202 if (transferEth && balance.gt(sendEthCost)) {
203 const value = balance.sub(sendEthCost);
205 to: toWallet.ownership.keys.address,
207 gasPrice: gasPriceBN,
209 nonce: inviteNonce++,
211 let stx = new Tx(tx);
212 stx.sign(fromWallet.privateKey);
213 const rawTx = hexify(stx.serialize());
214 web3.eth.sendSignedTransaction(rawTx).catch(err => {
215 console.log('error sending value tx, who cares', err);
219 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