@@ -37,9 +37,9 @@ use crate::storage::{
37
37
impls:: CURRENT_STORAGE_VERSION ,
38
38
storage_api:: {
39
39
block_aux_data:: { BlockAuxData , BlockWithExtraData } ,
40
- ApiServerStorageError , BlockInfo , CoinOrTokenStatistic , Delegation , FungibleTokenData ,
41
- LockedUtxo , NftWithOwner , Order , PoolBlockStats , PoolDataWithExtraInfo , TransactionInfo ,
42
- Utxo , UtxoWithExtraInfo ,
40
+ AmountWithDecimals , ApiServerStorageError , BlockInfo , CoinOrTokenStatistic , Delegation ,
41
+ FungibleTokenData , LockedUtxo , NftWithOwner , Order , PoolBlockStats , PoolDataWithExtraInfo ,
42
+ TransactionInfo , Utxo , UtxoWithExtraInfo ,
43
43
} ,
44
44
} ;
45
45
@@ -172,7 +172,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
172
172
pub async fn get_address_balances (
173
173
& self ,
174
174
address : & str ,
175
- ) -> Result < Vec < ( CoinOrTokenId , Amount , u8 ) > , ApiServerStorageError > {
175
+ ) -> Result < BTreeMap < CoinOrTokenId , AmountWithDecimals > , ApiServerStorageError > {
176
176
let rows = self
177
177
. tx
178
178
. query (
@@ -207,7 +207,13 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
207
207
) )
208
208
} ) ?;
209
209
210
- Ok ( ( coin_or_token_id, amount, number_of_decimals as u8 ) )
210
+ Ok ( (
211
+ coin_or_token_id,
212
+ AmountWithDecimals {
213
+ amount,
214
+ decimals : number_of_decimals as u8 ,
215
+ } ,
216
+ ) )
211
217
} )
212
218
. collect ( )
213
219
}
@@ -339,7 +345,8 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
339
345
LIMIT 1)
340
346
)
341
347
ON CONFLICT (address, coin_or_token_id) DO UPDATE
342
- SET block_height = $2, amount = $4;
348
+ SET block_height = EXCLUDED.block_height, amount = EXCLUDED.amount
349
+ WHERE ml.latest_address_balance_cache.block_height < EXCLUDED.block_height;
343
350
"# ,
344
351
& [ & address. to_string ( ) , & height, & coin_or_token_id. encode ( ) , & amount. encode ( ) ] ,
345
352
)
@@ -352,7 +359,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
352
359
INSERT INTO ml.address_balance (address, block_height, coin_or_token_id, amount)
353
360
VALUES ($1, $2, $3, $4)
354
361
ON CONFLICT (address, block_height, coin_or_token_id)
355
- DO UPDATE SET amount = $4 ;
362
+ DO UPDATE SET amount = EXCLUDED.amount ;
356
363
"# ,
357
364
& [ & address. to_string ( ) , & height, & coin_or_token_id. encode ( ) , & amount. encode ( ) ] ,
358
365
)
@@ -1277,7 +1284,12 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
1277
1284
INSERT INTO ml.latest_delegations_cache (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce, creation_block_height)
1278
1285
VALUES($1, $2, $3, $4, $5, $6, $7)
1279
1286
ON CONFLICT (pool_id, delegation_id) DO UPDATE
1280
- SET block_height = $2, balance = $4, spend_destination = $5, next_nonce = $6, creation_block_height = $7;
1287
+ SET block_height = EXCLUDED.block_height,
1288
+ balance = EXCLUDED.balance,
1289
+ spend_destination = EXCLUDED.spend_destination,
1290
+ next_nonce = EXCLUDED.next_nonce,
1291
+ creation_block_height = EXCLUDED.creation_block_height
1292
+ WHERE ml.latest_delegations_cache.block_height < EXCLUDED.block_height;
1281
1293
"# ,
1282
1294
& [
1283
1295
& delegation_id. as_str ( ) ,
@@ -1298,7 +1310,11 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
1298
1310
INSERT INTO ml.delegations (delegation_id, block_height, pool_id, balance, spend_destination, next_nonce, creation_block_height)
1299
1311
VALUES($1, $2, $3, $4, $5, $6, $7)
1300
1312
ON CONFLICT (delegation_id, block_height) DO UPDATE
1301
- SET pool_id = $3, balance = $4, spend_destination = $5, next_nonce = $6, creation_block_height = $7;
1313
+ SET pool_id = EXCLUDED.pool_id,
1314
+ balance = EXCLUDED.balance,
1315
+ spend_destination = EXCLUDED.spend_destination,
1316
+ next_nonce = EXCLUDED.next_nonce,
1317
+ creation_block_height = EXCLUDED.creation_block_height;
1302
1318
"# ,
1303
1319
& [
1304
1320
& delegation_id. as_str ( ) ,
@@ -2078,9 +2094,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
2078
2094
2079
2095
self . tx
2080
2096
. execute (
2081
- "INSERT INTO ml.fungible_token (token_id, block_height, issuance, ticker) VALUES ($1, $2, $3, $4)
2082
- ON CONFLICT (token_id, block_height) DO UPDATE
2083
- SET issuance = $3, ticker = $4;" ,
2097
+ "INSERT INTO ml.fungible_token (token_id, block_height, issuance, ticker) VALUES ($1, $2, $3, $4);" ,
2084
2098
& [
2085
2099
& token_id. encode ( ) ,
2086
2100
& height,
@@ -2094,7 +2108,7 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
2094
2108
let coin_or_token_id = CoinOrTokenId :: TokenId ( token_id) ;
2095
2109
self . tx
2096
2110
. execute (
2097
- "INSERT INTO ml.coin_or_token_decimals (coin_or_token_id, block_height, number_of_decimals) VALUES ($1, $2, $3) ON CONFLICT (coin_or_token_id) DO NOTHING ;" ,
2111
+ "INSERT INTO ml.coin_or_token_decimals (coin_or_token_id, block_height, number_of_decimals) VALUES ($1, $2, $3);" ,
2098
2112
& [ & coin_or_token_id. encode ( ) , & height, & ( issuance. number_of_decimals as i16 ) ] ,
2099
2113
)
2100
2114
. await
@@ -2103,6 +2117,31 @@ impl<'a, 'b> QueryFromConnection<'a, 'b> {
2103
2117
Ok ( ( ) )
2104
2118
}
2105
2119
2120
+ pub async fn set_fungible_token_data (
2121
+ & mut self ,
2122
+ token_id : TokenId ,
2123
+ block_height : BlockHeight ,
2124
+ issuance : FungibleTokenData ,
2125
+ ) -> Result < ( ) , ApiServerStorageError > {
2126
+ let height = Self :: block_height_to_postgres_friendly ( block_height) ;
2127
+
2128
+ self . tx
2129
+ . execute (
2130
+ "INSERT INTO ml.fungible_token (token_id, block_height, issuance, ticker) VALUES ($1, $2, $3, $4)
2131
+ ON CONFLICT (token_id, block_height) DO UPDATE
2132
+ SET issuance = $3, ticker = $4;" ,
2133
+ & [
2134
+ & token_id. encode ( ) ,
2135
+ & height,
2136
+ & issuance. encode ( ) ,
2137
+ & issuance. token_ticker ,
2138
+ ] ,
2139
+ )
2140
+ . await
2141
+ . map_err ( |e| ApiServerStorageError :: LowLevelStorageError ( e. to_string ( ) ) ) ?;
2142
+ Ok ( ( ) )
2143
+ }
2144
+
2106
2145
pub async fn get_fungible_token_issuance (
2107
2146
& self ,
2108
2147
token_id : TokenId ,
0 commit comments