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 nextNetworkKeyRevision = 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(
71 nextNetworkKeyRevision
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);
147 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
148 txs = txs.map(tx => {
149 tx.from = fromWallet.address;
150 tx.nonce = inviteNonce++;
151 tx.gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'), 10);
152 totalCost = totalCost + tx.gasPrice * tx.gas;
156 let txPairs = txs.map(tx => {
157 let stx = new Tx(tx);
158 stx.sign(Buffer.from(fromWallet.privateKey, 'hex'));
160 raw: hexify(stx.serialize()),
166 // ensuring funding for transactions
169 progress(TRANSACTION_PROGRESS.FUNDING);
171 const usedTank = await tank.ensureFundsFor(
176 txPairs.map(p => p.raw),
182 // sending and awaiting transactions
185 progress(TRANSACTION_PROGRESS.TRANSFERRING);
187 await sendAndAwaitAll(web3, txPairs.map(p => p.signed), usedTank);
193 progress(TRANSACTION_PROGRESS.CLEANING);
195 // if non-trivial eth left in invite wallet, transfer to new ownership
196 let balance = await web3.eth.getBalance(fromWallet.address);
197 const gasPrice = parseInt(toWei(DEFAULT_GAS_PRICE_GWEI.toFixed(), 'gwei'));
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