Skip to content

Commit

Permalink
feat: lookup + depositIntoStrategy
Browse files Browse the repository at this point in the history
  • Loading branch information
loicttn committed Feb 2, 2024
1 parent 5169434 commit 9748632
Show file tree
Hide file tree
Showing 10 changed files with 342 additions and 30 deletions.
52 changes: 47 additions & 5 deletions src/contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,9 @@ static const uint32_t KILN_LR_DEPOSIT_INTO_STRATEGY_SELECTOR = 0xe7a050aa;
static const uint32_t KILN_LR_QUEUE_WITHDRAWAL_SELECTOR = 0xf123991e;
// --- cast sig "queueWithdrawals((address[],uint256[],address)[])"
static const uint32_t KILN_LR_QUEUE_WITHDRAWALS_SELECTOR = 0x0dd8dd02;
// --- cast sig "completeQueuedWithdrawal((address,address,address,uint256,uint32,address[],uint256[]),address[],uint256,bool)"
static const uint32_t KILN_LR_COMPLETE_QUEUED_WITHDRAWAL_SELECTOR = 0x60d7faed;
// --- cast sig "completeQueuedWithdrawals((address,address,address,uint256,uint32,address[],uint256[])[],address[][],uint256[],bool[])"
static const uint32_t KILN_LR_COMPLETE_QUEUED_WITHDRAWALS_SELECTOR = 0x33404396;
// --- cast sig "completeQueuedWithdrawal((address[],uint256[],address,(address,uint96),uint32,address),address[],uint256,bool)"
static const uint32_t KILN_LR_COMPLETE_QUEUED_WITHDRAWAL_SELECTOR = 0xf3be65d3;



const uint32_t KILN_SELECTORS[NUM_SELECTORS] = {
Expand All @@ -64,5 +63,48 @@ const uint32_t KILN_SELECTORS[NUM_SELECTORS] = {
KILN_LR_QUEUE_WITHDRAWAL_SELECTOR,
KILN_LR_QUEUE_WITHDRAWALS_SELECTOR,
KILN_LR_COMPLETE_QUEUED_WITHDRAWAL_SELECTOR,
KILN_LR_COMPLETE_QUEUED_WITHDRAWALS_SELECTOR
};



const char lr_strategy_addresses[LR_STRATEGIES_COUNT][ADDRESS_STR_LEN] = {
"0x54945180dB7943c0ed0FEE7EdaB2Bd24620256bc", // cbETH
"0x93c4b944D05dfe6df7645A86cd2206016c51564D", // stETH
"0x1BeE69b7dFFfA4E2d53C2a2Df135C388AD25dCD2", // rETH
"0x9d7eD45EE2E8FC5482fa2428f15C971e6369011d", // ETHx
"0x13760F50a9d7377e4F20CB8CF9e4c26586c658ff", // ankrETH
"0xa4C637e0F704745D182e4D38cAb7E7485321d059", // OETH
"0x57ba429517c3473B6d34CA9aCd56c0e735b94c02", // osETH
"0x0Fe4F44beE93503346A3Ac9EE5A26b130a5796d6", // swETH
"0x7CA911E83dabf90C90dD3De5411a10F1A6112184", // wBETH
"0x8CA7A5d6f3acd3A7A8bC468a8CD0FB14B6BD28b6", // sfrxETH
"0x298aFB19A105D59E74658C4C334Ff360BadE6dd2" // mETH
};

const char lr_erc20_addresses[LR_STRATEGIES_COUNT][ADDRESS_STR_LEN] = {
"0xBe9895146f7AF43049ca1c1AE358B0541Ea49704", // cbETH
"0xae7ab96520DE3A18E5e111B5EaAb095312D7fE84", // stETH
"0xae78736Cd615f374D3085123A210448E74Fc6393", // rETH
"0xA35b1B31Ce002FBF2058D22F30f95D405200A15b", // ETHx
"0xE95A203B1a91a908F9B9CE46459d101078c2c3cb", // ankrETH
"0x856c4Efb76C1D1AE02e20CEB03A2A6a08b0b8dC3", // OETH
"0xf1C9acDc66974dFB6dEcB12aA385b9cD01190E38", // osETH
"0xf951E335afb289353dc249e82926178EaC7DEd78", // swETH
"0xa2E3356610840701BDf5611a53974510Ae27E2e1", // wBETH
"0xac3E018457B222d93114458476f3E3416Abbe38F", // sfrxETH
"0xd5F7838F5C461fefF7FE49ea5ebaF7728bB0ADfa", // mETH
};

const char lr_tickers[LR_STRATEGIES_COUNT][MAX_TICKER_LEN] = {
"cbETH",
"stETH",
"rETH",
"ETHx",
"ankrETH",
"OETH",
"osETH",
"swETH",
"wBETH",
"sfrxETH",
"mETH"
};
10 changes: 9 additions & 1 deletion src/handle_finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,20 @@ void handle_finalize(ethPluginFinalize_t *msg) {
case KILN_V2_REQUEST_EXIT:
case KILN_V2_MULTICLAIM:
case KILN_V2_CLAIM:
msg->numScreens = 1;
msg->result = ETH_PLUGIN_RESULT_OK;
break;
case KILN_LR_ERC20_APPROVE:
msg->numScreens = 2;
msg->result = ETH_PLUGIN_RESULT_OK;
break;
case KILN_LR_DEPOSIT_INTO_STRATEGY:
msg->numScreens = 2;
msg->result = ETH_PLUGIN_RESULT_OK;
break;
case KILN_LR_QUEUE_WITHDRAWAL:
case KILN_LR_QUEUE_WITHDRAWALS:
case KILN_LR_COMPLETE_QUEUED_WITHDRAWAL:
case KILN_LR_COMPLETE_QUEUED_WITHDRAWALS:
msg->numScreens = 1;
msg->result = ETH_PLUGIN_RESULT_OK;
break;
Expand Down
8 changes: 6 additions & 2 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ void handle_init_contract(ethPluginInitContract_t *msg) {
case KILN_V1_WITHDRAW:
case KILN_V1_WITHDRAW_EL:
case KILN_V1_WITHDRAW_CL:
context->next_param = WITHDRAW_VALIDATION_OFFSET;
break;

case KILN_V1_BATCH_WITHDRAW:
Expand All @@ -59,11 +58,16 @@ void handle_init_contract(ethPluginInitContract_t *msg) {
break;

case KILN_LR_ERC20_APPROVE:
context->next_param = LR_ERC20_APPROVE_SPENDER;
break;
case KILN_LR_DEPOSIT_INTO_STRATEGY:
context->next_param = LR_DEPOSIT_INTO_STRATEGY_STRATEGY;
break;
case KILN_LR_QUEUE_WITHDRAWAL:
break;
case KILN_LR_QUEUE_WITHDRAWALS:
break;
case KILN_LR_COMPLETE_QUEUED_WITHDRAWAL:
case KILN_LR_COMPLETE_QUEUED_WITHDRAWALS:
break;

default:
Expand Down
130 changes: 128 additions & 2 deletions src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,131 @@
#include "kiln_plugin.h"

/*
* Compare two addresses
*
* @param a: first address
* @param b: second address
*
* @return true if the addresses are the same
*/
bool compare_addresses(const char *a, const char *b) {
for (size_t i = 0; i < ADDRESS_STR_LEN; i += 1) {
if (tolower((unsigned char)a[i]) != tolower((unsigned char)b[i])) {
return false;
}
}
return true;
}

/*
* If address is a known erc20, update lr display context with its name
* otherwise set it to "UNKNOWN"
*
* @param address: address to check
* @param context: context to update
*
* @note impacts the following context storage:
* `context->lr_erc20_name`: name of the erc20
*/
void find_lr_known_erc20(const char *address, context_t *context) {
for (size_t i = 0; i < LR_STRATEGIES_COUNT; i++) {
if (compare_addresses(address, &lr_erc20_addresses[i])) {
strncpy(context->lr_erc20_name, &lr_tickers[i], MAX_TICKER_LEN);
return;
}
}
// if unknown erc20, indicate it
strncpy(context->lr_erc20_name, "UNKNOWN", MAX_TICKER_LEN);
}

/*
* If address is a known strategy, update lr display context with its name
* otherwise set it to "UNKNOWN"
*
* @param address: address to check
* @param context: context to update
*
* @note impacts the following context storage:
* `context->lr_strategy_name`: name of the strategy
*/
void find_lr_known_strategy(const char *address, context_t *context) {
for (size_t i = 0; i < LR_STRATEGIES_COUNT; i++) {
if (compare_addresses(address, &lr_strategy_addresses[i])) {
strncpy(context->lr_strategy_name, &lr_tickers[i], MAX_TICKER_LEN);
return;
}
}
// if unknown strategy, indicate it
strncpy(context->lr_strategy_name, "UNKNOWN", MAX_TICKER_LEN);
}

/*
* Handle the parameters for the depositIntoStrategy(strategy,erc20,amount)
* selector
*
* @param msg: message containing the parameter
* @param context: context to update
*
* @note impacts the following context storage:
* `context->lr_strategy_address`: address of the strategy
* `context->lr_strategy_name`: name of the strategy
* `context->lr_erc20_address`: address of the erc20
* `context->lr_erc20_name`: name of the erc20
* `context->lr_erc20_amount`: amount of the erc20
*/
void handle_lr_deposit_into_strategy(ethPluginProvideParameter_t *msg, context_t *context) {
uint8_t buffer[ADDRESS_LENGTH];

switch (context->next_param) {
case LR_DEPOSIT_INTO_STRATEGY_STRATEGY:
copy_address(
buffer,
msg->parameter,
sizeof(buffer)
);
getEthDisplayableAddress(
buffer,
context->lr_strategy_address,
sizeof(context->lr_strategy_address),
msg->pluginSharedRW->sha3,
0
);
find_lr_known_strategy(context->lr_strategy_address, context);

context->next_param = LR_DEPOSIT_INTO_STRATEGY_TOKEN;
break;
case LR_DEPOSIT_INTO_STRATEGY_TOKEN:
copy_address(
buffer,
msg->parameter,
sizeof(buffer)
);
getEthDisplayableAddress(
buffer,
context->lr_erc20_address,
sizeof(context->lr_erc20_address),
msg->pluginSharedRW->sha3,
0
);
find_lr_known_erc20(context->lr_erc20_address, context);

context->next_param = LR_DEPOSIT_INTO_STRATEGY_AMOUNT;
break;
case LR_DEPOSIT_INTO_STRATEGY_AMOUNT:
copy_parameter(
context->lr_erc20_amount,
msg->parameter,
sizeof(context->lr_erc20_amount)
);
context->next_param = LR_DEPOSIT_INTO_STRATEGY_UNEXPECTED_PARAMETER;
break;
default:
PRINTF("Param not supported: %d\n", context->next_param);
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
}

void handle_provide_parameter(ethPluginProvideParameter_t *msg) {
context_t *context = (context_t *) msg->pluginContext;

Expand Down Expand Up @@ -37,12 +163,12 @@ void handle_provide_parameter(ethPluginProvideParameter_t *msg) {
msg->result = ETH_PLUGIN_RESULT_OK;
break;

case KILN_LR_ERC20_APPROVE:
case KILN_LR_DEPOSIT_INTO_STRATEGY:
handle_lr_deposit_into_strategy(msg, context);
break;
case KILN_LR_QUEUE_WITHDRAWAL:
case KILN_LR_QUEUE_WITHDRAWALS:
case KILN_LR_COMPLETE_QUEUED_WITHDRAWAL:
case KILN_LR_COMPLETE_QUEUED_WITHDRAWALS:
msg->result = ETH_PLUGIN_RESULT_OK;
break;

Expand Down
4 changes: 0 additions & 4 deletions src/handle_query_contract_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,6 @@ void handle_query_contract_id(ethQueryContractID_t *msg) {
strlcpy(msg->version, "Complete Queued Withdrawal", msg->versionLength);
break;

case KILN_LR_COMPLETE_QUEUED_WITHDRAWALS:
strlcpy(msg->version, "Complete Queued Withdrawals", msg->versionLength);
break;

default:
PRINTF("Selector Index not supported: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
21 changes: 15 additions & 6 deletions src/handle_query_contract_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,25 @@ static bool claim_ui_v2(ethQueryContractUI_t *msg) {
return ret;
}

static bool erc20_approve_lr(ethQueryContractUI_t *msg) {
static bool deposit_into_stragey_ui_lr(ethQueryContractUI_t *msg, context_t *context) {
bool ret = false;

switch (msg->screenIndex) {
case 0:
strlcpy(msg->title, "Approve", msg->titleLength);
strlcpy(msg->msg, "LR Token", msg->msgLength);
strlcpy(msg->title, "Strategy", msg->titleLength);
strlcpy(msg->msg, context->lr_strategy_name, MAX_TICKER_LEN);
ret = true;
break;
case 1:
strlcpy(msg->title, "Amount", msg->titleLength);
amountToString(context->lr_erc20_amount,
sizeof(context->lr_erc20_amount),
2,
context->lr_erc20_name,
msg->msg,
msg->msgLength);
ret = true;
break;

default:
PRINTF("Received an invalid screenIndex\n");
break;
Expand Down Expand Up @@ -234,8 +243,8 @@ void handle_query_contract_ui(ethQueryContractUI_t *msg) {
ret = claim_ui_v2(msg);
break;

case KILN_LR_ERC20_APPROVE:
ret = erc20_approve_lr(msg);
case KILN_LR_DEPOSIT_INTO_STRATEGY:
ret = deposit_into_stragey_ui_lr(msg, context);
break;

default:
Expand Down
49 changes: 39 additions & 10 deletions src/kiln_plugin.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <string.h>
#include <ctype.h>

#include "eth_internals.h"
#include "eth_plugin_interface.h"
Expand Down Expand Up @@ -32,8 +33,7 @@
// --- 14. queueWithdrawal(uint256[],address[],uint256[],address,bool)
// --- 15. queueWithdrawals((address[],uint256[],address)[])
// --- 16. completeQueuedWithdrawal((address,address,address,uint256,uint32,address[],uint256[]),address[],uint256,bool)
// --- 17. completeQueuedWithdrawals((address,address,address,uint256,uint32,address[],uint256[])[],address[][],uint256[],bool[])
#define NUM_SELECTORS 18
#define NUM_SELECTORS 17

// Selectors available (see mapping above).
typedef enum {
Expand All @@ -49,27 +49,56 @@ typedef enum {
KILN_V2_REQUEST_EXIT,
KILN_V2_MULTICLAIM,
KILN_V2_CLAIM,
KILN_LR_ERC20_APPROVE,
KILN_LR_DEPOSIT_INTO_STRATEGY,
KILN_LR_QUEUE_WITHDRAWAL,
KILN_LR_QUEUE_WITHDRAWALS,
KILN_LR_COMPLETE_QUEUED_WITHDRAWAL,
} selector_t;

extern const uint32_t KILN_SELECTORS[NUM_SELECTORS];

// ADDRESS_STR_LEN is 0x + addr + \0
#define ADDRESS_STR_LEN 43

// Parameters for deposit selector.
typedef enum {
DEPOSIT_UNEXPECTED_PARAMETER,
} deposit_parameters;

// Parameters for withdraw selectors (applies to withdraw, withdrawEL, withdrawCL)
// Parameters for LR approve selector.
typedef enum {
WITHDRAW_VALIDATION_OFFSET = 0,
WITHDRAW_VALIDATION_LENGTH = 1,
WITHDRAW_VALIDATION_KEY_PART_1 = 2, // BLS keys are 48 bytes, thus they are
WITHDRAW_VALIDATION_KEY_PART_2 = 3, // taking 2x32 bytes parameters.
WITHDRAW_UNEXPECTED_PARAMETER,
} withdraw_parameters;
LR_ERC20_APPROVE_SPENDER = 0,
LR_ERC20_APPROVE_AMOUNT,
LR_ERC20_APPROVE_UNEXPECTED_PARAMETER,
} lr_approve_parameters;

extern const uint32_t KILN_SELECTORS[NUM_SELECTORS];
// Parameters for LR deposit into strategy selector.
typedef enum {
LR_DEPOSIT_INTO_STRATEGY_STRATEGY = 0,
LR_DEPOSIT_INTO_STRATEGY_TOKEN,
LR_DEPOSIT_INTO_STRATEGY_AMOUNT,
LR_DEPOSIT_INTO_STRATEGY_UNEXPECTED_PARAMETER,
} lr_deposit_into_strategy_parameters;

#define LR_STRATEGIES_COUNT 11

extern const char lr_strategy_addresses[LR_STRATEGIES_COUNT][ADDRESS_STR_LEN];
extern const char lr_erc20_addresses[LR_STRATEGIES_COUNT][ADDRESS_STR_LEN];
extern const char lr_tickers[LR_STRATEGIES_COUNT][MAX_TICKER_LEN];

typedef struct context_t {
uint8_t next_param;

// parameters for LR functions
char lr_strategy_address[ADDRESS_STR_LEN];
char lr_strategy_name[MAX_TICKER_LEN];

char lr_erc20_address[ADDRESS_STR_LEN];
char lr_erc20_name[MAX_TICKER_LEN];
uint8_t lr_erc20_amount[INT256_LENGTH];


selector_t selectorIndex;
} context_t;

Expand Down
Loading

0 comments on commit 9748632

Please sign in to comment.