capsule-corp-ternoa / ternoa-js

Ternoa JavaScript SDK to build on top of the Ternoa Chain ⚙️
https://docs.ternoa.network
Apache License 2.0
24 stars 10 forks source link

Hash no working on Ternoa explorer #154

Closed Nzenou closed 1 year ago

Nzenou commented 1 year ago

Describe the bug

Hello,

I am using the code below, to transfer Caps for customers the only problem is that the hash that I receive is not working on the Ternoa explorer, I have no results when I search with the hash of the transaction, but I can find the transaction by searching with my wallet address and looking for it in Extrinsics section.

Is it possible to get a workaround to directly have the good hash?

Thanks in advance

To reproduce

use the submitTxHash

Expected behavior

receive a hash that is not recognized

Screenshots

Capture d’écran 2023-01-31 à 18 58 05

Desktop (please complete the following information)

No response

Smartphone (please complete the following information)

No response

Additional context

No response

ipapandinas commented 1 year ago

hello @Nzenou 🙋‍♂️

thank you for pointing this out! you're right the submitted transaction hash is not returned by submitTxBlocking; we will definitively improve this in our upcoming release.

in the meanwhile here is a work around available right now:

Proposition 1: you just need the transaction hash; you don't care about block's information and about the Transfer event

import { getKeyringFromSeed } from "./account"
import { balancesTransferTx } from "./balance"
import { initializeApi, signTxHex, submitTxHex } from "./blockchain"

const main = async () => {
  await initializeApi()
  const keyring = await getKeyringFromSeed(SEED)

  const rawTxHash = await balancesTransferTx(TO_ADDRESS, 1)
  const signedTxHash = await signTxHex(keyring, rawTxHash)
  const txHash = await submitTxHex(signedTxHash)

  console.log(txHash)
}

Proposition 2: you need the transaction hash + block information + Transfer event data

import type { ISubmittableResult } from "@polkadot/types/types"
import { getKeyringFromSeed } from "./account"
import { balancesTransferTx } from "./balance"
import { BlockInfo, ConditionalVariable, getRawApi, initializeApi, signTxHex, submitTxHex } from "./blockchain"
import { BalancesTransferEvent, BlockchainEvent, BlockchainEvents } from "./events"

const main = async () => {
  await initializeApi()
  const api = getRawApi()
  const keyring = await getKeyringFromSeed(SEED)
  const conVar = new ConditionalVariable(500)
  const chainEvents: BlockchainEvents = new BlockchainEvents([])
  const blockInfo = new BlockInfo()

  const callback = async ({ events, status }: ISubmittableResult) => {
    if (status.isInBlock) {
      chainEvents.inner = events.map((eventRecord) => BlockchainEvent.fromEvent(eventRecord.event))
      const blockHash = status.asInBlock.toString()
      const blockData = await api.rpc.chain.getBlock(blockHash)
      blockInfo.blockHash = blockHash
      blockInfo.block = blockData.block
      conVar.notify()
    }
  }

  const rawTxHash = await balancesTransferTx(TO_ADDRESS, 1)
  const signedTxHash = await signTxHex(keyring, rawTxHash)
  const txHash = await submitTxHex(signedTxHash, callback)
  await conVar.wait()
  const transferEvent = chainEvents.findEventOrThrow(BalancesTransferEvent)

  console.log({ chainEvents, blockInfo, transferEvent, txHash })
}

Note - I destructured submitTxBlocking with the full flow: tx creation, tx signing and tx submit