首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从async.waterfall调用外部函数

从async.waterfall调用外部函数
EN

Stack Overflow用户
提问于 2021-12-27 08:40:53
回答 1查看 73关注 0票数 0

我对JS有点陌生,并且使用现有的分叉库(这个库非常复杂,文档也不多)--所以请容忍我。

我正在开发一个挖掘池系统,特别是添加了一个功能,用户可以在其中设置一个自定义支付金额,而不是较低的池限制。我已经将用户工作者地址的值写入redis中了。

我现在要做的是添加逻辑,以便:

  1. 使用工作过的员工地址查看他们是否设置了自定义支付值(下面是我的外部函数)
  2. 如果设置好了,就需要一些逻辑来确定是否应该立即支付或将其添加到余额中。

现有的瀑布脚本如下:https://github.com/mardock2009/Ravencoin-Pool/blob/master/libs/paymentProcessor.js#L575

在瀑布之前,我添加了一个类似于此的函数(我尝试过多次迭代):

代码语言:javascript
复制
getCustomPayoutAmount = function(worker, cback) {
    var logger = loggerFactory.getLogger('PaymentProcessing', 'getCustomPayoutAmount');

    var redisClient = redis.createClient(portalConfig.redis.port, portalConfig.redis.host);             
    logger.debug('Getting custom Payout for worker: %s', worker);
    var payoutAmount = new BigNumber(0);

    redisClient.hget('ravencoin:workers:customPayoutAmount', worker,  function(error, result) {
        if (error) {
            logger.error('Error getCustomPayoutAmount: %s', error);
            payoutAmount = 0;
        }
        logger.debug('Got custom payout amount for worker: %s, Payout Amount: %s', worker, result);
        if (result > 10) {
            payoutAmount = new BigNumber(result);
            logger.debug('Parsed Float Amount: %s', payoutAmount);
        } else {
            logger.debug('Else lower than: %s', payoutAmount);
            payoutAmount = new BigNumber(0);
        }       
    });
    cback( new BigNumber(payoutAmount));    
};

然后在第575号线,我打电话给它(也许?):

代码语言:javascript
复制
                    var customPayoutAmount = new BigNumber(0);

                    getCustomPayoutAmount(worker, function(returnCustomPayoutAmount) {
                        logger.debug('Callback customPayoutAmount = %s', returnCustomPayoutAmount);
                        customPayoutAmount = returnCustomPayoutAmount;
                    });

                    logger.debug('PP> customPayoutAmount = %s', customPayoutAmount);

然后,最后,一些if/elseif逻辑来处理不同的情况:

代码语言:javascript
复制
if (toSend.isGreaterThanOrEqualTo(minPayment)) {

                        if (toSend.isGreaterThanOrEqualTo(customPayoutAmount) && !customPayoutAmount.isZero()) {
                            //Amount Sent is higher than the custom amount set for this worker. Pay it out.
                            logger.debug('PP> Worker %s have their custom minimum payout amount: (%s above minimum %s)', w, toSend.toString(10), customPayoutAmount.toString(10));
                            totalSent = totalSent.plus(toSend);              
                            logger.debug('PP> totalSent = %s', totalSent.toString(10));
                            var address = worker.address = (worker.address || getProperAddress(w));              
                            logger.debug('PP> address = %s', address);
                            worker.sent = addressAmounts[address] = toSend;
                            logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                            worker.balanceChange = BigNumber.min(worker.balance, worker.sent).multipliedBy(new BigNumber(-1));
                            logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));

                        } else if (toSend.isLessThan(customPayoutAmount) && !customPayoutAmount.isZero()){
                            //Amount is higher than the minimum payment but not higher than the custom amount set for this worker. Add it to their balance.
                            //Did not meet the pool minimum, no custom amount. Add to balance.
                            logger.debug('PP> Worker %s have not reached minimum payout from their custom set payout amount threshold %s', w, customPayoutAmount.toString(10));
                            worker.balanceChange = BigNumber.max(toSend.minus(worker.balance), new BigNumber(0));
                            logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));
                            worker.sent = new BigNumber(0);
                            logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                            if (worker.balanceChange > 0) {
                                if (balanceAmounts[address] != null && balanceAmounts[address].isGreaterThan(0)) {
                                    balanceAmounts[address] = balanceAmounts[address].plus(worker.balanceChange);
                                } else {
                                    balanceAmounts[address] = worker.balanceChange;
                                }
                            }
                        }
                        
                        if (toSend.isGreaterThanOrEqualTo(minPayment) && customPayoutAmount.isZero()) {
                            //Meets the pool minimum payment, no custom amount. Pay out based on the pool minimum payment.
                            logger.debug('PP> Worker %s have reached minimum payout threshold (%s above minimum %s)', w, toSend.toString(10), minPayment.toString(10));
                            totalSent = totalSent.plus(toSend);              
                            logger.debug('PP> totalSent = %s', totalSent.toString(10));
                            var address = worker.address = (worker.address || getProperAddress(w));              
                            logger.debug('PP> address = %s', address);
                            worker.sent = addressAmounts[address] = toSend;
                            logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                            worker.balanceChange = BigNumber.min(worker.balance, worker.sent).multipliedBy(new BigNumber(-1));
                            logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));
                        }

                        
                    } else {
                        //Did not meet the pool minimum, no custom amount. Add to balance.
                        logger.debug('PP> Worker %s have not reached minimum payout threshold %s', w, minPayment.toString(10));
                        worker.balanceChange = BigNumber.max(toSend.minus(worker.balance), new BigNumber(0));
                        logger.debug('PP> worker.balanceChange = %s', worker.balanceChange.toString(10));
                        worker.sent = new BigNumber(0);
                        logger.debug('PP> worker.sent = %s', worker.sent.toString(10));
                        if (worker.balanceChange > 0) {
                            if (balanceAmounts[address] != null && balanceAmounts[address].isGreaterThan(0)) {
                                balanceAmounts[address] = balanceAmounts[address].plus(worker.balanceChange);
                            } else {
                                balanceAmounts[address] = worker.balanceChange;
                            }
                        }
                    }

我面临的主要问题是,我无法从瀑布内部的redis那里得到价值。我假设是因为它是异步的,andIi没有编写异步代码--这也会导致它在需要值时不一定按顺序运行。

再说一遍,我知道这是个烂摊子,我可能搞错了,所以希望有人能对这位可怜的菜鸟有所了解。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-12-27 14:13:28

您可以使用async.each()

代码语言:javascript
复制
function(workers, rounds, addressAccount, callback) {
  var trySend = function(withholdPercent) {
    ...
    async.each(workers, function(worker, callback) {
      ...
      getCustomPayoutAmount(worker, function(customPayoutAmount) {
        ...
        if (toSend.isGreaterThanOrEqualTo(minPayment)) {
          if (toSend.isGreaterThanOrEqualTo(customPayoutAmount) && !customPayoutAmount.isZero()) {
            ...
          }
          ...
        }
        callback();
      });
    })
    .then(function() {
      if (Object.keys(addressAmounts).length === 0) {
        logger.info('PP> No workers was chosen for paying out');
        callback(null, workers, rounds, []);
        return;
      }
      ...
      daemon.cmd('sendmany', [addressAccount || '', addressAmounts, 1, ""], function(result) {
        ...
      }
    });
  };
  trySend(new BigNumber(0);
}

而在'getCustomPayoutAmount‘中,’cback()‘应该在'redisClient.hget()’的回调中调用,如下所示

代码语言:javascript
复制
getCustomPayoutAmount = function(worker, cback) {
    ...

    redisClient.hget('ravencoin:workers:customPayoutAmount', worker,  function(error, result) {
        ...
        cback(payoutAmount);   
    });
     
};

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70493307

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档