test-amountMath.js
1 // @ts-check 2 /* eslint-disable import/order */ 3 import { describe } from './prepare-riteway.js'; 4 import { AmountWrapper } from '../src/shared/utils/amountMath.js'; 5 import { AmountMath, makeIssuerKit } from '@agoric/ertp'; 6 7 const usdcKit = makeIssuerKit('USDC'); 8 9 const usdc = x => AmountMath.make(usdcKit.brand, x); 10 11 const AmountUSDC = x => AmountWrapper(usdc(x)); 12 13 const testData = [ 14 10n, 15 50n, 16 60n, 17 10n, 18 20n, 19 55n, 20 240n, 21 43n, 22 6000n, 23 20n, 24 40n, 25 50n, 26 10_000n, 27 41n, 28 12_050n 29 ]; 30 31 const testUsdcAmounts = testData.map(AmountUSDC); 32 33 /** 34 * Description placeholder 35 * @date 7/18/2023 - 1:25:11 AM 36 * 37 * @param {*} x 38 * @param {*} y 39 * @returns {*} 40 */ 41 const adder = (x, y) => x + y; 42 /** 43 * Description placeholder 44 * @date 7/18/2023 - 1:25:11 AM 45 * 46 * @param {*} data 47 * @returns {*} 48 */ 49 const sum = data => data.reduce(adder, 0n); 50 51 describe('max :: Amount ', async assert => { 52 const ex1 = AmountUSDC(10n); 53 const ex2 = AmountUSDC(40n); 54 assert({ 55 given: '2 new Amounts', 56 should: 'return the greater of the two', 57 actual: ex1.max(ex2).value, 58 expected: usdc(40n) 59 }); 60 const [fst, snd] = testUsdcAmounts; 61 62 assert({ 63 given: 'the first 2 Amounts from testUsdcAmounts array', 64 should: 'return the greater of the two', 65 actual: fst.max(snd).value, 66 expected: usdc(50n) 67 }); 68 69 const findMax = testUsdcAmounts.reduce((acc, val) => acc.max(val)); 70 71 assert({ 72 given: 'a list of Amounts', 73 should: 'return the largest amount in the list', 74 actual: findMax.value, 75 expected: usdc(12_050n) 76 }); 77 }); 78 79 describe('min :: Amount ', async assert => { 80 const ex1 = AmountUSDC(10n); 81 const ex2 = AmountUSDC(40n); 82 assert({ 83 given: '2 new Amounts', 84 should: 'return the smaller of the two', 85 actual: ex1.min(ex2).value, 86 expected: usdc(10n) 87 }); 88 const [fst, snd] = testUsdcAmounts; 89 90 assert({ 91 given: 'the first 2 Amounts from testUsdcAmounts array', 92 should: 'return the smaller of the two', 93 actual: fst.min(snd).value, 94 expected: usdc(10n) 95 }); 96 97 const findMin = testUsdcAmounts.reduce((acc, val) => acc.min(val)); 98 99 assert({ 100 given: 'a list of Amounts', 101 should: 'return the largest amount in the list', 102 actual: findMin.value, 103 expected: usdc(10n) 104 }); 105 }); 106 107 describe('add :: Amount', async assert => { 108 assert({ 109 given: 'two new Amounts', 110 should: 'return the sum of both amounts', 111 actual: AmountUSDC(10n).add(AmountUSDC(50n)).value, 112 expected: usdc(60n) 113 }); 114 const result = testData.reduce( 115 (acc, val) => acc.add(AmountUSDC(val)), 116 AmountUSDC(0n) 117 ); 118 119 const sumOfTestInts = sum(testData); 120 121 assert({ 122 given: 'a list of USDC Amounts', 123 should: 'return the sum of all amounts in the list', 124 actual: result.value, 125 expected: usdc(sumOfTestInts) 126 }); 127 }); 128 129 describe('subtract :: Amount', async assert => { 130 assert({ 131 given: 'two values', 132 should: 'return the difference', 133 actual: AmountUSDC(100n).subtract(AmountUSDC(25n)).value, 134 expected: usdc(75n) 135 }); 136 });