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

Commit

Permalink
Updated to use the latest plugin SDK
Browse files Browse the repository at this point in the history
  • Loading branch information
apaillier-ledger committed Oct 19, 2023
1 parent 88e5bc3 commit 6df3ccd
Show file tree
Hide file tree
Showing 9 changed files with 50 additions and 210 deletions.
2 changes: 1 addition & 1 deletion ethereum-plugin-sdk
5 changes: 2 additions & 3 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ void handle_receive_address_lookup(ethPluginFinalize_t *msg, context_t *context)
}
}

void handle_finalize(void *parameters) {
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
void handle_finalize(ethPluginFinalize_t *msg) {
context_t *context = (context_t *) msg->pluginContext;

if (context->valid) {
Expand Down Expand Up @@ -53,4 +52,4 @@ void handle_finalize(void *parameters) {
PRINTF("Context not valid\n");
msg->result = ETH_PLUGIN_RESULT_FALLBACK;
}
}
}
4 changes: 1 addition & 3 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@ static void handle_amount_value(const ethPluginInitContract_t *msg, context_t *c
}

// Called once to init.
void handle_init_contract(void *parameters) {
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;

void handle_init_contract(ethPluginInitContract_t *msg) {
if (msg->interfaceVersion != ETH_PLUGIN_INTERFACE_VERSION_LATEST) {
msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE;
return;
Expand Down
3 changes: 1 addition & 2 deletions src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,7 @@ void handle_batch_call(ethPluginProvideParameter_t *msg, context_t *context) {
}
}

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

msg->result = ETH_PLUGIN_RESULT_OK;
Expand Down
3 changes: 1 addition & 2 deletions src/handle_provide_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@ void handle_received_address(const ethPluginProvideInfo_t *msg, context_t *conte
sizeof(context->contract_address_received));
}

void handle_provide_token(void *parameters) {
ethPluginProvideInfo_t *msg = (ethPluginProvideInfo_t *) parameters;
void handle_provide_token(ethPluginProvideInfo_t *msg) {
context_t *context = (context_t *) msg->pluginContext;

switch (context->selectorIndex) {
Expand Down
5 changes: 2 additions & 3 deletions src/handle_query_contract_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ void handle_init_cfa_screen(ethQueryContractID_t *msg, const context_t *context)
}
}

void handle_query_contract_id(void *parameters) {
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
void handle_query_contract_id(ethQueryContractID_t *msg) {
const context_t *context = (context_t *) msg->pluginContext;

strlcpy(msg->name, PLUGIN_NAME, msg->nameLength);
Expand All @@ -36,4 +35,4 @@ void handle_query_contract_id(void *parameters) {
return;
}
msg->result = ETH_PLUGIN_RESULT_OK;
}
}
87 changes: 41 additions & 46 deletions src/handle_query_contract_ui.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdbool.h>
#include "ricochet_plugin.h"
#include <limits.h>

Expand Down Expand Up @@ -39,18 +40,18 @@ static void decimalToAmount(unsigned long long value, context_t *context) {
} while (value != 0);
}

static void set_amount_ui(ethQueryContractUI_t *msg, const context_t *context) {
static bool set_amount_ui(ethQueryContractUI_t *msg, const context_t *context) {
strlcpy(msg->title, "Send", msg->titleLength);

amountToString(context->amount,
return amountToString(context->amount,
sizeof(context->amount),
DEFAULT_DECIMAL,
context->ticker_sent,
msg->msg,
msg->msgLength);
}

static int set_cfa_from_ui(ethQueryContractUI_t *msg, context_t *context) {
static bool set_cfa_from_ui(ethQueryContractUI_t *msg, context_t *context) {
strlcpy(msg->title, "From", msg->titleLength);

contract_address_ticker_t *currentTicker = NULL;
Expand All @@ -70,29 +71,31 @@ static int set_cfa_from_ui(ethQueryContractUI_t *msg, context_t *context) {
if (context->method_id != STOP_STREAM) {
unsigned long long value;
if (amountToDecimal(context, sizeof(context->amount), &value)) {
return -1;
return false;
}
// switch from token per sec to token per month for UX only.
if (__builtin_umulll_overflow(value, 2592000, &value)) {
return -1;
return false;
}
decimalToAmount(value, context);

amountToString(context->amount,
if (!amountToString(context->amount,
sizeof(context->amount),
DEFAULT_DECIMAL,
context->ticker_sent,
msg->msg,
msg->msgLength);
msg->msgLength)) {
return false;
}

strlcat(msg->msg, " per month", msg->msgLength);
} else {
strlcpy(msg->msg, context->ticker_sent, msg->msgLength);
}
return 0;
return true;
}

static void set_cfa_to_ui(ethQueryContractUI_t *msg, context_t *context) {
static bool set_cfa_to_ui(ethQueryContractUI_t *msg, context_t *context) {
strlcpy(msg->title, "To", msg->titleLength);

contract_address_ticker_t *currentTicker = NULL;
Expand All @@ -109,9 +112,10 @@ static void set_cfa_to_ui(ethQueryContractUI_t *msg, context_t *context) {
}
}
strlcpy(msg->msg, context->ticker_received, msg->msgLength);
return true;
}

static int set_batch_call_from_ui(ethQueryContractUI_t *msg, context_t *context) {
static bool set_batch_call_from_ui(ethQueryContractUI_t *msg, context_t *context) {
strlcpy(msg->title, "From", msg->titleLength);

contract_address_ticker_t *currentTicker = NULL;
Expand All @@ -130,26 +134,28 @@ static int set_batch_call_from_ui(ethQueryContractUI_t *msg, context_t *context)

unsigned long long value;
if (amountToDecimal(context, sizeof(context->amount), &value)) {
return -1;
return false;
}
// switch from token per sec to token per month for UX only.
if (__builtin_umulll_overflow(value, 2592000, &value)) {
return -1;
return false;
}
decimalToAmount(value, context);

amountToString(context->amount,
if (!amountToString(context->amount,
sizeof(context->amount),
DEFAULT_DECIMAL,
context->ticker_sent,
msg->msg,
msg->msgLength);
msg->msgLength)) {
return false;
}

strlcat(msg->msg, " per month", msg->msgLength);
return 0;
return true;
}

static void set_batch_call_to_ui(ethQueryContractUI_t *msg, context_t *context) {
static bool set_batch_call_to_ui(ethQueryContractUI_t *msg, context_t *context) {
strlcpy(msg->title, "To", msg->titleLength);

contract_address_ticker_t *currentTicker = NULL;
Expand All @@ -166,33 +172,34 @@ static void set_batch_call_to_ui(ethQueryContractUI_t *msg, context_t *context)
}
}
strlcpy(msg->msg, context->ticker_received, msg->msgLength);
return true;
}

static void set_upgrade_to_eth_send_ui(ethQueryContractUI_t *msg, const context_t *context) {
static bool set_upgrade_to_eth_send_ui(ethQueryContractUI_t *msg, const context_t *context) {
strlcpy(msg->title, "Send", msg->titleLength);

amountToString(msg->pluginSharedRO->txContent->value.value,
return amountToString(msg->pluginSharedRO->txContent->value.value,
msg->pluginSharedRO->txContent->value.length,
DEFAULT_DECIMAL,
context->ticker_sent,
msg->msg,
msg->msgLength);
}

static void set_upgrade_to_eth_received_ui(ethQueryContractUI_t *msg, const context_t *context) {
static bool set_upgrade_to_eth_received_ui(ethQueryContractUI_t *msg, const context_t *context) {
strlcpy(msg->title, "Receive", msg->titleLength);

amountToString(msg->pluginSharedRO->txContent->value.value,
return amountToString(msg->pluginSharedRO->txContent->value.value,
msg->pluginSharedRO->txContent->value.length,
DEFAULT_DECIMAL,
context->ticker_received,
msg->msg,
msg->msgLength);
}

static void set_receive_ui(ethQueryContractUI_t *msg, const context_t *context) {
static bool set_receive_ui(ethQueryContractUI_t *msg, const context_t *context) {
strlcpy(msg->title, "Receive", msg->titleLength);
amountToString(context->amount,
return amountToString(context->amount,
sizeof(context->amount),
DEFAULT_DECIMAL,
context->ticker_received,
Expand All @@ -214,13 +221,12 @@ static screens_t get_screen(const ethQueryContractUI_t *msg) {
}
}

void handle_query_contract_ui(void *parameters) {
ethQueryContractUI_t *msg = (ethQueryContractUI_t *) parameters;
void handle_query_contract_ui(ethQueryContractUI_t *msg) {
context_t *context = (context_t *) msg->pluginContext;
bool ret = false;

memset(msg->title, 0, msg->titleLength);
memset(msg->msg, 0, msg->msgLength);
msg->result = ETH_PLUGIN_RESULT_OK;

screens_t screen = get_screen(msg);

Expand All @@ -230,57 +236,47 @@ void handle_query_contract_ui(void *parameters) {
case UPGRADE:
switch (screen) {
case SEND_SCREEN:
set_amount_ui(msg, context);
ret = set_amount_ui(msg, context);
break;
case RECEIVE_SCREEN:
set_receive_ui(msg, context);
ret = set_receive_ui(msg, context);
break;
default:
PRINTF("Received an invalid screenIndex\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
break;
case CALL_AGREEMENT:
switch (screen) {
case SEND_SCREEN:
if (set_cfa_from_ui(msg, context)) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
}
ret = set_cfa_from_ui(msg, context);
break;
case RECEIVE_SCREEN:
set_cfa_to_ui(msg, context);
ret = set_cfa_to_ui(msg, context);
break;
default:
PRINTF("Received an invalid screenIndex\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
break;
case UPGRADE_TO_ETH:
switch (screen) {
case SEND_SCREEN:
set_upgrade_to_eth_send_ui(msg, context);
ret = set_upgrade_to_eth_send_ui(msg, context);
break;
case RECEIVE_SCREEN:
set_upgrade_to_eth_received_ui(msg, context);
ret = set_upgrade_to_eth_received_ui(msg, context);
break;
default:
PRINTF("Received an invalid screenIndex\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
break;

case BATCH_CALL:
switch (screen) {
case SEND_SCREEN:
if (set_batch_call_from_ui(msg, context)) {
msg->result = ETH_PLUGIN_RESULT_ERROR;
}
ret = set_batch_call_from_ui(msg, context);
break;
case RECEIVE_SCREEN:
set_batch_call_to_ui(msg, context);
ret = set_batch_call_to_ui(msg, context);
break;
default:
break;
Expand All @@ -289,7 +285,6 @@ void handle_query_contract_ui(void *parameters) {

default:
PRINTF("Missing selectorIndex: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}
}
msg->result = ret ? ETH_PLUGIN_RESULT_OK : ETH_PLUGIN_RESULT_ERROR;
}
Loading

0 comments on commit 6df3ccd

Please sign in to comment.