Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: eth-sdk-plugin upgraded #12

Merged
merged 2 commits into from
Feb 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 4 additions & 57 deletions src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
#include "quickswap_plugin.h"

// Copies the whole parameter (32 bytes long) from `src` to `dst`.
// Useful for numbers, data...
static void copy_parameter(uint8_t *dst, size_t dst_len, uint8_t *src) {
// Take the minimum between dst_len and parameter_length to make sure we don't overwrite memory.
size_t len = MIN(dst_len, PARAMETER_LENGTH);
memcpy(dst, src, len);
}

// Copy amount sent parameter to amount_sent
static void handle_amount_sent(const ethPluginProvideParameter_t *msg,
quickswap_parameters_t *context) {
copy_parameter(context->amount_sent, sizeof(context->amount_sent), msg->parameter);
copy_parameter(context->amount_sent, msg->parameter, sizeof(context->amount_sent));
}

// Copy amount sent parameter to amount_received
static void handle_amount_received(const ethPluginProvideParameter_t *msg,
quickswap_parameters_t *context) {
copy_parameter(context->amount_received, sizeof(context->amount_received), msg->parameter);
copy_parameter(context->amount_received, msg->parameter, sizeof(context->amount_received));
}

static void handle_beneficiary(const ethPluginProvideParameter_t *msg,
Expand Down Expand Up @@ -67,8 +59,8 @@ static void handle_value_sent(const ethPluginProvideParameter_t *msg,
ethPluginSharedRO_t *pluginSharedRO = (ethPluginSharedRO_t *) msg->pluginSharedRO;

copy_parameter(context->amount_sent,
pluginSharedRO->txContent->value.length,
pluginSharedRO->txContent->value.value);
pluginSharedRO->txContent->value.value,
pluginSharedRO->txContent->value.length);
}

static void handle_swap_exact_tokens(ethPluginProvideParameter_t *msg,
Expand Down Expand Up @@ -169,51 +161,6 @@ static void handle_swap_tokens_for_exact_tokens(ethPluginProvideParameter_t *msg
}
}

static void handle_swap_exact_eth_for_tokens(ethPluginProvideParameter_t *msg,
quickswap_parameters_t *context) {
switch (context->next_param) {
case AMOUNT_RECEIVED: // amountOut
handle_amount_received(msg, context);

handle_value_sent(msg, context);

context->next_param = PATHS_OFFSET;
break;

case PATHS_OFFSET:
context->next_param = BENEFICIARY;
break;

case BENEFICIARY:
handle_beneficiary(msg, context);
context->next_param = PATH;
context->skip++; // we skip deadline
break;

case PATH: // len(path)
context->next_param = TOKEN_SENT;
break;

case TOKEN_SENT: // path[0]
handle_token_sent(msg, context);
context->next_param = TOKEN_RECEIVED;
break;

case TOKEN_RECEIVED: // path[len(path) - 1]
handle_token_received(msg, context);
context->next_param = NONE;
break;

case NONE:
break;

default:
PRINTF("Param not supported\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
}

static void handle_add_remove_liquidity(ethPluginProvideParameter_t *msg,
quickswap_parameters_t *context) {
switch (context->next_param) {
Expand Down
21 changes: 13 additions & 8 deletions src/handle_provide_token.c
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#include "quickswap_plugin.h"

void handle_provide_token(void *parameters) {
ethPluginProvideToken_t *msg = (ethPluginProvideToken_t *) parameters;
ethPluginProvideInfo_t *msg = (ethPluginProvideInfo_t *) parameters;
quickswap_parameters_t *context = (quickswap_parameters_t *) msg->pluginContext;
PRINTF("QUICKSWAP plugin provide token: 0x%p, 0x%p\n", msg->token1, msg->token2);

PRINTF("QUICKSWAP plugin provide token: 0x%p, 0x%p\n",
msg->item1->token.address,
msg->item2->token.address);

if (ADDRESS_IS_NETWORK_TOKEN(context->contract_address_sent)) {
context->decimals_sent = WEI_TO_ETHER;
context->tokens_found |= TOKEN_SENT_FOUND;
} else if (msg->token1 != NULL) {
context->decimals_sent = msg->token1->decimals;
strlcpy(context->ticker_sent, (char *) msg->token1->ticker, sizeof(context->ticker_sent));
} else if (msg->item1 != NULL) {
context->decimals_sent = msg->item1->token.decimals;
strlcpy(context->ticker_sent,
(char *) msg->item1->token.ticker,
sizeof(context->ticker_sent));
context->tokens_found |= TOKEN_SENT_FOUND;
} else {
// CAL did not find the token and token is not ETH.
Expand All @@ -22,10 +27,10 @@ void handle_provide_token(void *parameters) {
if (ADDRESS_IS_NETWORK_TOKEN(context->contract_address_received)) {
context->decimals_received = WEI_TO_ETHER;
context->tokens_found |= TOKEN_RECEIVED_FOUND;
} else if (msg->token2 != NULL) {
context->decimals_received = msg->token2->decimals;
} else if (msg->item2 != NULL) {
context->decimals_received = msg->item2->token.decimals;
strlcpy(context->ticker_received,
(char *) msg->token2->ticker,
(char *) msg->item2->token.ticker,
sizeof(context->ticker_received));
context->tokens_found |= TOKEN_RECEIVED_FOUND;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void quickswap_plugin_call(int message, void *parameters) {
case ETH_PLUGIN_FINALIZE:
handle_finalize(parameters);
break;
case ETH_PLUGIN_PROVIDE_TOKEN:
case ETH_PLUGIN_PROVIDE_INFO:
handle_provide_token(parameters);
break;
case ETH_PLUGIN_QUERY_CONTRACT_ID:
Expand Down
Binary file modified tests/elfs/ethereum_nanos.elf
Binary file not shown.
Binary file modified tests/elfs/ethereum_nanox.elf
Binary file not shown.
Binary file modified tests/elfs/quickswap_nanos.elf
Binary file not shown.
Binary file modified tests/elfs/quickswap_nanox.elf
Binary file not shown.
2 changes: 1 addition & 1 deletion tests/src/test.fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ function processTest(device, contractName, testLabel, testDirSuffix, rawTxHex, s
}


function populateTransaction(contractAddr, inputData, chainId, value = "0.1") {
function populateTransaction(contractAddr, inputData, chainId, value = "0.0") {
// Get the generic transaction template
let unsignedTx = genericTx;
//adapt to the appropriate network
Expand Down