refactor: use pRetry to prevent recursive call

This commit is contained in:
José Oliveira 2021-02-23 17:40:10 +00:00 committed by Josh Harvey
parent d1bf6f5f3c
commit e326f4c0e5
3 changed files with 42 additions and 30 deletions

View file

@ -1,4 +1,5 @@
const _ = require('lodash/fp') const _ = require('lodash/fp')
const pRetry = require('p-retry')
const jsonRpc = require('../../common/json-rpc') const jsonRpc = require('../../common/json-rpc')
const coinUtils = require('../../../coin-utils') const coinUtils = require('../../../coin-utils')
@ -54,34 +55,28 @@ function sendCoins (account, address, cryptoAtoms, cryptoCode) {
const coins = cryptoAtoms.shift(-unitScale).toFixed(8) const coins = cryptoAtoms.shift(-unitScale).toFixed(8)
const checkSendStatus = function (opid) { const checkSendStatus = function (opid) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
function checker(timeout) { fetch('z_getoperationstatus', [[opid]])
setTimeout(() => { .then(res => {
fetch('z_getoperationstatus', [[opid]]) const status = _.get('status', res[0])
.then(res => { switch (status) {
const status = _.get('status', res[0]) case 'success':
switch(status) { resolve(res[0])
case 'success': break
resolve(res[0]) case 'failed':
break throw new pRetry.AbortError(res[0].error)
case 'failed': case 'executing':
reject(res[0].error) reject(res[0].error)
break break
case 'executing': }
if (timeout > 0) { })
checker(timeout - 500)
} else {
reject(res)
}
}
})
}, 500)
}
checker(10000)
}) })
} }
const checker = async () => {
return pRetry(checkSendStatus, { retries: 20, minTimeout: 300, factor: 1.05 })
}
return checkCryptoCode(cryptoCode) return checkCryptoCode(cryptoCode)
.then(() => fetch('z_sendmany', ["ANY_TADDR", [{address, amount: coins}]])) .then(() => fetch('z_sendmany', ["ANY_TADDR", [{address, amount: coins}]]))
.then(checkSendStatus) .then(checker)
.then((res) => { .then((res) => {
return { return {
fee: _.get('params.fee', res), fee: _.get('params.fee', res),

26
package-lock.json generated
View file

@ -1577,6 +1577,11 @@
"resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz", "resolved": "https://registry.npmjs.org/@types/range-parser/-/range-parser-1.2.3.tgz",
"integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA==" "integrity": "sha512-ewFXqrQHlFsgc09MK5jP5iR7vumV/BYayNC6PgJO2LPe8vrnNFyjQjSppfEngITi0qvfKtzFvgKymGheFM9UOA=="
}, },
"@types/retry": {
"version": "0.12.0",
"resolved": "https://registry.npmjs.org/@types/retry/-/retry-0.12.0.tgz",
"integrity": "sha512-wWKOClTTiizcZhXnPY4wikVAwmdYHp8q6DmC+EJUzAMsycb7HB32Kh9RN4+0gExjmPmZSAQjgURXIGATPegAvA=="
},
"@types/secp256k1": { "@types/secp256k1": {
"version": "4.0.1", "version": "4.0.1",
"resolved": "https://registry.npmjs.org/@types/secp256k1/-/secp256k1-4.0.1.tgz", "resolved": "https://registry.npmjs.org/@types/secp256k1/-/secp256k1-4.0.1.tgz",
@ -7075,6 +7080,16 @@
"timed-out": "^4.0.0", "timed-out": "^4.0.0",
"url-parse-lax": "^1.0.0", "url-parse-lax": "^1.0.0",
"url-to-options": "^1.0.1" "url-to-options": "^1.0.1"
},
"dependencies": {
"p-timeout": {
"version": "1.2.1",
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-1.2.1.tgz",
"integrity": "sha1-XrOzU7f86Z8QGhA4iAuwVOu+o4Y=",
"requires": {
"p-finally": "^1.0.0"
}
}
} }
}, },
"graceful-fs": { "graceful-fs": {
@ -11478,12 +11493,13 @@
"resolved": "https://registry.npmjs.org/p-reduce/-/p-reduce-1.0.0.tgz", "resolved": "https://registry.npmjs.org/p-reduce/-/p-reduce-1.0.0.tgz",
"integrity": "sha1-GMKw3ZNqRpClKfgjH1ig/bakffo=" "integrity": "sha1-GMKw3ZNqRpClKfgjH1ig/bakffo="
}, },
"p-timeout": { "p-retry": {
"version": "1.2.1", "version": "4.4.0",
"resolved": "https://registry.npmjs.org/p-timeout/-/p-timeout-1.2.1.tgz", "resolved": "https://registry.npmjs.org/p-retry/-/p-retry-4.4.0.tgz",
"integrity": "sha1-XrOzU7f86Z8QGhA4iAuwVOu+o4Y=", "integrity": "sha512-gVB/tBsG+3AHI1SyDHRrX6n9ZL0Bcbifps9W9/Bgu3Oyu4/OrAh8SvDzDsvpP0oxfCt3oWNT+0fQ9LyUGwBTLg==",
"requires": { "requires": {
"p-finally": "^1.0.0" "@types/retry": "^0.12.0",
"retry": "^0.12.0"
} }
}, },
"p-try": { "p-try": {

View file

@ -59,6 +59,7 @@
"nocache": "^2.1.0", "nocache": "^2.1.0",
"numeral": "^2.0.3", "numeral": "^2.0.3",
"p-each-series": "^1.0.0", "p-each-series": "^1.0.0",
"p-retry": "^4.4.0",
"pg-native": "^3.0.0", "pg-native": "^3.0.0",
"pg-promise": "^7.4.1", "pg-promise": "^7.4.1",
"pify": "^3.0.0", "pify": "^3.0.0",