Skip to content

Instantly share code, notes, and snippets.

@tobi-bams
Created April 20, 2022 08:14
Show Gist options
  • Save tobi-bams/9d69035b410e2cc0e1ee947e0a978a1e to your computer and use it in GitHub Desktop.
Save tobi-bams/9d69035b410e2cc0e1ee947e0a978a1e to your computer and use it in GitHub Desktop.
Creating a PSBT transaction
const { Axios } = require("./util/index");
const GetUnspentUtxo = async () => {
try {
const response = await Axios("listunspent");
// console.log(response.data.result);
return response.data.result;
} catch (error) {
console.log(error);
throw error;
}
};
const SelectUtxos = async (amount) => {
const utxos = await GetUnspentUtxo();
const selectedUtxos = [];
let currentAmount = 0;
for (let i = 0; i < utxos.length; i++) {
let utxo = utxos[i];
let input = { txid: utxo.txid, vout: utxo.vout };
currentAmount = currentAmount + utxo.amount * 100000000;
selectedUtxos.push(input);
if (currentAmount > amount * 100000000) {
break;
}
}
return selectedUtxos;
};
const CreatePSBTTransaction = async () => {
const utxos = await SelectUtxos(0.0001);
const body = [
[...utxos],
[{ tb1qpvf0hh2fmu8pp3mkwwvp38enfwtd534p096vzy: 0.0001 }],
];
try {
const transaction = await Axios("walletcreatefundedpsbt", body);
console.log(transaction.data);
} catch (error) {
throw error;
}
};
CreatePSBTTransaction();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment