Skip to content

Commit

Permalink
Batch calls for recent transactions (MystenLabs#1850)
Browse files Browse the repository at this point in the history
* add getTransactionWithEffectsBatch

* change recent transaction card to use batch json-rpc

* remove excess type label

* remove extra whitespace

* set loaded to false if errors happen loading transactions

* tweak error message for getting latest transactions

* fix type annotation
  • Loading branch information
Stella Cannefax authored May 7, 2022
1 parent d290a43 commit 402ed43
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 49 deletions.
87 changes: 38 additions & 49 deletions explorer/client/src/components/transaction-card/RecentTxCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,58 +45,46 @@ type TxnData = {
async function getRecentTransactions(txNum: number): Promise<TxnData[]> {
try {
// Get the latest transactions
// TODO add batch transaction kind TransactionDigest
// TODO sui.js to get the latest transactions meta data
const transactions = await rpc
.getRecentTransactions(txNum)
.then((res: GetTxnDigestsResponse) => res);

const txLatest = await Promise.all(
transactions.map(async (tx) => {
const [seq, digest] = tx;
return await rpc
.getTransactionWithEffects(digest)
.then((txEff: TransactionEffectsResponse) => {
const res: CertifiedTransaction = txEff.certificate;
const singleTransaction = getSingleTransactionKind(
res.data
const digests = transactions.map((tx) => tx[1]);
const txLatest = await rpc
.getTransactionWithEffectsBatch(digests)
.then((txEffs: TransactionEffectsResponse[]) => {
return txEffs.map((txEff, i) => {
const [seq, digest] = transactions[i];
const res: CertifiedTransaction = txEff.certificate;
const singleTransaction = getSingleTransactionKind(
res.data
);
if (!singleTransaction) {
throw new Error(
`Transaction kind not supported yet ${res.data.kind}`
);
if (!singleTransaction) {
throw new Error(
`Transaction kind not supported yet ${res.data.kind}`
);
}
const txKind = getTransactionKind(res.data);
const recipient = getTransferTransaction(
res.data
)?.recipient;

return {
seq,
txId: digest,
status: getExecutionStatusType(
txEff.effects.status
),
txGas: getTotalGasUsed(txEff.effects.status),
kind: txKind,
From: res.data.sender,
...(recipient
? {
To: recipient,
}
: {}),
};
})
.catch((err) => {
console.error(
'Failed to get transaction details for txn digest',
digest,
err
);
return null;
});
})
);
}
const txKind = getTransactionKind(res.data);
const recipient = getTransferTransaction(
res.data
)?.recipient;

return {
seq,
txId: digest,
status: getExecutionStatusType(txEff.effects.status),
txGas: getTotalGasUsed(txEff.effects.status),
kind: txKind,
From: res.data.sender,
...(recipient
? {
To: recipient,
}
: {}),
};
});
});

// Remove failed transactions and sort by sequence number
return txLatest
.filter((itm) => itm)
Expand Down Expand Up @@ -143,6 +131,7 @@ function LatestTxCard() {
...initState,
loadState: 'fail',
});
setIsLoaded(false);
});

return () => {
Expand All @@ -160,8 +149,8 @@ function LatestTxCard() {
if (!isLoaded && results.loadState === 'fail') {
return (
<ErrorResult
id="latestTx"
errorMsg="There was an issue getting the latest transaction"
id=""
errorMsg="There was an issue getting the latest transactions"
/>
);
}
Expand Down
18 changes: 18 additions & 0 deletions sdk/typescript/src/providers/json-rpc-provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ export class JsonRpcProvider extends Provider {
}
}

async getTransactionWithEffectsBatch(
digests: TransactionDigest[]
): Promise<TransactionEffectsResponse[]> {
const requests = digests.map(d => ({
method: 'sui_getTransaction',
args: [d],
}));
try {
return await this.client.batchRequestWithType(
requests,
isTransactionEffectsResponse
);
} catch (err) {
const list = digests.join(', ').substring(0, -2);
throw new Error(`Error getting transaction effects: ${err} for digests [${list}]`);
}
}

async getTransaction(
digest: TransactionDigest
): Promise<CertifiedTransaction> {
Expand Down

0 comments on commit 402ed43

Please sign in to comment.