diff --git a/packages/cli/src/deployment.ts b/packages/cli/src/deployment.ts index 27702141d..2a1639216 100644 --- a/packages/cli/src/deployment.ts +++ b/packages/cli/src/deployment.ts @@ -59,10 +59,9 @@ import { loadConfig, retryFetch, taskIdToVariable, - waitTxConfirmed, waitUserConfirmation } from './utils' -import { groupOfAddress } from '@alephium/web3' +import { groupOfAddress, waitForTxConfirmation } from '@alephium/web3' import { codegen, genLoadDeployments } from './codegen' import { Project, ProjectArtifact } from './project' @@ -381,12 +380,7 @@ function createDeployer( console.log(`Deploying contract ${taskId}`) console.log(`Deployer - group ${signer.group} - ${signer.address}`) const deployResult = await contractFactory.deploy(signer, params) - const confirmed = await waitTxConfirmed( - web3.getCurrentNodeProvider(), - deployResult.txId, - confirmations, - requestInterval - ) + const confirmed = await waitForTxConfirmation(deployResult.txId, confirmations, requestInterval) const result: DeployContractExecutionResult = { txId: deployResult.txId, unsignedTx: deployResult.unsignedTx, @@ -433,12 +427,7 @@ function createDeployer( } console.log(`Executing script ${taskId}`) const executeResult = await executableScript.execute(signer, params) - const confirmed = await waitTxConfirmed( - web3.getCurrentNodeProvider(), - executeResult.txId, - confirmations, - requestInterval - ) + const confirmed = await waitForTxConfirmation(executeResult.txId, confirmations, requestInterval) const runScriptResult: RunScriptResult = { ...executeResult, gasPrice: executeResult.gasPrice.toString(), diff --git a/packages/cli/src/utils.ts b/packages/cli/src/utils.ts index fa9e74847..0d318beaa 100644 --- a/packages/cli/src/utils.ts +++ b/packages/cli/src/utils.ts @@ -121,24 +121,6 @@ export function getNetwork( return { ...defaultValues, ...networkInput } } -function isConfirmed(txStatus: node.TxStatus): txStatus is node.Confirmed { - return txStatus.type === 'Confirmed' -} - -export async function waitTxConfirmed( - provider: NodeProvider, - txId: string, - confirmations: number, - requestInterval: number -): Promise { - const status = await provider.transactions.getTransactionsStatus({ txId: txId }) - if (isConfirmed(status) && status.chainConfirmations >= confirmations) { - return status - } - await new Promise((r) => setTimeout(r, requestInterval)) - return waitTxConfirmed(provider, txId, confirmations, requestInterval) -} - export const retryFetch = fetchRetry.default(fetch, { retries: 20, retryDelay: 1000 diff --git a/packages/web3/src/transaction/index.ts b/packages/web3/src/transaction/index.ts index 090832b74..589f5fb35 100644 --- a/packages/web3/src/transaction/index.ts +++ b/packages/web3/src/transaction/index.ts @@ -18,3 +18,4 @@ along with the library. If not, see . export * from './status' export * from './sign-verify' +export * from './utils' diff --git a/packages/web3/src/transaction/utils.ts b/packages/web3/src/transaction/utils.ts new file mode 100644 index 000000000..d82071c03 --- /dev/null +++ b/packages/web3/src/transaction/utils.ts @@ -0,0 +1,38 @@ +/* +Copyright 2018 - 2022 The Alephium Authors +This file is part of the alephium project. + +The library is free software: you can redistribute it and/or modify +it under the terms of the GNU Lesser General Public License as published by +the Free Software Foundation, either version 3 of the License, or +(at your option) any later version. + +The library is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU Lesser General Public License for more details. + +You should have received a copy of the GNU Lesser General Public License +along with the library. If not, see . +*/ + +import { node } from '../api' +import { getCurrentNodeProvider } from '../global' + +function isConfirmed(txStatus: node.TxStatus): txStatus is node.Confirmed { + return txStatus.type === 'Confirmed' +} + +export async function waitForTxConfirmation( + txId: string, + confirmations: number, + requestInterval: number +): Promise { + const provider = getCurrentNodeProvider() + const status = await provider.transactions.getTransactionsStatus({ txId: txId }) + if (isConfirmed(status) && status.chainConfirmations >= confirmations) { + return status + } + await new Promise((r) => setTimeout(r, requestInterval)) + return waitForTxConfirmation(txId, confirmations, requestInterval) +} diff --git a/test/exchange.test.ts b/test/exchange.test.ts index be1ecf002..dd302e232 100644 --- a/test/exchange.test.ts +++ b/test/exchange.test.ts @@ -30,9 +30,9 @@ import { ALPH_TOKEN_ID, getALPHDepositInfo, groupOfAddress, - BlockSubscription + BlockSubscription, + waitForTxConfirmation } from '@alephium/web3' -import { waitTxConfirmed } from '@alephium/cli' import { EventEmitter } from 'stream' import * as bip39 from 'bip39' import { testPrivateKey } from '@alephium/web3-test' @@ -53,7 +53,7 @@ async function getGasFee(txIds: string[]): Promise { async function waitTxsConfirmed(txIds: string[]): Promise { const nodeProvider = web3.getCurrentNodeProvider() for (const txId of txIds) { - await waitTxConfirmed(nodeProvider, txId, 1, 1000) + await waitForTxConfirmation(txId, 1, 1000) } } @@ -214,7 +214,7 @@ class Exchange { throw new Error('Not enough balance') } const result = await transfer(this.wallet, user.address, ALPH_TOKEN_ID, amount) - await waitTxConfirmed(this.nodeProvider, result.txId, 1, 1000) + await waitForTxConfirmation(result.txId, 1, 1000) this.withdrawTxs.push(result.txId) const remain = balance - (amount + WithdrawFee) if (remain === 0n) { @@ -313,7 +313,7 @@ describe('exchange', function () { signerAddress: testWallet.address, destinations }) - await waitTxConfirmed(nodeProvider, result.txId, 1, 1000) + await waitForTxConfirmation(result.txId, 1, 1000) poolRewardTxNumber += 1 } }