From 196f6120ce46487770206f1ceb1769d18bee488c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rafael=20C=C3=A1rdenas?= Date: Fri, 10 Dec 2021 14:18:35 -0600 Subject: [PATCH] fix: fetch abi on tx /with-transfers (#895) --- src/datastore/postgres-store.ts | 17 ++++++-- src/tests/api-tests.ts | 74 +++++++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 3 deletions(-) diff --git a/src/datastore/postgres-store.ts b/src/datastore/postgres-store.ts index b7f7ba79..17518172 100644 --- a/src/datastore/postgres-store.ts +++ b/src/datastore/postgres-store.ts @@ -5331,9 +5331,13 @@ export class PgDataStore tx_id: string; }): Promise { return this.query(async client => { - const queryParams: (string | Buffer)[] = [stxAddress, hexToBuffer(tx_id)]; + const queryParams: (string | Buffer | DbTxTypeId)[] = [ + stxAddress, + hexToBuffer(tx_id), + DbTxTypeId.ContractCall, + ]; const resultQuery = await client.query< - TxQueryResult & { + ContractTxQueryResult & { count: number; event_index?: number; event_type?: number; @@ -5375,7 +5379,14 @@ export class PgDataStore events.event_type_id as event_type, events.amount as event_amount, events.sender as event_sender, - events.recipient as event_recipient + events.recipient as event_recipient, + CASE WHEN transactions.type_id = $3 THEN ( + SELECT abi + FROM smart_contracts + WHERE smart_contracts.contract_id = transactions.contract_call_contract_id + ORDER BY abi != 'null' DESC, canonical DESC, microblock_canonical DESC, block_height DESC + LIMIT 1 + ) END as abi FROM transactions LEFT JOIN events ON transactions.tx_id = events.tx_id AND transactions.tx_id = $2 ORDER BY block_height DESC, microblock_sequence DESC, tx_index DESC, event_index DESC diff --git a/src/tests/api-tests.ts b/src/tests/api-tests.ts index 0db0ba5b..f1a35fe4 100644 --- a/src/tests/api-tests.ts +++ b/src/tests/api-tests.ts @@ -4823,6 +4823,80 @@ describe('api tests', () => { ], }; expect(JSON.parse(fetchAddrTx3.text)).toEqual(expectedResp6); + + const fetchAddrTx4 = await supertest(api.server).get( + `/extended/v1/address/${testAddr5}/0x1232/with_transfers` + ); + expect(fetchAddrTx4.status).toBe(200); + expect(fetchAddrTx4.type).toBe('application/json'); + const expectedResp7 = { + stx_received: '0', + stx_sent: '4321', + stx_transfers: [ + { + amount: '4321', + recipient: 'ST2F8G7616B2F8PYG216BX9AJCHP7YRK7ND7M0ZN3', + sender: 'ST3V11C6X2EBFN72RMS3B1NYQ1BX98F61GVYRDRXW', + }, + ], + tx: { + anchor_mode: 'any', + block_hash: '0x1234', + block_height: 1, + burn_block_time: 39486, + burn_block_time_iso: '1970-01-01T10:58:06.000Z', + canonical: true, + contract_call: { + contract_id: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world', + function_args: [ + { + hex: '0x010000000000000000000000000001e240', + name: 'amount', + repr: 'u123456', + type: 'uint', + }, + { + hex: '0x0d0000000568656c6c6f', + name: 'desc', + repr: '"hello"', + type: 'string-ascii', + }, + ], + function_name: 'test-contract-fn', + function_signature: + '(define-public (test-contract-fn (amount uint) (desc string-ascii)))', + }, + event_count: 5, + execution_cost_read_count: 0, + execution_cost_read_length: 0, + execution_cost_runtime: 0, + execution_cost_write_count: 0, + execution_cost_write_length: 0, + fee_rate: '10', + is_unanchored: false, + microblock_canonical: true, + microblock_hash: '', + microblock_sequence: 2147483647, + nonce: 0, + parent_block_hash: '', + parent_burn_block_time: 1626122935, + parent_burn_block_time_iso: '2021-07-12T20:48:55.000Z', + post_condition_mode: 'allow', + post_conditions: [], + sender_address: 'ST27W5M8BRKA7C5MZE2R1S1F4XTPHFWFRNHA9M04Y.hello-world', + sponsor_address: 'ST3J8EVYHVKH6XXPD61EE8XEHW4Y2K83861225AB1', + sponsored: false, + tx_id: '0x1232', + tx_index: 5, + tx_result: { + hex: '0x0100000000000000000000000000000001', + repr: 'u1', + }, + tx_status: 'success', + tx_type: 'contract_call', + }, + }; + expect(JSON.parse(fetchAddrTx4.text)).toEqual(expectedResp7); }); test('list contract log events', async () => {