1import * as azimuth from 'azimuth-js';
2import * as tank from './tank';
4import { hexify } from './txn';
6 deriveNetworkSeedFromUrbitWallet,
10import { addHexPrefix } from './utils/address';
12 sendAndAwaitAllSerial,
14 sendSignedTransaction,
16import getSuggestedGasPrice from './getSuggestedGasPrice';
17import { GAS_LIMITS } from './constants';
18import { toBN } from 'web3-utils';
19import { safeToWei } from './lib';
21// the initial network key revision is always 1
22const INITIAL_NETWORK_KEY_REVISION = 1;
24export const TRANSACTION_PROGRESS = {
33export async function reticketPointBetweenWallets({
44 nextRevision = INITIAL_NETWORK_KEY_REVISION,
48 const askForFunding = (address, minBalance, balance) =>
51 value: { address, minBalance, balance },
54 const gotFunding = () => onUpdate({ type: 'gotFunding' });
55 const progress = state => onUpdate({ type: 'progress', state });
58 // generate transactions
61 progress(TRANSACTION_PROGRESS.GENERATING);
63 // transfer to invite wallet, so it can configure
64 //NOTE doing this in the invite wallet instead of the final wallet makes
65 // failure cases "solvable" by just telling the user to fully retry.
66 // in other words, the invite flow is effectively idempotent.
68 //TODO no harm done if we already owned it, but should still get a bool arg
69 // for skipping this, if it isn't too big a burden for callers
70 const transferTmpTx = azimuth.ecliptic.transferPoint(
76 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
78 // configure networking public keys
79 const seed = await deriveNetworkSeedFromUrbitWallet(toWallet, nextRevision);
81 const networkSeed = seed.matchWith({
83 throw new Error('network seed not derived');
88 const networkKeys = deriveNetworkKeys(networkSeed);
90 const keysTx = azimuth.ecliptic.configureKeys(
93 addHexPrefix(networkKeys.crypt.public),
94 addHexPrefix(networkKeys.auth.public),
96 /* discontinuous */ false
98 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
100 // configure management proxy
102 const managementTx = azimuth.ecliptic.setManagementProxy(
105 toWallet.management.keys.address
107 managementTx.gas = GAS_LIMITS.SET_PROXY;
109 // set spawn & voting proxies situationally
111 let txs = [transferTmpTx, keysTx, managementTx];
113 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
115 const spawnTx = azimuth.ecliptic.setSpawnProxy(
118 toWallet.spawn.keys.address
120 spawnTx.gas = GAS_LIMITS.SET_PROXY;
124 azimuth.azimuth.getPointSize(point) === azimuth.azimuth.PointSize.Galaxy
126 const votingTx = azimuth.ecliptic.setVotingProxy(
129 toWallet.voting.keys.address
131 votingTx.gas = GAS_LIMITS.SET_PROXY;
136 // transfer configured point to user's new wallet
138 const transferFinalTx = azimuth.ecliptic.transferPoint(
141 toWallet.ownership.keys.address,
144 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
145 txs.push(transferFinalTx);
148 // finalizing & signing transactions
151 progress(TRANSACTION_PROGRESS.SIGNING);
153 const gasPriceGwei = (await getSuggestedGasPrice(networkType)).toFixed();
154 const chainId = await web3.eth.net.getId();
155 const gasPriceWeiBN = toBN(safeToWei(gasPriceGwei, 'gwei'));
156 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
157 const totalCost = txs.reduce(
158 (acc, tx) => acc.add(gasPriceWeiBN.mul(toBN(tx.gas))),
164 // Must be done in serial else hardware will give nonce errors
165 for (let i = 0; i < txs.length; i++) {
166 const stx = await signTransaction({
168 walletType: fromWalletType,
169 walletHdPath: fromWalletHdPath,
172 nonce: inviteNonce + i,
174 gasPrice: gasPriceGwei,
175 gasLimit: txs[i].gas,
180 raw: hexify(stx.serialize()),
185 inviteNonce = inviteNonce + txs.length;
188 // ensuring funding for transactions
191 progress(TRANSACTION_PROGRESS.FUNDING);
193 const usedTank = await tank.ensureFundsFor(
199 txPairs.map(p => p.raw),
205 // sending and awaiting transactions
208 progress(TRANSACTION_PROGRESS.TRANSFERRING);
210 const receipts = await sendAndAwaitAllSerial(
212 txPairs.map(p => p.signed),
216 const realCost = gasPriceWeiBN.mul(
217 receipts.reduce((sum, receipt) => {
218 return sum.add(toBN(receipt.gasUsed));
226 progress(TRANSACTION_PROGRESS.CLEANING);
228 let balance = toBN(await web3.eth.getBalance(fromWallet.address));
229 const gasLimit = GAS_LIMITS.SEND_ETH;
230 const sendEthCost = gasPriceWeiBN.mul(toBN(gasLimit));
232 // if we have funds left over from what the gas tank gave us, send those back
233 if (usedTank && realCost.add(sendEthCost).lt(totalCost)) {
235 const remainder = totalCost.sub(realCost).sub(sendEthCost);
236 console.log('returning to gas tank', remainder.toString());
237 const txn = { to: tank.TANK_ADDRESS, value: remainder };
238 const stx = await signTransaction({
240 walletType: fromWalletType,
241 walletHdPath: fromWalletHdPath,
245 gasPrice: gasPriceGwei,
249 sendSignedTransaction(web3, stx);
251 // only update these after the above succeeded
253 balance = balance.sub(remainder).sub(sendEthCost);
255 console.log('error returning gas, safely ignored:');
260 // if non-trivial eth left in invite wallet, transfer to new ownership
261 if (transferEth && balance.gt(sendEthCost)) {
263 const value = balance.sub(sendEthCost);
265 to: toWallet.ownership.keys.address,
268 const stx = await signTransaction({
270 walletType: fromWalletType,
271 walletHdPath: fromWalletHdPath,
275 gasPrice: gasPriceGwei,
279 sendSignedTransaction(web3, stx);
281 console.log('error sending value tx, safely ignored:');
286 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