Skip to content

Commit

Permalink
Use Xmacro to centralize selectors definition
Browse files Browse the repository at this point in the history
  • Loading branch information
fbeutin-ledger committed Oct 26, 2023
1 parent 6e9e41b commit 6812c9a
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 27 deletions.
15 changes: 3 additions & 12 deletions src/contract.c → src/boilerplate_plugin.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@
#include <stdint.h>
#include "boilerplate_plugin.h"

// List of selectors supported by this plugin.
// EDIT THIS: Adapt the variable names and change the `0x` values to match your selectors.
static const uint32_t SWAP_EXACT_ETH_FOR_TOKENS_SELECTOR = 0x7ff36ab5;
static const uint32_t BOILERPLATE_DUMMY_SELECTOR_2 = 0x13374242;

// Array of all the different boilerplate selectors. Make sure this follows the same order as the
// enum defined in `boilerplate_plugin.h`
// EDIT THIS: Use the names of the array declared above.
const uint32_t BOILERPLATE_SELECTORS[NUM_SELECTORS] = {
SWAP_EXACT_ETH_FOR_TOKENS_SELECTOR,
BOILERPLATE_DUMMY_SELECTOR_2,
};
// This array will be automatically expanded to map all selector_t names with the correct value.
// Do not modify !
const uint32_t SELECTORS[SELECTOR_COUNT] = {SELECTORS_LIST(TO_VALUE)};
55 changes: 41 additions & 14 deletions src/boilerplate_plugin.h
Original file line number Diff line number Diff line change
@@ -1,25 +1,55 @@
/*******************************************************************************
* Plugin Boilerplate
* (c) 2023 Ledger
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/

#pragma once

#include <string.h>
#include "eth_internals.h"
#include "eth_plugin_interface.h"
#include <string.h>

// Number of selectors defined in this plugin. Should match the enum `selector_t`.
// EDIT THIS: Put in the number of selectors your plugin is going to support.
#define NUM_SELECTORS 2

// Name of the plugin.
// EDIT THIS: Replace with your plugin name.
#define PLUGIN_NAME "PluginBoilerplate"

// Enumeration of the different selectors possible.
// Should follow the exact same order as the array declared in main.c
// EDIT THIS: Change the naming (`selector_t`), and add your selector names.
typedef enum {
SWAP_EXACT_ETH_FOR_TOKENS = 0,
BOILERPLATE_DUMMY_2,
// All possible selectors of your plugin.
// EDIT THIS: Enter your selectors here, in the format X(NAME, value)
// A Xmacro below will create for you:
// - an enum named selector_t with every NAME
// - a map named SELECTORS associating each NAME with it's value
#define SELECTORS_LIST(X) \
X(SWAP_EXACT_ETH_FOR_TOKENS, 0x7ff36ab5) \
X(BOILERPLATE_DUMMY_2, 0x13374242)

// Xmacro helpers to define the enum and map
// Do not modify !
#define TO_ENUM(selector_name, selector_id) selector_name,
#define TO_VALUE(selector_name, selector_id) selector_id,

// This enum will be automatically expanded to hold all selector names.
// The value SELECTOR_COUNT can be used to get the number of defined selectors
// Do not modify !
typedef enum selector_e {
SELECTORS_LIST(TO_ENUM) SELECTOR_COUNT,
} selector_t;

// This array will be automatically expanded to map all selector_t names with the correct value.
// Do not modify !
extern const uint32_t SELECTORS[SELECTOR_COUNT];

// Enumeration used to parse the smart contract data.
// EDIT THIS: Adapt the parameter names here.
typedef enum {
Expand All @@ -31,9 +61,6 @@ typedef enum {
UNEXPECTED_PARAMETER,
} parameter;

// EDIT THIS: Rename `BOILERPLATE` to be the same as the one initialized in `main.c`.
extern const uint32_t BOILERPLATE_SELECTORS[NUM_SELECTORS];

// Shared global memory with Ethereum app. Must be at most 5 * 32 bytes.
// EDIT THIS: This struct is used by your plugin to save the parameters you parse. You
// will need to adapt this struct to your plugin.
Expand Down
2 changes: 1 addition & 1 deletion src/handle_init_contract.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ void handle_init_contract(ethPluginInitContract_t *msg) {
memset(context, 0, sizeof(*context));

size_t index;
if (!find_selector(U4BE(msg->selector, 0), BOILERPLATE_SELECTORS, NUM_SELECTORS, &index)) {
if (!find_selector(U4BE(msg->selector, 0), SELECTORS, SELECTOR_COUNT, &index)) {
PRINTF("Error: selector not found!\n");
msg->result = ETH_PLUGIN_RESULT_UNAVAILABLE;
return;
Expand Down

0 comments on commit 6812c9a

Please sign in to comment.