-
Notifications
You must be signed in to change notification settings - Fork 33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
added accumulateProof fn #51
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could you pls run
npm run lint
and
npm run format
@@ -95,11 +95,63 @@ export function checkResponseError(err: unknown) { | |||
} | |||
} | |||
} | |||
|
|||
function accumulateProofs( | |||
proofs: Proof[], |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's use the Array<> notation, for consistency. 😅
case 'middle': { | ||
while (temp.length && total < requiredAmount) { | ||
const first = temp.shift(); | ||
total += first!.amount; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just to make the linter (and me) happy
case 'middle':
while (total < requiredAmount) {
const first = temp.shift();
if (!first) { break }
total += first.amount;
result.push(first);
if (total >= requiredAmount) {
break;
}
const last = temp.pop();
if (!last) { break }
total += last.amount;
result.push(last);
}
break;
strategy: 'middle' | 'ascending' | 'descending' | ||
) { | ||
const result: Proof[] = []; | ||
const temp = proofs.slice().sort((a, b) => a.amount - b.amount); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not
const temp = proofs.slice()
if (strategy === 'descending') {
temp.sort((a, b) => b.amount - a.amount);
} else {
temp.sort((a, b) => a.amount - b.amount);
}
and than
case 'descending':
case 'ascending':
for (let i = 0; i < temp.length; i++) {
total += temp[i].amount;
result.push(temp[i]);
if (total >= requiredAmount) {
break;
}
}
break;
eslint yells at me for using |
We can expose this function on |
@BilligsterUser @Egge7 what shall we do with this PR? Has everything been resolved? |
I´m still waiting for @Egge7 |
@Egge21M what you wanna do with this? |
Added a utility function that will iterate over an array of proofs and gather them until a certain amount is reached. The function will take a strategy argument, which determines in which order proofs are selected:
The function returns an object with three properties: