on master help sign in
Merge pull request #402 from urbit/m/release-deposit-fix

fix partially deposited release contracts

commite15d0c1c2731b4482d72140ba43b93cb28b736d4 authorFang <git@fang.io> dateThu 27 Feb 2020, 13:34 (7 years ago) parenta46fd3625d06ca
3 files changed+39−15
package-lock.json+3−3 package.json+1−1 src/lib/starRelease.js+35−11
package-lock.json+3−3
@@ -2653,9 +2653,9 @@
26532653 }
26542654 },
26552655 "azimuth-js": {
2656− "version": "0.17.0",
2657− "resolved": "https://registry.npmjs.org/azimuth-js/-/azimuth-js-0.17.0.tgz",
2658− "integrity": "sha512-g1/4gfhMHrj6NjCp96c5FBS202nIBnGiXcGTl9i0kh3rB6AE85rd0Un+z+qOhrVJbUdJF0axTvw3xsB5o5u+7Q==",
2656+ "version": "0.18.0",
2657+ "resolved": "https://registry.npmjs.org/azimuth-js/-/azimuth-js-0.18.0.tgz",
2658+ "integrity": "sha512-i8mkXIfWN9GKQcd8KsjQNbf393HVOadIMN6v40KgRnMIjM6dMgUP5zoEbCsG2Zn0SgLRgw0kiNyXHGVitdumiw==",
26592659 "requires": {
26602660 "azimuth-solidity": "1.2.1",
26612661 "ethereumjs-util": "^5.2.0",
package.json+1−1
@@ -19,7 +19,7 @@
1919 "@ledgerhq/hw-transport-u2f": "^5.5.0",
2020 "@welldone-software/why-did-you-render": "^3.2.3",
2121 "async-retry": "^1.2.3",
22− "azimuth-js": "^0.17.0",
22+ "azimuth-js": "^0.18.0",
2323 "azimuth-solidity": "^1.2.0",
2424 "bip32": "^1.0.2",
2525 "bip39": "^2.5.0",
src/lib/starRelease.js+35−11
@@ -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}