Skip to content
This repository has been archived by the owner on Nov 29, 2024. It is now read-only.

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth2207 committed Mar 6, 2024
1 parent 618adad commit 8bdd81f
Show file tree
Hide file tree
Showing 3 changed files with 268 additions and 366 deletions.
69 changes: 37 additions & 32 deletions src/lib/LibTrancheSpaceOrders.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,24 @@
pragma solidity =0.8.19;

import {Vm} from "forge-std/Vm.sol";
import {Strings} from "openzeppelin-contracts/contracts/utils/Strings.sol";
import {Strings} from "openzeppelin-contracts/contracts/utils/Strings.sol";

// STRATEGY PARAMS
uint256 constant TRANCHE_SPACE_PER_SECOND = 11574074e6;
uint256 constant TRANCHE_SPACE_RECHARGE_DELAY = 0;
uint256 constant TRANCHE_SIZE_BASE = 5000e18;
uint256 constant TRANCHE_SIZE_GROWTH = 1e18;
uint256 constant IO_RATIO_BASE = 6e18;
uint256 constant IO_RATIO_BASE = 6e18;
uint256 constant IO_RATIO_GROWTH = 1e17;
uint256 constant MIN_TRANCHE_SPACE_DIFF = 1e17;
uint256 constant TRANCHE_SNAP_THRESHOLD = 1e16;
uint256 constant AMOUNT_IS_OUTPUT = 0;

library LibTrancheSpaceOrders {

using Strings for address;
using Strings for uint256;
using Strings for uint256;

struct TrancheSpaceOrder{
struct TrancheSpaceOrder {
uint256 trancheSpacePerSecond;
uint256 trancheSpaceRechargeDelay;
uint256 trancheSizeBase;
Expand All @@ -36,7 +35,7 @@ library LibTrancheSpaceOrders {
address referenceReserve;
}

struct TestTrancheSpaceOrder{
struct TestTrancheSpaceOrder {
uint256 trancheSpacePerSecond;
uint256 trancheSpaceRechargeDelay;
uint256 trancheSizeBase;
Expand All @@ -55,11 +54,10 @@ library LibTrancheSpaceOrders {
address referenceReserve;
}

function getTrancheSpaceOrder(
Vm vm,
address orderBookSubparser,
TrancheSpaceOrder memory trancheSpaceOrderConfig
) internal returns (bytes memory trancheSpaceOrder) {
function getTrancheSpaceOrder(Vm vm, address orderBookSubparser, TrancheSpaceOrder memory trancheSpaceOrderConfig)
internal
returns (bytes memory trancheSpaceOrder)
{
string[] memory ffi = new string[](45);
ffi[0] = "rain";
ffi[1] = "dotrain";
Expand All @@ -73,7 +71,8 @@ library LibTrancheSpaceOrders {
ffi[9] = "--bind";
ffi[10] = string.concat("tranche-space-per-second=", trancheSpaceOrderConfig.trancheSpacePerSecond.toString());
ffi[11] = "--bind";
ffi[12] = string.concat("tranche-space-recharge-delay=", trancheSpaceOrderConfig.trancheSpaceRechargeDelay.toString());
ffi[12] =
string.concat("tranche-space-recharge-delay=", trancheSpaceOrderConfig.trancheSpaceRechargeDelay.toString());
ffi[13] = "--bind";
ffi[14] = "tranche-size-expr='constant-growth";
ffi[15] = "--bind";
Expand All @@ -89,15 +88,18 @@ library LibTrancheSpaceOrders {
ffi[25] = "--bind";
ffi[26] = string.concat("reference-stable=", trancheSpaceOrderConfig.referenceStable.toHexString());
ffi[27] = "--bind";
ffi[28] = string.concat("reference-stable-decimals=", trancheSpaceOrderConfig.referenceStableDecimals.toString());
ffi[28] =
string.concat("reference-stable-decimals=", trancheSpaceOrderConfig.referenceStableDecimals.toString());
ffi[29] = "--bind";
ffi[30] = string.concat("reference-reserve=", trancheSpaceOrderConfig.referenceReserve.toHexString());
ffi[31] = "--bind";
ffi[32] = string.concat("reference-reserve-decimals=", trancheSpaceOrderConfig.referenceReserveDecimals.toString());
ffi[32] =
string.concat("reference-reserve-decimals=", trancheSpaceOrderConfig.referenceReserveDecimals.toString());
ffi[33] = "--bind";
ffi[34] = string.concat("min-tranche-space-diff=", trancheSpaceOrderConfig.minTrancheSpaceDiff.toString());
ffi[35] = "--bind";
ffi[36] = string.concat("tranche-space-snap-threshold=", trancheSpaceOrderConfig.trancheSpaceSnapThreshold.toString());
ffi[36] =
string.concat("tranche-space-snap-threshold=", trancheSpaceOrderConfig.trancheSpaceSnapThreshold.toString());
ffi[37] = "--bind";
ffi[38] = string.concat("amount-is-output=", trancheSpaceOrderConfig.amountIsOutput.toString());
ffi[39] = "--bind";
Expand Down Expand Up @@ -126,9 +128,12 @@ library LibTrancheSpaceOrders {
ffi[7] = "--entrypoint";
ffi[8] = "handle-io";
ffi[9] = "--bind";
ffi[10] = string.concat("tranche-space-per-second=", testTrancheSpaceOrderConfig.trancheSpacePerSecond.toString());
ffi[10] =
string.concat("tranche-space-per-second=", testTrancheSpaceOrderConfig.trancheSpacePerSecond.toString());
ffi[11] = "--bind";
ffi[12] = string.concat("tranche-space-recharge-delay=", testTrancheSpaceOrderConfig.trancheSpaceRechargeDelay.toString());
ffi[12] = string.concat(
"tranche-space-recharge-delay=", testTrancheSpaceOrderConfig.trancheSpaceRechargeDelay.toString()
);
ffi[13] = "--bind";
ffi[14] = "tranche-size-expr='constant-growth";
ffi[15] = "--bind";
Expand All @@ -144,15 +149,20 @@ library LibTrancheSpaceOrders {
ffi[25] = "--bind";
ffi[26] = string.concat("reference-stable=", testTrancheSpaceOrderConfig.referenceStable.toHexString());
ffi[27] = "--bind";
ffi[28] = string.concat("reference-stable-decimals=", testTrancheSpaceOrderConfig.referenceStableDecimals.toString());
ffi[28] =
string.concat("reference-stable-decimals=", testTrancheSpaceOrderConfig.referenceStableDecimals.toString());
ffi[29] = "--bind";
ffi[30] = string.concat("reference-reserve=", testTrancheSpaceOrderConfig.referenceReserve.toHexString());
ffi[31] = "--bind";
ffi[32] = string.concat("reference-reserve-decimals=", testTrancheSpaceOrderConfig.referenceReserveDecimals.toString());
ffi[32] = string.concat(
"reference-reserve-decimals=", testTrancheSpaceOrderConfig.referenceReserveDecimals.toString()
);
ffi[33] = "--bind";
ffi[34] = string.concat("min-tranche-space-diff=", testTrancheSpaceOrderConfig.minTrancheSpaceDiff.toString());
ffi[35] = "--bind";
ffi[36] = string.concat("tranche-space-snap-threshold=", testTrancheSpaceOrderConfig.trancheSpaceSnapThreshold.toString());
ffi[36] = string.concat(
"tranche-space-snap-threshold=", testTrancheSpaceOrderConfig.trancheSpaceSnapThreshold.toString()
);
ffi[37] = "--bind";
ffi[38] = string.concat("amount-is-output=", testTrancheSpaceOrderConfig.amountIsOutput.toString());
ffi[39] = "--bind";
Expand All @@ -162,15 +172,15 @@ library LibTrancheSpaceOrders {
ffi[43] = "--bind";
ffi[44] = "io-ratio-multiplier='io-ratio-multiplier-identity";
ffi[45] = "--bind";
ffi[46] = string.concat("test-tranche-space-before=", testTrancheSpaceOrderConfig.testTrancheSpaceBefore.toString());
ffi[46] =
string.concat("test-tranche-space-before=", testTrancheSpaceOrderConfig.testTrancheSpaceBefore.toString());
ffi[47] = "--bind";
ffi[48] = string.concat("test-last-update-time=", testTrancheSpaceOrderConfig.testLastTimeUpdate.toString());
ffi[49] = "--bind";
ffi[50] = string.concat("test-now=", testTrancheSpaceOrderConfig.testNow.toString());


testTrancheSpaceOrder = bytes.concat(getSubparserPrelude(orderBookSubparser), vm.ffi(ffi));
}
}

function getCalculateTranche(
Vm vm,
Expand All @@ -179,7 +189,6 @@ library LibTrancheSpaceOrders {
uint256 testLastTimeUpdate,
uint256 testNow
) internal returns (bytes memory calculateTranche) {

string[] memory ffi = new string[](25);
ffi[0] = "rain";
ffi[1] = "dotrain";
Expand All @@ -206,9 +215,8 @@ library LibTrancheSpaceOrders {
ffi[22] = string.concat("test-last-update-time=", testLastTimeUpdate.toString());
ffi[23] = "--bind";
ffi[24] = string.concat("test-now=", testNow.toString());

calculateTranche = bytes.concat(getSubparserPrelude(orderBookSubparser), vm.ffi(ffi));

calculateTranche = bytes.concat(getSubparserPrelude(orderBookSubparser), vm.ffi(ffi));
}

function getHandleIo(
Expand All @@ -218,7 +226,6 @@ library LibTrancheSpaceOrders {
uint256 testLastTimeUpdate,
uint256 testNow
) internal returns (bytes memory calculateTranche) {

string[] memory ffi = new string[](31);
ffi[0] = "rain";
ffi[1] = "dotrain";
Expand Down Expand Up @@ -251,14 +258,12 @@ library LibTrancheSpaceOrders {
ffi[28] = string.concat("test-last-update-time=", testLastTimeUpdate.toString());
ffi[29] = "--bind";
ffi[30] = string.concat("test-now=", testNow.toString());

calculateTranche = bytes.concat(getSubparserPrelude(orderBookSubparser), vm.ffi(ffi));

calculateTranche = bytes.concat(getSubparserPrelude(orderBookSubparser), vm.ffi(ffi));
}

function getSubparserPrelude(address obSubparser) internal pure returns (bytes memory) {
bytes memory RAINSTRING_OB_SUBPARSER = bytes(string.concat("using-words-from ", obSubparser.toHexString(), " "));
return RAINSTRING_OB_SUBPARSER;
}

}
}
}
77 changes: 28 additions & 49 deletions test/TrancheSpaceTest.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ pragma solidity =0.8.19;
import {Vm} from "forge-std/Vm.sol";
import {Test, console2} from "forge-std/Test.sol";
import {IParserV1} from "rain.interpreter.interface/interface/IParserV1.sol";
import {IInterpreterV2,SourceIndexV2} from "rain.interpreter.interface/interface/IInterpreterV2.sol";
import {IInterpreterV2, SourceIndexV2} from "rain.interpreter.interface/interface/IInterpreterV2.sol";
import {IInterpreterStoreV2} from "rain.interpreter.interface/interface/IInterpreterStoreV2.sol";
import {ISubParserV2} from "rain.interpreter.interface/interface/ISubParserV2.sol";
import {IExpressionDeployerV3} from "rain.interpreter.interface/interface/IExpressionDeployerV3.sol";
Expand All @@ -15,15 +15,13 @@ import "rain.math.fixedpoint/lib/LibFixedPointDecimalArithmeticOpenZeppelin.sol"
import "rain.math.fixedpoint/lib/LibFixedPointDecimalScale.sol";
import "rain.interpreter.interface/lib/caller/LibEncodedDispatch.sol";
import "rain.interpreter.interface/lib/ns/LibNamespace.sol";



contract TrancheSpaceTest is Test {
using Strings for address;
using Strings for uint256;

using LibFixedPointDecimalArithmeticOpenZeppelin for uint256;
using LibFixedPointDecimalScale for uint256;
using LibFixedPointDecimalScale for uint256;

IParserV1 public PARSER;
IInterpreterV2 public INTERPRETER;
Expand Down Expand Up @@ -54,7 +52,7 @@ contract TrancheSpaceTest is Test {
INTERPRETER = IInterpreterV2(0xbbe5a04A9a20c47b1A93e755aE712cb84538cd5a);
EXPRESSION_DEPLOYER = IExpressionDeployerV3(0xc64B01aB4b5549dE91e5A4425883Dff87Ceaaf29);
ORDERBOOK_SUPARSER = ISubParserV2(0x14c5D39dE54D498aFD3C803D3B5c88bbEcadcc48);
}
}

function testSpaceModelling() public {
string memory file = "./test/csvs/tranche-space.csv";
Expand All @@ -69,7 +67,8 @@ contract TrancheSpaceTest is Test {
uint256 trancheSpace = uint256(1e17 * i);
address expression;
{
LibTrancheSpaceOrders.TestTrancheSpaceOrder memory testTrancheSpaceOrderConfig = LibTrancheSpaceOrders.TestTrancheSpaceOrder(
LibTrancheSpaceOrders.TestTrancheSpaceOrder memory testTrancheSpaceOrderConfig = LibTrancheSpaceOrders
.TestTrancheSpaceOrder(
TRANCHE_SPACE_PER_SECOND,
TRANCHE_SPACE_RECHARGE_DELAY,
TRANCHE_SIZE_BASE,
Expand All @@ -87,14 +86,12 @@ contract TrancheSpaceTest is Test {
REFERENCE_STABLE,
REFERENCE_RESERVE
);
(bytes memory bytecode, uint256[] memory constants) = PARSER.parse(
LibTrancheSpaceOrders.getTestTrancheSpaceOrder(
vm,
address(ORDERBOOK_SUPARSER),
testTrancheSpaceOrderConfig
(bytes memory bytecode, uint256[] memory constants) = PARSER.parse(
LibTrancheSpaceOrders.getTestTrancheSpaceOrder(
vm, address(ORDERBOOK_SUPARSER), testTrancheSpaceOrderConfig
)
);
(,, expression,) = EXPRESSION_DEPLOYER.deployExpression2(bytecode, constants);
(,, expression,) = EXPRESSION_DEPLOYER.deployExpression2(bytecode, constants);
}
(uint256[] memory sellStack,) = IInterpreterV2(INTERPRETER).eval2(
IInterpreterStoreV2(address(STORE)),
Expand All @@ -104,12 +101,12 @@ contract TrancheSpaceTest is Test {
new uint256[](0)
);

string memory line = string.concat(trancheSpace.toString(), ",", sellStack[1].toString(), ",", sellStack[0].toString());
string memory line =
string.concat(trancheSpace.toString(), ",", sellStack[1].toString(), ",", sellStack[0].toString());

vm.writeLine(file, line);
}

}
vm.writeLine(file, line);
}
}

function testCalculateTranche(uint256 trancheSpaceBefore, uint256 delay) public {
trancheSpaceBefore = bound(trancheSpaceBefore, 0, 100e18);
Expand All @@ -120,20 +117,15 @@ contract TrancheSpaceTest is Test {
LibNamespace.qualifyNamespace(StateNamespace.wrap(uint256(uint160(ORDER_OWNER))), address(this));

uint256[][] memory sellOrderContext = getSellOrderContext(11223344);

address expression;
{

(bytes memory bytecode, uint256[] memory constants) = PARSER.parse(
LibTrancheSpaceOrders.getCalculateTranche(
vm,
address(ORDERBOOK_SUPARSER),
trancheSpaceBefore,
lastTimeUpdate,
lastTimeUpdate + delay
(bytes memory bytecode, uint256[] memory constants) = PARSER.parse(
LibTrancheSpaceOrders.getCalculateTranche(
vm, address(ORDERBOOK_SUPARSER), trancheSpaceBefore, lastTimeUpdate, lastTimeUpdate + delay
)
);
(,, expression,) = EXPRESSION_DEPLOYER.deployExpression2(bytecode, constants);
(,, expression,) = EXPRESSION_DEPLOYER.deployExpression2(bytecode, constants);
}
(uint256[] memory stack,) = IInterpreterV2(INTERPRETER).eval2(
IInterpreterStoreV2(address(STORE)),
Expand All @@ -144,8 +136,7 @@ contract TrancheSpaceTest is Test {
);
assertEq(stack[2], SaturatingMath.saturatingSub(trancheSpaceBefore, stack[4]));
assertEq(stack[3], lastTimeUpdate + delay);

}
}

function testHandleIo(uint256 outputTokenTraded, uint256 trancheSpaceBefore, uint256 delay) public {
outputTokenTraded = bound(outputTokenTraded, 1e18, 1000000e18);
Expand All @@ -161,17 +152,13 @@ contract TrancheSpaceTest is Test {
{
address calculateTrancheExpression;
{

(bytes memory calculateTrancheBytecode, uint256[] memory calculateTrancheConstants) = PARSER.parse(
LibTrancheSpaceOrders.getCalculateTranche(
vm,
address(ORDERBOOK_SUPARSER),
trancheSpaceBefore,
lastTimeUpdate,
lastTimeUpdate + delay
(bytes memory calculateTrancheBytecode, uint256[] memory calculateTrancheConstants) = PARSER.parse(
LibTrancheSpaceOrders.getCalculateTranche(
vm, address(ORDERBOOK_SUPARSER), trancheSpaceBefore, lastTimeUpdate, lastTimeUpdate + delay
)
);
(,, calculateTrancheExpression,) = EXPRESSION_DEPLOYER.deployExpression2(calculateTrancheBytecode, calculateTrancheConstants);
(,, calculateTrancheExpression,) =
EXPRESSION_DEPLOYER.deployExpression2(calculateTrancheBytecode, calculateTrancheConstants);
}
(uint256[] memory calculateTrancheStack,) = IInterpreterV2(INTERPRETER).eval2(
IInterpreterStoreV2(address(STORE)),
Expand All @@ -183,11 +170,7 @@ contract TrancheSpaceTest is Test {

(bytes memory bytecode, uint256[] memory constants) = PARSER.parse(
LibTrancheSpaceOrders.getHandleIo(
vm,
address(ORDERBOOK_SUPARSER),
trancheSpaceBefore,
lastTimeUpdate,
lastTimeUpdate + delay
vm, address(ORDERBOOK_SUPARSER), trancheSpaceBefore, lastTimeUpdate, lastTimeUpdate + delay
)
);
(,, address handleIoExpression,) = EXPRESSION_DEPLOYER.deployExpression2(bytecode, constants);
Expand All @@ -208,7 +191,6 @@ contract TrancheSpaceTest is Test {
new uint256[](0)
);
}

}

function getSellOrderContext(uint256 orderHash) internal pure returns (uint256[][] memory context) {
Expand Down Expand Up @@ -242,8 +224,5 @@ contract TrancheSpaceTest is Test {
context[4] = outputsContext;
}
}
}



}
}
}
Loading

0 comments on commit 8bdd81f

Please sign in to comment.