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 getConditional(contracts, address) {
68 const [commitment, batches] = await Promise.all([
69 conditionalSR.getCommitment(contracts, address),
70 conditionalSR.getBatches(contracts, address),
73 if (commitment === null || batches === null) {
74 return { total: 0, available: 0, withdrawn: 0, batchLimits: [] };
77 const { total } = commitment;
78 const withdrawLimits = await Promise.all(
79 batches.map((_, idx) =>
80 conditionalSR.getWithdrawLimit(contracts, address, idx)
83 const available = withdrawLimits.reduce((acc, val) => acc + val, 0);
85 const withdrawnAmounts = await conditionalSR.getWithdrawn(contracts, address);
87 const withdrawn = withdrawnAmounts.reduce((acc, val) => acc + val, 0);
88 const batchLimits = withdrawLimits.map(
89 (limit, idx) => limit - withdrawnAmounts[idx]
92 return { total, available, withdrawn, batchLimits };
95export async function getLinear(contracts, address) {
96 const { amount, withdrawn } = await linearSR.getBatch(contracts, address);
98 const available = // Contract errors on withdrawing from a user that does not have any
99 amount > 0 ? await linearSR.getWithdrawLimit(contracts, address) : 0;
100 return { available, withdrawn, total: amount };
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