on e5e005a help sign in
bridge/src/lib/reticket.js
286 lines 7.3 kB history raw
1import * as azimuth from 'azimuth-js';
2import * as tank from './tank';
4import { hexify } from './txn';
5import {
6 deriveNetworkSeedFromUrbitWallet,
7 deriveNetworkKeys,
8 CRYPTO_SUITE_VERSION,
9} from './keys';
10import { addHexPrefix } from './utils/address';
11import {
12 sendAndAwaitAllSerial,
13 signTransaction,
14 sendSignedTransaction,
15} from './txn';
16import getSuggestedGasPrice from './getSuggestedGasPrice';
17import { GAS_LIMITS, INITIAL_NETWORK_KEY_REVISION } from './constants';
18import { toBN } from 'web3-utils';
19import { safeToWei } from './lib';
20import { isGalaxy, isPlanet } from './utils/point';
22export const TRANSACTION_PROGRESS = {
23 GENERATING: 0.01,
24 SIGNING: 0.15,
25 FUNDING: 0.3,
26 TRANSFERRING: 0.55,
27 CLEANING: 0.95,
28 DONE: 1.0,
29};
31export async function reticketPointBetweenWallets({
32 fromWallet,
33 fromWalletType,
34 fromWalletHdPath,
35 toWallet,
36 point,
37 web3,
38 contracts,
39 networkType,
40 onUpdate,
41 transferEth = false,
42 nextRevision = INITIAL_NETWORK_KEY_REVISION,
43 txnSigner,
44 txnSender,
45 doSetSpawnProxy = true,
46}) {
47 const askForFunding = (address, minBalance, balance) =>
48 onUpdate({
49 type: 'askFunding',
50 value: { address, minBalance, balance },
51 });
53 const gotFunding = () => onUpdate({ type: 'gotFunding' });
54 const progress = state => onUpdate({ type: 'progress', state });
56 //
57 // generate transactions
58 //
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(
70 contracts,
71 point,
72 fromWallet.address,
73 false
74 );
75 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
77 // configure networking public keys
78 const seed = await deriveNetworkSeedFromUrbitWallet(toWallet, nextRevision);
80 const networkSeed = seed.matchWith({
81 Nothing: () => {
82 throw new Error('network seed not derived');
83 },
84 Just: p => p.value,
85 });
87 const networkKeys = deriveNetworkKeys(networkSeed);
89 const keysTx = azimuth.ecliptic.configureKeys(
90 contracts,
91 point,
92 addHexPrefix(networkKeys.crypt.public),
93 addHexPrefix(networkKeys.auth.public),
94 CRYPTO_SUITE_VERSION,
95 /* discontinuous */ false
96 );
97 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
99 // configure management proxy
101 const managementTx = azimuth.ecliptic.setManagementProxy(
102 contracts,
103 point,
104 toWallet.management.keys.address
105 );
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(
114 contracts,
115 point,
116 toWallet.spawn.keys.address
117 );
118 spawnTx.gas = GAS_LIMITS.SET_PROXY;
119 txs.push(spawnTx);
120 }
122 if (isGalaxy(point)) {
123 const votingTx = azimuth.ecliptic.setVotingProxy(
124 contracts,
125 point,
126 toWallet.voting.keys.address
127 );
128 votingTx.gas = GAS_LIMITS.SET_PROXY;
129 txs.push(votingTx);
130 }
132 // transfer configured point to user's new wallet
134 const transferFinalTx = azimuth.ecliptic.transferPoint(
135 contracts,
136 point,
137 toWallet.ownership.keys.address,
138 false
139 );
140 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
141 txs.push(transferFinalTx);
143 //
144 // finalizing & signing transactions
145 //
147 progress(TRANSACTION_PROGRESS.SIGNING);
149 const gasPriceData = (
150 await getSuggestedGasPrice(networkType)
151 ).average;
152 const gasPriceGwei = gasPriceData.price.toFixed();
153 const chainId = await web3.eth.net.getId();
154 const gasPriceWeiBN = toBN(safeToWei(gasPriceGwei, 'gwei'));
155 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
156 const totalCost = txs.reduce(
157 (acc, tx) => acc.add(gasPriceWeiBN.mul(toBN(tx.gas))),
158 toBN(0)
159 );
161 let txPairs = [];
163 // Must be done in serial else hardware will give nonce errors
164 for (let i = 0; i < txs.length; i++) {
165 const stx = await signTransaction({
166 wallet: fromWallet,
167 walletType: fromWalletType,
168 walletHdPath: fromWalletHdPath,
169 networkType,
170 txn: txs[i],
171 nonce: inviteNonce + i,
172 chainId,
173 gasPriceData,
174 gasLimit: txs[i].gas,
175 txnSigner,
176 txnSender,
177 });
178 txPairs.push({
179 raw: hexify(stx.serialize()),
180 signed: stx,
181 });
182 }
184 inviteNonce = inviteNonce + txs.length;
186 //
187 // ensuring funding for transactions
188 //
190 progress(TRANSACTION_PROGRESS.FUNDING);
192 const usedTank = await tank.ensureFundsFor(
193 web3,
194 point,
195 fromWallet.address,
196 fromWalletType,
197 totalCost,
198 txPairs.map(p => p.raw),
199 askForFunding,
200 gotFunding
201 );
203 //
204 // sending and awaiting transactions
205 //
207 progress(TRANSACTION_PROGRESS.TRANSFERRING);
209 const receipts = await sendAndAwaitAllSerial(
210 web3,
211 txPairs.map(p => p.signed),
212 usedTank
213 );
215 const realCost = gasPriceWeiBN.mul(
216 receipts.reduce((sum, receipt) => {
217 return sum.add(toBN(receipt.gasUsed));
218 }, toBN(0))
219 );
221 //
222 // move leftover eth
223 //
225 progress(TRANSACTION_PROGRESS.CLEANING);
227 let balance = toBN(await web3.eth.getBalance(fromWallet.address));
228 const gasLimit = GAS_LIMITS.SEND_ETH;
229 const sendEthCost = gasPriceWeiBN.mul(toBN(gasLimit));
231 // if we have funds left over from what the gas tank gave us, send those back
232 if (usedTank && realCost.add(sendEthCost).lt(totalCost)) {
233 try {
234 const remainder = totalCost.sub(realCost).sub(sendEthCost);
235 console.log('returning to gas tank', remainder.toString());
236 const txn = { to: tank.TANK_ADDRESS, value: remainder };
237 const stx = await signTransaction({
238 wallet: fromWallet,
239 walletType: fromWalletType,
240 walletHdPath: fromWalletHdPath,
241 txn,
242 chainId,
243 networkType,
244 gasPriceData,
245 gasLimit,
246 nonce: inviteNonce,
247 });
248 sendSignedTransaction(web3, stx);
250 // only update these after the above succeeded
251 inviteNonce++;
252 balance = balance.sub(remainder).sub(sendEthCost);
253 } catch (err) {
254 console.log('error returning gas, safely ignored:');
255 console.log(err);
256 }
257 }
259 // if non-trivial eth left in invite wallet, transfer to new ownership
260 if (transferEth && balance.gt(sendEthCost)) {
261 try {
262 const value = balance.sub(sendEthCost);
263 const txn = {
264 to: toWallet.ownership.keys.address,
265 value: value,
266 };
267 const stx = await signTransaction({
268 wallet: fromWallet,
269 walletType: fromWalletType,
270 walletHdPath: fromWalletHdPath,
271 txn,
272 chainId,
273 networkType,
274 gasPriceData,
275 gasLimit,
276 nonce: inviteNonce,
277 });
278 sendSignedTransaction(web3, stx);
279 } catch (err) {
280 console.log('error sending value tx, safely ignored:');
281 console.log(err);
282 }
283 }
285 progress(TRANSACTION_PROGRESS.DONE);