on master help sign in
Restructure invite transaction order
6be0701Fang
src/lib/invite.js+73−71
@@ -38,22 +38,14 @@ const TRANSACTION_STATES = {
38 label: 'Signing Transactions...',
38 label: 'Signing Transactions...',
39 progress: 0.15,
39 progress: 0.15,
40 },
40 },
41− FUNDING_INVITE: {
41+ FUNDING: {
42− label: 'Funding Invite Wallet...',
42+ label: 'Funding Transactions...',
43 progress: 0.3,
43 progress: 0.3,
44 },
44 },
45 CLAIMING: {
45 CLAIMING: {
46 label: 'Claiming Invite...',
46 label: 'Claiming Invite...',
47 progress: 0.55,
47 progress: 0.55,
48 },
48 },
49− FUNDING_RECIPIENT: {
50− label: 'Funding Recipient Wallet...',
51− progress: 0.65,
52− },
53− CONFIGURING: {
54− label: 'Configuring Point...',
55− progress: 0.85,
56− },
57 CLEANING: {
49 CLEANING: {
58 label: 'Cleaning Up...',
50 label: 'Cleaning Up...',
59 progress: 0.95,
51 progress: 0.95,
@@ -108,6 +100,7 @@ async function claimPointFromInvite({
108 web3,
100 web3,
109 contracts,
101 contracts,
110 onUpdate,
102 onUpdate,
103+ transferEth,
111}) {
104}) {
112 const askForFunding = (address, minBalance, balance) =>
105 const askForFunding = (address, minBalance, balance) =>
113 onUpdate({
106 onUpdate({
@@ -120,61 +113,28 @@ async function claimPointFromInvite({
120
113
121 const inviteAddress = inviteWallet.address;
114 const inviteAddress = inviteWallet.address;
122
115
123− progress(TRANSACTION_STATES.GENERATING);
116+ //
124−
117+ // generate transactions
125− // transfer from invite wallet to new wallet
118+ //
126− const transferTx = azimuth.ecliptic.transferPoint(
127− contracts,
128− point,
129− wallet.ownership.keys.address
130− );
131− transferTx.gas = 500000; //TODO can maybe be lower?
132−
133− // ping gas tank with txs if needed
134− let inviteNonce = await web3.eth.getTransactionCount(inviteAddress);
135− transferTx.nonce = inviteNonce++;
136− transferTx.gasPrice = 20000000000; //NOTE we pay the premium for faster ux
137− transferTx.from = inviteAddress;
138−
139− progress(TRANSACTION_STATES.SIGNING);
140− // NOTE: using web3.eth.accounts.signTransaction is broken (1.0.0-beta51)
141− const transferStx = new Tx(transferTx);
142− transferStx.sign(inviteWallet.privateKey);
143− const rawTransferStx = hexify(transferStx.serialize());
144− const transferCost = transferTx.gas * transferTx.gasPrice;
145−
146− progress(TRANSACTION_STATES.FUNDING_INVITE);
147−
148− let usedTank = await tank.ensureFundsFor(
149− web3,
150− point,
151− inviteAddress,
152− transferCost,
153− [rawTransferStx],
154− askForFunding,
155− gotFunding
156− );
157−
158− progress(TRANSACTION_STATES.CLAIMING);
159
119
160− // send transaction
120+ progress(TRANSACTION_STATES.GENERATING);
161− await sendAndAwaitConfirm(web3, [Just(transferStx)], usedTank);
162
121
163− //
122+ // transfer to invite wallet, so it can configure
164− // we're gonna be operating as the new wallet from here on out, so change
123+ //NOTE doing this in the invite wallet instead of the final wallet makes
165− // the relevant values
124+ // failure cases "solvable" by just telling the user to fully retry
166− const newAddress = wallet.ownership.keys.address;
167
125
168− // configure management proxy
126+ //TODO no harm done if we already owned it, but should still get a bool arg
169− const managementTx = azimuth.ecliptic.setManagementProxy(
127+ // for skipping this, if it isn't too big a burden for callers
128+ let transferTmpTx = azimuth.ecliptic.transferPoint(
170 contracts,
129 contracts,
171 point,
130 point,
172− wallet.management.keys.address
131+ inviteAddress,
132+ false
173 );
133 );
174− managementTx.gas = 200000;
134+ transferTmpTx.gas = 500000; //TODO can maybe be lower?
175− managementTx.nonce = 0;
176
135
177 // configure networking public keys
136 // configure networking public keys
137+
178 //TODO feels like more of this logic should live in a lib?
138 //TODO feels like more of this logic should live in a lib?
179 const seed = await attemptNetworkSeedDerivation(true, {
139 const seed = await attemptNetworkSeedDerivation(true, {
180 walletType: WALLET_TYPES.TICKET,
140 walletType: WALLET_TYPES.TICKET,
@@ -201,10 +161,19 @@ async function claimPointFromInvite({
201 false
161 false
202 );
162 );
203 keysTx.gas = 150000;
163 keysTx.gas = 150000;
204− keysTx.nonce = 1;
205
164
165+ // configure management proxy
166+
167+ let managementTx = azimuth.ecliptic.setManagementProxy(
168+ contracts,
169+ point,
170+ wallet.management.keys.address
171+ );
206− let txs = [managementTx, keysTx];
172+ managementTx.gas = 200000;
173+
174+ // set spawn & voting proxies situationally
207
175
176+ let txs = [transferTmpTx, keysTx, managementTx];
208 if (
177 if (
209 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
178 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
210 ) {
179 ) {
@@ -214,7 +183,6 @@ async function claimPointFromInvite({
214 wallet.spawn.keys.address
183 wallet.spawn.keys.address
215 );
184 );
216 spawnTx.gas = 200000;
185 spawnTx.gas = 200000;
217− spawnTx.nonce = 2;
218 txs.push(spawnTx);
186 txs.push(spawnTx);
219
187
220 if (
188 if (
@@ -226,42 +194,76 @@ async function claimPointFromInvite({
226 wallet.voting.keys.address
194 wallet.voting.keys.address
227 );
195 );
228 votingTx.gas = 200000;
196 votingTx.gas = 200000;
229− votingTx.nonce = 3;
230 txs.push(votingTx);
197 txs.push(votingTx);
231 }
198 }
232 }
199 }
233
200
201+ // transfer configured point to user's new wallet
202+
203+ let transferFinalTx = azimuth.ecliptic.transferPoint(
204+ contracts,
205+ point,
206+ wallet.ownership.keys.address,
207+ false
208+ );
209+ transferFinalTx.gas = 500000; //TODO can maybe be lower?
210+ txs.push(transferFinalTx);
211+
212+ //
213+ // finalizing & signing transactions
214+ //
215+
216+ progress(TRANSACTION_STATES.SIGNING);
217+
234 let totalCost = 0;
218 let totalCost = 0;
219+ let inviteNonce = await web3.eth.getTransactionCount(inviteAddress);
235 txs = txs.map(tx => {
220 txs = txs.map(tx => {
221+ tx.from = inviteAddress;
222+ tx.nonce = inviteNonce++;
236− tx.gasPrice = 20000000000;
223+ tx.gasPrice = 20000000000; //NOTE we pay the premium for faster ux
237− tx.from = newAddress;
238 totalCost = totalCost + tx.gasPrice * tx.gas;
224 totalCost = totalCost + tx.gasPrice * tx.gas;
239 return tx;
225 return tx;
240 });
226 });
241
227
242 let txPairs = txs.map(tx => {
228 let txPairs = txs.map(tx => {
243 let stx = new Tx(tx);
229 let stx = new Tx(tx);
244− stx.sign(Buffer.from(wallet.ownership.keys.private, 'hex'));
230+ stx.sign(Buffer.from(inviteWallet.privateKey, 'hex'));
245 return {
231 return {
246 raw: '0x' + stx.serialize().toString('hex'),
232 raw: '0x' + stx.serialize().toString('hex'),
247 signed: stx,
233 signed: stx,
248 };
234 };
249 });
235 });
250
236
237+ //
238+ // ensuring funding for transactions
239+ //
240+
251− progress(TRANSACTION_STATES.FUNDING_RECIPIENT);
241+ progress(TRANSACTION_STATES.FUNDING);
252
242
253− usedTank = await tank.ensureFundsFor(
243+ const usedTank = await tank.ensureFundsFor(
254 web3,
244 web3,
255 point,
245 point,
256− newAddress,
246+ inviteAddress,
257 totalCost,
247 totalCost,
258 txPairs.map(p => p.raw),
248 txPairs.map(p => p.raw),
259− askForFunding
249+ askForFunding,
250+ gotFunding
260 );
251 );
252+ console.log('used tank', usedTank);
253+
254+ //
255+ // sending and awaiting transactions
256+ //
261
257
262− progress(TRANSACTION_STATES.CONFIGURING);
258+ progress(TRANSACTION_STATES.CLAIMING);
263
259
260+ console.log('sending txpairs', txPairs.length);
264 await sendAndAwaitConfirm(web3, txPairs.map(p => Just(p.signed)), usedTank);
261 await sendAndAwaitConfirm(web3, txPairs.map(p => Just(p.signed)), usedTank);
262+ console.log('all done');
263+
264+ //
265+ // move leftover eth
266+ //
265
267
266 progress(TRANSACTION_STATES.CLEANING);
268 progress(TRANSACTION_STATES.CLEANING);
267
269
@@ -270,11 +272,11 @@ async function claimPointFromInvite({
270 const gasPrice = 20000000000;
272 const gasPrice = 20000000000;
271 const gasLimit = 21000;
273 const gasLimit = 21000;
272 const sendEthCost = gasPrice * gasLimit;
274 const sendEthCost = gasPrice * gasLimit;
273− if (balance > sendEthCost) {
275+ if (transferEth && balance > sendEthCost) {
274 const value = balance - sendEthCost;
276 const value = balance - sendEthCost;
275 console.log('sending', value);
277 console.log('sending', value);
276 const tx = {
278 const tx = {
277− to: newAddress,
279+ to: wallet.ownership.keys.address,
278 value: value,
280 value: value,
279 gasPrice: gasPrice,
281 gasPrice: gasPrice,
280 gas: gasLimit,
282 gas: gasLimit,
src/views/Activate/PassportTransfer.js+1
@@ -99,6 +99,7 @@ export default function PassportTransfer({ className, resetActivateRouter }) {
99 web3: _web3,
99 web3: _web3,
100 contracts: _contracts,
100 contracts: _contracts,
101 onUpdate: handleUpdate,
101 onUpdate: handleUpdate,
102+ transferEth: true,
102 });
103 });
103
104
104 // set the global wallet
105 // set the global wallet