Skip to content

Commit

Permalink
Merge pull request #10 from blooo-io/feat/LAPQS-7-implement-swap-eth-…
Browse files Browse the repository at this point in the history
…for-exact-tokens-method-and-test

feat: added swapETHForExactTokens method and test
  • Loading branch information
n4l5u0r authored Feb 1, 2022
2 parents 4edc54d + 9c0accd commit b1d667e
Show file tree
Hide file tree
Showing 245 changed files with 1,126 additions and 186 deletions.
2 changes: 1 addition & 1 deletion ethereum-plugin-sdk
8 changes: 1 addition & 7 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,14 @@ void handle_finalize(void *parameters) {

if (context->valid) {
msg->numScreens = 2;
if ((context->selectorIndex == SWAP_EXACT_TOKENS_FOR_TOKENS) &&
(strncmp(context->beneficiary,
(const unsigned char *) NULL_ETH_ADDRESS,
ADDRESS_LENGTH) != 0)) {
if ((strncmp(context->beneficiary, (const char *) NULL_ETH_ADDRESS, ADDRESS_LENGTH) != 0)) {
// An addiitonal screen is required to display the `beneficiary` field.
msg->numScreens += 1;
}
if (!ADDRESS_IS_NETWORK_TOKEN(context->contract_address_sent)) {
// Address is not network token (0xeee...) so we will need to look up the token in the
// CAL.
msg->tokenLookup1 = context->contract_address_sent;
print_bytes(context->contract_address_sent, sizeof(context->contract_address_sent));
PRINTF("Setting address sent to: %.*H\n",
ADDRESS_LENGTH,
context->contract_address_sent);
Expand All @@ -40,8 +36,6 @@ void handle_finalize(void *parameters) {
ADDRESS_LENGTH,
context->contract_address_received);
msg->tokenLookup2 = context->contract_address_received;
print_bytes(context->contract_address_received,
sizeof(context->contract_address_received));
} else {
msg->tokenLookup2 = NULL;
}
Expand Down
16 changes: 16 additions & 0 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,28 @@ void handle_init_contract(void *parameters) {
case SWAP_EXACT_TOKENS_FOR_TOKENS:
case SWAP_EXACT_TOKENS_FOR_ETH:
case SWAP_EXACT_TOKENS_FOR_TOKENS_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
case SWAP_EXACT_TOKENS_FOR_ETH_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
context->next_param = AMOUNT_SENT;
break;
case SWAP_TOKENS_FOR_EXACT_TOKENS:
case SWAP_EXACT_ETH_FOR_TOKENS:
case SWAP_ETH_FOR_EXACT_TOKENS:
case SWAP_TOKENS_FOR_EXACT_ETH:
case SWAP_EXACT_ETH_FOR_TOKENS_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
context->next_param = AMOUNT_RECEIVED;
break;

case ADD_LIQUIDITY:
case ADD_LIQUIDITY_ETH:
case REMOVE_LIQUIDITY:
case REMOVE_LIQUIDITY_ETH:
case REMOVE_LIQUIDITY_WITH_PERMIT:
case REMOVE_LIQUIDITY_ETH_WITH_PERMIT:
case REMOVE_LIQUIDITY_ETH_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
case REMOVE_LIQUIDITY_ETH_WITH_PERMIT_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
context->next_param = TOKEN_SENT;
break;

default:
PRINTF("Missing selectorIndex\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
168 changes: 151 additions & 17 deletions src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,42 @@ static void handle_token_received(const ethPluginProvideParameter_t *msg,
PRINTF("TOKEN RECEIVED: %.*H\n", ADDRESS_LENGTH, context->contract_address_received);
}

static void handle_token_sent_eth(quickswap_parameters_t *context) {
memset(context->contract_address_sent, 0, sizeof(context->contract_address_sent));
memcpy(context->contract_address_sent,
QUICKSWAP_ETH_ADDRESS,
sizeof(context->contract_address_sent));
PRINTF("TOKEN SENT: %.*H\n", ADDRESS_LENGTH, context->contract_address_sent);
}

// Copy amount sent parameter to amount_sent
static void handle_value_sent(const ethPluginProvideParameter_t *msg,
quickswap_parameters_t *context) {
ethPluginSharedRO_t *pluginSharedRO = (ethPluginSharedRO_t *) msg->pluginSharedRO;

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

static void handle_swap_exact_tokens(ethPluginProvideParameter_t *msg,
quickswap_parameters_t *context) {
switch (context->next_param) {
case AMOUNT_SENT: // amountIn
handle_amount_sent(msg, context);
context->next_param = AMOUNT_RECEIVED;
context->checkpoint = msg->parameterOffset;
break;
case AMOUNT_RECEIVED: // amountOut
handle_amount_received(msg, context);
context->next_param = PATHS_OFFSET;
break;
case PATHS_OFFSET:
context->offset = U2BE(msg->parameter, PARAMETER_LENGTH - 2);
context->next_param = BENEFICIARY;
break;
case BENEFICIARY: // to
handle_beneficiary(msg, context);
context->next_param = PATH;
context->skip++;
break;
case PATH: // len(path)
handle_array_len(msg, context);
Expand All @@ -79,14 +100,12 @@ static void handle_swap_exact_tokens(ethPluginProvideParameter_t *msg,
break;
case TOKEN_RECEIVED: // path[len(path) - 1]
handle_token_received(msg, context);
context->next_param = BENEFICIARY;
break;
case BENEFICIARY: // to
handle_beneficiary(msg, context);
context->next_param = NONE;
break;

case NONE:
break;

default:
PRINTF("Unsupported param\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand All @@ -99,32 +118,50 @@ static void handle_swap_tokens_for_exact_tokens(ethPluginProvideParameter_t *msg
switch (context->next_param) {
case AMOUNT_RECEIVED: // amountOut
handle_amount_received(msg, context);
context->next_param = AMOUNT_SENT;
context->checkpoint = msg->parameterOffset;

if (context->selectorIndex == SWAP_EXACT_ETH_FOR_TOKENS ||
context->selectorIndex == SWAP_ETH_FOR_EXACT_TOKENS ||
context->selectorIndex ==
SWAP_EXACT_ETH_FOR_TOKENS_SUPPORTING_FEE_ON_TRANSFER_TOKENS) {
handle_value_sent(msg, context);
context->next_param = PATHS_OFFSET;
} else {
context->next_param = AMOUNT_SENT;
}
break;

case AMOUNT_SENT: // amountIn
handle_amount_sent(msg, context);
context->next_param = PATHS_OFFSET;
break;

case PATHS_OFFSET:
context->offset = U2BE(msg->parameter, PARAMETER_LENGTH - sizeof(context->offset));
context->next_param = BENEFICIARY;
break;

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

case PATH: // len(path)
context->skip = msg->parameter[PARAMETER_LENGTH - 1] - 2;
context->next_param = TOKEN_SENT;
context->checkpoint = msg->parameterOffset + PARAMETER_LENGTH;
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("Unsupported param\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand All @@ -136,27 +173,106 @@ static void handle_swap_exact_eth_for_tokens(ethPluginProvideParameter_t *msg,
quickswap_parameters_t *context) {
switch (context->next_param) {
case AMOUNT_RECEIVED: // amountOut
context->checkpoint = msg->parameterOffset;
handle_amount_received(msg, context);

handle_value_sent(msg, context);

context->next_param = PATHS_OFFSET;
break;

case PATHS_OFFSET:
context->offset = U2BE(msg->parameter, PARAMETER_LENGTH - sizeof(context->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->skip = msg->parameter[PARAMETER_LENGTH - 1] - 2;
context->next_param = TOKEN_SENT;
context->checkpoint = msg->parameterOffset + PARAMETER_LENGTH;
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) {
case TOKEN_SENT: // tokenA
handle_token_sent(msg, context);
context->next_param = TOKEN_RECEIVED;
break;
case TOKEN_RECEIVED: // TokenB
handle_token_received(msg, context);
context->next_param = AMOUNT_SENT;
if (context->selectorIndex == ADD_LIQUIDITY) {
context->skip = 2;
} else {
context->skip = 1;
}

break;
case AMOUNT_SENT: // TokenA Min Amount
handle_amount_sent(msg, context);
context->next_param = AMOUNT_RECEIVED;
break;
case AMOUNT_RECEIVED: // TokenB Min Amount
handle_amount_received(msg, context);
context->next_param = BENEFICIARY;
break;
case BENEFICIARY: // to
handle_beneficiary(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_eth(ethPluginProvideParameter_t *msg,
quickswap_parameters_t *context) {
switch (context->next_param) {
case TOKEN_SENT: // tokenA
handle_token_sent_eth(context);
handle_token_received(msg, context);
context->next_param = AMOUNT_RECEIVED;
context->skip = 1;
break;
case AMOUNT_RECEIVED: // TokenA Min Amount
handle_amount_received(msg, context);
context->next_param = AMOUNT_SENT;
break;
case AMOUNT_SENT: // ETH Min Amount
handle_amount_sent(msg, context);
context->next_param = BENEFICIARY;
break;
case BENEFICIARY: // to
handle_beneficiary(msg, context);
context->next_param = NONE;
break;
case NONE:
break;
default:
Expand Down Expand Up @@ -188,14 +304,32 @@ void handle_provide_parameter(void *parameters) {
case SWAP_EXACT_TOKENS_FOR_TOKENS:
case SWAP_EXACT_TOKENS_FOR_ETH:
case SWAP_EXACT_TOKENS_FOR_TOKENS_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
case SWAP_EXACT_TOKENS_FOR_ETH_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
handle_swap_exact_tokens(msg, context);
break;

case SWAP_EXACT_ETH_FOR_TOKENS:
case SWAP_ETH_FOR_EXACT_TOKENS:
case SWAP_EXACT_ETH_FOR_TOKENS_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
case SWAP_TOKENS_FOR_EXACT_TOKENS:
case SWAP_TOKENS_FOR_EXACT_ETH:
handle_swap_tokens_for_exact_tokens(msg, context);
break;
case SWAP_EXACT_ETH_FOR_TOKENS:
handle_swap_exact_eth_for_tokens(msg, context);

case ADD_LIQUIDITY:
case REMOVE_LIQUIDITY:
case REMOVE_LIQUIDITY_WITH_PERMIT:
handle_add_remove_liquidity(msg, context);
break;

case ADD_LIQUIDITY_ETH:
case REMOVE_LIQUIDITY_ETH:
case REMOVE_LIQUIDITY_ETH_WITH_PERMIT:
case REMOVE_LIQUIDITY_ETH_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
case REMOVE_LIQUIDITY_ETH_WITH_PERMIT_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
handle_add_remove_liquidity_eth(msg, context);
break;

default:
PRINTF("Selector Index %d not supported\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
5 changes: 2 additions & 3 deletions src/handle_provide_token.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ void handle_provide_token(void *parameters) {
} else {
// CAL did not find the token and token is not ETH.
context->decimals_sent = DEFAULT_DECIMAL;
strlcpy(context->ticker_sent, "???", sizeof(context->ticker_sent));
// We will need an additional screen to display a warning message.
msg->additionalScreens++;
}

if (ADDRESS_IS_NETWORK_TOKEN(context->contract_address_received)) {
Expand All @@ -31,9 +31,8 @@ void handle_provide_token(void *parameters) {
} else {
// CAL did not find the token and token is not ETH.
context->decimals_received = DEFAULT_DECIMAL;
strlcpy(context->ticker_received, "???", sizeof(context->ticker_received));
// We will need an additional screen to display a warning message.
msg->additionalScreens++;
}

msg->result = ETH_PLUGIN_RESULT_OK;
}
19 changes: 19 additions & 0 deletions src/handle_query_contract_id.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,27 @@ void handle_query_contract_id(void *parameters) {
case SWAP_EXACT_ETH_FOR_TOKENS:
case SWAP_TOKENS_FOR_EXACT_TOKENS:
case SWAP_EXACT_TOKENS_FOR_TOKENS_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
case SWAP_EXACT_TOKENS_FOR_ETH_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
case SWAP_ETH_FOR_EXACT_TOKENS:
case SWAP_TOKENS_FOR_EXACT_ETH:
case SWAP_EXACT_ETH_FOR_TOKENS_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
strlcpy(msg->version, "Swap", msg->versionLength);
break;

case ADD_LIQUIDITY:
case ADD_LIQUIDITY_ETH:
strlcpy(msg->version, "Add Liquidity", msg->versionLength);
break;

case REMOVE_LIQUIDITY:
case REMOVE_LIQUIDITY_ETH:
case REMOVE_LIQUIDITY_WITH_PERMIT:
case REMOVE_LIQUIDITY_ETH_WITH_PERMIT:
case REMOVE_LIQUIDITY_ETH_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
case REMOVE_LIQUIDITY_ETH_WITH_PERMIT_SUPPORTING_FEE_ON_TRANSFER_TOKENS:
strlcpy(msg->version, "Remove Liquidity", msg->versionLength);
break;

default:
PRINTF("Selector Index :%d not supported\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
Loading

0 comments on commit b1d667e

Please sign in to comment.