fix: error reading contract abi #850

This commit is contained in:
M Hassan Tariq
2021-12-02 16:24:39 +05:00
committed by GitHub
parent 216b9ab715
commit f9b4e72517
4 changed files with 328 additions and 45 deletions

View File

@@ -927,9 +927,11 @@ export async function getTxFromDataStore(
function parseContractsWithDbTxs(contracts: DbSmartContract[], dbTxs: DbTx[]): Transaction[] {
const transactions: Transaction[] = [];
contracts.forEach(contract => {
const dbTx = dbTxs.find(tx => tx.contract_call_contract_id === contract.contract_id);
if (dbTx) {
dbTxs.forEach(dbTx => {
const contract = contracts.find(
contract => contract.contract_id === dbTx.contract_call_contract_id
);
if (contract) {
const transaction = parseContractCallMetadata(
{ found: true, result: contract },
parseDbTx(dbTx) as ContractCallTransaction,
@@ -938,6 +940,8 @@ function parseContractsWithDbTxs(contracts: DbSmartContract[], dbTxs: DbTx[]): T
if (transaction) {
transactions.push(transaction as Transaction);
}
} else {
transactions.push(parseDbTx(dbTx));
}
});
return transactions;
@@ -948,17 +952,21 @@ function parseContractsWithMempoolTxs(
dbMempoolTx: DbMempoolTx[]
): MempoolTransaction[] {
const transactions: MempoolTransaction[] = [];
contracts.forEach(contract => {
const dbMempool = dbMempoolTx.find(tx => tx.contract_call_contract_id === contract.contract_id);
if (dbMempool) {
dbMempoolTx.forEach(dbMempoolTx => {
const contract = contracts.find(
contract => contract.contract_id === dbMempoolTx.contract_call_contract_id
);
if (contract) {
const transaction = parseContractCallMetadata(
{ found: true, result: contract },
parseDbMempoolTx(dbMempool) as MempoolContractCallTransaction,
dbMempool
parseDbMempoolTx(dbMempoolTx) as MempoolContractCallTransaction,
dbMempoolTx
);
if (transaction) {
transactions.push(transaction as MempoolTransaction);
}
} else {
transactions.push(parseDbMempoolTx(dbMempoolTx));
}
});
return transactions;
@@ -1000,6 +1008,9 @@ function parseContractCallMetadata(
if (!contract.found) {
throw new Error(`Failed to lookup smart contract by ID ${parsedTx.contract_call.contract_id}`);
}
if (!contract.result.abi) {
return parsedTx;
}
const contractAbi: ClarityAbi = JSON.parse(contract.result.abi);
const functionAbi = contractAbi.functions.find(
fn => fn.name === parsedTx.contract_call.function_name

View File

@@ -4545,18 +4545,18 @@ export class PgDataStore
async getSmartContractList(contractIds: string[]) {
return this.query(async client => {
const result = await client.query<{
tx_id: Buffer;
canonical: boolean;
contract_id: string;
canonical: boolean;
tx_id: Buffer;
block_height: number;
source_code: string;
abi: string;
}>(
`
SELECT tx_id, canonical, contract_id, block_height, source_code, abi
FROM smart_contracts
WHERE contract_id = ANY($1)
ORDER BY abi != 'null' DESC, canonical DESC, microblock_canonical DESC, block_height DESC
SELECT DISTINCT ON (contract_id) contract_id, canonical, tx_id, block_height, source_code, abi
FROM smart_contracts
WHERE contract_id = ANY($1)
ORDER BY contract_id DESC, abi != 'null' DESC, canonical DESC, microblock_canonical DESC, block_height DESC
`,
[contractIds]
);

View File

@@ -62,4 +62,11 @@ export async function up(pgm: MigrationBuilder): Promise<void> {
pgm.createIndex('smart_contracts', 'canonical');
pgm.createIndex('smart_contracts', 'contract_id');
pgm.createIndex('smart_contracts', [
{ name: 'contract_id', sort: 'DESC' },
{ name: 'canonical', sort: 'DESC' },
{ name: 'microblock_canonical', sort: 'DESC' },
{ name: 'block_height', sort: 'DESC' },
{ name: 'abi', sort: 'DESC' }
]);
}

File diff suppressed because one or more lines are too long