Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there any way to get the tx hash after sending the transaction? #79

Open
0xBeycan opened this issue Jan 12, 2025 · 0 comments
Open

Comments

@0xBeycan
Copy link

I use the method below, but it must be both slow and unstable. Is there any way to get the tx hash directly?

    async send(): Promise<TransactionId> {
        await this.wallet.send(this.signedData)
        return await this.provider.findTxHashByMessageHash(
            this.wallet.address,
            this.signedData.hash().toString('base64')
        )
    }
// funcs
    async retry<T>(fn: () => Promise<T>, options: { retries: number; delay: number }): Promise<T> {
        let lastError: Error | undefined
        for (let i = 0; i < options.retries; i++) {
            try {
                return await fn()
            } catch (e) {
                if (e instanceof Error) {
                    lastError = e
                }
                await new Promise((resolve) => setTimeout(resolve, options.delay))
            }
        }
        throw lastError ?? new Error('Unknown error')
    }

    async findTxHashByMessageHash(address: Address, _hash: string): Promise<string> {
        return await this.retry(
            async () => {
                const transactions = await this.client1.getTransactions(address, {
                    limit: 5
                })
                for (const tx of transactions) {
                    const hash = tx.inMessage?.body.hash().toString('base64')
                    if (hash === _hash) {
                        return tx.hash().toString('hex')
                    }
                }
                throw new Error('Transaction not found')
            },
            { retries: 30, delay: 1000 }
        )
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant