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 console.log(suggestedGasPrice.toFixed());
154 const gasPriceBN = toBN(gasPrice);
155 console.log(gasPrice);
156 let inviteNonce = await web3.eth.getTransactionCount(fromWallet.address);
158 const totalCost = txs.reduce(
159 (acc, tx) => acc.add(gasPriceBN.mul(toBN(tx.gas))),
165 let txPairs = await Promise.all(
166 txs.map(async (tx, idx) => {
167 const stx = await signTransaction({
169 walletType: fromWalletType,
170 walletHdPath: fromWalletHdPath,
173 nonce: inviteNonce + idx,
175 gasPrice: suggestedGasPrice.toFixed(),
182 raw: hexify(stx.serialize()),
188 inviteNonce = inviteNonce + txs.length;
191 // ensuring funding for transactions
194 progress(TRANSACTION_PROGRESS.FUNDING);
196 const usedTank = await tank.ensureFundsFor(
201 txPairs.map(p => p.raw),
207 // sending and awaiting transactions
210 progress(TRANSACTION_PROGRESS.TRANSFERRING);
212 console.log(txPairs);
213 await sendAndAwaitAllSerial(web3, txPairs.map(p => p.signed), usedTank);
219 progress(TRANSACTION_PROGRESS.CLEANING);
221 // if non-trivial eth left in invite wallet, transfer to new ownership
222 let balance = toBN(await web3.eth.getBalance(fromWallet.address));
223 const gasLimit = GAS_LIMITS.SEND_ETH;
224 const sendEthCost = gasPriceBN.mul(toBN(gasLimit));
225 if (transferEth && balance.gt(sendEthCost)) {
226 const value = balance.sub(sendEthCost);
228 to: toWallet.ownership.keys.address,
231 const stx = signTransaction({
233 walletType: fromWalletType,
234 walletHdPath: fromWalletHdPath,
238 gasPrice: gasPriceBN,
240 nonce: inviteNonce++,
242 // let stx = new Tx(tx);
243 // stx.sign(fromWallet.privateKey);
244 sendSignedTransaction(web3, stx).catch(err => {
245 console.log('error sending value tx, who cares', err);
249 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