Skip to content

Commit

Permalink
devop: send btc ordinals
Browse files Browse the repository at this point in the history
  • Loading branch information
kvhnuke committed Feb 12, 2024
1 parent 98d34f8 commit 4f5308b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export default async (
items: [
{
contract: contractAddress,
id: item.nft_id,
id: item.extra_metadata.ordinal_details.location,
image: item.image_url || item.previews.image_medium_url,
name: item.contract.name,
url: `https://ordinals.com/inscription/${item.contract_address}`,
Expand Down
16 changes: 15 additions & 1 deletion packages/extension/src/providers/bitcoin/libs/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ const isAddress = (address: string, network: BitcoinNetworkInfo): boolean => {
}
};

const getTxInfo = (utxos: HaskoinUnspentType[]): BTCTxInfo => {
const getTxInfo = (
utxos: HaskoinUnspentType[],
ordinalUTXO?: HaskoinUnspentType
): BTCTxInfo => {
const txInfo: BTCTxInfo = {
inputs: [],
outputs: [],
Expand All @@ -31,6 +34,17 @@ const getTxInfo = (utxos: HaskoinUnspentType[]): BTCTxInfo => {
},
});
});
if (ordinalUTXO) {
txInfo.inputs.unshift({
hash: ordinalUTXO.txid,
index: ordinalUTXO.index,
raw: ordinalUTXO.raw,
witnessUtxo: {
script: ordinalUTXO.pkscript,
value: ordinalUTXO.value,
},
});
}
return txInfo;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ const updateUTXOs = async () => {
return api.getUTXOs(addressFrom.value).then((utxos) => {
accountUTXOs.value = utxos;
const txSize = calculateSizeBasedOnType(
accountUTXOs.value.length,
accountUTXOs.value.length + (isSendToken.value ? 0 : 1),
2,
(props.network as BitcoinNetwork).networkInfo.paymentType
);
Expand Down Expand Up @@ -398,11 +398,36 @@ const selectNFT = (item: NFTItemWithCollectionName) => {
const sendAction = async () => {
const keyring = new PublicKeyRing();
const fromAccountInfo = await keyring.getAccount(addressFrom.value);
const txInfo = getBTCTxInfo(accountUTXOs.value);
let txInfo = getBTCTxInfo(accountUTXOs.value);
const balance = toBN(selectedAsset.value.balance!);
const toAmount = isSendToken.value
? toBN(toBase(sendAmount.value, selectedAsset.value.decimals))
: toBN(0);
let toAmount = toBN(toBase(sendAmount.value, selectedAsset.value.decimals));
if (isSendToken.value) {
txInfo.outputs.push({
address: addressTo.value,
value: toAmount.toNumber(),
});
} else {
const api = (await props.network.api()) as BitcoinAPI;
const [txid, index] = selectedNft.value.id.split(":");
const ordinalTx = await api.getTransactionStatus(txid);
const ordinalOutput = ordinalTx!.outputs[parseInt(index)];
txInfo = getBTCTxInfo(accountUTXOs.value, {
address: ordinalOutput.address,
block: {
height: ordinalTx!.blockNumber,
position: -1, // not needed
},
index: parseInt(index),
pkscript: ordinalOutput.pkscript,
txid,
value: ordinalOutput.value,
});
toAmount = toBN(ordinalOutput.value);
txInfo.outputs.push({
address: addressTo.value,
value: ordinalOutput.value,
});
}
const currentFee = toBN(
toBase(
gasCostValues.value[selectedFee.value].nativeValue,
Expand All @@ -411,11 +436,6 @@ const sendAction = async () => {
);
const remainder = balance.sub(toAmount).sub(currentFee);
txInfo.outputs.push({
address: addressTo.value,
value: toAmount.toNumber(),
});
if (remainder.gtn(0)) {
txInfo.outputs.push({
address: props.network.displayAddress(addressFrom.value),
Expand Down

1 comment on commit 4f5308b

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.