|
1 | 1 | --- |
2 | | -title: Utils |
| 2 | +title: Util |
3 | 3 | icon: exchange |
4 | 4 | ordinal: 10 |
5 | 5 | --- |
@@ -553,6 +553,249 @@ transactions: `Array` |
553 | 553 | } |
554 | 554 | ] |
555 | 555 |
|
| 556 | +### `decodeOpReturn` |
| 557 | + |
| 558 | +Get high-level SLP data on a transaction by decoding the OP_RETURN data. |
| 559 | +Throws an error if the txid is not an SLP transaction. |
| 560 | + |
| 561 | +Note: It is convention in the SLP protocol to use the lowest denomination |
| 562 | +of decimal in the calculations. e.g. Using integer `satoshis` for calculations |
| 563 | +instead of floating point `bitcoins`. For this reason, the decimal information |
| 564 | +for the token only exists in the Genesis transaction, and other transactions |
| 565 | +display all token quantities as large integers. |
| 566 | + |
| 567 | +#### Arguments |
| 568 | + |
| 569 | +1. txid: `string` **required**. The txid of an SLP transaction |
| 570 | + |
| 571 | +#### Result |
| 572 | + |
| 573 | +slpData: `Object` |
| 574 | + |
| 575 | +#### Examples |
| 576 | + |
| 577 | + // A Genesis SLP transaction. |
| 578 | + (async () => { |
| 579 | + try { |
| 580 | + const txid = |
| 581 | + "bd158c564dd4ef54305b14f44f8e94c44b649f246dab14bcb42fb0d0078b8a90" |
| 582 | + |
| 583 | + const slpData = await SLP.Utils.decodeOpReturn(txid) |
| 584 | + console.log(slpData) |
| 585 | + } catch (error) { |
| 586 | + console.error(error) |
| 587 | + } |
| 588 | + })() |
| 589 | + |
| 590 | + // returns |
| 591 | + { |
| 592 | + "tokenType": 1, |
| 593 | + "transactionType": "genesis", |
| 594 | + "ticker": "SLPSDK", |
| 595 | + "name": "SLP SDK example using BITBOX", |
| 596 | + "documentUrl": "developer.bitcoin.com", |
| 597 | + "documentHash": "", |
| 598 | + "decimals": 8, |
| 599 | + "mintBatonVout": 2, |
| 600 | + "initialQty": 507, |
| 601 | + "tokensSentTo": "bitcoincash:qpcqs0n5xap26un2828n55gan2ylj7wavvzeuwdx05", |
| 602 | + "batonHolder": "bitcoincash:qpcqs0n5xap26un2828n55gan2ylj7wavvzeuwdx05" |
| 603 | + } |
| 604 | + |
| 605 | + // A Mint SLP transaction. |
| 606 | + (async () => { |
| 607 | + try { |
| 608 | + const txid = |
| 609 | + "65f21bbfcd545e5eb515e38e861a9dfe2378aaa2c4e458eb9e59e4d40e38f3a4" |
| 610 | + |
| 611 | + const slpData = await SLP.Utils.decodeOpReturn(txid) |
| 612 | + console.log(slpData) |
| 613 | + } catch (error) { |
| 614 | + console.error(error) |
| 615 | + } |
| 616 | + })() |
| 617 | + |
| 618 | + // returns |
| 619 | + { |
| 620 | + "tokenType": 1, |
| 621 | + "transactionType": "mint", |
| 622 | + "tokenId": "023cd3e95a3947058b994fd15a9a4c47937a9d9b6e0c0b1b5898d2ce84f354e4", |
| 623 | + "mintBatonVout": 2, |
| 624 | + "batonStillExists": true, |
| 625 | + "quantity": "10000000000", |
| 626 | + "tokensSentTo": "bitcoincash:qqss4zp80hn6szsa4jg2s9fupe7g5tcg5u5k0yyr2q", |
| 627 | + "batonHolder": "bitcoincash:qqss4zp80hn6szsa4jg2s9fupe7g5tcg5u5k0yyr2q" |
| 628 | + } |
| 629 | + |
| 630 | + // A Send SLP transaction. |
| 631 | + (async () => { |
| 632 | + try { |
| 633 | + const txid = |
| 634 | + "4f922565af664b6fdf0a1ba3924487344be721b3d8815c62cafc8a51e04a8afa" |
| 635 | + |
| 636 | + const slpData = await SLP.Utils.decodeOpReturn(txid) |
| 637 | + console.log(slpData) |
| 638 | + } catch (error) { |
| 639 | + console.error(error) |
| 640 | + } |
| 641 | + })() |
| 642 | + |
| 643 | + // returns |
| 644 | + { |
| 645 | + "tokenType": 1, |
| 646 | + "transactionType": "send", |
| 647 | + "tokenId": "023cd3e95a3947058b994fd15a9a4c47937a9d9b6e0c0b1b5898d2ce84f354e4", |
| 648 | + "spendData": [ |
| 649 | + { |
| 650 | + "quantity": "300000000", |
| 651 | + "sentTo": "bitcoincash:qzv7t2pzn2d0pklnetdjt65crh6fe8vnhuwvhsk2nn", |
| 652 | + "vout": 1 |
| 653 | + }, |
| 654 | + { |
| 655 | + "quantity": "60400000000", |
| 656 | + "sentTo": "bitcoincash:qqss4zp80hn6szsa4jg2s9fupe7g5tcg5u5k0yyr2q", |
| 657 | + "vout": 2 |
| 658 | + } |
| 659 | + ] |
| 660 | + } |
| 661 | + |
| 662 | +### `isTokenUtxo` |
| 663 | + |
| 664 | +Given an array of UTXOs, this function returns an array of Boolean values |
| 665 | +indicating if each UTXO belongs to an SLP transaction (true) or not (false). |
| 666 | +If false, the UTXO is safe for a wallet to spend in a normal BCH transaction. |
| 667 | + |
| 668 | +#### Arguments |
| 669 | + |
| 670 | +1. utxos: `Array` of utxo objects **required**. |
| 671 | + |
| 672 | +#### Result |
| 673 | + |
| 674 | +- `Array` of Boolean values. |
| 675 | + - `true`: The UTXO belongs to an SLP token. |
| 676 | + - `false`: The UTXO does not belong to an SLP token. |
| 677 | + |
| 678 | +#### Examples |
| 679 | + |
| 680 | + (async () => { |
| 681 | + try { |
| 682 | + const u = await SLP.Address.utxo("bitcoincash:qpcqs0n5xap26un2828n55gan2ylj7wavvzeuwdx05") |
| 683 | + const utxos = u.utxos |
| 684 | + |
| 685 | + console.log(`utxos: ${JSON.stringify(utxos,null,2)}`) |
| 686 | + |
| 687 | + const isSLPUtxo: await SLP.Utils.isTokenUtxo(utxos) |
| 688 | + console.log(`isSLPUtxo: ${JSON.stringify(canSpend,null,2}`) |
| 689 | + } catch (error) { |
| 690 | + console.error(error) |
| 691 | + } |
| 692 | + })() |
| 693 | + |
| 694 | + // returns |
| 695 | + utxos: [ |
| 696 | + { |
| 697 | + txid: |
| 698 | + "bd158c564dd4ef54305b14f44f8e94c44b649f246dab14bcb42fb0d0078b8a90", |
| 699 | + vout: 3, |
| 700 | + amount: 0.00002015, |
| 701 | + satoshis: 2015, |
| 702 | + height: 594892, |
| 703 | + confirmations: 5 |
| 704 | + }, |
| 705 | + { |
| 706 | + txid: |
| 707 | + "bd158c564dd4ef54305b14f44f8e94c44b649f246dab14bcb42fb0d0078b8a90", |
| 708 | + vout: 2, |
| 709 | + amount: 0.00000546, |
| 710 | + satoshis: 546, |
| 711 | + height: 594892, |
| 712 | + confirmations: 5 |
| 713 | + } |
| 714 | + ] |
| 715 | + |
| 716 | + isSLPUtxo: |
| 717 | + [ |
| 718 | + false, |
| 719 | + true |
| 720 | + ] |
| 721 | + |
| 722 | +### `tokenUtxoDetails` |
| 723 | + |
| 724 | +Hydrate a UTXO with SLP token metadata. |
| 725 | + |
| 726 | +Expects an array of UTXO objects as input. Returns an array of equal size. |
| 727 | +If the UTXO does not belong to a SLP transaction, it will return false. |
| 728 | +If the UTXO is part of an SLP transaction, it will return the UTXO object |
| 729 | +with additional SLP information attached. |
| 730 | + |
| 731 | +#### Arguments |
| 732 | + |
| 733 | +1. utxos: `Array` of utxo objects **required**. |
| 734 | + |
| 735 | +#### Result |
| 736 | + |
| 737 | +- `Array` |
| 738 | + - `false`: The UTXO does not belong to an SLP token and is safe to spend. |
| 739 | + - `Object`: The same UTXO object hydrated with SLP token metadata. |
| 740 | + |
| 741 | +#### Examples |
| 742 | + |
| 743 | + (async () => { |
| 744 | + try { |
| 745 | + const u = await SLP.Address.utxo("bitcoincash:qpcqs0n5xap26un2828n55gan2ylj7wavvzeuwdx05") |
| 746 | + const utxos = u.utxos |
| 747 | + |
| 748 | + console.log(`utxos: ${JSON.stringify(utxos,null,2)}`) |
| 749 | + |
| 750 | + const isSLPUtxo: await SLP.Utils.tokenUtxoDetails(utxos) |
| 751 | + console.log(`isSLPUtxo: ${JSON.stringify(canSpend,null,2}`) |
| 752 | + } catch (error) { |
| 753 | + console.error(error) |
| 754 | + } |
| 755 | + })() |
| 756 | + |
| 757 | + // returns |
| 758 | + utxos: [ |
| 759 | + { |
| 760 | + txid: |
| 761 | + "bd158c564dd4ef54305b14f44f8e94c44b649f246dab14bcb42fb0d0078b8a90", |
| 762 | + vout: 3, |
| 763 | + amount: 0.00002015, |
| 764 | + satoshis: 2015, |
| 765 | + height: 594892, |
| 766 | + confirmations: 5 |
| 767 | + }, |
| 768 | + { |
| 769 | + txid: |
| 770 | + "bd158c564dd4ef54305b14f44f8e94c44b649f246dab14bcb42fb0d0078b8a90", |
| 771 | + vout: 2, |
| 772 | + amount: 0.00000546, |
| 773 | + satoshis: 546, |
| 774 | + height: 594892, |
| 775 | + confirmations: 5 |
| 776 | + } |
| 777 | + ] |
| 778 | + |
| 779 | + isSLPUtxo: |
| 780 | + [ |
| 781 | + false, |
| 782 | + { |
| 783 | + "txid": "bd158c564dd4ef54305b14f44f8e94c44b649f246dab14bcb42fb0d0078b8a90", |
| 784 | + "vout": 2, |
| 785 | + "amount": 0.00000546, |
| 786 | + "satoshis": 546, |
| 787 | + "height": 594892, |
| 788 | + "confirmations": 5, |
| 789 | + "tokenType": "minting-baton", |
| 790 | + "tokenId": "bd158c564dd4ef54305b14f44f8e94c44b649f246dab14bcb42fb0d0078b8a90", |
| 791 | + "tokenTicker": "SLPSDK", |
| 792 | + "tokenName": "SLP SDK example using BITBOX", |
| 793 | + "tokenDocumentUrl": "developer.bitcoin.com", |
| 794 | + "tokenDocumentHash": "", |
| 795 | + "decimals": 8 |
| 796 | + } |
| 797 | + ] |
| 798 | + |
556 | 799 | ### `burnTotal` |
557 | 800 |
|
558 | 801 | List input, output and burn total for slp transaction |
|
0 commit comments