@@ -1383,28 +1383,51 @@ export class PgWriteStore extends PgStore {
13831383 for ( const { tx, stxEvents, ftEvents, nftEvents } of txs ) {
13841384 // Mark principals who participated in this transaction, along with the type of token balance
13851385 // they affected.
1386- const principals = new Map < string , { stx : boolean ; ft : boolean ; nft : boolean } > ( ) ;
1387- const addPrincipal = ( principal : string , affected ?: 'stx' | 'ft' | 'nft' ) => {
1388- const flags = principals . get ( principal ) || { stx : false , ft : false , nft : false } ;
1389- if ( affected ) {
1390- principals . set ( principal , { ...flags , [ affected ] : true } ) ;
1386+ const principals = new Map <
1387+ string ,
1388+ { stx : boolean ; ft : boolean ; nft : boolean ; stx_sent : bigint ; stx_received : bigint }
1389+ > ( ) ;
1390+ const addPrincipal = (
1391+ principal : string ,
1392+ assetType ?: 'stx' | 'ft' | 'nft' ,
1393+ stx_sent ?: bigint ,
1394+ stx_received ?: bigint
1395+ ) => {
1396+ const sent = stx_sent ?? BigInt ( 0 ) ;
1397+ const received = stx_received ?? BigInt ( 0 ) ;
1398+ const entry = principals . get ( principal ) || {
1399+ stx : false ,
1400+ ft : false ,
1401+ nft : false ,
1402+ stx_sent : sent ,
1403+ stx_received : received ,
1404+ } ;
1405+ if ( assetType ) {
1406+ principals . set ( principal , {
1407+ ...entry ,
1408+ [ assetType ] : true ,
1409+ stx_sent : entry . stx_sent + sent ,
1410+ stx_received : entry . stx_received + received ,
1411+ } ) ;
13911412 } else {
1392- principals . set ( principal , flags ) ;
1413+ principals . set ( principal , entry ) ;
13931414 }
13941415 } ;
13951416
1396- // Add sender but only mark as stx if not sponsored, otherwise they didn't pay a fee
1417+ // Add sender but only mark as stx if not sponsored, otherwise they didn't pay a fee. Do not
1418+ // record amounts yet because that will be included in stx_events below.
13971419 addPrincipal ( tx . sender_address , tx . sponsor_address ? undefined : 'stx' ) ;
1398- if ( tx . sponsor_address ) addPrincipal ( tx . sponsor_address , 'stx' ) ;
1420+ if ( tx . sponsor_address ) addPrincipal ( tx . sponsor_address , 'stx' , BigInt ( tx . fee_rate ) ) ;
13991421 if ( tx . token_transfer_recipient_address )
14001422 addPrincipal ( tx . token_transfer_recipient_address , 'stx' ) ;
1401- // Add contract call and smart contract ids, no stx spent
1423+
1424+ // Add contract call and smart contract ids, no stx spent yet.
14021425 if ( tx . contract_call_contract_id ) addPrincipal ( tx . contract_call_contract_id ) ;
14031426 if ( tx . smart_contract_contract_id ) addPrincipal ( tx . smart_contract_contract_id ) ;
14041427
14051428 for ( const event of stxEvents ) {
1406- if ( event . sender ) addPrincipal ( event . sender , 'stx' ) ;
1407- if ( event . recipient ) addPrincipal ( event . recipient , 'stx' ) ;
1429+ if ( event . sender ) addPrincipal ( event . sender , 'stx' , event . amount ) ;
1430+ if ( event . recipient ) addPrincipal ( event . recipient , 'stx' , BigInt ( 0 ) , event . amount ) ;
14081431 }
14091432 for ( const event of ftEvents ) {
14101433 if ( event . sender ) addPrincipal ( event . sender , 'ft' ) ;
@@ -1414,7 +1437,7 @@ export class PgWriteStore extends PgStore {
14141437 if ( event . sender ) addPrincipal ( event . sender , 'nft' ) ;
14151438 if ( event . recipient ) addPrincipal ( event . recipient , 'nft' ) ;
14161439 }
1417- for ( const [ principal , { stx, ft, nft } ] of principals . entries ( ) ) {
1440+ for ( const [ principal , { stx, ft, nft, stx_sent , stx_received } ] of principals . entries ( ) ) {
14181441 values . push ( {
14191442 principal,
14201443 tx_id : tx . tx_id ,
@@ -1428,6 +1451,8 @@ export class PgWriteStore extends PgStore {
14281451 stx_balance_affected : stx ,
14291452 ft_balance_affected : ft ,
14301453 nft_balance_affected : nft ,
1454+ stx_sent,
1455+ stx_received,
14311456 } ) ;
14321457 }
14331458 }
0 commit comments