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 { GAS_LIMITS, DEFAULT_GAS_PRICE_GWEI } from './constants';
14import { toWei } from 'web3-utils';
16// the initial network key revision is always 1
17const INITIAL_NETWORK_KEY_REVISION = 1;
19export const TRANSACTION_PROGRESS = {
28export async function reticketPointBetweenWallets({
36 nextRevision = INITIAL_NETWORK_KEY_REVISION,
38 const askForFunding = (address, minBalance, balance) =>
41 value: { address, minBalance, balance },
44 const gotFunding = () => onUpdate({ type: 'gotFunding' });
45 const progress = state => onUpdate({ type: 'progress', state });
48 // generate transactions
51 progress(TRANSACTION_PROGRESS.GENERATING);
53 // transfer to invite wallet, so it can configure
54 //NOTE doing this in the invite wallet instead of the final wallet makes
55 // failure cases "solvable" by just telling the user to fully retry.
56 // in other words, the invite flow is effectively idempotent.
58 //TODO no harm done if we already owned it, but should still get a bool arg
59 // for skipping this, if it isn't too big a burden for callers
60 const transferTmpTx = azimuth.ecliptic.transferPoint(
66 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
68 // configure networking public keys
69 const seed = await deriveNetworkSeedFromUrbitWallet(toWallet, nextRevision);
71 const networkSeed = seed.matchWith({
73 throw new Error('network seed not derived');
78 const networkKeys = deriveNetworkKeys(networkSeed);
80 const keysTx = azimuth.ecliptic.configureKeys(
83 addHexPrefix(networkKeys.crypt.public),
84 addHexPrefix(networkKeys.auth.public),
86 /* discontinuous */ false
88 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
90 // configure management proxy
92 const managementTx = azimuth.ecliptic.setManagementProxy(
95 toWallet.management.keys.address
97 managementTx.gas = GAS_LIMITS.SET_PROXY;
99 // set spawn & voting proxies situationally
101 let txs = [transferTmpTx, keysTx, managementTx];
103 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
105 const spawnTx = azimuth.ecliptic.setSpawnProxy(
108 toWallet.spawn.keys.address
110 spawnTx.gas = GAS_LIMITS.SET_PROXY;
114 azimuth.azimuth.getPointSize(point) === azimuth.azimuth.PointSize.Galaxy
116 const votingTx = azimuth.ecliptic.setVotingProxy(
119 toWallet.voting.keys.address
121 votingTx.gas = GAS_LIMITS.SET_PROXY;
126 // transfer configured point to user's new wallet
128 const transferFinalTx = azimuth.ecliptic.transferPoint(
131 toWallet.ownership.keys.address,
134 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
135 txs.push(transferFinalTx);
138 // finalizing & signing transactions
141 progress(TRANSACTION_PROGRESS.SIGNING);
144 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
145 txs = txs.map(tx => {
146 tx.from = fromWallet.address;
147 tx.nonce = inviteNonce++;
148 tx.gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'), 10);
149 totalCost = totalCost + tx.gasPrice * tx.gas;
153 let txPairs = txs.map(tx => {
154 let stx = new Tx(tx);
155 stx.sign(Buffer.from(fromWallet.privateKey, 'hex'));
157 raw: hexify(stx.serialize()),
163 // ensuring funding for transactions
166 progress(TRANSACTION_PROGRESS.FUNDING);
168 const usedTank = await tank.ensureFundsFor(
173 txPairs.map(p => p.raw),
179 // sending and awaiting transactions
182 progress(TRANSACTION_PROGRESS.TRANSFERRING);
184 await sendAndAwaitAll(web3, txPairs.map(p => p.signed), usedTank);
190 progress(TRANSACTION_PROGRESS.CLEANING);
192 // if non-trivial eth left in invite wallet, transfer to new ownership
193 let balance = await web3.eth.getBalance(fromWallet.address);
194 const gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'));
195 const gasLimit = GAS_LIMITS.SEND_ETH;
196 const sendEthCost = gasPrice * gasLimit;
197 if (transferEth && balance > sendEthCost) {
198 const value = balance - sendEthCost;
200 to: toWallet.ownership.keys.address,
204 nonce: inviteNonce++,
206 let stx = new Tx(tx);
207 stx.sign(fromWallet.privateKey);
208 const rawTx = hexify(stx.serialize());
209 web3.eth.sendSignedTransaction(rawTx).catch(err => {
210 console.log('error sending value tx, who cares', err);
214 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