Files
app-ethereum/src/handle_get_printable_amount.c

46 lines
1.6 KiB
C
Raw Normal View History

2020-06-29 15:43:02 +02:00
#include "handle_get_printable_amount.h"
#include "shared_context.h"
#include "ethUtils.h"
#include "utils.h"
#include "uint256.h"
#include "string.h"
#include <stdint.h>
#include <os.h>
2020-06-29 15:43:02 +02:00
int handle_get_printable_amount(get_printable_amount_parameters_t* params, chain_config_t* config) {
2020-06-29 15:43:02 +02:00
uint8_t decimals;
char ticker[MAX_TICKER_LEN];
2020-11-24 10:23:45 +01:00
memset(params->printable_amount, 0, sizeof(params->printable_amount));
2020-06-29 15:43:02 +02:00
if (params->amount_length > 32) {
PRINTF("Amount is too big, 32 bytes max but buffer has %u bytes", params->amount_length);
return 0;
2020-06-29 15:43:02 +02:00
}
// If the amount is a fee, its value is nominated in ETH even if we're doing an ERC20 swap
2020-12-01 16:20:13 +01:00
if (params->is_fee) {
2020-06-29 15:43:02 +02:00
uint8_t ticker_len = strnlen(config->coinName, sizeof(config->coinName));
memcpy(ticker, config->coinName, ticker_len);
ticker[ticker_len] = '\0';
2020-06-29 15:43:02 +02:00
decimals = WEI_TO_ETHER;
} else {
// If the amount is *not* a fee, decimals and ticker are built from the given config
if (!parse_swap_config(params->coin_configuration,
params->coin_configuration_length,
ticker,
&decimals)) {
PRINTF("Error while parsing config\n");
return 0;
}
2020-06-29 15:43:02 +02:00
}
if (!amountToString(params->amount,
params->amount_length,
decimals,
ticker,
params->printable_amount,
sizeof(params->printable_amount))) {
THROW(EXCEPTION_OVERFLOW);
}
return 1;
2020-11-24 10:23:45 +01:00
}