freighter
freighter copied to clipboard
Can't submit transaction using freighter api
What version are you using?
"stellar-sdk": "^11.1.0", "@stellar/freighter-api": "^1.7.1",
What did you do?
` const serverStellar = new Horizon.Server(network.networkUrl) const contractId = CONTRACT_ID_TESTNET const contract = new Contract(contractId)
const sourceAccount = await StellarAPI.getAccount(account as string)
const transaction = new TransactionBuilder(sourceAccount, {
fee: '100',
networkPassphrase: Networks.TESTNET,
})
.addOperation(contract.call('increment', ...[]))
.setTimeout(300)
.build()
const transactionXdr = transaction.toXDR()
const signTx = await signTransaction(transactionXdr, TESTNET_DETAILS)
const result = await submitTxUsingHorizon(signTx, TESTNET_DETAILS.networkPassphrase, serverStellar)`
After get the signed transaction result from method signTransaction, i call submitTransaction and get this error "tx_malformed"
I try different way like submitTransaction using https://laboratory.stellar.org/#txsubmitter?network=test or change to call with sorobanRpc but it still the same. Here is the error when try to call with sorobanRpc:
And here is my code when using sorobanRpc:
`const network = await getNetworkDetails()
const serverStellar = getServer(network)
const contractId = CONTRACT_ID_TESTNET
const contract = new Contract(contractId)
const sourceAccount = await StellarAPI.getAccount(account as string)
const transaction = new TransactionBuilder(sourceAccount, {
fee: '100',
networkPassphrase: Networks.TESTNET,
})
.addOperation(contract.call('increment', ...[]))
.setTimeout(300)
.build()
const transactionXdr = transaction.toXDR()
const signTx = await signTransaction(transactionXdr, TESTNET_DETAILS)
const result = await submitTxUsingSoroban(signTx, TESTNET_DETAILS.networkPassphrase, serverStellar)`
`export const submitTxUsingSoroban = async ( signedXDR: string, networkPassphrase: string, server: SorobanRpc.Server ) => { const tx = TransactionBuilder.fromXDR(signedXDR, networkPassphrase)
const sendResponse = await server.sendTransaction(tx)
if (sendResponse.errorResult) { throw new Error(ERRORS.UNABLE_TO_SUBMIT_TX) }
if (sendResponse.status === SendTxStatus.Pending) { let txResponse = await server.getTransaction(sendResponse.hash)
// Poll this until the status is not "NOT_FOUND"
while (txResponse.status === SorobanRpc.Api.GetTransactionStatus.NOT_FOUND) {
// See if the transaction is complete
// eslint-disable-next-line no-await-in-loop
txResponse = await server.getTransaction(sendResponse.hash)
// Wait a second
// eslint-disable-next-line no-await-in-loop
await new Promise((resolve) => setTimeout(resolve, 1000))
}
if (txResponse.status === SorobanRpc.Api.GetTransactionStatus.SUCCESS) {
return txResponse.resultXdr.toXDR('base64')
}
// eslint-disable-next-line no-else-return
}
throw new Error(Unabled to submit transaction, status: ${sendResponse.status}
)
}`
But when im try to submit transaction without using freighter but only use generate key => it success, here is my code of it: `const contract = new Contract(CONTRACT_ID_TESTNET) const sourceKeypair = Keypair.fromSecret(SECRET_KEY) const sourceAccount = await sorobanServer.getAccount(sourceKeypair.publicKey()) // const sourceAccount = await StellarAPI.getAccount(account as string)
const transaction = new TransactionBuilder(sourceAccount, {
fee: '100',
networkPassphrase: Networks.TESTNET,
})
.addOperation(contract.call('increment', ...[]))
.setTimeout(300)
.build()
console.log('transaction', transaction.toXDR())
const preparedTransaction = await sorobanServer.prepareTransaction(transaction)
// const resSign = await signTransaction(transaction.toEnvelope().toXDR('base64'), {
// networkPassphrase: Networks.FUTURENET,
// network: 'FUTURENET',
// })
preparedTransaction.sign(sourceKeypair)
console.log('preparedTransaction', preparedTransaction.toXDR())
const sendResponse = await sorobanServer.sendTransaction(preparedTransaction)
console.log(`Sent transaction: ${JSON.stringify(sendResponse)}`)
if (sendResponse.status === 'PENDING') {
let getResponse = await sorobanServer.getTransaction(sendResponse.hash)
while (getResponse.status === 'NOT_FOUND') {
console.log('Waiting for transaction confirmation...')
// See if the transaction is complete
getResponse = await sorobanServer.getTransaction(sendResponse.hash)
// Wait one second
await new Promise((resolve) => setTimeout(resolve, 1000))
}
console.log(`getTransaction response: ${JSON.stringify(getResponse)}`)
if (getResponse.status === 'SUCCESS') {
// Make sure the transaction's resultMetaXDR is not empty
if (!getResponse.resultMetaXdr) {
throw 'Empty resultMetaXDR in getTransaction response'
}
// Find the return value from the contract and return it
const transactionMeta = getResponse.resultMetaXdr
const returnValue = transactionMeta.v3().sorobanMeta()!.returnValue()
console.log(`Transaction result: ${returnValue.value()}`)
} else {
throw `Transaction failed: ${JSON.stringify(getResponse)}`
}
}`
FYI, here is my contractId just in case: CBKGL7NJY3W5EVGVSYPK6FAFA7AIO76R4F4OOYFFOKLKZLJPCPF3JS55
What did you expect to see?
Transaction can submitted to stellar network.
What did you see instead?
Cant submit transaction
This is a bit tough to follow. There could be some issues with your code, but hard to decipher here. If this is still an issue, can you please create a test repo so I can pull this down and test?