|
| 1 | +const { buildScenarios } = require('../util/scenario'); |
| 2 | + |
| 3 | +let now = Date.now(); |
| 4 | + |
| 5 | +let cash_scen_info = { |
| 6 | + tokens: [ |
| 7 | + { token: 'usdc', balances: { ashley: 1000 } }, |
| 8 | + { token: 'zrx', balances: { bert: 1000000 } }, |
| 9 | + { token: 'comp' } |
| 10 | + ], |
| 11 | + validators: ['alice', 'bob'], |
| 12 | + freeze_time: now, |
| 13 | + initial_yield: 300, |
| 14 | + initial_yield_start_ms: now |
| 15 | +}; |
| 16 | + |
| 17 | +buildScenarios('Cash Scenarios', cash_scen_info, [ |
| 18 | + { |
| 19 | + name: 'Cash Interest', |
| 20 | + scenario: async ({ ashley, bert, cash, chain, usdc }) => { |
| 21 | + await ashley.lock(1000, usdc); |
| 22 | + await ashley.transfer(10, cash, bert); |
| 23 | + expect(await ashley.chainBalance(usdc)).toEqual(1000); |
| 24 | + expect(await ashley.chainBalance(cash)).toEqual(-10.01); // $10 + 1¢ transfer fee |
| 25 | + expect(await bert.chainBalance(cash)).toEqual(10); |
| 26 | + await chain.accelerateTime({years: 1}); |
| 27 | + expect(await ashley.chainBalance(usdc)).toEqual(1000); |
| 28 | + expect(await ashley.chainBalance(cash)).toBeCloseTo(-10.314849, 4); // $10.01 @ 3% for 1 Year Continously Compounding |
| 29 | + expect(await bert.chainBalance(cash)).toBeCloseTo(10.304545, 4); // // $10 @ 3% for 1 Year Continously Compounding |
| 30 | + } |
| 31 | + }, |
| 32 | + { |
| 33 | + name: 'Collateral Borrowed Interest Lump Sum', |
| 34 | + scenario: async ({ ashley, bert, chuck, cash, chain, usdc, zrx }) => { |
| 35 | + await chain.setFixedRate(usdc, 500); // 5% APY fixed |
| 36 | + await bert.lock(1000000, zrx); |
| 37 | + await bert.transfer(1000, usdc, chuck); |
| 38 | + expect(await bert.chainBalance(usdc)).toEqual(-1000); |
| 39 | + expect(await chuck.chainBalance(usdc)).toEqual(1000); |
| 40 | + expect(await bert.chainBalance(cash)).toEqual(-0.01); // 1¢ transfer fee |
| 41 | + expect(await chuck.chainBalance(cash)).toEqual(0); |
| 42 | + await chain.accelerateTime({years: 1}); |
| 43 | + expect(await bert.chainBalance(usdc)).toEqual(-1000); |
| 44 | + expect(await chuck.chainBalance(usdc)).toEqual(1000); |
| 45 | + expect(await bert.chainBalance(cash)).toBeCloseTo(-51.53272669767585, 3); // -50 * Math.exp(0.03) - 0.01 |
| 46 | + expect(await chuck.chainBalance(cash)).toBeCloseTo(51.52272669767585, 3); // 50 * Math.exp(0.03) |
| 47 | + } |
| 48 | + }, |
| 49 | + { |
| 50 | + name: 'Collateral Borrowed Interest 12-Month Chunked', |
| 51 | + scenario: async ({ ashley, bert, chuck, cash, chain, usdc, zrx }) => { |
| 52 | + await chain.setFixedRate(usdc, 500); // 5% APY fixed |
| 53 | + await bert.lock(1000000, zrx); |
| 54 | + await bert.transfer(1000, usdc, chuck); |
| 55 | + expect(await bert.chainBalance(usdc)).toEqual(-1000); |
| 56 | + expect(await chuck.chainBalance(usdc)).toEqual(1000); |
| 57 | + expect(await bert.chainBalance(cash)).toEqual(-0.01); // 1¢ transfer fee |
| 58 | + expect(await chuck.chainBalance(cash)).toEqual(0); |
| 59 | + for (const i in [...new Array(12)]) { |
| 60 | + await chain.accelerateTime({months: 1}); |
| 61 | + } |
| 62 | + expect(await bert.chainBalance(usdc)).toEqual(-1000); |
| 63 | + expect(await chuck.chainBalance(usdc)).toEqual(1000); |
| 64 | + expect(await bert.chainBalance(cash)).toBeCloseTo(-50.79, 1); // ~ -50*(1+0.015) - 0.01 |
| 65 | + expect(await chuck.chainBalance(cash)).toBeCloseTo(50.78, 1); // ~ -50*(1+0.015) |
| 66 | + } |
| 67 | + }, |
| 68 | + { |
| 69 | + name: 'Multi-Collateral and Cash Netting', |
| 70 | + scenario: async ({ ashley, bert, chuck, cash, chain, comp, usdc, zrx }) => { |
| 71 | + await chain.setFixedRate(usdc, 500); // 5% APY fixed |
| 72 | + await chain.setFixedRate(comp, 1000); // 10% APY fixed |
| 73 | + await bert.lock(1000000, zrx); |
| 74 | + await bert.transfer(300.01, cash, chuck); |
| 75 | + await bert.transfer(1000, usdc, chuck); |
| 76 | + await chuck.transfer(1, comp, bert); |
| 77 | + // Chuck has +1000 USDC @ 5% [Price=$1] [Util=100%] |
| 78 | + // Chuck has -1 COMP @ 10% [Price=$229.125] [Util=100%] |
| 79 | + // Chuck has 300 Cash @ 3% APY |
| 80 | + expect(await chuck.chainBalance(usdc)).toEqual(1000); |
| 81 | + expect(await chuck.chainBalance(comp)).toEqual(-1); |
| 82 | + expect(await chuck.chainBalance(cash)).toEqual(300); |
| 83 | + await chain.accelerateTime({years: 1}); |
| 84 | + expect(await chuck.chainBalance(usdc)).toEqual(1000); |
| 85 | + expect(await chuck.chainBalance(comp)).toEqual(-1); |
| 86 | + /* |
| 87 | + { Cash } { USDC Interest } { Comp Interest } { Cash APY } |
| 88 | +
|
| 89 | + ( 300 + 1000 * 1 * 0.05 - 1 * 229.125 * 0.1 ) * Math.exp(0.03) |
| 90 | + */ |
| 91 | + expect(await chuck.chainBalance(cash)).toBeCloseTo(337.048797374521, 3); |
| 92 | + } |
| 93 | + } |
| 94 | +]); |
0 commit comments