Skip to content

Commit

Permalink
Merge pull request #7 from blooo-io/feat/LDG-285-implement-the-withdr…
Browse files Browse the repository at this point in the history
…aw-method-and-test

Feat/ldg 285 implement the withdraw method and test
  • Loading branch information
GuilaneDen authored Sep 14, 2023
2 parents 0c566d1 + ba8f03a commit 2c6cbaf
Show file tree
Hide file tree
Showing 126 changed files with 5,244 additions and 17 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ Smart contracts covered by this plugin are:
| Ethereum | TransparentUpgradeableProxy | 0x777777c9898d384f785ee44acfe945efdff5f3e0|
| Ethereum | InitializableAdminUpgradeabilityProxy | 0xc5c9fb6223a989208df27dcee33fc59ff5c26fff|
| Ethereum | GraphProxy | 0xf55041e37e12cd407ad00ce2910b8269b01263b9|
| Ethereum | SushiBar | 0x8798249c2e607446efb7ad49ec89dd1865ff4272|
| Ethereum | TransparentUpgradeableProxy | 0xc3d688b66703497daa19211eedff47f25384cdc3|
| Ethereum | CometRewards | 0x1b0e765f6224c21223aea2af16c1c46e38885a40|
| Polygon | TransparentUpgradeableProxy | 0xf25212e676d1f7f89cd72ffee66158f541246445|
| Polygon | CometRewards | 0x45939657d1ca34a8fa39a924b71d28fe8431e581|


## Methods
Expand Down Expand Up @@ -68,6 +73,11 @@ Methods covered by this plugin are:
| GraphProxy | 0x026e402b | delegate |
| GraphProxy | 0x4d99dd16 | undelegate |
| GraphProxy | 0x51a60b02 | withdrawDelegated |
| SushiBar | 0xa59f3e0c | enter |
| SushiBar | 0x67dfd4c9 | leave |
| TransparentUpgradeableProxy | 0xf2b9fdb8 | supply |
| TransparentUpgradeableProxy | 0xf3fef3a3 | withdraw |
| CometRewards | 0xb7034f7e | claim |


## Build
Expand Down
10 changes: 10 additions & 0 deletions src/contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ static const uint8_t STAKEKIT_GRT_WITHDRAW_DELEGATED_SELECTOR[SELECTOR_SIZE] = {
0xa6,
0x0b,
0x02};
static const uint8_t STAKEKIT_ENTER_SELECTOR[SELECTOR_SIZE] = {0xa5, 0x9f, 0x3e, 0x0c};
static const uint8_t STAKEKIT_LEAVE_SELECTOR[SELECTOR_SIZE] = {0x67, 0xdf, 0xd4, 0xc9};
static const uint8_t STAKEKIT_COMET_SUPPLY_SELECTOR[SELECTOR_SIZE] = {0xf2, 0xb9, 0xfd, 0xb8};
static const uint8_t STAKEKIT_COMET_WITHDRAW_SELECTOR[SELECTOR_SIZE] = {0xf3, 0xfe, 0xf3, 0xa3};
static const uint8_t STAKEKIT_COMET_CLAIM_SELECTOR[SELECTOR_SIZE] = {0xb7, 0x03, 0x4f, 0x7e};

// Array of all the different StakeKit selectors.
const uint8_t *const STAKEKIT_SELECTORS[NUM_STAKEKIT_SELECTORS] = {
Expand All @@ -58,6 +63,11 @@ const uint8_t *const STAKEKIT_SELECTORS[NUM_STAKEKIT_SELECTORS] = {
STAKEKIT_GRT_DELEGATE_SELECTOR,
STAKEKIT_GRT_UNDELEGATE_SELECTOR,
STAKEKIT_GRT_WITHDRAW_DELEGATED_SELECTOR,
STAKEKIT_ENTER_SELECTOR,
STAKEKIT_LEAVE_SELECTOR,
STAKEKIT_COMET_SUPPLY_SELECTOR,
STAKEKIT_COMET_WITHDRAW_SELECTOR,
STAKEKIT_COMET_CLAIM_SELECTOR,
};

// Ask dummy address ETH
Expand Down
15 changes: 15 additions & 0 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@ void handle_finalize(void *parameters) {
case CLAIM_SELF_APECOIN:
msg->numScreens = 0;
break;
case COMET_CLAIM:
msg->numScreens = 2;
break;
case MORPHO_SUPPLY_1:
case MORPHO_SUPPLY_3:
msg->numScreens = 2;
msg->tokenLookup1 = context->contract_address_sent;
break;
case MORPHO_SUPPLY_2:
case COMET_SUPPLY:
msg->numScreens = 1;
msg->tokenLookup1 = context->contract_address_sent;
break;
case MORPHO_WITHDRAW_1:
case COMET_WITHDRAW:
msg->numScreens = 1;
msg->tokenLookup2 = context->contract_address_received;
break;
Expand All @@ -34,6 +39,16 @@ void handle_finalize(void *parameters) {
context->decimals_sent = 0;
strlcpy(context->ticker_sent, DEFAULT_TICKER, sizeof(context->ticker_sent));
break;
case ENTER:
msg->numScreens = 1;
context->decimals_sent = DEFAULT_DECIMAL;
strlcpy(context->ticker_sent, SUSHI_TICKER, sizeof(context->ticker_sent));
break;
case LEAVE:
msg->numScreens = 1;
context->decimals_sent = DEFAULT_DECIMAL;
strlcpy(context->ticker_sent, XSUSHI_TICKER, sizeof(context->ticker_sent));
break;
case STAKE:
msg->numScreens = 1;
context->decimals_sent = DEFAULT_DECIMAL;
Expand Down
5 changes: 5 additions & 0 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ void handle_init_contract(void *parameters) {
case CLAIM_TOKENS:
case BUY_VOUCHER:
case SELL_VOUCHER_NEW:
case ENTER:
case LEAVE:
context->next_param = AMOUNT_SENT;
break;
case WITHDRAW_SELF_APECOIN:
Expand All @@ -56,15 +58,18 @@ void handle_init_contract(void *parameters) {
case GRT_DELEGATE:
case GRT_UNDELEGATE:
case GRT_WITHDRAW_DELEGATED:
case COMET_CLAIM:
context->next_param = RECIPIENT;
break;
case MORPHO_SUPPLY_1:
case MORPHO_SUPPLY_2:
case MORPHO_SUPPLY_3:
case COMET_SUPPLY:
context->next_param = TOKEN_SENT;
break;
case MORPHO_WITHDRAW_1:
case MORPHO_WITHDRAW_2:
case COMET_WITHDRAW:
context->next_param = TOKEN_RECEIVED;
break;
case SWAP_TO:
Expand Down
34 changes: 30 additions & 4 deletions src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ static void handle_morpho_supply_1_3(ethPluginProvideParameter_t *msg,
plugin_parameters_t *context) {
switch (context->next_param) {
case TOKEN_SENT:
copy_address(context->contract_address_sent, msg->parameter, INT256_LENGTH);
copy_address(context->contract_address_sent, msg->parameter, ADDRESS_LENGTH);
context->next_param = RECIPIENT;
break;
case RECIPIENT:
Expand All @@ -77,7 +77,7 @@ static void handle_morpho_supply_1_3(ethPluginProvideParameter_t *msg,
static void handle_morpho_supply_2(ethPluginProvideParameter_t *msg, plugin_parameters_t *context) {
switch (context->next_param) {
case TOKEN_SENT:
copy_address(context->contract_address_sent, msg->parameter, INT256_LENGTH);
copy_address(context->contract_address_sent, msg->parameter, ADDRESS_LENGTH);
context->next_param = AMOUNT_SENT;
break;
case AMOUNT_SENT:
Expand All @@ -94,7 +94,7 @@ static void handle_morpho_withdraw_1(ethPluginProvideParameter_t *msg,
plugin_parameters_t *context) {
switch (context->next_param) {
case TOKEN_RECEIVED:
copy_address(context->contract_address_received, msg->parameter, INT256_LENGTH);
copy_address(context->contract_address_received, msg->parameter, ADDRESS_LENGTH);
context->next_param = AMOUNT_RECEIVED;
break;
case AMOUNT_RECEIVED:
Expand All @@ -114,7 +114,7 @@ static void handle_morpho_withdraw_2(ethPluginProvideParameter_t *msg,
plugin_parameters_t *context) {
switch (context->next_param) {
case TOKEN_RECEIVED:
copy_address(context->contract_address_received, msg->parameter, INT256_LENGTH);
copy_address(context->contract_address_received, msg->parameter, ADDRESS_LENGTH);
context->next_param = AMOUNT_RECEIVED;
break;
case AMOUNT_RECEIVED:
Expand All @@ -134,6 +134,25 @@ static void handle_morpho_withdraw_2(ethPluginProvideParameter_t *msg,
}
}

static void handle_comet_claim(ethPluginProvideParameter_t *msg, plugin_parameters_t *context) {
switch (context->next_param) {
case RECIPIENT: // Put the Comet protocol address in contract_address_sent
copy_address(context->contract_address_sent, msg->parameter, ADDRESS_LENGTH);
context->next_param = RECIPIENT_2;
break;
case RECIPIENT_2:
copy_address(context->recipient, msg->parameter, ADDRESS_LENGTH);
context->next_param = NONE;
break;
case NONE:
break;
default:
PRINTF("Param not supported\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
}

void handle_provide_parameter(void *parameters) {
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
plugin_parameters_t *context = (plugin_parameters_t *) msg->pluginContext;
Expand All @@ -154,6 +173,8 @@ void handle_provide_parameter(void *parameters) {
switch (context->selectorIndex) {
case DEPOSIT_SELF_APECOIN:
case CLAIM_TOKENS:
case ENTER:
case LEAVE:
copy_parameter(context->amount_sent, msg->parameter, INT256_LENGTH);
break;
case BUY_VOUCHER:
Expand Down Expand Up @@ -184,12 +205,14 @@ void handle_provide_parameter(void *parameters) {
handle_morpho_supply_1_3(msg, context);
break;
case MORPHO_WITHDRAW_1:
case COMET_WITHDRAW:
handle_morpho_withdraw_1(msg, context);
break;
case MORPHO_WITHDRAW_2:
handle_morpho_withdraw_2(msg, context);
break;
case MORPHO_SUPPLY_2:
case COMET_SUPPLY:
handle_morpho_supply_2(msg, context);
break;
case PARASPACE_DEPOSIT:
Expand All @@ -201,6 +224,9 @@ void handle_provide_parameter(void *parameters) {
copy_address(context->recipient, msg->parameter, ADDRESS_LENGTH);
context->skip = 1;
break;
case COMET_CLAIM:
handle_comet_claim(msg, context);
break;
default:
PRINTF("Selector Index %d not supported\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
11 changes: 11 additions & 0 deletions src/handle_query_contract_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,13 @@ void handle_query_contract_id(void *parameters) {
case MORPHO_SUPPLY_1:
case MORPHO_SUPPLY_2:
case MORPHO_SUPPLY_3:
case COMET_SUPPLY:
strlcpy(msg->version, "Supply", msg->versionLength);
break;
case MORPHO_WITHDRAW_1:
case MORPHO_WITHDRAW_2:
case PARASPACE_WITHDRAW:
case COMET_WITHDRAW:
strlcpy(msg->version, "Withdraw", msg->versionLength);
break;
case PARASPACE_DEPOSIT:
Expand All @@ -63,6 +65,15 @@ void handle_query_contract_id(void *parameters) {
case GRT_WITHDRAW_DELEGATED:
strlcpy(msg->version, "Withdraw Delegated", msg->versionLength);
break;
case ENTER:
strlcpy(msg->version, "Enter", msg->versionLength);
break;
case LEAVE:
strlcpy(msg->version, "Leave", msg->versionLength);
break;
case COMET_CLAIM:
strlcpy(msg->version, "Claim", msg->versionLength);
break;
default:
PRINTF("Selector Index :%d not supported\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
70 changes: 59 additions & 11 deletions src/handle_query_contract_ui.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ static void set_send_ui(ethQueryContractUI_t *msg, plugin_parameters_t *context)
case MORPHO_SUPPLY_3:
case PARASPACE_DEPOSIT:
case GRT_DELEGATE:
case ENTER:
case LEAVE:
case COMET_SUPPLY:
strlcpy(msg->title, "Send", msg->titleLength);
break;
case CLAIM_TOKENS:
Expand Down Expand Up @@ -64,6 +67,7 @@ static void set_receive_ui(ethQueryContractUI_t *msg, plugin_parameters_t *conte
case MORPHO_WITHDRAW_1:
case MORPHO_WITHDRAW_2:
case PARASPACE_WITHDRAW:
case COMET_WITHDRAW:
strlcpy(msg->title, "Receive", msg->titleLength);
break;
default:
Expand Down Expand Up @@ -106,6 +110,9 @@ static void set_recipient_ui(ethQueryContractUI_t *msg, plugin_parameters_t *con
case GRT_WITHDRAW_DELEGATED:
strlcpy(msg->title, "From", msg->titleLength);
break;
case COMET_CLAIM:
strlcpy(msg->title, "Owner", msg->titleLength);
break;
default:
PRINTF("Unhandled selector Index: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand All @@ -129,6 +136,34 @@ static void set_recipient_ui(ethQueryContractUI_t *msg, plugin_parameters_t *con
chainid);
}

static void set_recipient_2_ui(ethQueryContractUI_t *msg, plugin_parameters_t *context) {
switch (context->selectorIndex) {
case COMET_CLAIM:
strlcpy(msg->title, "Comet Protocol", msg->titleLength);
break;
default:
PRINTF("Unhandled selector Index: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}

// Prefix the address with `0x`.
msg->msg[0] = '0';
msg->msg[1] = 'x';

// We need a random chainID for legacy reasons with `getEthAddressStringFromBinary`.
// Setting it to `0` will make it work with every chainID :)
uint64_t chainid = 0;

// Get the string representation of the address stored in `context->beneficiary`. Put it in
// `msg->msg`.
getEthAddressStringFromBinary(
context->contract_address_sent,
msg->msg + 2, // +2 here because we've already prefixed with '0x'.
msg->pluginSharedRW->sha3,
chainid);
}

// Set UI for "Warning" screen.
static void set_warning_ui(ethQueryContractUI_t *msg,
const plugin_parameters_t *context __attribute__((unused))) {
Expand Down Expand Up @@ -179,8 +214,8 @@ static screens_t get_screen_amount_sent_recipient(ethQueryContractUI_t *msg,
}
}

static screens_t get_screen_morpho_supply(ethQueryContractUI_t *msg,
plugin_parameters_t *context __attribute__((unused))) {
static screens_t get_screen_supply(ethQueryContractUI_t *msg,
plugin_parameters_t *context __attribute__((unused))) {
bool token_sent_found = context->tokens_found & TOKEN_SENT_FOUND;

switch (msg->screenIndex) {
Expand Down Expand Up @@ -235,6 +270,18 @@ static screens_t get_screen_morpho_withdraw(ethQueryContractUI_t *msg,
}
}

static screens_t get_screen_comet_claim(ethQueryContractUI_t *msg,
plugin_parameters_t *context __attribute__((unused))) {
switch (msg->screenIndex) {
case 0:
return RECIPIENT_SCREEN;
case 1:
return RECIPIENT_2_SCREEN;
default:
return ERROR;
}
}

static screens_t get_screen_amount_sent_receive(ethQueryContractUI_t *msg,
plugin_parameters_t *context
__attribute__((unused))) {
Expand Down Expand Up @@ -271,23 +318,18 @@ static screens_t get_screen_recipient(ethQueryContractUI_t *msg,
// Helper function that returns the enum corresponding to the screen that should be displayed.
static screens_t get_screen(ethQueryContractUI_t *msg,
plugin_parameters_t *context __attribute__((unused))) {
// Remove if not used from here
bool token_sent_found = context->tokens_found & TOKEN_SENT_FOUND;
bool token_received_found = context->tokens_found & TOKEN_RECEIVED_FOUND;

bool both_tokens_found = token_received_found && token_sent_found;
bool both_tokens_not_found = !token_received_found && !token_sent_found;
// To here

switch (context->selectorIndex) {
case DEPOSIT_SELF_APECOIN:
case CLAIM_TOKENS:
case BUY_VOUCHER:
case SELL_VOUCHER_NEW:
case ENTER:
case LEAVE:
return get_screen_amount_sent(msg, context);
case WITHDRAW_SELF_APECOIN:
case SWAP_TO:
case PARASPACE_WITHDRAW:
case COMET_WITHDRAW:
return get_screen_receive(msg, context);
case GRT_UNDELEGATE:
case GRT_WITHDRAW_DELEGATED:
Expand All @@ -302,14 +344,17 @@ static screens_t get_screen(ethQueryContractUI_t *msg,
case MORPHO_SUPPLY_1:
case MORPHO_SUPPLY_2:
case MORPHO_SUPPLY_3:
return get_screen_morpho_supply(msg, context);
case COMET_SUPPLY:
return get_screen_supply(msg, context);
case SWAP_FROM:
return get_screen_amount_sent_receive(msg, context);
case STAKE:
return get_screen_value_sent(msg, context);
case MORPHO_WITHDRAW_1:
case MORPHO_WITHDRAW_2:
return get_screen_morpho_withdraw(msg, context);
case COMET_CLAIM:
return get_screen_comet_claim(msg, context);
default:
return ERROR;
}
Expand Down Expand Up @@ -337,6 +382,9 @@ void handle_query_contract_ui(void *parameters) {
case RECIPIENT_SCREEN:
set_recipient_ui(msg, context);
break;
case RECIPIENT_2_SCREEN:
set_recipient_2_ui(msg, context);
break;
case WARN_SCREEN:
set_warning_ui(msg, context);
break;
Expand Down
Loading

0 comments on commit 2c6cbaf

Please sign in to comment.