Skip to content

Commit

Permalink
fix: encapsulate natSafeMath.isGTE
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelfig committed Nov 5, 2020
1 parent 738406d commit 658c223
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 6 deletions.
1 change: 1 addition & 0 deletions packages/zoe/src/contractSupport/safeMath.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,5 @@ export const natSafeMath = harden({
multiply: (x, y) => Nat(x * y),
floorDivide: (x, y) => Nat(Math.floor(x / y)),
ceilDivide: (x, y) => Nat(Math.ceil(x / y)),
isGTE: (x, y) => x >= y,
});
9 changes: 5 additions & 4 deletions packages/zoe/src/contractSupport/statistics.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
* @typedef {Object} TypedMath
* @property {(a: T, b: T) => T} add
* @property {(a: T, b: T) => T} divide
* @property {(a: T, b: T) => boolean} isGTE
*/

/**
Expand All @@ -15,12 +16,12 @@
*/
export const calculateMedian = (samples, math) => {
const sorted = samples.sort((a, b) => {
if (a < b) {
if (!math.isGTE(a, b)) {
return -1;
} else if (a === b) {
return 0;
} else if (!math.isGTE(b, a)) {
return 1;
}
return 1;
return 0;
});

if (sorted.length === 0) {
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/contracts/priceAggregator.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
import '../../exported';
import './exported';

const { add, multiply, floorDivide, ceilDivide } = natSafeMath;
const { add, multiply, floorDivide, ceilDivide, isGTE } = natSafeMath;

/**
* This contract aggregates price values from a set of oracles and provides a
Expand Down Expand Up @@ -163,7 +163,7 @@ const start = async zcf => {
const updateQuote = async (samples, timestamp) => {
const median = calculateMedian(
samples.filter(sample => sample > 0 && Number.isSafeInteger(sample)),
{ add, divide: floorDivide },
{ add, divide: floorDivide, isGTE },
);

// console.error('found median', median, 'of', samples);
Expand Down

0 comments on commit 658c223

Please sign in to comment.