1import * as azimuth from 'azimuth-js';
2import * as tank from './tank';
4import { hexify } from './txn';
6 deriveNetworkSeedFromUrbitWallet,
10import { addHexPrefix } from './wallet';
12 sendAndAwaitAllSerial,
14 sendSignedTransaction,
16import getSuggestedGasPrice from './getSuggestedGasPrice';
17import { GAS_LIMITS } from './constants';
18import { toWei, toBN } from 'web3-utils';
20// the initial network key revision is always 1
21const INITIAL_NETWORK_KEY_REVISION = 1;
23export const TRANSACTION_PROGRESS = {
32export async function reticketPointBetweenWallets({
43 nextRevision = INITIAL_NETWORK_KEY_REVISION,
45 const askForFunding = (address, minBalance, balance) =>
48 value: { address, minBalance, balance },
51 const gotFunding = () => onUpdate({ type: 'gotFunding' });
52 const progress = state => onUpdate({ type: 'progress', state });
55 // generate transactions
58 progress(TRANSACTION_PROGRESS.GENERATING);
60 // transfer to invite wallet, so it can configure
61 //NOTE doing this in the invite wallet instead of the final wallet makes
62 // failure cases "solvable" by just telling the user to fully retry.
63 // in other words, the invite flow is effectively idempotent.
65 //TODO no harm done if we already owned it, but should still get a bool arg
66 // for skipping this, if it isn't too big a burden for callers
67 const transferTmpTx = azimuth.ecliptic.transferPoint(
73 transferTmpTx.gas = GAS_LIMITS.TRANSFER;
75 // configure networking public keys
76 const seed = await deriveNetworkSeedFromUrbitWallet(toWallet, nextRevision);
78 const networkSeed = seed.matchWith({
80 throw new Error('network seed not derived');
85 const networkKeys = deriveNetworkKeys(networkSeed);
87 const keysTx = azimuth.ecliptic.configureKeys(
90 addHexPrefix(networkKeys.crypt.public),
91 addHexPrefix(networkKeys.auth.public),
93 /* discontinuous */ false
95 keysTx.gas = GAS_LIMITS.CONFIGURE_KEYS;
97 // configure management proxy
99 const managementTx = azimuth.ecliptic.setManagementProxy(
102 toWallet.management.keys.address
104 managementTx.gas = GAS_LIMITS.SET_PROXY;
106 // set spawn & voting proxies situationally
108 let txs = [transferTmpTx, keysTx, managementTx];
110 azimuth.azimuth.getPointSize(point) !== azimuth.azimuth.PointSize.Planet
112 const spawnTx = azimuth.ecliptic.setSpawnProxy(
115 toWallet.spawn.keys.address
117 spawnTx.gas = GAS_LIMITS.SET_PROXY;
121 azimuth.azimuth.getPointSize(point) === azimuth.azimuth.PointSize.Galaxy
123 const votingTx = azimuth.ecliptic.setVotingProxy(
126 toWallet.voting.keys.address
128 votingTx.gas = GAS_LIMITS.SET_PROXY;
133 // transfer configured point to user's new wallet
135 const transferFinalTx = azimuth.ecliptic.transferPoint(
138 toWallet.ownership.keys.address,
141 transferFinalTx.gas = GAS_LIMITS.TRANSFER;
142 txs.push(transferFinalTx);
145 // finalizing & signing transactions
148 progress(TRANSACTION_PROGRESS.SIGNING);
150 const suggestedGasPrice = await getSuggestedGasPrice(networkType);
151 const chainId = await web3.eth.net.getId();
152 const gasPrice = toWei(suggestedGasPrice.toFixed(), 'gwei');
153 const gasPriceBN = toBN(gasPrice);
154 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
155 const totalCost = txs.reduce(
156 (acc, tx) => acc.add(gasPriceBN.mul(toBN(tx.gas))),
162 // Must be done in serial else hardware will give nonce errors
163 for (let i = 0; i < txs.length; i++) {
164 const stx = await signTransaction({
166 walletType: fromWalletType,
167 walletHdPath: fromWalletHdPath,
170 nonce: inviteNonce + i,
172 gasPrice: suggestedGasPrice.toFixed(),
173 gasLimit: txs[i].gas,
176 raw: hexify(stx.serialize()),
181 inviteNonce = inviteNonce + txs.length;
184 // ensuring funding for transactions
187 progress(TRANSACTION_PROGRESS.FUNDING);
189 const usedTank = await tank.ensureFundsFor(
195 txPairs.map(p => p.raw),
201 // sending and awaiting transactions
204 progress(TRANSACTION_PROGRESS.TRANSFERRING);
206 await sendAndAwaitAllSerial(web3, txPairs.map(p => p.signed), usedTank);
212 progress(TRANSACTION_PROGRESS.CLEANING);
214 // if non-trivial eth left in invite wallet, transfer to new ownership
215 let balance = toBN(await web3.eth.getBalance(fromWallet.address));
216 const gasLimit = GAS_LIMITS.SEND_ETH;
217 const sendEthCost = gasPriceBN.mul(toBN(gasLimit));
218 if (transferEth && balance.gt(sendEthCost)) {
219 const value = balance.sub(sendEthCost);
221 to: toWallet.ownership.keys.address,
224 const stx = signTransaction({
226 walletType: fromWalletType,
227 walletHdPath: fromWalletHdPath,
231 gasPrice: gasPriceBN,
233 nonce: inviteNonce++,
235 // let stx = new Tx(tx);
236 // stx.sign(fromWallet.privateKey);
237 sendSignedTransaction(web3, stx).catch(err => {
238 console.log('error sending value tx, who cares', err);
242 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