refactor: use pRetry to prevent recursive call
This commit is contained in:
parent
74d3691812
commit
7d07964e7e
3 changed files with 42 additions and 30 deletions
|
|
@ -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) {
|
|
||||||
setTimeout(() => {
|
|
||||||
fetch('z_getoperationstatus', [[opid]])
|
fetch('z_getoperationstatus', [[opid]])
|
||||||
.then(res => {
|
.then(res => {
|
||||||
const status = _.get('status', res[0])
|
const status = _.get('status', res[0])
|
||||||
switch(status) {
|
switch (status) {
|
||||||
case 'success':
|
case 'success':
|
||||||
resolve(res[0])
|
resolve(res[0])
|
||||||
break
|
break
|
||||||
case 'failed':
|
case 'failed':
|
||||||
|
throw new pRetry.AbortError(res[0].error)
|
||||||
|
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
26
package-lock.json
generated
|
|
@ -2316,6 +2316,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",
|
||||||
|
|
@ -8123,6 +8128,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": {
|
||||||
|
|
@ -12713,12 +12728,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": {
|
||||||
|
|
|
||||||
|
|
@ -60,6 +60,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",
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue