Search code examples
javascriptnode.jssolidityweb3js

Async function stops processing and fails to throw exc only on production aws ec2 cluster


I've run into an unexplainable error condition when I deploy the below code to my production site. Locally, all of these functions were tested and working.

I've tried gaining insight into the promises where it seems to fails, but they don't trip any exceptions. The usersApiGateway.getNonce is a get request and I verified that I get the correct return value when I send the get request on POSTMAN.

On my AWS logs it basically shows it executes up until the point designated below, and then just stops. And then after maybe 10 minutes it quits attempting to call the process via asyncTimeout and it then is permanently stuck. Any help on how I can find the root cause of this is appreciated.

The offending function:

async createBuyOrder(tokenAddress, amountSell, amountBuy) {

    amountSell = new BigNumber(amountSell);
    amountBuy  = new BigNumber(amountBuy);

    let nonce = '';
    [amountBuy, amountSell, nonce] = await Promise.all([
      web3Service.convertToWei(tokenAddress, amountBuy),
      web3Service.convertToWei(ETHER_ADDRESS, amountSell),
      usersApiGateway.getNonce(this.adminAccount)
    ]);

   // FUNCTION FAILS TO RETURN ANYTHING AFTER THIS POINT
   // Console.log for nonce, amount buy/sell/buy order below fail to show up
   // that is what I mean by not working

    const buyOrder = {
        "addressBuy" : tokenAddress,
        "amountBuy"  : amountBuy,
        "addressSell": ETHER_ADDRESS,
        "amountSell" : amountSell,
        "nonce"      : nonce,
    }

    await this.createOrder(buyOrder);
}

This is called from this function:

async populateOrderBook(tokenAddress, numOrders = 1) {

  for(let i = numOrders; i > 0; i--) {
    for(let j = -1; j < numOrders - i; j++){
      try {
            await this.createBuyOrder(tokenAddress, BUYORDER_amountSell, BUYORDER_amountBuy);
            await this.createSellOrder(tokenAddress, SELLORDER_amountBuy, SELLORDER_amountSell);
          } catch(exc) {
            console.log(exc)
          }
    }
  }
}

Which is called periodically in an init() function of the class

    asyncTimeout(async () => {
        try {
          await Promise.all([
            this.populateOrderBook(tokenAddress, 3)
          ]);
        } catch (exc) {
            console.error('Error while populating order book from Kyber');
            console.error(exc);
        }
    }, 60000);

I've tested the seemingly offending function from web3Service it wants to hang on and it seems to work just fine locally

   async convertToWei(tokenAddress, amount) {
    const numberOfDecimals = await this.tokenDecimals(tokenAddress);
    return new BigNumber(toBaseUnit(amount, numberOfDecimals));
   }

Solution

  • It turns out my node connection to the ethereum blockchain was not functioning. I used the connection to determine how many decimal places for my convertToWei function call, and since the connection was down it just got stuck in a loop that could never resolve.