Useful scripts

Automatic re-try mechanism built on top of SDK.

In this example, the following happens:

  1. The user calls txCreate, which will fail (because the server is offline);
  2. Failed requests get stored in failedQueue array (this would ideally be a redis cache or another persistent datastore);
  3. Failed requests will be fetched from that array and retried every 1sec using processFailedRequests.
const { Notabene } = require('@notabene/nodejs');

const client = new Notabene({
  baseURL: 'http://localhost:3000',
  audience: 'http://localhost:3000',
  clientId: '...', // Add your own client ID
  clientSecret: '...', // Add your own client secret
});

const txCreate = {
    "transactionAsset": "ETH",
    "transactionAmount": "3100000000000000000",
    "originatorVASPdid": "did:ethr:0x249a33298144197bb591362bc5764f31201548fd",
    "beneficiaryVASPname": "Notabene VASP SG",
    "skipBeneficiaryDataValidation": false,
    "transactionBlockchainInfo": {
        "origin": "0x0000f",
        "destination": "0x1111"
    },
    "originator": {
        "originatorPersons": [
            {
                "naturalPerson": {
                    "name": [
                        {
                            "nameIdentifier": [
                                {
                                    "primaryIdentifier": "Wayne",
                                    "secondaryIdentifier": "Bruce"
                                }
                            ]
                        }
                    ],
                    "nationalIdentification": {
                        "nationalIdentifier": "AABBCCDDEEFF0011223344",
                        "nationalIdentifierType": "CCPT",
                        "countryOfIssue": "NZ"
                    },
                    "dateAndPlaceOfBirth": {
                        "dateOfBirth": "1900-01-01",
                        "placeOfBirth": "Planet Earth"
                    },
                    "geographicAddress": [{ "addressLine": ["Cool Road /-.st"], "country":"BE", "addressType": "HOME"}]
                }
            }
        ],
        "accountNumber": [
            "01234567890"
        ]
    },
    
    "beneficiary": {
        "beneficiaryPersons": [
            {
                "naturalPerson": {
                    "name": [
                        {
                            "nameIdentifier": [
                                {
                                    "primaryIdentifier": "Pennyworth",
                                    "secondaryIdentifier": "Alfred"
                                }
                            ]
                        }
                    ]
                }
            }
        ],
        "accountNumber": [
            "01234567890"
        ]
    }
}

// THIS IS A TOY EXAMPLE

const failedQueue = []; // recommended: use a persistent datastore (Redis or database)

async function main() {

    // Call txCreate as usual
    const args = [txCreate, ]
    try {
        const output = await client.transaction.create( ...args );
        // process output
    } catch(err) {
        // txCreate fails (eg: API has downtime)
        // recommended: check & report status code internally (maybe the request is invalid)
        console.error(err);
        // push info about the failed request into the failedQueue
        failedQueue.push({
            module: 'transaction',
            function: 'create',
            args 
        })
    }

    // function to retry failed requests from the failedQueue.
    async function processFailedRequests() {
        // take request from the queue
        const failedRequest = failedQueue.pop();
        console.log('retrying: ' + failedRequest.module + '.' + failedRequest.function);
        try {
            const output = await client[failedRequest.module][failedRequest.function](...failedRequest.args);
            // process output
        } catch(err) {
            // recommended: check & report status code internally (maybe the request is invalid)
            console.error(err);
            // if request fails again, put it back into the queue
            failedQueue.push({
                module: 'transaction',
                function: 'create',
                args 
            })
        }
    
    }

    // infinite loop that checks for failed requests every 1s
    while (true) {
        if (failedQueue.length) {
            processFailedRequests();
        }
        await sleep(1000); // suggestion: use exponential timeout strategy for retrying individual requests
    }
}

main();

function sleep(ms) {
  return new Promise((resolve) => {
    setTimeout(resolve, ms);
  });
}