add erc20 db lookup
This commit is contained in:
parent
e8ff130a57
commit
7acd136435
|
@ -0,0 +1,80 @@
|
|||
#include "eth_db.h"
|
||||
#include <string.h>
|
||||
|
||||
#define FS_PAIRING_CHAIN 0x4348
|
||||
#define FS_PAIRING_ERC20 0x3020
|
||||
|
||||
#define ERC20_NET_LEN 24
|
||||
|
||||
struct __attribute__((packed)) chain_raw_desc {
|
||||
fs_entry_t _entry;
|
||||
uint32_t chain_id;
|
||||
char data[];
|
||||
};
|
||||
|
||||
struct __attribute__((packed)) erc20_raw_desc {
|
||||
fs_entry_t _entry;
|
||||
uint8_t net_count;
|
||||
uint8_t data[];
|
||||
};
|
||||
|
||||
fs_action_t _eth_db_match_chain(void* ctx, fs_entry_t* entry) {
|
||||
if (entry->magic != FS_PAIRING_CHAIN) {
|
||||
return FS_REJECT;
|
||||
}
|
||||
|
||||
chain_desc_t* chain = (chain_desc_t*) ctx;
|
||||
struct chain_raw_desc* chain_data = (struct chain_raw_desc*) entry;
|
||||
|
||||
return chain_data->chain_id == chain->chain_id ? FS_ACCEPT : FS_REJECT;
|
||||
}
|
||||
|
||||
fs_action_t _eth_db_match_erc20(void* ctx, fs_entry_t* entry) {
|
||||
if (entry->magic != FS_PAIRING_ERC20) {
|
||||
return FS_REJECT;
|
||||
}
|
||||
|
||||
erc20_desc_t* erc20 = (erc20_desc_t*) ctx;
|
||||
struct erc20_raw_desc* erc20_data = (struct erc20_raw_desc*) entry;
|
||||
|
||||
int count = erc20_data->net_count;
|
||||
uint8_t* data = erc20_data->data;
|
||||
|
||||
while (count--) {
|
||||
if (!memcmp(&erc20->chain, data, 4) && !memcmp(erc20->addr, &data[4], 20)) {
|
||||
return FS_ACCEPT;
|
||||
}
|
||||
|
||||
data += ERC20_NET_LEN;
|
||||
}
|
||||
|
||||
return FS_REJECT;
|
||||
}
|
||||
|
||||
app_err_t eth_db_lookup_chain(chain_desc_t* chain) {
|
||||
struct chain_raw_desc* chain_data = (struct chain_raw_desc*) fs_find(_eth_db_match_chain, chain);
|
||||
|
||||
if (!chain_data) {
|
||||
return ERR_DATA;
|
||||
}
|
||||
|
||||
chain->ticker = chain_data->data;
|
||||
chain->name = chain->ticker + strlen(chain->ticker);
|
||||
chain->short_name = chain->name + strlen(chain->name);
|
||||
|
||||
return ERR_OK;
|
||||
}
|
||||
|
||||
app_err_t eth_db_lookup_erc20(erc20_desc_t* erc20) {
|
||||
struct erc20_raw_desc* erc20_data = (struct erc20_raw_desc*) fs_find(_eth_db_match_erc20, erc20);
|
||||
|
||||
if (!erc20_data) {
|
||||
return ERR_DATA;
|
||||
}
|
||||
|
||||
uint8_t* data = erc20_data->data + (erc20_data->net_count * ERC20_NET_LEN);
|
||||
erc20->decimals = *(data++);
|
||||
erc20->ticker = (char *) data;
|
||||
|
||||
return ERR_OK;
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef _ETH_DB_H_
|
||||
#define _ETH_DB_H_
|
||||
|
||||
#include "storage/fs.h"
|
||||
#include "error.h"
|
||||
|
||||
typedef struct {
|
||||
uint32_t chain_id;
|
||||
const char *ticker;
|
||||
const char *name;
|
||||
const char *short_name;
|
||||
} chain_desc_t;
|
||||
|
||||
typedef struct {
|
||||
uint32_t chain;
|
||||
const uint8_t* addr;
|
||||
const char *ticker;
|
||||
uint8_t decimals;
|
||||
} erc20_desc_t;
|
||||
|
||||
app_err_t eth_db_lookup_chain(chain_desc_t* chain);
|
||||
app_err_t eth_db_lookup_erc20(erc20_desc_t* erc20);
|
||||
|
||||
#endif
|
|
@ -16,10 +16,11 @@ def serialize_addresses(addresses):
|
|||
|
||||
def serialize_db(f, chains, tokens):
|
||||
for chain in chains.values():
|
||||
chain_len = 4 + len(chain["ticker"]) + 1 + len(chain["name"]) + 1
|
||||
chain_len = 4 + len(chain["ticker"]) + 1 + len(chain["name"]) + 1 + len(chain["shortName"]) + 1
|
||||
f.write(struct.pack("<HHI", CHAIN_MAGIC, chain_len, chain["id"]))
|
||||
f.write(bytes(chain["ticker"], "ascii") + b'\0')
|
||||
f.write(bytes(chain["name"], "ascii") + b'\0')
|
||||
f.write(bytes(chain["shortName"], "ascii") + b'\0')
|
||||
|
||||
for token in tokens.values():
|
||||
addresses = serialize_addresses(token["addresses"])
|
||||
|
@ -44,6 +45,7 @@ def process_token(tokens, chains, token_json, chains_json):
|
|||
chain = {
|
||||
"id": chain_id,
|
||||
"name": chain_json["name"],
|
||||
"shortName": chain_json["shortName"],
|
||||
"ticker": chain_json["nativeCurrency"]["symbol"],
|
||||
"decimals": chain_json["nativeCurrency"]["decimals"],
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue