What you can code
Init Contract

Init Contract

The first case condition is handle_init_contract.
In the corresponding handle_init_contract.c, selectorIndex is set as the first parameter to parse. Let's dive deeper into the parameters.

Methods in a smart contract have parameters (or arguments). For example, swapExactEthforTokens has this format:

swapExactETHForTokens(uint256 amountOutMin,
                                                           address[] path,
                                                           address to,
                                                           uint256 deadline)

So, the parameters for this function are:

  1. uint256 amountOutMin: is the minimum amount in tokens the user will get from their swap.
  2. address[] path: is an array of paths. If we look at the contract source code on Etherscan, the only paths used are path[0] and path[1], where path[0] is the OUTGOING token, and path[1] the INCOMING one.
  3. address to: is the address to which the tokens are sent back.
  4. uint256 deadline: is the swap deadline after which it will fail.

These parameters are passed to your plugin in the same format order of the function. Parameters always have 32 bytes.

The goal of this plugin is to display on the Ledger screen: the amount of ETH from the wallet, the return currency and address to which the tokens are sent.

Hence, we need to parse amountOutMin, path[1], and to. We don't care about deadline since we will not show this information. We also don't need to parse path[0] since we know we are sending ETH.

Parsing these fields is done in the next call, handle_provide_parameter.c. We need to set the first parameter to parse, amountOutMin.

switch (context->selectorIndex) {
        context->next_param = MIN_AMOUNT_RECEIVED;
        context->next_param = TOKEN_RECEIVED;
    // Keep this
        PRINTF("Missing selectorIndex: %d\n", context->selectorIndex);
        msg->result = ETH_PLUGIN_RESULT_ERROR;

To trap errors in the plugin msg->result is set to ETH_PLUGIN_RESULT_ERROR

Now let's not forget to add this name to parameter in boilerplate_plugin.h.

typedef enum {
    MIN_AMOUNT_RECEIVED = 0, // Min amount of tokens the user will get
    TOKEN_RECEIVED, // Address of the tokens the user is swapping to
    BENEFICIARY, // Address of the recipient of the tokens
    // ...
} parameter;

Next, let's jump into handle_provide_parameter.c.

Copyright © Ledger SAS. All rights reserved. Ledger, Ledger Nano S, Ledger Vault, Bolos are registered trademarks of Ledger SAS