@@ -65,21 +65,36 @@ function generateLinearWithdrawTxs(contracts, amount, to) {
6565}
6666
6767export async function getConditional(contracts, address) {
68− const [commitment, batches] = await Promise.all([
68+ const [commitment, batches, remaining] = await Promise.all([
6969 conditionalSR.getCommitment(contracts, address),
7070 conditionalSR.getBatches(contracts, address),
71+ conditionalSR.getRemainingStars(contracts, address),
7172 ]);
7273
7374 if (commitment === null || batches === null) {
7475 return { total: 0, available: 0, withdrawn: 0, batchLimits: [] };
7576 }
7677
77− const { total } = commitment;
78− const withdrawLimits = await Promise.all(
79− batches.map((_, idx) =>
80− conditionalSR.getWithdrawLimit(contracts, address, idx)
81− )
82− );
78+ const forfeited = await conditionalSR.getForfeited(contracts, address);
79+ const total = remaining.length;
80+ let balance = total;
81+
82+ const withdrawLimits = await batches.reduce(async (p, _, idx) => {
83+ const acc = await p;
84+ if (forfeited[idx] || balance === 0) {
85+ return [...acc, 0];
86+ }
87+ const limit = await conditionalSR.getWithdrawLimit(contracts, address, idx);
88+ if (limit <= balance) {
89+ balance -= limit;
90+ return [...acc, limit];
91+ }
92+ const remaining = balance;
93+ balance = 0;
94+
95+ return [...acc, remaining];
96+ }, Promise.resolve([]));
97+
8398 const available = withdrawLimits.reduce((acc, val) => acc + val, 0);
8499
85100 const withdrawnAmounts = await conditionalSR.getWithdrawn(contracts, address);
@@ -93,9 +108,18 @@ export async function getConditional(contracts, address) {
93108}
94109
95110export async function getLinear(contracts, address) {
96− const { amount, withdrawn } = await linearSR.getBatch(contracts, address);
111+ const remaining = (await linearSR.getRemainingStars(contracts, address))
112+ .length;
113+ const batch = await linearSR.getBatch(contracts, address);
114+ const { amount, withdrawn } = batch;
115+
116+ // Contract errors on withdrawing from a user that does not have any
117+ const available =
118+ amount > 0
119+ ? Math.min(await linearSR.getWithdrawLimit(contracts, address), remaining)
120+ : 0;
121+
122+ const total = Math.min(amount, remaining);
97123
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 };
124+ return { available, withdrawn, total };
101125}