Skip to content

Commit

Permalink
Merge pull request #360 from alephium/minor
Browse files Browse the repository at this point in the history
Minor
  • Loading branch information
polarker authored Jun 4, 2024
2 parents fadc126 + e57069a commit 747bfb0
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 37 deletions.
17 changes: 3 additions & 14 deletions packages/cli/src/deployment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'

Expand Down Expand Up @@ -381,12 +380,7 @@ function createDeployer<Settings = unknown>(
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,
Expand Down Expand Up @@ -433,12 +427,7 @@ function createDeployer<Settings = unknown>(
}
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(),
Expand Down
18 changes: 0 additions & 18 deletions packages/cli/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,24 +121,6 @@ export function getNetwork<Settings = unknown>(
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<node.Confirmed> {
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
Expand Down
1 change: 1 addition & 0 deletions packages/web3/src/transaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,4 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.

export * from './status'
export * from './sign-verify'
export * from './utils'
38 changes: 38 additions & 0 deletions packages/web3/src/transaction/utils.ts
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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<node.Confirmed> {
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)
}
10 changes: 5 additions & 5 deletions test/exchange.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -53,7 +53,7 @@ async function getGasFee(txIds: string[]): Promise<bigint> {
async function waitTxsConfirmed(txIds: string[]): Promise<void> {
const nodeProvider = web3.getCurrentNodeProvider()
for (const txId of txIds) {
await waitTxConfirmed(nodeProvider, txId, 1, 1000)
await waitForTxConfirmation(txId, 1, 1000)
}
}

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
}
}
Expand Down

0 comments on commit 747bfb0

Please sign in to comment.