Skip to content

Commit

Permalink
Merge pull request #157 from ainblockchain/feature/platfowner/feature
Browse files Browse the repository at this point in the history
Add missing methods for transactions
  • Loading branch information
platfowner authored Feb 26, 2024
2 parents 5188c68 + bd8d90d commit 56ba075
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
31 changes: 27 additions & 4 deletions __tests__/ain.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -535,8 +535,8 @@ describe('ain-js', function() {
})
});

it('getTransaction for sendTransaction with isDryrun = true', async function () {
const tx = await ain.getTransaction(targetTxHash);
it('getTransactionByHash for sendTransaction with isDryrun = true', async function () {
const tx = await ain.getTransactionByHash(targetTxHash);
expect(tx).toStrictEqual(null); // The tx is NOT in the blockchain.
});

Expand All @@ -553,8 +553,8 @@ describe('ain-js', function() {
})
});

it('getTransaction for sendTransaction', async function () {
const tx = await ain.getTransaction(targetTxHash);
it('getTransactionByHash for sendTransaction', async function () {
const tx = await ain.getTransactionByHash(targetTxHash);
expect(tx.transaction.tx_body.operation).toStrictEqual(targetTx);
});

Expand Down Expand Up @@ -781,6 +781,29 @@ describe('ain-js', function() {
expect(thrownError.code).toEqual(30401);
expect(thrownError.message).toEqual('Invalid batch transaction format.');
});

it('getPendingTransactions', async function () {
const txs = await ain.getPendingTransactions();
expect(txs).not.toBeNull();
});

it('getTransactionPoolSizeUtilization', async function () {
const txs = await ain.getTransactionPoolSizeUtilization();
expect(txs).not.toBeNull();
});

it('getTransactionByBlockHashAndIndex', async function () {
const genesisBlockNumber = 0;
const genesisBlock = await ain.getBlock(genesisBlockNumber);
const tx = await ain.getTransactionByBlockHashAndIndex(genesisBlock.hash, 0);
expect(tx).not.toBeNull();
});

it('getTransactionByBlockNumberAndIndex', async function () {
const genesisBlockNumber = 0;
const tx = await ain.getTransactionByBlockNumberAndIndex(genesisBlockNumber, 0);
expect(tx).not.toBeNull();
});
});

describe('Database', function() {
Expand Down
38 changes: 37 additions & 1 deletion src/ain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,15 +124,51 @@ export default class Ain {
{[byHash ? 'hash' : 'number']: blockHashOrBlockNumber});
}

/**
* Fetches pending transaction.
* @returns {Promise<any>}
*/
getPendingTransactions(): Promise<any> {
return this.provider.send('ain_getPendingTransactions', {});
}

/**
* Fetches transaction pool size utilization.
* @returns {Promise<any>}
*/
getTransactionPoolSizeUtilization(): Promise<any> {
return this.provider.send('ain_getTransactionPoolSizeUtilization', {});
}

/**
* Fetches a transaction's information with a transaction hash.
* @param {string} transactionHash The transaction hash.
* @returns {Promise<TransactionInfo>}
*/
getTransaction(transactionHash: string): Promise<TransactionInfo> {
getTransactionByHash(transactionHash: string): Promise<TransactionInfo> {
return this.provider.send('ain_getTransactionByHash', { hash: transactionHash });
}

/**
* Fetches a transaction's information with a block hash and an index.
* @param {string} blockHash The block hash.
* @param {number} index The transaction index in the block
* @returns {Promise<TransactionInfo>}
*/
getTransactionByBlockHashAndIndex(blockHash: string, index: Number): Promise<TransactionInfo> {
return this.provider.send('ain_getTransactionByBlockHashAndIndex', { block_hash: blockHash, index });
}

/**
* Fetches a transaction's information with a block hash and an index.
* @param {string} blockNumber The block number.
* @param {number} index The transaction index in the block
* @returns {Promise<TransactionInfo>}
*/
getTransactionByBlockNumberAndIndex(blockNumber: Number, index: Number): Promise<TransactionInfo> {
return this.provider.send('ain_getTransactionByBlockNumberAndIndex', { block_number: blockNumber, index });
}

/**
* Fetches a blockchain app's state usage information with an app name.
* @param {string} appName The blockchain app name.
Expand Down

0 comments on commit 56ba075

Please sign in to comment.