This tutorial shows how to look up Checks by their sender or recipient, in JavaScript.
- You should be familiar with the basics of using the xrpl.js client library.
- To get any results, the addresses you're looking up must have at least one Check entry in the ledger. See also: Send a Check.
The complete source code for this tutorial is available in the source repository for this website:
To get a list of all incoming and outgoing Checks for an account, use the account_objects command and set the type field of the request to checks. You may need to make multiple requests if the result is paginated.
// Loop through account objects until marker is undefined ----------------------
let currentMarker = null
let checksFound = []
do {
const request = {
command: 'account_objects',
account: address,
ledger_index: 'validated',
type: 'check'
}
if (currentMarker) {
request.marker = currentMarker
}
const response = await client.request(request)
checksFound = checksFound.concat(response.result.account_objects)
currentMarker = response.result.marker
} while (currentMarker)The response may include Checks where the account from the request is the sender or the recipient. Each member of the account_objects array of the response represents one Check. For each such Check object, the address in the Destination is address of that Check's recipient, such as in the following code:
// Filter results by recipient ----------------------
// To filter by sender, check Account field instead of Destination
const checksByRecipient = []
for (const check of checksFound) {
if (check.Destination == address) {
checksByRecipient.push(check)
}
}
// Print results ----------------------
if (checksByRecipient.length === 0) {
console.log('No checks found.')
} else {
console.log('Checks: \n', JSON.stringify(checksByRecipient, null, 2))
}To filter by sender, check the address in the Account field of the Check instead.
index field. You'll need this value to cash or cancel the Check.