diff --git a/src/oracle/tss-sign/signer.js b/src/oracle/tss-sign/signer.js index 69181da..39d77b4 100644 --- a/src/oracle/tss-sign/signer.js +++ b/src/oracle/tss-sign/signer.js @@ -116,23 +116,29 @@ async function waitForAccountNonce(address, nonce) { return !cancelled } - -function sendTx(tx) { - return httpClient - .post('/api/v1/broadcast?sync=true', tx, { - headers: { - 'Content-Type': 'text/plain' - } - }) - .catch(async (err) => { +async function sendTx(tx) { + while (true) { + try { + return await httpClient + .post('/api/v1/broadcast?sync=true', tx, { + headers: { + 'Content-Type': 'text/plain' + } + }) + } catch (err) { if (err.response.data.message.includes('Tx already exists in cache')) { logger.debug('Tx already exists in cache') return true } - logger.info('Something failed, restarting: %o', err.response) - await delay(1000) - return await sendTx(tx) - }) + if (err.response.data.message.includes(' < ')) { + logger.warn('Insufficient funds, waiting for funds to income') + await delay(60000) + } else { + logger.info('Something failed, restarting: %o', err.response) + await delay(10000) + } + } + } } function sign(keysFile, hash, tx, publicKey, signerAddress) { diff --git a/tests/test/ethToBnc.js b/tests/test/ethToBnc.js index 4bd5e08..4ae6ef6 100644 --- a/tests/test/ethToBnc.js +++ b/tests/test/ethToBnc.js @@ -7,45 +7,60 @@ const { HOME_BRIDGE_ADDRESS } = process.env const { controller1 } = require('./utils/proxyController') -module.exports = (getUsers) => { +module.exports = (getUsers, amount = 5, getBncPrefundedUser = null) => { describe('exchange of tokens in eth => bnc direction', function () { let info let users let ethBalances let bncBalances let bncBridgeSequence + let bncPrefundedUser before(async function () { users = getUsers() info = await controller1.getInfo() ethBalances = await Promise.all(users.map((user) => user.getErcBalance())) bncBalances = await seqMap(users, (user) => user.getBepBalance()) - + if (getBncPrefundedUser) { + bncPrefundedUser = getBncPrefundedUser() + } bncBridgeSequence = await getBncSequence(info.foreignBridgeAddress) - await Promise.all(users.map((user, i) => user.approveErc(HOME_BRIDGE_ADDRESS, 5 + i))) + await Promise.all(users.map((user, i) => user.approveErc(HOME_BRIDGE_ADDRESS, amount + i))) }) it('should accept exchange requests', async function () { - await Promise.all(users.map((user, i) => user.exchangeErc(5 + i))) + await Promise.all(users.map((user, i) => user.exchangeErc(amount + i))) const newEthBalances = await Promise.all(users.map((user) => user.getErcBalance())) for (let i = 0; i < 3; i += 1) { - assert.strictEqual(newEthBalances[i], ethBalances[i] - 5 - i, `Balance of ${users[i].ethAddress} did not updated as expected`) + assert.strictEqual(newEthBalances[i], ethBalances[i] - amount - i, `Balance of ${users[i].ethAddress} did not updated as expected`) } }) - it('should make exchange transaction on bnc side', async function () { - this.timeout(300000) - await waitPromise( - () => getBncSequence(info.foreignBridgeAddress), - (sequence) => sequence === bncBridgeSequence + 1 - ) - }) + if (bncPrefundedUser) { + it('should make exchange transaction on bnc side with waiting for enough funds on bnc side', async function () { + this.timeout(300000) + await delay(120000) + await bncPrefundedUser.transferBepBnb(info.foreignBridgeAddress, amount * 3 + 10, 0) + await waitPromise( + () => getBncSequence(info.foreignBridgeAddress), + (sequence) => sequence === bncBridgeSequence + 1 + ) + }) + } else { + it('should make exchange transaction on bnc side', async function () { + this.timeout(300000) + await waitPromise( + () => getBncSequence(info.foreignBridgeAddress), + (sequence) => sequence === bncBridgeSequence + 1 + ) + }) + } it('should make correct exchange transaction', async function () { await delay(10000) const newBncBalances = await Promise.all(users.map((user) => user.getBepBalance())) for (let i = 0; i < 3; i += 1) { - assert.strictEqual(newBncBalances[i], bncBalances[i] + 5 + i, `Balance of ${users[i].bncAddress} did not updated as expected`) + assert.strictEqual(newBncBalances[i], bncBalances[i] + amount + i, `Balance of ${users[i].bncAddress} did not updated as expected`) } }) }) diff --git a/tests/test/index.js b/tests/test/index.js index 3acb9bb..8ae7840 100644 --- a/tests/test/index.js +++ b/tests/test/index.js @@ -21,6 +21,7 @@ const { controller1 } = require('./utils/proxyController') describe('bridge tests', function () { let users + let bncPrefundedUser before(async function () { users = await seqMap(usersConfig, (user) => createUser(user.privateKey)) @@ -29,7 +30,6 @@ describe('bridge tests', function () { describe('generation of initial epoch keys', function () { let info let ethPrefundedUser - let bncPrefundedUser before(async function () { ethPrefundedUser = await createUser(HOME_PRIVATE_KEY, 'eth') @@ -99,5 +99,5 @@ describe('bridge tests', function () { testEthToBnc(() => users) testBncToEth(() => users) testEthToBncWithRestart(() => users, 2) - testEthToBnc(() => users) + testEthToBnc(() => users, 500, bncPrefundedUser) }) diff --git a/tests/test/utils/bncClient.js b/tests/test/utils/bncClient.js index cd461fb..bd18a30 100644 --- a/tests/test/utils/bncClient.js +++ b/tests/test/utils/bncClient.js @@ -36,7 +36,7 @@ module.exports = async function main(privateKey) { await client.multiSend(from, outputs, 'funding') }, async exchange(to, value) { - await client.transfer(from, to, value.toString(), FOREIGN_ASSET, 'exchange') + await client.transfer(from, to, value.toString(), FOREIGN_ASSET, '') } } }