Make erc20 approval for known contracts more generic and simple
+ No need to provide additional ERC20 definition anymore + Simpler code + More generic = Still not scalable - Takes a bit more space than the previous solution
This commit is contained in:
@@ -3,48 +3,52 @@
|
||||
#include "eth_plugin_handler.h"
|
||||
#include "shared_context.h"
|
||||
#include "ethUtils.h"
|
||||
#include "ethUstream.h"
|
||||
#include "utils.h"
|
||||
|
||||
typedef enum { ERC20_TRANSFER = 0, ERC20_APPROVE } erc20Selector_t;
|
||||
|
||||
typedef enum { TARGET_ADDRESS = 0, TARGET_CONTRACT, TARGET_COMPOUND } targetType_t;
|
||||
typedef enum { TARGET_ADDRESS = 0, TARGET_CONTRACT } targetType_t;
|
||||
|
||||
#define MAX_CONTRACT_NAME_LEN 15
|
||||
|
||||
typedef struct erc20_parameters_t {
|
||||
uint8_t selectorIndex;
|
||||
uint8_t destinationAddress[21];
|
||||
uint8_t amount[INT256_LENGTH];
|
||||
uint8_t ticker_1[MAX_TICKER_LEN];
|
||||
uint8_t ticker_2[MAX_TICKER_LEN];
|
||||
uint8_t ticker[MAX_TICKER_LEN];
|
||||
uint8_t decimals;
|
||||
uint8_t target;
|
||||
uint8_t contract_name[MAX_CONTRACT_NAME_LEN];
|
||||
} erc20_parameters_t;
|
||||
|
||||
typedef struct ticker_binding_t {
|
||||
char ticker1[MAX_TICKER_LEN];
|
||||
char ticker2[MAX_TICKER_LEN];
|
||||
} ticker_binding_t;
|
||||
typedef struct contract_t {
|
||||
char name[MAX_CONTRACT_NAME_LEN];
|
||||
uint8_t address[ADDRESS_LENGTH];
|
||||
} contract_t;
|
||||
|
||||
#define NUM_COMPOUND_BINDINGS 9
|
||||
const ticker_binding_t const COMPOUND_BINDINGS[NUM_COMPOUND_BINDINGS] = {
|
||||
{"DAI", "CDAI"},
|
||||
{"WETH", "CETH"},
|
||||
{"USDC", "CUSDC"},
|
||||
{"ZRX", "CZRX"},
|
||||
{"USDT", "CUSDT"},
|
||||
{"WBTC", "CBTC"},
|
||||
{"BAT", "CBAT"},
|
||||
{"REPv2", "CREP"},
|
||||
{"SAI", "CSAI"},
|
||||
#define NUM_CONTRACTS 11
|
||||
const contract_t const CONTRACTS[NUM_CONTRACTS] = {
|
||||
// Compound
|
||||
{"Compound DAI", {0x5d, 0x3a, 0x53, 0x6e, 0x4d, 0x6d, 0xbd, 0x61, 0x14, 0xcc, 0x1e, 0xad, 0x35, 0x77, 0x7b, 0xab, 0x94, 0x8e, 0x36, 0x43}},
|
||||
{"Compound ETH", {0x4d, 0xdc, 0x2d, 0x19, 0x39, 0x48, 0x92, 0x6d, 0x02, 0xf9, 0xb1, 0xfe, 0x9e, 0x1d, 0xaa, 0x07, 0x18, 0x27, 0x0e, 0xd5}},
|
||||
{"Compound USDC", {0x39, 0xaa, 0x39, 0xc0, 0x21, 0xdf, 0xba, 0xe8, 0xfa, 0xc5, 0x45, 0x93, 0x66, 0x93, 0xac, 0x91, 0x7d, 0x5e, 0x75, 0x63}},
|
||||
{"Compound ZRX", {0xb3, 0x31, 0x9f, 0x5d, 0x18, 0xbc, 0x0d, 0x84, 0xdd, 0x1b, 0x48, 0x25, 0xdc, 0xde, 0x5d, 0x5f, 0x72, 0x66, 0xd4, 0x07}},
|
||||
{"Compound USDT", {0xf6, 0x50, 0xc3, 0xd8, 0x8d, 0x12, 0xdb, 0x85, 0x5b, 0x8b, 0xf7, 0xd1, 0x1b, 0xe6, 0xc5, 0x5a, 0x4e, 0x07, 0xdc, 0xc9}},
|
||||
{"Compound WBTC", {0xc1, 0x1b, 0x12, 0x68, 0xc1, 0xa3, 0x84, 0xe5, 0x5c, 0x48, 0xc2, 0x39, 0x1d, 0x8d, 0x48, 0x02, 0x64, 0xa3, 0xa7, 0xf4}},
|
||||
{"Compound BAT", {0x6c, 0x8c, 0x6b, 0x02, 0xe7, 0xb2, 0xbe, 0x14, 0xd4, 0xfa, 0x60, 0x22, 0xdf, 0xd6, 0xd7, 0x59, 0x21, 0xd9, 0x0e, 0x4e}},
|
||||
{"Compound REP", {0x15, 0x80, 0x79, 0xee, 0x67, 0xfc, 0xe2, 0xf5, 0x84, 0x72, 0xa9, 0x65, 0x84, 0xa7, 0x3c, 0x7a, 0xb9, 0xac, 0x95, 0xc1}},
|
||||
{"Compound SAI", {0xf5, 0xdc, 0xe5, 0x72, 0x82, 0xa5, 0x84, 0xd2, 0x74, 0x6f, 0xaf, 0x15, 0x93, 0xd3, 0x12, 0x1f, 0xca, 0xc4, 0x44, 0xdc}},
|
||||
{"Compound UNI", {0x35, 0xa1, 0x80, 0x00, 0x23, 0x0d, 0xa7, 0x75, 0xca, 0xc2, 0x48, 0x73, 0xd0, 0x0f, 0xf8, 0x5b, 0xcc, 0xde, 0xd5, 0x50}},
|
||||
// Paraswap
|
||||
{"Paraswap", {0x1b, 0xd4, 0x35, 0xf3, 0xc0, 0x54, 0xb6, 0xe9, 0x01, 0xb7, 0xb1, 0x08, 0xa0, 0xab, 0x76, 0x17, 0xc8, 0x08, 0x67, 0x7b}}
|
||||
};
|
||||
|
||||
bool check_token_binding(char *ticker1,
|
||||
char *ticker2,
|
||||
const ticker_binding_t *bindings,
|
||||
size_t num_bindings) {
|
||||
for (size_t i = 0; i < num_bindings; i++) {
|
||||
ticker_binding_t *binding = (ticker_binding_t *) PIC(&bindings[i]);
|
||||
if (strncmp(binding->ticker1, ticker1, strnlen(binding->ticker1, MAX_TICKER_LEN)) == 0 &&
|
||||
strncmp(binding->ticker2, ticker2, strnlen(binding->ticker2, MAX_TICKER_LEN)) == 0) {
|
||||
bool check_contract(erc20_parameters_t* context) {
|
||||
for (size_t i = 0; i < NUM_CONTRACTS; i++) {
|
||||
contract_t *contract = (contract_t *) PIC(&CONTRACTS[i]);
|
||||
if (memcmp(contract->address, context->destinationAddress, ADDRESS_LENGTH) == 0) {
|
||||
strncpy((char*)context->contract_name, contract->name, sizeof(context->contract_name));
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -131,7 +135,6 @@ void erc20_plugin_call(int message, void *parameters) {
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
} else if (context->selectorIndex == ERC20_APPROVE) {
|
||||
msg->tokenLookup1 = msg->pluginSharedRO->txContent->destination;
|
||||
msg->tokenLookup2 = context->destinationAddress;
|
||||
msg->numScreens = 2;
|
||||
msg->uiType = ETH_UI_TYPE_GENERIC;
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
@@ -146,19 +149,11 @@ void erc20_plugin_call(int message, void *parameters) {
|
||||
(msg->token2 != NULL));
|
||||
if (msg->token1 != NULL) {
|
||||
context->target = TARGET_ADDRESS;
|
||||
strcpy((char *) context->ticker_1, (char *) msg->token1->ticker);
|
||||
strcpy((char *) context->ticker, (char *) msg->token1->ticker);
|
||||
context->decimals = msg->token1->decimals;
|
||||
if (context->selectorIndex == ERC20_APPROVE) {
|
||||
if (msg->token2 != NULL) {
|
||||
if (check_contract(context)) {
|
||||
context->target = TARGET_CONTRACT;
|
||||
strcpy((char *) context->ticker_2, (char *) msg->token2->ticker);
|
||||
// test if we're doing a Compound allowance
|
||||
if (check_token_binding((char *) msg->token1->ticker,
|
||||
(char *) msg->token2->ticker,
|
||||
COMPOUND_BINDINGS,
|
||||
NUM_COMPOUND_BINDINGS)) {
|
||||
context->target = TARGET_COMPOUND;
|
||||
}
|
||||
}
|
||||
}
|
||||
msg->result = ETH_PLUGIN_RESULT_OK;
|
||||
@@ -182,12 +177,12 @@ void erc20_plugin_call(int message, void *parameters) {
|
||||
strcpy(msg->title, "Amount");
|
||||
if (ismaxint(context->amount, sizeof(context->amount))) {
|
||||
strcpy(msg->msg, "Unlimited ");
|
||||
strcat(msg->msg, (char *) context->ticker_1);
|
||||
strcat(msg->msg, (char *) context->ticker);
|
||||
} else {
|
||||
amountToString(context->amount,
|
||||
sizeof(context->amount),
|
||||
context->decimals,
|
||||
(char *) context->ticker_1,
|
||||
(char *) context->ticker,
|
||||
msg->msg,
|
||||
100);
|
||||
}
|
||||
@@ -196,14 +191,7 @@ void erc20_plugin_call(int message, void *parameters) {
|
||||
case 1:
|
||||
if (context->target >= TARGET_CONTRACT) {
|
||||
strcpy(msg->title, "Contract");
|
||||
if (context->target == TARGET_COMPOUND) {
|
||||
strcpy(msg->msg, "Compound ");
|
||||
strcat(msg->msg,
|
||||
(char *) context->ticker_2 +
|
||||
1); // remove the 'c' char at beginning of compound ticker
|
||||
} else {
|
||||
strcpy(msg->msg, (char *) context->ticker_2);
|
||||
}
|
||||
strcpy(msg->msg, (char *) context->contract_name);
|
||||
} else {
|
||||
strcpy(msg->title, "Address");
|
||||
msg->msg[0] = '0';
|
||||
|
||||
Reference in New Issue
Block a user