Skip to content

Commit

Permalink
update strat
Browse files Browse the repository at this point in the history
  • Loading branch information
Siddharth2207 committed Jul 8, 2024
1 parent c0fee29 commit 96d1cca
Show file tree
Hide file tree
Showing 3 changed files with 190 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,14 @@ subgraphs:
base-community: https://api.thegraph.com/subgraphs/name/h20liquidity/base-0x2aee87

orderbooks:
base:
base-community:
address: 0x2AeE87D75CD000583DAEC7A28db103B1c0c18b76
network: base-community
subgraph: base-community

deployers:
base:
address: 0xfca89cD12Ba1346b1ac570ed988AB43b812733fe
base-community:
address: 0x56394785a22b3BE25470a0e03eD9E0a939C47b9b
network: base-community

tokens:
Expand All @@ -44,18 +44,18 @@ orders:
orderbook: base-community
inputs:
- token: base-usdc
vault-id:
vault-id: 0x7f18c87908babda92ceaaf2fd5539a175360b25322939d8ef3682d14479c4c94
outputs:
- token: base-wlth
vault-id:
vault-id: 0x7f18c87908babda92ceaaf2fd5539a175360b25322939d8ef3682d14479c4c94
base-wlth-buy:
orderbook: base-community
inputs:
- token: base-wlth
vault-id:
vault-id: 0x007f0b0501c677a80add43e246b5ab8999703a6d5fc3fd3eb025685426ea5a65
outputs:
- token: base-usdc
vault-id:
vault-id: 0x007f0b0501c677a80add43e246b5ab8999703a6d5fc3fd3eb025685426ea5a65

scenarios:
base-wlth-dca-univ3:
Expand All @@ -64,11 +64,11 @@ scenarios:
orderbook: base-community
bindings:
# Ask for now, registry in future.
uniswap-words: 0xbc609623F5020f6Fc7481024862cD5EE3FFf52D7
orderbook-subparser: 0x8f037f2a3fF2dee510486D9C63A47A245991a4C1
uniswap-words: 0xD6B34F97d4A8Cb38D0544dB241CB3f335866f490
orderbook-subparser: 0x8D96ea3EF24D7123882c51CE4325b89bc0d63f9e

# Uniswap V3 factory addresses and init code
uniswap-v3-factory: '[uniswap-v3-factory]'
uniswap-v3-factory: 0x33128a8fC17869897dcE68Ed026d694621f6FDfD
uniswap-v3-init-code: '[uniswap-v3-init-code]'

# Twap fee for tokens
Expand Down Expand Up @@ -96,7 +96,7 @@ scenarios:
buy:
bindings:
# Input token, this is the token being received.
input-token: 0x6ab4E20f36ca48B61ECd66c0450fDf665Fa130be
input-token: 0x99b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D

# Output token, this is the token that is being offered.
# Output token, mean amount
Expand All @@ -105,13 +105,13 @@ scenarios:
mean-output-amount : 10

# Output token, this is the token being sold
output-token: 0xc2132D05D31c914a87C6611C10748AEb04B58e8F
output-token: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913

# Reserve token, If the output token is paired with the input token on a uniswap v3,
# then the reserve is the input token.
# If the output token is NOT paired with the input token on a uniswap v3 pool,
# then the reserve address is the token which the ouput token is paired with.
reserve-token: 0x6ab4E20f36ca48B61ECd66c0450fDf665Fa130be
reserve-token: 0x99b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D

# Quote and Twap multiplier to get quote and 30min twap for input-output token pair.
# If the output token is paired with the input token on a uniswap v3,
Expand All @@ -122,9 +122,9 @@ scenarios:
twap-multiplier: '''twap-multiplier-identity'

# Params for bounty auction which is input token denominated. Min price, max price, time in seconds and unit increase
bounty-min: 1.1
bounty-max: 27
bounty-unit-increase: 0.001
bounty-min: 1.5
bounty-max: 25
bounty-unit-increase: 1
bounty-unit-time: 60

# Cooldown between two trades in seconds. 3600, default, is 1 hour.
Expand All @@ -144,7 +144,7 @@ scenarios:

# Minimum io-ratio for the order.
# Strategy will not buy output token with input token for a lower ratio.
min-ratio: 80
min-ratio: 20
scenarios:
prod:
bindings:
Expand All @@ -158,7 +158,7 @@ scenarios:
enforce-twap-check: '''enforce-twap-check-chart'
ensure-min-ratio: '''ensure-min-ratio-chart'
ensure-cooldown: '''ensure-cooldown-chart'
plottables: '''plottables-chart'
plottables: '''plottables-prod'
metric:
runs: 1
bindings:
Expand All @@ -178,21 +178,21 @@ scenarios:
sell:
bindings:
# Input token, this is the token being received.
input-token: 0xc2132D05D31c914a87C6611C10748AEb04B58e8F
input-token: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913

# Output token, this is the token that is being offered.
# Amount of output tokens offered per trade.
# For gas expensive networks mean amount needs to be higher to compensate for gas fee and bounty.
mean-output-amount : 900
mean-output-amount : 2500

# Output token, this is the token being bought
output-token: 0x6ab4E20f36ca48B61ECd66c0450fDf665Fa130be
output-token: 0x99b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D

# Reserve token, If the output token is paired with the input token on a uniswap v3,
# then the reserve is the input token.
# If the output token is NOT paired with the input token on a uniswap v3 pool,
# then the reserve address is the token which the ouput token is paired with.
reserve-token: 0xc2132D05D31c914a87C6611C10748AEb04B58e8F
reserve-token: 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913

# Quote and Twap multiplier to get quote and 30min twap for input-output token pair.
# If the output token is paired with the input token on a uniswap v3,
Expand All @@ -204,7 +204,7 @@ scenarios:

# Params for bounty auction which is input token denominated.
bounty-min: 0.012
bounty-max: 0.3
bounty-max: 0.5
bounty-unit-increase: 0.01
bounty-unit-time: 60

Expand All @@ -227,7 +227,7 @@ scenarios:
# Minimum io-ratio for the order.
# Strategy will not buy input token with output token for a higher ratio.
# Can also be laelled min sell price.
min-ratio: 0.01
min-ratio: 0.005
scenarios:
prod:
bindings:
Expand All @@ -241,7 +241,7 @@ scenarios:
enforce-twap-check: '''enforce-twap-check-chart'
ensure-min-ratio: '''ensure-min-ratio-chart'
ensure-cooldown: '''ensure-cooldown-chart'
plottables: '''plottables-chart'
plottables: '''plottables-prod'
metric:
runs: 1
bindings:
Expand Down Expand Up @@ -520,13 +520,14 @@ deployments:
:call<'enforce-twap-check>(twap-ratio);

#ensure-cooldown-metric
_ _: block-timestamp() 0;
_ _: now() 0;

#ensure-cooldown-chart
_ _: test-current-time test-last-time;
current-time: test-current-time,
last-time: sub(current-time mod(current-time 86400));

#ensure-cooldown-prod
current-time: block-timestamp(),
current-time: now(),
last-time: get(hash(order-hash() cooldown-key)),
:ensure(
greater-than-or-equal-to(
Expand Down
2 changes: 1 addition & 1 deletion strategies/wlth/wlth-grid-trading.rain
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ scenarios:
# Uniswap v3 factory contract address and corresponding init code hash.
# If the factory address and the init code aren't available as a literals
# then non-literal address and hash value can be given as bindings.
uniswap-v3-factory: '[uniswap-v3-factory]'
uniswap-v3-factory: 0x33128a8fC17869897dcE68Ed026d694621f6FDfD
uniswap-v3-init-code: '[uniswap-v3-init-code]'

# Orderbook subparser for orderbook words.
Expand Down
160 changes: 160 additions & 0 deletions test/wlth/WlthDcaOracleUniv3Test.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// SPDX-License-Identifier: CAL
pragma solidity =0.8.25;
import {console2, Test} from "forge-std/Test.sol";

import {
IOrderBookV3,
IO,
OrderV2,
OrderConfigV2,
TakeOrderConfigV2,
TakeOrdersConfigV2
} from "rain.orderbook.interface/interface/IOrderBookV3.sol";
import {IOrderBookV3ArbOrderTaker} from "rain.orderbook.interface/interface/IOrderBookV3ArbOrderTaker.sol";
import {IParserV1} from "rain.interpreter.interface/interface/IParserV1.sol";
import {IExpressionDeployerV3} from "rain.interpreter.interface/interface/IExpressionDeployerV3.sol";
import { EvaluableConfigV3, SignedContextV1} from "rain.interpreter.interface/interface/IInterpreterCallerV2.sol";
import {IInterpreterV2,SourceIndexV2} from "rain.interpreter.interface/interface/IInterpreterV2.sol";
import {IInterpreterStoreV2} from "rain.interpreter.interface/interface/IInterpreterStoreV2.sol";
import {StrategyTests, IRouteProcessor, LibStrategyDeployment, LibComposeOrders} from "h20.test-std/StrategyTests.sol";
import {LibEncodedDispatch} from "rain.interpreter.interface/lib/caller/LibEncodedDispatch.sol";
import {StateNamespace, LibNamespace, FullyQualifiedNamespace} from "rain.interpreter.interface/lib/ns/LibNamespace.sol";
import {Strings} from "openzeppelin-contracts/contracts/utils/Strings.sol";
import {SafeERC20, IERC20} from "openzeppelin-contracts/contracts/token/ERC20/utils/SafeERC20.sol";
import "h20.test-std/lib/LibProcessStream.sol";

uint256 constant VAULT_ID = uint256(keccak256("vault"));

/// @dev https://basescan.org/address/0x99b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D
IERC20 constant BASE_WLTH= IERC20(0x99b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D);

/// @dev https://basescan.org/address/0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913
IERC20 constant BASE_USDC = IERC20(0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913);

function baseWlthIo() pure returns (IO memory) {
return IO(address(BASE_WLTH), 18, VAULT_ID);
}

function baseUsdcIo() pure returns (IO memory) {
return IO(address(BASE_USDC), 6, VAULT_ID);
}

contract WlthDcaOracleUniv3Test is StrategyTests {

using SafeERC20 for IERC20;
using Strings for address;

uint256 constant FORK_BLOCK_NUMBER = 16825797;

function selectFork() internal {
uint256 fork = vm.createFork(vm.envString("RPC_URL_BASE"));
vm.selectFork(fork);
vm.rollFork(FORK_BLOCK_NUMBER);
}

function getNamespace() public view returns (FullyQualifiedNamespace) {
return LibNamespace.qualifyNamespace(StateNamespace.wrap(0), address(this));
}

function setUp() public {
selectFork();

PARSER = IParserV1(0xF836f2746B407136a5bCB515495949B1edB75184);
STORE = IInterpreterStoreV2(0x6E4b01603edBDa617002A077420E98C86595748E);
INTERPRETER = IInterpreterV2(0x379b966DC6B117dD47b5Fc5308534256a4Ab1BCC);
EXPRESSION_DEPLOYER = IExpressionDeployerV3(0x56394785a22b3BE25470a0e03eD9E0a939C47b9b);
ORDERBOOK = IOrderBookV3(0x2AeE87D75CD000583DAEC7A28db103B1c0c18b76);
ARB_INSTANCE = IOrderBookV3ArbOrderTaker(0x199b22ce0c9fD88476cCaA2d2aB253Af38BAE3Ae);
ROUTE_PROCESSOR = IRouteProcessor(address(0x83eC81Ae54dD8dca17C3Dd4703141599090751D1));
EXTERNAL_EOA = address(0x654FEf5Fb8A1C91ad47Ba192F7AA81dd3C821427);
APPROVED_EOA = address(0x669845c29D9B1A64FFF66a55aA13EB4adB889a88);
ORDER_OWNER = address(0x19f95a84aa1C48A2c6a7B2d5de164331c86D030C);

EXTERNAL_EOA = address(0x654FEf5Fb8A1C91ad47Ba192F7AA81dd3C821427);
APPROVED_EOA = address(0x669845c29D9B1A64FFF66a55aA13EB4adB889a88);
ORDER_OWNER = address(0x19f95a84aa1C48A2c6a7B2d5de164331c86D030C);
}

function testWlthDcaOracleUniV3Buy() public {
// Input vaults
IO[] memory inputVaults = new IO[](1);
inputVaults[0] = baseWlthIo();

// Output vaults
IO[] memory outputVaults = new IO[](1);
outputVaults[0] = baseUsdcIo();

uint256 expectedRatio = 39.505651241666644068e18;
uint256 expectedAmountOutputMax = 10.853037074011243840e18;

LibStrategyDeployment.StrategyDeployment memory strategy = LibStrategyDeployment.StrategyDeployment(
getEncodedSellWlthRoute(address(ARB_INSTANCE)),
getEncodedBuyWlthRoute(address(ARB_INSTANCE)),
0,
0,
1e18,
10000e6,
expectedRatio,
expectedAmountOutputMax,
"strategies/wlth/wlth-dca-oracle-univ3.rain",
"base-wlth-dca-univ3.buy.deviation.prod",
"./lib/h20.test-std/lib/rain.orderbook",
"./lib/h20.test-std/lib/rain.orderbook/Cargo.toml",
inputVaults,
outputVaults
);

// OrderBook 'takeOrder'
checkStrategyCalculationsArbOrder(strategy);
}

function testWlthDcaOracleUniV3Sell() public {
// Input vaults
IO[] memory inputVaults = new IO[](1);
inputVaults[0] = baseUsdcIo();

// Output vaults
IO[] memory outputVaults = new IO[](1);
outputVaults[0] = baseWlthIo();

uint256 expectedRatio = 0.023566726830084264e18;
uint256 expectedAmountOutputMax = 2713.259268502810960000e18;

LibStrategyDeployment.StrategyDeployment memory strategy = LibStrategyDeployment.StrategyDeployment(
getEncodedBuyWlthRoute(address(ARB_INSTANCE)),
getEncodedSellWlthRoute(address(ARB_INSTANCE)),
0,
0,
1e6,
10000e18,
expectedRatio,
expectedAmountOutputMax,
"strategies/wlth/wlth-dca-oracle-univ3.rain",
"base-wlth-dca-univ3.sell.deviation.prod",
"./lib/h20.test-std/lib/rain.orderbook",
"./lib/h20.test-std/lib/rain.orderbook/Cargo.toml",
inputVaults,
outputVaults
);

// OrderBook 'takeOrder'
checkStrategyCalculationsArbOrder(strategy);
}

function getEncodedBuyWlthRoute(address toAddress) internal pure returns (bytes memory) {
bytes memory ROUTE_PRELUDE =
hex"02833589fCD6eDb6E08f4c7C32D4f71b54bdA0291301ffff011536EE1506e24e5A36Be99C73136cD82907A902E01";

return abi.encode(bytes.concat(ROUTE_PRELUDE, abi.encodePacked(address(toAddress))));
}

function getEncodedSellWlthRoute(address toAddress) internal pure returns (bytes memory) {
bytes memory ROUTE_PRELUDE =
hex"0299b2B1A2aDB02B38222ADcD057783D7e5D1FCC7D01ffff011536EE1506e24e5A36Be99C73136cD82907A902E00";

return abi.encode(bytes.concat(ROUTE_PRELUDE, abi.encodePacked(address(toAddress))));
}



}

0 comments on commit 96d1cca

Please sign in to comment.