0

After several checks made with the help of the community here on Stack-Exchange, I was able to move forward with my problem.

Now I have reached the point that the transactions are not sent even though the wallet is active and connected correctly.

Checking the logs of my Monero full-node on Testnet, when I try to send the transaction, I get this error log:

2023-08-19 16:46:23.292 E Key image already spent in blockchain: b561176c54524cde8f5311d8304f45a2fae1c48395a0502cc30e48a86df6d2c7
2023-08-19 16:46:23.293 I tx used wrong inputs, rejected
2023-08-19 16:46:23.293 E Transaction verification failed: <1291ff117cab830744fc7f7e401046656c623db1e04a718fd5b6dbbeced27d64>
2023-08-19 16:46:23.294 W [on_send_raw_tx]: tx verification failed: double spend, invalid input

Apparently the transaction uses wrong inputs, but I can't figure out why.

Below is the complete js code using the monero-javascript library:

async function sendTransaction(toAddress, amount) {
    const daemon = await monerojs.connectToDaemonRpc("http://localhost:28081");    
    let wallet = await MoneroWalletFull.openWallet({
        path: `<mypath>`,
        password: password,
        networkType: MoneroNetworkType.TESTNET,
        serverUri: "http://127.0.0.1:28081"
      });
  daemonHeight = await daemon.getHeight();
  let sync = await syncWallet(wallet, daemonHeight)
  console.log(&quot;Wallet opened&quot;+ sync);

  walletHeight = await wallet.getHeight();

  console.error(&quot;Wallet height:&quot;, walletHeight);
  console.error(&quot;Daemon height:&quot;, daemonHeight);
  let isSynced = walletHeight === daemonHeight;
  if (isSynced) {
    console.log(&quot;Wallet is synchronized with the daemon&quot;);
  } else {
    console.error(&quot;Wallet is not synchronized with the daemon&quot;);
    console.error(&quot;Wallet height:&quot;, walletHeight);
    console.error(&quot;Daemon height:&quot;, daemonHeight);
    return;
  }

const balance = await getBalance(wallet);

if (balance &lt; amount) {
  throw new Error('Insufficient balance');
}

let unlockedBalance = await wallet.getUnlockedBalance(0, 0)

let amountInAtomic = ATOMICUNIT_PER_XMR * amount

if(unlockedBalance&lt;=amountInAtomic){
    throw new Error(&quot;Error no unlocked balance&quot;)
}
// TX SENDING AND CREATION
const tx = await wallet.createTx({
  accountIndex: 0,
  address: toAddress,
  amount: amountInAtomic.toString(),
  relay:true
});
let hash = tx.state.hash
return hash

}

async function syncWallet(wallet, height) { try { const syncResult = await wallet.sync(height); console.log('Sync progress:', syncResult); await wallet.save() if (!(await wallet.isSynced())) { throw new Error("Errore durante la sincronizzazione!") } else return true } catch (error) { throw new Error("Error during sync of the wallet: " + error.message); } } async function getBalance(wallet) { const balance = parseFloat((await wallet.getBalance(0)).toString()) / ATOMICUNIT_PER_XMR const unlockedBalance = await getUnlockedBalance(wallet) let balances= {balance,unlockedBalance} return balances }

async function getUnlockedBalance(wallet) { const balance = parseFloat((await wallet.getUnlockedBalance(0, 0)).toString()) / ATOMICUNIT_PER_XMR return balance }

Could you help me understand how to solve this error by providing me code examples or useful links to solve this problem?

AlexCav
  • 1
  • 4

0 Answers0