1import { linearSR, conditionalSR } from 'azimuth-js';
3function mapRange(amount, cb) {
4 return [...Array(amount).keys()].map(cb);
7// Preferentially withdraw from conditional contracts and use linear
9export function generateWithdrawTxs(contracts, batchLimits, amount, to) {
10 const [left, ...csrTxs] = generateConditionalWithdrawTxs(
16 const lsrTxs = generateLinearWithdrawTxs(contracts, left, to);
17 return [...csrTxs, ...lsrTxs];
21 * Recurse over batchLimits to generate a valid number of withdraw txs.
22 * amount may exceed the number of withdraw txs available in the consolidated SR contract.
24 * @param {object} contracts - The azimuth contracts object
25 * @param {array} batchLimits - An array of the maximum withdraw limit for each batch
26 * @param {amount} amount - The number of txs to be generated
27 * @param {string} to - The addresss to withdraw to
28 * @return {array} An array with the number of transactions missing as the head
29 * (i.e. requested amount exceeded available), and the generated transactions
30 * as the rest of the list
32function generateConditionalWithdrawTxs(
39 // Successfully withdrawn all requested, none missing
43 // Return missing amount in head of list
44 if (batchLimits.length === 0) {
47 const [next, ...rest] = batchLimits;
48 const currentAmount = Math.min(amount, next);
50 ...generateConditionalWithdrawTxs(
53 amount - currentAmount,
57 ...mapRange(currentAmount, () =>
58 conditionalSR.withdrawTo(contracts, offset, to)
63function generateLinearWithdrawTxs(contracts, amount, to) {
64 return mapRange(amount, () => linearSR.withdrawTo(contracts, to));
67export async function getLockupKind(contracts, address) {
68 const conditional = await conditionalSR.getCommitment(contracts, address);
69 const linear = await linearSR.getBatch(contracts, address);
70 const haveConditional = conditional.total > 0;
71 const haveLinear = linear.amount > 0;
72 if (haveConditional && !haveLinear) return 'conditional';
73 if (!haveConditional && haveLinear) return 'linear';
74 if (haveConditional && haveLinear) return 'both';
78export async function getConditional(contracts, address) {
79 const [commitment, batches, remaining] = await Promise.all([
80 conditionalSR.getCommitment(contracts, address),
81 conditionalSR.getBatches(contracts, address),
82 conditionalSR.getRemainingStars(contracts, address),
85 if (batches === [] || commitment.total === '0') {
86 return { total: 0, available: 0, withdrawn: 0, batchLimits: [] };
89 const total = remaining.length;
92 //NOTE we don't strictly need to convert the received number-strings to
93 // integers, but it helps keep the shenanigans at bay.
95 // does not account for forfeiting or withdrawl
98 batches.map((_, idx) =>
99 conditionalSR.getWithdrawLimit(contracts, address, idx)
102 ).map(n => parseInt(n));
105 await conditionalSR.getWithdrawn(contracts, address)
106 ).map(n => parseInt(n));
108 const forfeited = await conditionalSR.getForfeited(contracts, address);
110 const batchLimits = [];
112 for (let i = 0; i < limits.length; i++) {
113 // skip batch if forfeited or no stars left
114 if (forfeited[i] || balance === 0) {
118 const availableInBatch = limits[i] - withdrawn[i];
119 if (balance < availableInBatch) {
120 batchLimits.push(balance);
124 batchLimits.push(availableInBatch);
125 balance = balance - availableInBatch;
128 const withdrawnTotal = withdrawn.reduce((acc, val) => acc + val, 0);
129 const available = batchLimits.reduce((acc, val) => acc + val, 0);
134 withdrawn: withdrawnTotal,
136 approvedTransferTo: commitment.approvedTransferTo,
140export async function getLinear(contracts, address) {
141 const batch = await linearSR.getBatch(contracts, address);
143 //NOTE careful, we get the batch data as strings!
144 const amount = parseInt(batch.amount);
145 const withdrawn = parseInt(batch.withdrawn);
148 return { available: 0, withdrawn: 0, total: 0 };
151 //NOTE be careful, getting the withdraw limit for an address that doesn't
152 // have a lockup throws an exception!
153 const limit = await linearSR.getWithdrawLimit(contracts, address);
154 const remaining = (await linearSR.getRemainingStars(contracts, address))
157 const available = Math.min(limit - withdrawn, remaining);
158 const total = Math.min(amount, remaining);
164 approvedTransferTo: batch.approvedTransferTo,
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