diff --git a/01-Contracts/contracts/StreamExchange.sol b/01-Contracts/contracts/StreamExchange.sol index 04501f53..5bcd78b3 100644 --- a/01-Contracts/contracts/StreamExchange.sol +++ b/01-Contracts/contracts/StreamExchange.sol @@ -122,7 +122,6 @@ contract StreamExchange is Ownable, SuperAppBase, UsingTellor { newCtx = ctx; // NOTE: Trigger a distribution if there's any inputToken - // console.log("Need to swap this before open new flow",ISuperToken(_exchange.inputToken).balanceOf(address(this))); if (ISuperToken(_exchange.inputToken).balanceOf(address(this)) > 0 && doDistributeFirst) { newCtx = _exchange._distribute(newCtx); } @@ -131,8 +130,6 @@ contract StreamExchange is Ownable, SuperAppBase, UsingTellor { int96 changeInFlowRate = _exchange.cfa.getNetFlow(_exchange.inputToken, address(this)) - _exchange.totalInflow; _exchange.streams[requester].rate = _exchange.streams[requester].rate + changeInFlowRate; - console.log("Requester",requester); - console.log("rate", uint(int(_exchange.streams[requester].rate))); // if (_exchange.streams[requester].rate == 0) { // // Delete the subscription @@ -141,8 +138,8 @@ contract StreamExchange is Ownable, SuperAppBase, UsingTellor { // newCtx = _exchange._deleteSubscriptionWithContext(newCtx, address(this), _exchange.subsidyIndexId, requester, _exchange.subsidyToken); // } else { // Update the subscription - newCtx = _exchange._updateSubscriptionWithContext(newCtx, _exchange.outputIndexId, requester, uint128(uint(int(_exchange.streams[requester].rate))), _exchange.outputToken); - newCtx = _exchange._updateSubscriptionWithContext(newCtx, _exchange.subsidyIndexId, requester, uint128(uint(int(_exchange.streams[requester].rate))), _exchange.subsidyToken); + newCtx = _exchange._updateSubscriptionWithContext(newCtx, _exchange.outputIndexId, requester, uint128(uint(int(_exchange.streams[requester].rate)))/100, _exchange.outputToken); + newCtx = _exchange._updateSubscriptionWithContext(newCtx, _exchange.subsidyIndexId, requester, uint128(uint(int(_exchange.streams[requester].rate)))/100, _exchange.subsidyToken); // } _exchange.totalInflow = _exchange.totalInflow + changeInFlowRate; diff --git a/01-Contracts/test/SteamExchange.test.js b/01-Contracts/test/SteamExchange.test.js index c9f33a45..3ff00e0a 100644 --- a/01-Contracts/test/SteamExchange.test.js +++ b/01-Contracts/test/SteamExchange.test.js @@ -366,8 +366,43 @@ describe("StreamExchange", () => { await daix.transfer(u.bob.address, "3000000000000000000", {from: u.admin.address}); await daix.transfer(u.alice.address, "3000000000000000000", {from: u.admin.address}); - // Set oracle price + const inflowRate = toWad(0.00004000); + // Test 1 streamer + let bobInitialEth = await ethx.balanceOf(u.bob.address) + let bobInitialDai = await daix.balanceOf(u.bob.address) + await u.bob.flow({ flowRate: inflowRate, recipient: u.app }); + // Go forward in time + console.log("Go forward in time") + await traveler.advanceTimeAndBlock(3600*2); await tp.submitValue(1, oraclePrice); + let bobFinalDai = await daix.balanceOf(u.bob.address) + await app.distribute() + let bobFinalEth = await ethx.balanceOf(u.bob.address) + + console.log("Bob Initial ETH:", bobInitialEth.toString()) + console.log("Bob Initial Dai:", bobInitialDai.toString()) + console.log("Bob Final ETH:", bobFinalEth.toString()) + console.log("Bob Final Dai:", bobFinalDai.toString()) + console.log("Exhcange rate:", (bobInitialDai - bobFinalDai) / (bobFinalEth - bobInitialEth)) + + + let aliceInitialEth = await ethx.balanceOf(u.alice.address) + let aliceInitialDai = await daix.balanceOf(u.alice.address) + await u.alice.flow({ flowRate: inflowRate, recipient: u.app }); + // Go forward in time + console.log("Go forward in time") + await traveler.advanceTimeAndBlock(3600*2); + await tp.submitValue(1, oraclePrice); + let aliceFinalDai = await daix.balanceOf(u.alice.address) + await app.distribute() + let aliceFinalEth = await ethx.balanceOf(u.alice.address) + + console.log("alice Initial ETH:", aliceInitialEth.toString()) + console.log("alice Initial Dai:", aliceInitialDai.toString()) + console.log("alice Final ETH:", aliceFinalEth.toString()) + console.log("alice Final Dai:", aliceFinalDai.toString()) + console.log("Exhcange rate:", (aliceInitialDai - aliceFinalDai) / (aliceFinalEth - aliceInitialEth)) + // For holding measurements var appBalances = {ethx: [], daix: [], ric: []} @@ -377,10 +412,10 @@ describe("StreamExchange", () => { // Alice and Bob start streaming to the app - const inflowRate = toWad(0.00004000); await u.bob.flow({ flowRate: inflowRate, recipient: u.app }); await u.alice.flow({ flowRate: inflowRate*2, recipient: u.app }); + ownerBalances.ethx.push(await ethx.balanceOf(u.admin.address)); aliceBalances.ethx.push(await ethx.balanceOf(u.alice.address)); bobBalances.ethx.push(await ethx.balanceOf(u.bob.address)); aliceBalances.daix.push(await daix.balanceOf(u.alice.address)); @@ -389,7 +424,7 @@ describe("StreamExchange", () => { // Go forward in time console.log("Go forward in time") - await traveler.advanceTimeAndBlock(3600*3); + await traveler.advanceTimeAndBlock(3600*5); await tp.submitValue(1, oraclePrice); aliceBalances.daix.push(await daix.balanceOf(u.alice.address)); @@ -404,15 +439,24 @@ describe("StreamExchange", () => { await app.distribute() + ownerBalances.ethx.push(await ethx.balanceOf(u.admin.address)); aliceBalances.ethx.push(await ethx.balanceOf(u.alice.address)); bobBalances.ethx.push(await ethx.balanceOf(u.bob.address)); let bobDeltaEthx = bobBalances.ethx[0] - bobBalances.ethx[1] let aliceDeltaEthx = aliceBalances.ethx[0] - aliceBalances.ethx[1] + let ownerDeltaEthx = ownerBalances.ethx[0] - ownerBalances.ethx[1] + let appBalance = await ethx.balanceOf(app.address) console.log("Exchange bob rate", bobDeltaDaix / bobDeltaEthx) console.log("Exchange alice rate", aliceDeltaDaix / aliceDeltaEthx) - + console.log("App balance", appBalance.toString()) + console.log("Alice delta", aliceDeltaEthx) + console.log("Bob delta", bobDeltaEthx) + console.log("Alice deltaDaix", aliceDeltaDaix) + console.log("Bob deltaDaix", bobDeltaDaix) + console.log("Owner delta", ownerDeltaEthx) + console.log("Fee rate", ownerDeltaEthx / (aliceDeltaEthx + bobDeltaEthx + ownerDeltaEthx)) // Go forward in time console.log("Go forward in time") @@ -435,6 +479,12 @@ describe("StreamExchange", () => { bobDeltaEthx = bobBalances.ethx[1] - bobBalances.ethx[2] aliceDeltaEthx = aliceBalances.ethx[1] - aliceBalances.ethx[2] + console.log("Alice delta", aliceDeltaEthx) + console.log("Bob delta", bobDeltaEthx) + console.log("Alice deltaDaix", aliceDeltaDaix) + console.log("Bob deltaDaix", bobDeltaDaix) + console.log("Owner delta", ownerDeltaEthx) + console.log("Exchange bob rate", bobDeltaDaix / bobDeltaEthx) console.log("Exchange alice rate", aliceDeltaDaix / aliceDeltaEthx)