Skip to content

Commit

Permalink
Merge pull request #3 from blooo-io/feat/LAPP-4-add-minttoken-selecto…
Browse files Browse the repository at this point in the history
…r-in-main-c

feat(poap ledger plugin): added mintToken method to poap plugin
  • Loading branch information
n4l5u0r authored Nov 22, 2021
2 parents 1992e09 + 996311c commit c332c7f
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 205 deletions.
5 changes: 0 additions & 5 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,11 @@ void handle_finalize(void *parameters) {

msg->uiType = ETH_UI_TYPE_GENERIC;

// EDIT THIS: Set the total number of screen you will need.
msg->numScreens = 2;
// EDIT THIS: Handle this case like you wish to (i.e. maybe no additional screen needed?).
// If the beneficiary is NOT the sender, we will need an additional screen to display it.
if (memcmp(msg->address, context->beneficiary, ADDRESS_LENGTH) != 0) {
msg->numScreens += 1;
}

// EDIT THIS: set `tokenLookup1` (and maybe `tokenLookup2`) to point to
// token addresses you will info for (such as decimals, ticker...).
msg->tokenLookup1 = context->token_received;

msg->result = ETH_PLUGIN_RESULT_OK;
Expand Down
20 changes: 2 additions & 18 deletions src/handle_init_contract.c
Original file line number Diff line number Diff line change
@@ -1,19 +1,13 @@
#include "poap_plugin.h"

// Called once to init.
void handle_init_contract(void *parameters) {
// Cast the msg to the type of structure we expect (here, ethPluginInitContract_t).
ethPluginInitContract_t *msg = (ethPluginInitContract_t *) parameters;

// Make sure we are running a compatible version.
if (msg->interfaceVersion != ETH_PLUGIN_INTERFACE_VERSION_LATEST) {
// If not the case, return the `UNAVAILABLE` status.
msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE;
return;
}

// Double check that the `context_t` struct is not bigger than the maximum size (defined by
// `msg->pluginContextLength`).
if (msg->pluginContextLength < sizeof(context_t)) {
PRINTF("Plugin parameters structure is bigger than allowed size\n");
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand All @@ -22,39 +16,29 @@ void handle_init_contract(void *parameters) {

context_t *context = (context_t *) msg->pluginContext;

// Initialize the context (to 0).
memset(context, 0, sizeof(*context));

// Look for the index of the selectorIndex passed in by `msg`.
uint8_t i;
for (i = 0; i < NUM_SELECTORS; i++) {
if (memcmp((uint8_t *) PIC(POAP_SELECTORS[i]), msg->selector, SELECTOR_SIZE) == 0) {
context->selectorIndex = i;
break;
}
}

// If `i == NUM_SELECTORS` it means we haven't found the selector. Return an error.
if (i == NUM_SELECTORS) {
msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE;
}

// Set `next_param` to be the first field we expect to parse.
// EDIT THIS: Adapt the `cases`, and set the `next_param` to be the first parameter you expect
// to parse.
switch (context->selectorIndex) {
case SWAP_EXACT_ETH_FOR_TOKENS:
context->next_param = MIN_AMOUNT_RECEIVED;
case MINT_TOKEN:
context->next_param = EVENT_ID;
break;
case POAP_DUMMY_2:
context->next_param = TOKEN_RECEIVED;
// Keep this
default:
PRINTF("Missing selectorIndex: %d\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
return;
}

// Return valid status.
msg->result = ETH_PLUGIN_RESULT_OK;
}
99 changes: 34 additions & 65 deletions src/handle_provide_parameter.c
Original file line number Diff line number Diff line change
@@ -1,82 +1,51 @@
#include "poap_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);
static void handle_beneficiary(const ethPluginProvideParameter_t *msg, context_t *context) {
memset(context->beneficiary, 0, sizeof(context->beneficiary));
memcpy(context->beneficiary,
&msg->parameter[PARAMETER_LENGTH - ADDRESS_LENGTH],
sizeof(context->beneficiary));
PRINTF("BENEFICIARY: %.*H\n", ADDRESS_LENGTH, context->beneficiary);
}

// Copies a 20 byte address (located in a 32 bytes parameter) `from `src` to `dst`.
// Useful for token addresses, user addresses...
static void copy_address(uint8_t *dst, size_t dst_len, uint8_t *src) {
// An address is 20 bytes long: so we need to make sure we skip the first 12 bytes!
size_t offset = PARAMETER_LENGTH - ADDRESS_LENGTH;
size_t len = MIN(dst_len, ADDRESS_LENGTH);
memcpy(dst, &src[offset], len);
}

// EDIT THIS: Remove this function and write your own handlers!
static void handle_swap_exact_eth_for_tokens(ethPluginProvideParameter_t *msg, context_t *context) {
if (context->go_to_offset) {
if (msg->parameterOffset != context->offset + SELECTOR_SIZE) {
return;
}
context->go_to_offset = false;
}
switch (context->next_param) {
case MIN_AMOUNT_RECEIVED: // amountOutMin
copy_parameter(context->amount_received,
sizeof(context->amount_received),
msg->parameter);
context->next_param = PATH_OFFSET;
break;
case PATH_OFFSET: // path
context->offset = U2BE(msg->parameter, PARAMETER_LENGTH - 2);
context->next_param = BENEFICIARY;
break;
case BENEFICIARY: // to
copy_address(context->beneficiary, sizeof(context->beneficiary), msg->parameter);
context->next_param = PATH_LENGTH;
context->go_to_offset = true;
break;
case PATH_LENGTH:
context->offset = msg->parameterOffset - SELECTOR_SIZE + PARAMETER_LENGTH * 2;
context->go_to_offset = true;
context->next_param = TOKEN_RECEIVED;
break;
case TOKEN_RECEIVED: // path[1] -> contract address of token received
copy_address(context->token_received, sizeof(context->token_received), msg->parameter);
context->next_param = UNEXPECTED_PARAMETER;
break;
// Keep this
default:
PRINTF("Param not supported: %d\n", context->next_param);
msg->result = ETH_PLUGIN_RESULT_ERROR;
break;
}
static void handle_token_received(const ethPluginProvideParameter_t *msg, context_t *context) {
memset(context->token_received, 0, sizeof(context->token_received));
memcpy(context->token_received,
&msg->parameter[PARAMETER_LENGTH - ADDRESS_LENGTH],
sizeof(context->token_received));
PRINTF("TOKEN RECEIVED: %.*H\n", ADDRESS_LENGTH, context->token_received);
}

void handle_provide_parameter(void *parameters) {
ethPluginProvideParameter_t *msg = (ethPluginProvideParameter_t *) parameters;
context_t *context = (context_t *) msg->pluginContext;
// We use `%.*H`: it's a utility function to print bytes. You first give
// the number of bytes you wish to print (in this case, `PARAMETER_LENGTH`) and then
// the address (here `msg->parameter`).
PRINTF("plugin provide parameter: offset %d\nBytes: %.*H\n",
msg->parameterOffset,
PARAMETER_LENGTH,
msg->parameter);

msg->result = ETH_PLUGIN_RESULT_OK;

// EDIT THIS: adapt the cases and the names of the functions.
if (context->skip) {
// Skip this step, and don't forget to decrease skipping counter.
context->skip--;
} else if ((context->offset) && msg->parameterOffset != context->checkpoint + context->offset) {
PRINTF("offset: %d, checkpoint: %d, parameterOffset: %d\n",
context->offset,
context->checkpoint,
msg->parameterOffset);
return;
}
context->offset = 0;
switch (context->selectorIndex) {
case SWAP_EXACT_ETH_FOR_TOKENS:
handle_swap_exact_eth_for_tokens(msg, context);
case EVENT_ID:
context->next_param = TOKEN_RECEIVED;
break;
case TOKEN_RECEIVED: // path[1] -> id of the token received
handle_token_received(msg, context);
context->next_param = BENEFICIARY;
break;
case BENEFICIARY: // to
handle_beneficiary(msg, context);
context->next_param = NONE;
break;
case POAP_DUMMY_2:
case NONE:
break;
default:
PRINTF("Selector Index not supported: %d\n", context->selectorIndex);
Expand Down
23 changes: 6 additions & 17 deletions src/handle_provide_token.c
Original file line number Diff line number Diff line change
@@ -1,29 +1,18 @@
#include "poap_plugin.h"

// EDIT THIS: Adapt this function to your needs! Remember, the information for tokens are held in
// `msg->token1` and `msg->token2`. If those pointers are `NULL`, this means the ethereum app didn't
// find any info regarding the requested tokens!
void handle_provide_token(void *parameters) {
ethPluginProvideToken_t *msg = (ethPluginProvideToken_t *) parameters;
context_t *context = (context_t *) msg->pluginContext;

if (msg->token1) {
// The Ethereum App found the information for the requested token!
// Store its decimals.
PRINTF("POAP plugin provide token: 0x%p\n", msg->token1);

if (msg->token1 != NULL) {
context->decimals = msg->token1->decimals;
// Store its ticker.
strlcpy(context->ticker, (char *) msg->token1->ticker, sizeof(context->ticker));

// Keep track that we found the token.
context->token_found = true;
context->tokens_found |= TOKEN_RECEIVED_FOUND;
} else {
// The Ethereum App did not manage to find the info for the requested token.
context->token_found = false;

// If we wanted to add a screen, say a warning screen for example, we could instruct the
// ethereum app to add an additional screen by setting `msg->additionalScreens` here, just
// like so:
// msg->additionalScreens = 1;
context->decimals = DEFAULT_DECIMAL;
msg->additionalScreens++;
}
msg->result = ETH_PLUGIN_RESULT_OK;
}
10 changes: 2 additions & 8 deletions src/handle_query_contract_id.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
#include "poap_plugin.h"

// Sets the first screen to display.
void handle_query_contract_id(void *parameters) {
ethQueryContractID_t *msg = (ethQueryContractID_t *) parameters;
context_t *context = (context_t *) msg->pluginContext;
// msg->name will be the upper sentence displayed on the screen.
// msg->version will be the lower sentence displayed on the screen.

// For the first screen, display the plugin name.
strlcpy(msg->name, PLUGIN_NAME, msg->nameLength);

// EDIT THIS: Adapt the cases by modifying the strings you pass to `strlcpy`.
switch (context->selectorIndex) {
case SWAP_EXACT_ETH_FOR_TOKENS:
strlcpy(msg->version, "Swap", msg->versionLength);
case MINT_TOKEN:
strlcpy(msg->version, "Mint", msg->versionLength);
break;
// Keep this
default:
PRINTF("Selector index: %d not supported\n", context->selectorIndex);
msg->result = ETH_PLUGIN_RESULT_ERROR;
Expand Down
Loading

0 comments on commit c332c7f

Please sign in to comment.