Encode addresses as base58

This commit is contained in:
Christian Kamm 2021-11-05 15:18:54 +01:00
parent 0ab99ac00b
commit 9f7f72c3cb
3 changed files with 56 additions and 52 deletions

View File

@ -80,6 +80,10 @@ pub type AccountTables = Vec<Arc<dyn AccountTable>>;
struct RawAccountTable {} struct RawAccountTable {}
pub fn encode_address(addr: &Pubkey) -> String {
bs58::encode(&addr.to_bytes()).into_string()
}
#[async_trait] #[async_trait]
impl AccountTable for RawAccountTable { impl AccountTable for RawAccountTable {
fn table_name(&self) -> &str { fn table_name(&self) -> &str {
@ -91,8 +95,8 @@ impl AccountTable for RawAccountTable {
client: &postgres_query::Caching<tokio_postgres::Client>, client: &postgres_query::Caching<tokio_postgres::Client>,
account_write: &AccountWrite, account_write: &AccountWrite,
) -> Result<(), anyhow::Error> { ) -> Result<(), anyhow::Error> {
let pubkey: &[u8] = &account_write.pubkey.to_bytes(); let pubkey = encode_address(&account_write.pubkey);
let owner: &[u8] = &account_write.owner.to_bytes(); let owner = encode_address(&account_write.owner);
// TODO: should update for same write_version to work with websocket input // TODO: should update for same write_version to work with websocket input
let query = postgres_query::query!( let query = postgres_query::query!(

View File

@ -10,7 +10,7 @@ use {
std::{cmp, error, mem}, std::{cmp, error, mem},
}; };
use crate::{AccountTable, AccountWrite}; use crate::{encode_address, AccountTable, AccountWrite};
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct SqlNumericI80F48(pub I80F48); pub struct SqlNumericI80F48(pub I80F48);
@ -262,11 +262,11 @@ impl AccountTable for MangoAccountTable {
// TODO: Also filter on mango_group? // TODO: Also filter on mango_group?
let pubkey: &[u8] = &account_write.pubkey.to_bytes(); let pubkey = encode_address(&account_write.pubkey);
let data = MangoAccount::load_from_bytes(&account_write.data)?; let data = MangoAccount::load_from_bytes(&account_write.data)?;
let owner: &[u8] = &data.owner.to_bytes(); let owner = encode_address(&data.owner);
let mango_group: &[u8] = &data.mango_group.to_bytes(); let mango_group = encode_address(&data.mango_group);
let version = data.meta_data.version as i16; let version = data.meta_data.version as i16;
let extra_info = &data.meta_data.extra_info as &[u8]; let extra_info = &data.meta_data.extra_info as &[u8];
let in_margin_basket = &data.in_margin_basket as &[bool]; let in_margin_basket = &data.in_margin_basket as &[bool];
@ -284,8 +284,8 @@ impl AccountTable for MangoAccountTable {
let spot_open_orders = data let spot_open_orders = data
.spot_open_orders .spot_open_orders
.iter() .iter()
.map(|key| key.to_bytes().to_vec()) .map(|key| encode_address(&key))
.collect::<Vec<Vec<u8>>>(); .collect::<Vec<String>>();
let perp_accounts = data let perp_accounts = data
.perp_accounts .perp_accounts
.iter() .iter()
@ -323,7 +323,7 @@ impl AccountTable for MangoAccountTable {
.collect::<Vec<SqlNumericU64>>(); .collect::<Vec<SqlNumericU64>>();
let msrm_amount = SqlNumericU64(data.msrm_amount); let msrm_amount = SqlNumericU64(data.msrm_amount);
let info = &data.info as &[u8]; let info = &data.info as &[u8];
let advanced_orders_key = &data.advanced_orders_key.to_bytes() as &[u8]; let advanced_orders_key = encode_address(&data.advanced_orders_key);
let padding = &data.padding as &[u8]; let padding = &data.padding as &[u8];
let query = postgres_query::query!( let query = postgres_query::query!(
@ -379,14 +379,14 @@ impl AccountTable for MangoAccountTable {
#[derive(Debug, ToSql)] #[derive(Debug, ToSql)]
struct TokenInfo { struct TokenInfo {
mint: Vec<u8>, mint: String,
root_bank: Vec<u8>, root_bank: String,
decimals: i16, decimals: i16,
padding: Vec<u8>, padding: Vec<u8>,
} }
#[derive(Debug, ToSql)] #[derive(Debug, ToSql)]
struct SpotMarketInfo { struct SpotMarketInfo {
spot_market: Vec<u8>, spot_market: String,
maint_asset_weight: SqlNumericI80F48, maint_asset_weight: SqlNumericI80F48,
init_asset_weight: SqlNumericI80F48, init_asset_weight: SqlNumericI80F48,
maint_liab_weight: SqlNumericI80F48, maint_liab_weight: SqlNumericI80F48,
@ -395,7 +395,7 @@ struct SpotMarketInfo {
} }
#[derive(Debug, ToSql)] #[derive(Debug, ToSql)]
struct PerpMarketInfo { struct PerpMarketInfo {
perp_market: Vec<u8>, perp_market: String,
maint_asset_weight: SqlNumericI80F48, maint_asset_weight: SqlNumericI80F48,
init_asset_weight: SqlNumericI80F48, init_asset_weight: SqlNumericI80F48,
maint_liab_weight: SqlNumericI80F48, maint_liab_weight: SqlNumericI80F48,
@ -428,7 +428,7 @@ impl AccountTable for MangoGroupTable {
// TODO: Also filter on mango_group pubkey? // TODO: Also filter on mango_group pubkey?
let pubkey: &[u8] = &account_write.pubkey.to_bytes(); let pubkey = encode_address(&account_write.pubkey);
let data = MangoGroup::load_from_bytes(&account_write.data)?; let data = MangoGroup::load_from_bytes(&account_write.data)?;
let version = data.meta_data.version as i16; let version = data.meta_data.version as i16;
let extra_info = &data.meta_data.extra_info as &[u8]; let extra_info = &data.meta_data.extra_info as &[u8];
@ -437,8 +437,8 @@ impl AccountTable for MangoGroupTable {
.tokens .tokens
.iter() .iter()
.map(|token| TokenInfo { .map(|token| TokenInfo {
mint: token.mint.to_bytes().to_vec(), mint: encode_address(&token.mint),
root_bank: token.root_bank.to_bytes().to_vec(), root_bank: encode_address(&token.root_bank),
decimals: token.decimals as i16, decimals: token.decimals as i16,
padding: token.padding.to_vec(), padding: token.padding.to_vec(),
}) })
@ -447,7 +447,7 @@ impl AccountTable for MangoGroupTable {
.spot_markets .spot_markets
.iter() .iter()
.map(|market| SpotMarketInfo { .map(|market| SpotMarketInfo {
spot_market: market.spot_market.to_bytes().to_vec(), spot_market: encode_address(&market.spot_market),
maint_asset_weight: SqlNumericI80F48(market.maint_asset_weight), maint_asset_weight: SqlNumericI80F48(market.maint_asset_weight),
init_asset_weight: SqlNumericI80F48(market.init_asset_weight), init_asset_weight: SqlNumericI80F48(market.init_asset_weight),
maint_liab_weight: SqlNumericI80F48(market.maint_liab_weight), maint_liab_weight: SqlNumericI80F48(market.maint_liab_weight),
@ -459,7 +459,7 @@ impl AccountTable for MangoGroupTable {
.perp_markets .perp_markets
.iter() .iter()
.map(|market| PerpMarketInfo { .map(|market| PerpMarketInfo {
perp_market: market.perp_market.to_bytes().to_vec(), perp_market: encode_address(&market.perp_market),
maint_asset_weight: SqlNumericI80F48(market.maint_asset_weight), maint_asset_weight: SqlNumericI80F48(market.maint_asset_weight),
init_asset_weight: SqlNumericI80F48(market.init_asset_weight), init_asset_weight: SqlNumericI80F48(market.init_asset_weight),
maint_liab_weight: SqlNumericI80F48(market.maint_liab_weight), maint_liab_weight: SqlNumericI80F48(market.maint_liab_weight),
@ -474,18 +474,18 @@ impl AccountTable for MangoGroupTable {
let oracles = data let oracles = data
.oracles .oracles
.iter() .iter()
.map(|key| key.to_bytes().to_vec()) .map(|key| encode_address(&key))
.collect::<Vec<Vec<u8>>>(); .collect::<Vec<String>>();
let signer_nonce = SqlNumericU64(data.signer_nonce); let signer_nonce = SqlNumericU64(data.signer_nonce);
let signer_key: &[u8] = &data.signer_key.to_bytes(); let signer_key = encode_address(&data.signer_key);
let admin: &[u8] = &data.admin.to_bytes(); let admin = encode_address(&data.admin);
let dex_program_id: &[u8] = &data.dex_program_id.to_bytes(); let dex_program_id = encode_address(&data.dex_program_id);
let mango_cache: &[u8] = &data.mango_cache.to_bytes(); let mango_cache = encode_address(&data.mango_cache);
let valid_interval = SqlNumericU64(data.valid_interval); let valid_interval = SqlNumericU64(data.valid_interval);
let insurance_vault: &[u8] = &data.insurance_vault.to_bytes(); let insurance_vault = encode_address(&data.insurance_vault);
let srm_vault: &[u8] = &data.srm_vault.to_bytes(); let srm_vault = encode_address(&data.srm_vault);
let msrm_vault: &[u8] = &data.msrm_vault.to_bytes(); let msrm_vault = encode_address(&data.msrm_vault);
let fees_vault: &[u8] = &data.fees_vault.to_bytes(); let fees_vault = encode_address(&data.fees_vault);
let padding = &data.padding as &[u8]; let padding = &data.padding as &[u8];
let query = postgres_query::query!( let query = postgres_query::query!(
@ -574,7 +574,7 @@ impl AccountTable for MangoCacheTable {
// TODO: This one can't be fitlered to only use the one for our mango_group? // TODO: This one can't be fitlered to only use the one for our mango_group?
let pubkey: &[u8] = &account_write.pubkey.to_bytes(); let pubkey = encode_address(&account_write.pubkey);
let data = MangoCache::load_from_bytes(&account_write.data)?; let data = MangoCache::load_from_bytes(&account_write.data)?;
let version = data.meta_data.version as i16; let version = data.meta_data.version as i16;
let extra_info = &data.meta_data.extra_info as &[u8]; let extra_info = &data.meta_data.extra_info as &[u8];

View File

@ -4,10 +4,10 @@
-- The table storing account writes, keeping only the newest write_version per slot -- The table storing account writes, keeping only the newest write_version per slot
CREATE TABLE account_write ( CREATE TABLE account_write (
pubkey BYTEA NOT NULL, pubkey VARCHAR(44) NOT NULL,
slot BIGINT NOT NULL, slot BIGINT NOT NULL,
write_version BIGINT NOT NULL, write_version BIGINT NOT NULL,
owner BYTEA, owner VARCHAR(44),
lamports BIGINT NOT NULL, lamports BIGINT NOT NULL,
executable BOOL NOT NULL, executable BOOL NOT NULL,
rent_epoch BIGINT NOT NULL, rent_epoch BIGINT NOT NULL,
@ -19,7 +19,7 @@ CREATE TABLE account_write (
CREATE TABLE slot ( CREATE TABLE slot (
slot BIGINT PRIMARY KEY, slot BIGINT PRIMARY KEY,
parent BIGINT, parent BIGINT,
status varchar(16) NOT NULL, status VARCHAR(16) NOT NULL,
uncle BOOL NOT NULL uncle BOOL NOT NULL
); );
@ -36,19 +36,19 @@ CREATE TYPE "PerpAccount" AS (
); );
CREATE TABLE mango_account_write ( CREATE TABLE mango_account_write (
pubkey BYTEA NOT NULL, pubkey VARCHAR(44) NOT NULL,
slot BIGINT NOT NULL, slot BIGINT NOT NULL,
write_version BIGINT NOT NULL, write_version BIGINT NOT NULL,
version INT2, version INT2,
is_initialized BOOL, is_initialized BOOL,
extra_info BYTEA, extra_info BYTEA,
mango_group BYTEA, mango_group VARCHAR(44),
owner BYTEA, owner VARCHAR(44),
in_margin_basket BOOL[], in_margin_basket BOOL[],
num_in_margin_basket INT2, num_in_margin_basket INT2,
deposits NUMERIC[], -- I80F48[] deposits NUMERIC[], -- I80F48[]
borrows NUMERIC[], -- I80F48[] borrows NUMERIC[], -- I80F48[]
spot_open_orders BYTEA[], spot_open_orders VARCHAR(44)[],
perp_accounts "PerpAccount"[], perp_accounts "PerpAccount"[],
order_market INT2[], order_market INT2[],
order_side INT2[], order_side INT2[],
@ -58,21 +58,21 @@ CREATE TABLE mango_account_write (
being_liquidated BOOL, being_liquidated BOOL,
is_bankrupt BOOL, is_bankrupt BOOL,
info BYTEA, info BYTEA,
advanced_orders_key BYTEA, advanced_orders_key VARCHAR(44),
padding BYTEA, padding BYTEA,
PRIMARY KEY (pubkey, slot, write_version) PRIMARY KEY (pubkey, slot, write_version)
); );
CREATE TYPE "TokenInfo" AS ( CREATE TYPE "TokenInfo" AS (
mint BYTEA, mint VARCHAR(44),
root_bank BYTEA, root_bank VARCHAR(44),
decimals INT2, decimals INT2,
padding BYTEA padding BYTEA
); );
CREATE TYPE "SpotMarketInfo" AS ( CREATE TYPE "SpotMarketInfo" AS (
spot_market BYTEA, spot_market VARCHAR(44),
maint_asset_weight NUMERIC, -- all I80F48 maint_asset_weight NUMERIC, -- all I80F48
init_asset_weight NUMERIC, init_asset_weight NUMERIC,
maint_liab_weight NUMERIC, maint_liab_weight NUMERIC,
@ -81,7 +81,7 @@ CREATE TYPE "SpotMarketInfo" AS (
); );
CREATE TYPE "PerpMarketInfo" AS ( CREATE TYPE "PerpMarketInfo" AS (
perp_market BYTEA, perp_market VARCHAR(44),
maint_asset_weight NUMERIC, -- all I80F48 maint_asset_weight NUMERIC, -- all I80F48
init_asset_weight NUMERIC, init_asset_weight NUMERIC,
maint_liab_weight NUMERIC, maint_liab_weight NUMERIC,
@ -94,7 +94,7 @@ CREATE TYPE "PerpMarketInfo" AS (
); );
CREATE TABLE mango_group_write ( CREATE TABLE mango_group_write (
pubkey BYTEA NOT NULL, pubkey VARCHAR(44) NOT NULL,
slot BIGINT NOT NULL, slot BIGINT NOT NULL,
write_version BIGINT NOT NULL, write_version BIGINT NOT NULL,
version INT2, version INT2,
@ -104,17 +104,17 @@ CREATE TABLE mango_group_write (
tokens "TokenInfo"[], tokens "TokenInfo"[],
spot_markets "SpotMarketInfo"[], spot_markets "SpotMarketInfo"[],
perp_markets "PerpMarketInfo"[], perp_markets "PerpMarketInfo"[],
oracles BYTEA[], oracles VARCHAR(44)[],
signer_nonce NUMERIC, -- u64 signer_nonce NUMERIC, -- u64
signer_key BYTEA, signer_key VARCHAR(44),
"admin" BYTEA, "admin" VARCHAR(44),
dex_program_id BYTEA, dex_program_id VARCHAR(44),
mango_cache BYTEA, mango_cache VARCHAR(44),
valid_interval NUMERIC, -- u64 valid_interval NUMERIC, -- u64
insurance_vault BYTEA, insurance_vault VARCHAR(44),
srm_vault BYTEA, srm_vault VARCHAR(44),
msrm_vault BYTEA, msrm_vault VARCHAR(44),
fees_vault BYTEA, fees_vault VARCHAR(44),
padding BYTEA, padding BYTEA,
PRIMARY KEY (pubkey, slot, write_version) PRIMARY KEY (pubkey, slot, write_version)
); );
@ -137,7 +137,7 @@ CREATE TYPE "PerpMarketCache" AS (
); );
CREATE TABLE mango_cache_write ( CREATE TABLE mango_cache_write (
pubkey BYTEA NOT NULL, pubkey VARCHAR(44) NOT NULL,
slot BIGINT NOT NULL, slot BIGINT NOT NULL,
write_version BIGINT NOT NULL, write_version BIGINT NOT NULL,
version INT2, version INT2,