1

I'd like to create a transaction using pure JavaScript on browser using Nami Wallet. The docs shows only cardano.signTx(tx, partialSign) and cardano.submitTx(tx), but don't explain very well how to build the Transaction object.

How can I create a full Transaction object, let's say, transfering a specific amount of ADA from one wallet to another?

1
  • Should you mark one of the answers as accepted?
    – gRebel
    Apr 28, 2022 at 5:12

3 Answers 3

2

On Cardano, a transaction consists of UTxOs spent and UTxOs produced. The Nami wallet has a function to extract its UTxOs:

cardano.getUtxos(amount?: Value, paginate?: {page: number, limit: number}) : [TransactionUnspentOutput]

These UTxOs and the recipient address (as well as some static variables of the mainnet like fees...) are theoretically all you need to construct the transaction that transfers funds from one wallet to the other. The construction is not trivial, however, and you would probably want to use a library like https://github.com/Emurgo/cardano-serialization-lib that does this for you.

2
  • 2
    I understand the concept, but would like more coding details of the implementation, because both Nami Wallet and cardano-serialization-lib documentation do not provide practical guidance to make this happen Dec 15, 2021 at 13:30
  • 1
    @DanielCukier did you solve this? I'd encourage you to share your solution as the lack of documentation is indeed a problem. Cardano wont succeed unless all of us do a better job of getting people educated on these day-to-day-tasks.
    – rx2347
    Apr 25, 2022 at 20:19
2

If you are open to integrating with a wallet other than Nami, then Typhon Wallet integration is seamless which does not require you to fetch UTXOs and manually build a transaction.

The below snippet is all you need to build a transaction.

const paymentTransactionResponse = await typhon.paymentTransaction({
  outputs: [
    {
      address:
        "addr_test1qz7jw975stagnvs00wsjny6y6gpazn86yvwcm2vy02j3up7mt68vuzvz4nzgs00x0shrgywvy674v6r2zcs8fxvvq27qfjq8np",
      amount: "6000000", // In Lovelace
    },
  ],
});

More examples, https://docs.typhonwallet.io/examples/paymentTransaction.html

1
  • Unfortunately this does not answer the question at all.
    – rx2347
    Apr 25, 2022 at 20:17
1

Is a long and awful process, Check this example using version 10.4 for all function definitions check my repo https://github.com/alice1989123/vending_machine/blob/main/Cardano/Wallet/Wallet.js

  1. Build the TransactionBody Inputs Outputs Fees, and more minting burning etc
  2. Hash the body and Sign it attach the witnesses to the the transaction,
  3. Send it
    async function sendAda(senderAddress, senderprvKeys, lovelaces, address) {
      const reciverAddress = wasm.Address.from_bech32(address);
      const wasmSender = wasm.Address.from_bech32(senderAddress);
    
      const outPutValue = wasm.Value.new(wasm.BigNum.from_str(`${lovelaces}`));
    
      const protocolParameters = await getProtocolParams();
    
      const output = wasm.TransactionOutput.new(reciverAddress, outPutValue);
    
      const txBuilder = InitTx(protocolParameters);
      txBuilder.add_output(output);
      const utoxs_ = await getUtxos(senderAddress);
      const utxos = wasm.TransactionUnspentOutputs.new();
      utoxs_.forEach((utxo) => utxos.add(utxo));
      txBuilder.add_inputs_from(utxos, 1);
      txBuilder.add_change_if_needed(wasmSender);
      const txBody = txBuilder.build();
      const tx = wasm.Transaction.new(txBody, wasm.TransactionWitnessSet.new());
      const hash = await SignAndSend(tx, senderprvKeys);
      return hash;
    }
1
  • 1
    I believe this won't work for my case, because I am using the browser wallet. I don't have access to the sender private key. What I want is to create a transaction and submit it to the user wallet, so the user can sign it before I submit the transaction to the blockchain Apr 29, 2022 at 0:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.