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, INITIAL_NETWORK_KEY_REVISION } from './constants';
18import { toBN } from 'web3-utils';
19import { gweiToWei } from './lib';
20import { isGalaxy, isPlanet } from './utils/point';
22export const TRANSACTION_PROGRESS = {
31export async function reticketPointBetweenWallets({
42 nextRevision = INITIAL_NETWORK_KEY_REVISION,
45 doSetSpawnProxy = true,
47 const askForFunding = (address, minBalance, balance) =>
50 value: { address, minBalance, balance },
53 const gotFunding = () => onUpdate({ type: 'gotFunding' });
54 const progress = state => onUpdate({ type: 'progress', state });
57 // generate transactions
60 progress(TRANSACTION_PROGRESS.GENERATING);
62 // transfer to invite wallet, so it can configure
63 //NOTE doing this in the invite wallet instead of the final wallet makes
64 // failure cases "solvable" by just telling the user to fully retry.
65 // in other words, the invite flow is effectively idempotent.
67 //TODO no harm done if we already owned it, but should still get a bool arg
68 // for skipping this, if it isn't too big a burden for callers
69 const transferTmpTx = azimuth.ecliptic.transferPoint(
75 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
77 // configure networking public keys
78 const seed = await deriveNetworkSeedFromUrbitWallet(toWallet, nextRevision);
80 const networkSeed = seed.matchWith({
82 throw new Error('network seed not derived');
87 const networkKeys = deriveNetworkKeys(networkSeed);
89 const keysTx = azimuth.ecliptic.configureKeys(
92 addHexPrefix(networkKeys.crypt.public),
93 addHexPrefix(networkKeys.auth.public),
95 /* discontinuous */ false
97 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
99 // configure management proxy
101 const managementTx = azimuth.ecliptic.setManagementProxy(
104 toWallet.management.keys.address
106 managementTx.gas = GAS_LIMITS.SET_PROXY;
108 // set spawn & voting proxies situationally
110 // skip this step if reticketing an L1 point with L2-migrated spawn proxy
111 let txs = [transferTmpTx, keysTx, managementTx];
112 if (!isPlanet(point) && doSetSpawnProxy) {
113 const spawnTx = azimuth.ecliptic.setSpawnProxy(
116 toWallet.spawn.keys.address
118 spawnTx.gas = GAS_LIMITS.SET_PROXY;
122 if (isGalaxy(point)) {
123 const votingTx = azimuth.ecliptic.setVotingProxy(
126 toWallet.voting.keys.address
128 votingTx.gas = GAS_LIMITS.SET_PROXY;
132 // transfer configured point to user's new wallet
134 const transferFinalTx = azimuth.ecliptic.transferPoint(
137 toWallet.ownership.keys.address,
140 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
141 txs.push(transferFinalTx);
144 // finalizing & signing transactions
147 progress(TRANSACTION_PROGRESS.SIGNING);
149 const gasPriceData = (await getSuggestedGasPrice(networkType, web3)).average;
150 const chainId = await web3.eth.net.getId();
151 const gasPriceWeiBN = gweiToWei(gasPriceData.price);
152 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
153 const totalCost = txs.reduce(
154 (acc, tx) => acc.add(gasPriceWeiBN.mul(toBN(tx.gas))),
160 // Must be done in serial else hardware will give nonce errors
161 for (let i = 0; i < txs.length; i++) {
162 const stx = await signTransaction({
164 walletType: fromWalletType,
165 walletHdPath: fromWalletHdPath,
168 nonce: inviteNonce + i,
171 gasLimit: txs[i].gas,
177 raw: hexify(stx.serialize()),
182 inviteNonce = inviteNonce + txs.length;
185 // ensuring funding for transactions
188 progress(TRANSACTION_PROGRESS.FUNDING);
190 const usedTank = await tank.ensureFundsFor(
196 txPairs.map(p => p.raw),
202 // sending and awaiting transactions
205 progress(TRANSACTION_PROGRESS.TRANSFERRING);
207 const receipts = await sendAndAwaitAllSerial(
209 txPairs.map(p => p.signed),
213 const realCost = gasPriceWeiBN.mul(
214 receipts.reduce((sum, receipt) => {
215 return sum.add(toBN(receipt.gasUsed));
223 progress(TRANSACTION_PROGRESS.CLEANING);
225 let balance = toBN(await web3.eth.getBalance(fromWallet.address));
226 const gasLimit = GAS_LIMITS.SEND_ETH;
227 const sendEthCost = gasPriceWeiBN.mul(toBN(gasLimit));
229 // if we have funds left over from what the gas tank gave us, send those back
230 if (usedTank && realCost.add(sendEthCost).lt(totalCost)) {
232 const remainder = totalCost.sub(realCost).sub(sendEthCost);
233 console.log('returning to gas tank', remainder.toString());
234 const txn = { to: tank.TANK_ADDRESS, value: remainder };
235 const stx = await signTransaction({
237 walletType: fromWalletType,
238 walletHdPath: fromWalletHdPath,
247 sendSignedTransaction(web3, stx);
249 // only update these after the above succeeded
251 balance = balance.sub(remainder).sub(sendEthCost);
253 console.log('error returning gas, safely ignored:');
258 // if non-trivial eth left in invite wallet, transfer to new ownership
259 if (transferEth && balance.gt(sendEthCost)) {
261 const value = balance.sub(sendEthCost);
263 to: toWallet.ownership.keys.address,
266 const stx = await signTransaction({
268 walletType: fromWalletType,
269 walletHdPath: fromWalletHdPath,
278 sendSignedTransaction(web3, stx);
280 console.log('error sending value tx, safely ignored:');
285 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