Fix Bad Request errors from the client side

This commit is contained in:
junderw 2019-04-08 15:24:19 +09:00
parent 8ec1911a26
commit 77bd66c22f
No known key found for this signature in database
GPG key ID: B256185D3A971908

View file

@ -35,10 +35,17 @@ function height () {
})
}
function _faucetRequest (address, value) {
return dhttp({
method: 'POST',
url: APIURL + '/r/faucet?address=' + address + '&value=' + value + '&key=' + APIPASS
})
}
async function faucet (address, value) {
let count = 0
let _unspents = []
const sleep = ms => new Promise(r => setTimeout(r, ms))
const sleep = ms => new Promise((resolve, reject) => setTimeout(resolve, ms))
do {
if (count > 0) {
if (count >= 5) throw new Error('Missing Inputs')
@ -46,10 +53,20 @@ async function faucet (address, value) {
await sleep(200)
}
const txId = await dhttp({
method: 'POST',
url: APIURL + '/r/faucet?address=' + address + '&value=' + value + '&key=' + APIPASS
})
const txId = await _faucetRequest(address, value)
.then(
v => v, // Pass success value as is
async err => {
// Bad Request error is fixed by making sure height is >= 432
const currentHeight = await height()
if (err.message === 'Bad Request' && currentHeight < 432) {
await mine(432 - currentHeight)
return _faucetRequest(address, value)
} else {
throw err
}
}
)
await sleep(100)