@lightprotocol/compressed-token 0.11.1 → 0.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/browser/index.cjs +3090 -2195
- package/dist/cjs/browser/index.cjs.map +1 -1
- package/dist/cjs/node/index.cjs +1273 -610
- package/dist/cjs/node/index.cjs.map +1 -1
- package/dist/es/browser/index.js +3017 -2158
- package/dist/es/browser/index.js.map +1 -1
- package/dist/types/index.d.ts +303 -218
- package/package.json +23 -21
package/dist/cjs/node/index.cjs
CHANGED
|
@@ -6,7 +6,7 @@ var anchor = require('@coral-xyz/anchor');
|
|
|
6
6
|
var require$$0 = require('buffer');
|
|
7
7
|
|
|
8
8
|
const IDL = {
|
|
9
|
-
version: '
|
|
9
|
+
version: '1.2.0',
|
|
10
10
|
name: 'light_compressed_token',
|
|
11
11
|
instructions: [
|
|
12
12
|
{
|
|
@@ -1062,7 +1062,8 @@ const IDL = {
|
|
|
1062
1062
|
name: 'inputCompressedAccountsWithMerkleContext',
|
|
1063
1063
|
type: {
|
|
1064
1064
|
vec: {
|
|
1065
|
-
defined:
|
|
1065
|
+
defined:
|
|
1066
|
+
'PackedCompressedAccountWithMerkleContext',
|
|
1066
1067
|
},
|
|
1067
1068
|
},
|
|
1068
1069
|
},
|
|
@@ -1070,7 +1071,8 @@ const IDL = {
|
|
|
1070
1071
|
name: 'outputCompressedAccounts',
|
|
1071
1072
|
type: {
|
|
1072
1073
|
vec: {
|
|
1073
|
-
defined:
|
|
1074
|
+
defined:
|
|
1075
|
+
'OutputCompressedAccountWithPackedContext',
|
|
1074
1076
|
},
|
|
1075
1077
|
},
|
|
1076
1078
|
},
|
|
@@ -1126,7 +1128,8 @@ const IDL = {
|
|
|
1126
1128
|
name: 'inputCompressedAccountsWithMerkleContext',
|
|
1127
1129
|
type: {
|
|
1128
1130
|
vec: {
|
|
1129
|
-
defined:
|
|
1131
|
+
defined:
|
|
1132
|
+
'PackedCompressedAccountWithMerkleContext',
|
|
1130
1133
|
},
|
|
1131
1134
|
},
|
|
1132
1135
|
},
|
|
@@ -1134,7 +1137,8 @@ const IDL = {
|
|
|
1134
1137
|
name: 'outputCompressedAccounts',
|
|
1135
1138
|
type: {
|
|
1136
1139
|
vec: {
|
|
1137
|
-
defined:
|
|
1140
|
+
defined:
|
|
1141
|
+
'OutputCompressedAccountWithPackedContext',
|
|
1138
1142
|
},
|
|
1139
1143
|
},
|
|
1140
1144
|
},
|
|
@@ -1154,12 +1158,6 @@ const IDL = {
|
|
|
1154
1158
|
name: 'isCompress',
|
|
1155
1159
|
type: 'bool',
|
|
1156
1160
|
},
|
|
1157
|
-
{
|
|
1158
|
-
name: 'signerSeeds',
|
|
1159
|
-
type: {
|
|
1160
|
-
vec: 'bytes',
|
|
1161
|
-
},
|
|
1162
|
-
},
|
|
1163
1161
|
{
|
|
1164
1162
|
name: 'cpiContext',
|
|
1165
1163
|
type: {
|
|
@@ -1385,7 +1383,8 @@ const IDL = {
|
|
|
1385
1383
|
name: 'outputCompressedAccounts',
|
|
1386
1384
|
type: {
|
|
1387
1385
|
vec: {
|
|
1388
|
-
defined:
|
|
1386
|
+
defined:
|
|
1387
|
+
'OutputCompressedAccountWithPackedContext',
|
|
1389
1388
|
},
|
|
1390
1389
|
},
|
|
1391
1390
|
},
|
|
@@ -1562,128 +1561,23 @@ const IDL = {
|
|
|
1562
1561
|
errors: [
|
|
1563
1562
|
{
|
|
1564
1563
|
code: 6000,
|
|
1565
|
-
name: '
|
|
1566
|
-
msg: '
|
|
1564
|
+
name: 'SignerCheckFailed',
|
|
1565
|
+
msg: 'Signer check failed',
|
|
1567
1566
|
},
|
|
1568
1567
|
{
|
|
1569
1568
|
code: 6001,
|
|
1570
|
-
name: '
|
|
1571
|
-
msg: '
|
|
1569
|
+
name: 'CreateTransferInstructionFailed',
|
|
1570
|
+
msg: 'Create transfer instruction failed',
|
|
1572
1571
|
},
|
|
1573
1572
|
{
|
|
1574
1573
|
code: 6002,
|
|
1575
|
-
name: '
|
|
1576
|
-
msg: '
|
|
1574
|
+
name: 'AccountNotFound',
|
|
1575
|
+
msg: 'Account not found',
|
|
1577
1576
|
},
|
|
1578
1577
|
{
|
|
1579
1578
|
code: 6003,
|
|
1580
|
-
name: '
|
|
1581
|
-
msg: '
|
|
1582
|
-
},
|
|
1583
|
-
{
|
|
1584
|
-
code: 6004,
|
|
1585
|
-
name: 'ComputeDecompressSumFailed',
|
|
1586
|
-
msg: 'ComputeDecompressSumFailed',
|
|
1587
|
-
},
|
|
1588
|
-
{
|
|
1589
|
-
code: 6005,
|
|
1590
|
-
name: 'SumCheckFailed',
|
|
1591
|
-
msg: 'SumCheckFailed',
|
|
1592
|
-
},
|
|
1593
|
-
{
|
|
1594
|
-
code: 6006,
|
|
1595
|
-
name: 'DecompressRecipientUndefinedForDecompress',
|
|
1596
|
-
msg: 'DecompressRecipientUndefinedForDecompress',
|
|
1597
|
-
},
|
|
1598
|
-
{
|
|
1599
|
-
code: 6007,
|
|
1600
|
-
name: 'CompressedPdaUndefinedForDecompress',
|
|
1601
|
-
msg: 'CompressedPdaUndefinedForDecompress',
|
|
1602
|
-
},
|
|
1603
|
-
{
|
|
1604
|
-
code: 6008,
|
|
1605
|
-
name: 'DeCompressAmountUndefinedForDecompress',
|
|
1606
|
-
msg: 'DeCompressAmountUndefinedForDecompress',
|
|
1607
|
-
},
|
|
1608
|
-
{
|
|
1609
|
-
code: 6009,
|
|
1610
|
-
name: 'CompressedPdaUndefinedForCompress',
|
|
1611
|
-
msg: 'CompressedPdaUndefinedForCompress',
|
|
1612
|
-
},
|
|
1613
|
-
{
|
|
1614
|
-
code: 6010,
|
|
1615
|
-
name: 'DeCompressAmountUndefinedForCompress',
|
|
1616
|
-
msg: 'DeCompressAmountUndefinedForCompress',
|
|
1617
|
-
},
|
|
1618
|
-
{
|
|
1619
|
-
code: 6011,
|
|
1620
|
-
name: 'DelegateSignerCheckFailed',
|
|
1621
|
-
msg: 'DelegateSignerCheckFailed',
|
|
1622
|
-
},
|
|
1623
|
-
{
|
|
1624
|
-
code: 6012,
|
|
1625
|
-
name: 'MintTooLarge',
|
|
1626
|
-
msg: 'Minted amount greater than u64::MAX',
|
|
1627
|
-
},
|
|
1628
|
-
{
|
|
1629
|
-
code: 6013,
|
|
1630
|
-
name: 'SplTokenSupplyMismatch',
|
|
1631
|
-
msg: 'SplTokenSupplyMismatch',
|
|
1632
|
-
},
|
|
1633
|
-
{
|
|
1634
|
-
code: 6014,
|
|
1635
|
-
name: 'HeapMemoryCheckFailed',
|
|
1636
|
-
msg: 'HeapMemoryCheckFailed',
|
|
1637
|
-
},
|
|
1638
|
-
{
|
|
1639
|
-
code: 6015,
|
|
1640
|
-
name: 'InstructionNotCallable',
|
|
1641
|
-
msg: 'The instruction is not callable',
|
|
1642
|
-
},
|
|
1643
|
-
{
|
|
1644
|
-
code: 6016,
|
|
1645
|
-
name: 'ArithmeticUnderflow',
|
|
1646
|
-
msg: 'ArithmeticUnderflow',
|
|
1647
|
-
},
|
|
1648
|
-
{
|
|
1649
|
-
code: 6017,
|
|
1650
|
-
name: 'HashToFieldError',
|
|
1651
|
-
msg: 'HashToFieldError',
|
|
1652
|
-
},
|
|
1653
|
-
{
|
|
1654
|
-
code: 6018,
|
|
1655
|
-
name: 'InvalidAuthorityMint',
|
|
1656
|
-
msg: 'Expected the authority to be also a mint authority',
|
|
1657
|
-
},
|
|
1658
|
-
{
|
|
1659
|
-
code: 6019,
|
|
1660
|
-
name: 'InvalidFreezeAuthority',
|
|
1661
|
-
msg: 'Provided authority is not the freeze authority',
|
|
1662
|
-
},
|
|
1663
|
-
{
|
|
1664
|
-
code: 6020,
|
|
1665
|
-
name: 'InvalidDelegateIndex',
|
|
1666
|
-
},
|
|
1667
|
-
{
|
|
1668
|
-
code: 6021,
|
|
1669
|
-
name: 'TokenPoolPdaUndefined',
|
|
1670
|
-
},
|
|
1671
|
-
{
|
|
1672
|
-
code: 6022,
|
|
1673
|
-
name: 'IsTokenPoolPda',
|
|
1674
|
-
msg: 'Compress or decompress recipient is the same account as the token pool pda.',
|
|
1675
|
-
},
|
|
1676
|
-
{
|
|
1677
|
-
code: 6023,
|
|
1678
|
-
name: 'InvalidTokenPoolPda',
|
|
1679
|
-
},
|
|
1680
|
-
{
|
|
1681
|
-
code: 6024,
|
|
1682
|
-
name: 'NoInputTokenAccountsProvided',
|
|
1683
|
-
},
|
|
1684
|
-
{
|
|
1685
|
-
code: 6025,
|
|
1686
|
-
name: 'NoInputsProvided',
|
|
1579
|
+
name: 'SerializationError',
|
|
1580
|
+
msg: 'Serialization error',
|
|
1687
1581
|
},
|
|
1688
1582
|
],
|
|
1689
1583
|
};
|
|
@@ -1693,20 +1587,37 @@ const IDL = {
|
|
|
1693
1587
|
* Packs Compressed Token Accounts.
|
|
1694
1588
|
*/
|
|
1695
1589
|
function packCompressedTokenAccounts(params) {
|
|
1696
|
-
const {
|
|
1590
|
+
const {
|
|
1591
|
+
inputCompressedTokenAccounts,
|
|
1592
|
+
outputStateTrees,
|
|
1593
|
+
remainingAccounts = [],
|
|
1594
|
+
rootIndices,
|
|
1595
|
+
tokenTransferOutputs,
|
|
1596
|
+
} = params;
|
|
1697
1597
|
const _remainingAccounts = remainingAccounts.slice();
|
|
1698
1598
|
let delegateIndex = null;
|
|
1699
|
-
if (
|
|
1700
|
-
inputCompressedTokenAccounts
|
|
1701
|
-
|
|
1599
|
+
if (
|
|
1600
|
+
inputCompressedTokenAccounts.length > 0 &&
|
|
1601
|
+
inputCompressedTokenAccounts[0].parsed.delegate
|
|
1602
|
+
) {
|
|
1603
|
+
delegateIndex = stateless_js.getIndexOrAdd(
|
|
1604
|
+
_remainingAccounts,
|
|
1605
|
+
inputCompressedTokenAccounts[0].parsed.delegate,
|
|
1606
|
+
);
|
|
1702
1607
|
}
|
|
1703
1608
|
/// TODO: move pubkeyArray to remainingAccounts
|
|
1704
1609
|
/// Currently just packs 'delegate' to pubkeyArray
|
|
1705
1610
|
const packedInputTokenData = [];
|
|
1706
1611
|
/// pack inputs
|
|
1707
1612
|
inputCompressedTokenAccounts.forEach((account, index) => {
|
|
1708
|
-
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1709
|
-
|
|
1613
|
+
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1614
|
+
_remainingAccounts,
|
|
1615
|
+
account.compressedAccount.merkleTree,
|
|
1616
|
+
);
|
|
1617
|
+
const nullifierQueuePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1618
|
+
_remainingAccounts,
|
|
1619
|
+
account.compressedAccount.nullifierQueue,
|
|
1620
|
+
);
|
|
1710
1621
|
packedInputTokenData.push({
|
|
1711
1622
|
amount: account.parsed.amount,
|
|
1712
1623
|
delegateIndex,
|
|
@@ -1724,15 +1635,27 @@ function packCompressedTokenAccounts(params) {
|
|
|
1724
1635
|
});
|
|
1725
1636
|
});
|
|
1726
1637
|
/// pack output state trees
|
|
1727
|
-
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
|
|
1638
|
+
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
|
|
1639
|
+
outputStateTrees,
|
|
1640
|
+
tokenTransferOutputs.length,
|
|
1641
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
1642
|
+
);
|
|
1728
1643
|
const packedOutputTokenData = [];
|
|
1729
1644
|
paddedOutputStateMerkleTrees.forEach((account, index) => {
|
|
1730
1645
|
var _a;
|
|
1731
|
-
const merkleTreeIndex = stateless_js.getIndexOrAdd(
|
|
1646
|
+
const merkleTreeIndex = stateless_js.getIndexOrAdd(
|
|
1647
|
+
_remainingAccounts,
|
|
1648
|
+
account,
|
|
1649
|
+
);
|
|
1732
1650
|
packedOutputTokenData.push({
|
|
1733
1651
|
owner: tokenTransferOutputs[index].owner,
|
|
1734
1652
|
amount: tokenTransferOutputs[index].amount,
|
|
1735
|
-
lamports: (
|
|
1653
|
+
lamports: (
|
|
1654
|
+
(_a = tokenTransferOutputs[index].lamports) === null ||
|
|
1655
|
+
_a === void 0
|
|
1656
|
+
? void 0
|
|
1657
|
+
: _a.eq(stateless_js.bn(0))
|
|
1658
|
+
)
|
|
1736
1659
|
? null
|
|
1737
1660
|
: tokenTransferOutputs[index].lamports,
|
|
1738
1661
|
merkleTreeIndex,
|
|
@@ -1740,7 +1663,7 @@ function packCompressedTokenAccounts(params) {
|
|
|
1740
1663
|
});
|
|
1741
1664
|
});
|
|
1742
1665
|
// to meta
|
|
1743
|
-
const remainingAccountMetas = _remainingAccounts.map(
|
|
1666
|
+
const remainingAccountMetas = _remainingAccounts.map(account => ({
|
|
1744
1667
|
pubkey: account,
|
|
1745
1668
|
isWritable: true,
|
|
1746
1669
|
isSigner: false,
|
|
@@ -1757,11 +1680,15 @@ const CPI_AUTHORITY_SEED = Buffer.from('cpi_authority');
|
|
|
1757
1680
|
const SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE = 1461600;
|
|
1758
1681
|
|
|
1759
1682
|
/** Address of the SPL Token program */
|
|
1760
|
-
const TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1683
|
+
const TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1684
|
+
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
|
|
1685
|
+
);
|
|
1761
1686
|
/** Address of the SPL Token 2022 program */
|
|
1762
1687
|
new web3_js.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
|
|
1763
1688
|
/** Address of the SPL Associated Token Account program */
|
|
1764
|
-
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1689
|
+
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1690
|
+
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
|
|
1691
|
+
);
|
|
1765
1692
|
/** Address of the special mint for wrapped native SOL in spl-token */
|
|
1766
1693
|
new web3_js.PublicKey('So11111111111111111111111111111111111111112');
|
|
1767
1694
|
/** Address of the special mint for wrapped native SOL in spl-token-2022 */
|
|
@@ -1791,9 +1718,88 @@ var Layout$1 = {};
|
|
|
1791
1718
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
1792
1719
|
* THE SOFTWARE.
|
|
1793
1720
|
*/
|
|
1794
|
-
Object.defineProperty(Layout$1,
|
|
1795
|
-
Layout$1.s16 =
|
|
1796
|
-
Layout$1.
|
|
1721
|
+
Object.defineProperty(Layout$1, '__esModule', { value: true });
|
|
1722
|
+
Layout$1.s16 =
|
|
1723
|
+
Layout$1.s8 =
|
|
1724
|
+
Layout$1.nu64be =
|
|
1725
|
+
Layout$1.u48be =
|
|
1726
|
+
Layout$1.u40be =
|
|
1727
|
+
Layout$1.u32be =
|
|
1728
|
+
Layout$1.u24be =
|
|
1729
|
+
Layout$1.u16be =
|
|
1730
|
+
Layout$1.nu64 =
|
|
1731
|
+
Layout$1.u48 =
|
|
1732
|
+
Layout$1.u40 =
|
|
1733
|
+
u32 =
|
|
1734
|
+
Layout$1.u32 =
|
|
1735
|
+
Layout$1.u24 =
|
|
1736
|
+
Layout$1.u16 =
|
|
1737
|
+
u8 =
|
|
1738
|
+
Layout$1.u8 =
|
|
1739
|
+
Layout$1.offset =
|
|
1740
|
+
Layout$1.greedy =
|
|
1741
|
+
Layout$1.Constant =
|
|
1742
|
+
Layout$1.UTF8 =
|
|
1743
|
+
Layout$1.CString =
|
|
1744
|
+
Layout$1.Blob =
|
|
1745
|
+
Layout$1.Boolean =
|
|
1746
|
+
Layout$1.BitField =
|
|
1747
|
+
Layout$1.BitStructure =
|
|
1748
|
+
Layout$1.VariantLayout =
|
|
1749
|
+
Layout$1.Union =
|
|
1750
|
+
Layout$1.UnionLayoutDiscriminator =
|
|
1751
|
+
Layout$1.UnionDiscriminator =
|
|
1752
|
+
Layout$1.Structure =
|
|
1753
|
+
Layout$1.Sequence =
|
|
1754
|
+
Layout$1.DoubleBE =
|
|
1755
|
+
Layout$1.Double =
|
|
1756
|
+
Layout$1.FloatBE =
|
|
1757
|
+
Layout$1.Float =
|
|
1758
|
+
Layout$1.NearInt64BE =
|
|
1759
|
+
Layout$1.NearInt64 =
|
|
1760
|
+
Layout$1.NearUInt64BE =
|
|
1761
|
+
Layout$1.NearUInt64 =
|
|
1762
|
+
Layout$1.IntBE =
|
|
1763
|
+
Layout$1.Int =
|
|
1764
|
+
Layout$1.UIntBE =
|
|
1765
|
+
Layout$1.UInt =
|
|
1766
|
+
Layout$1.OffsetLayout =
|
|
1767
|
+
Layout$1.GreedyCount =
|
|
1768
|
+
Layout$1.ExternalLayout =
|
|
1769
|
+
Layout$1.bindConstructorLayout =
|
|
1770
|
+
Layout$1.nameWithProperty =
|
|
1771
|
+
Layout_2 =
|
|
1772
|
+
Layout$1.Layout =
|
|
1773
|
+
Layout$1.uint8ArrayToBuffer =
|
|
1774
|
+
Layout$1.checkUint8Array =
|
|
1775
|
+
void 0;
|
|
1776
|
+
Layout$1.constant =
|
|
1777
|
+
Layout$1.utf8 =
|
|
1778
|
+
Layout$1.cstr =
|
|
1779
|
+
blob =
|
|
1780
|
+
Layout$1.blob =
|
|
1781
|
+
Layout$1.unionLayoutDiscriminator =
|
|
1782
|
+
Layout$1.union =
|
|
1783
|
+
Layout$1.seq =
|
|
1784
|
+
Layout$1.bits =
|
|
1785
|
+
struct =
|
|
1786
|
+
Layout$1.struct =
|
|
1787
|
+
Layout$1.f64be =
|
|
1788
|
+
Layout$1.f64 =
|
|
1789
|
+
Layout$1.f32be =
|
|
1790
|
+
Layout$1.f32 =
|
|
1791
|
+
Layout$1.ns64be =
|
|
1792
|
+
Layout$1.s48be =
|
|
1793
|
+
Layout$1.s40be =
|
|
1794
|
+
Layout$1.s32be =
|
|
1795
|
+
Layout$1.s24be =
|
|
1796
|
+
Layout$1.s16be =
|
|
1797
|
+
Layout$1.ns64 =
|
|
1798
|
+
Layout$1.s48 =
|
|
1799
|
+
Layout$1.s40 =
|
|
1800
|
+
Layout$1.s32 =
|
|
1801
|
+
Layout$1.s24 =
|
|
1802
|
+
void 0;
|
|
1797
1803
|
const buffer_1 = require$$0;
|
|
1798
1804
|
/* Check if a value is a Uint8Array.
|
|
1799
1805
|
*
|
|
@@ -1939,7 +1945,7 @@ class Layout {
|
|
|
1939
1945
|
return undefined;
|
|
1940
1946
|
}
|
|
1941
1947
|
}
|
|
1942
|
-
Layout$1.Layout = Layout;
|
|
1948
|
+
var Layout_2 = (Layout$1.Layout = Layout);
|
|
1943
1949
|
/* Provide text that carries a name (such as for a function that will
|
|
1944
1950
|
* be throwing an error) annotated with the property of a given layout
|
|
1945
1951
|
* (such as one for which the value was unacceptable).
|
|
@@ -1988,7 +1994,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
1988
1994
|
if (Object.prototype.hasOwnProperty.call(Class, 'layout_')) {
|
|
1989
1995
|
throw new Error('Class is already bound to a layout');
|
|
1990
1996
|
}
|
|
1991
|
-
if (!(layout &&
|
|
1997
|
+
if (!(layout && layout instanceof Layout)) {
|
|
1992
1998
|
throw new TypeError('layout must be a Layout');
|
|
1993
1999
|
}
|
|
1994
2000
|
if (Object.prototype.hasOwnProperty.call(layout, 'boundConstructor_')) {
|
|
@@ -1996,7 +2002,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
1996
2002
|
}
|
|
1997
2003
|
Class.layout_ = layout;
|
|
1998
2004
|
layout.boundConstructor_ = Class;
|
|
1999
|
-
layout.makeDestinationObject = (
|
|
2005
|
+
layout.makeDestinationObject = () => new Class();
|
|
2000
2006
|
Object.defineProperty(Class.prototype, 'encode', {
|
|
2001
2007
|
value(b, offset) {
|
|
2002
2008
|
return layout.encode(this, b, offset);
|
|
@@ -2066,7 +2072,7 @@ Layout$1.ExternalLayout = ExternalLayout;
|
|
|
2066
2072
|
*/
|
|
2067
2073
|
class GreedyCount extends ExternalLayout {
|
|
2068
2074
|
constructor(elementSpan = 1, property) {
|
|
2069
|
-
if (
|
|
2075
|
+
if (!Number.isInteger(elementSpan) || 0 >= elementSpan) {
|
|
2070
2076
|
throw new TypeError('elementSpan must be a (positive) integer');
|
|
2071
2077
|
}
|
|
2072
2078
|
super(-1, property);
|
|
@@ -2132,8 +2138,7 @@ class OffsetLayout extends ExternalLayout {
|
|
|
2132
2138
|
}
|
|
2133
2139
|
/** @override */
|
|
2134
2140
|
isCount() {
|
|
2135
|
-
return
|
|
2136
|
-
|| (this.layout instanceof UIntBE));
|
|
2141
|
+
return this.layout instanceof UInt || this.layout instanceof UIntBE;
|
|
2137
2142
|
}
|
|
2138
2143
|
/** @override */
|
|
2139
2144
|
decode(b, offset = 0) {
|
|
@@ -2286,7 +2291,7 @@ const V2E32 = Math.pow(2, 32);
|
|
|
2286
2291
|
* non-negative. */
|
|
2287
2292
|
function divmodInt64(src) {
|
|
2288
2293
|
const hi32 = Math.floor(src / V2E32);
|
|
2289
|
-
const lo32 = src -
|
|
2294
|
+
const lo32 = src - hi32 * V2E32;
|
|
2290
2295
|
return { hi32, lo32 };
|
|
2291
2296
|
}
|
|
2292
2297
|
/* Reconstruct Number from quotient and non-negative remainder */
|
|
@@ -2543,14 +2548,19 @@ class Sequence extends Layout {
|
|
|
2543
2548
|
if (!(elementLayout instanceof Layout)) {
|
|
2544
2549
|
throw new TypeError('elementLayout must be a Layout');
|
|
2545
2550
|
}
|
|
2546
|
-
if (
|
|
2547
|
-
|
|
2548
|
-
|
|
2549
|
-
|
|
2551
|
+
if (
|
|
2552
|
+
!(
|
|
2553
|
+
(count instanceof ExternalLayout && count.isCount()) ||
|
|
2554
|
+
(Number.isInteger(count) && 0 <= count)
|
|
2555
|
+
)
|
|
2556
|
+
) {
|
|
2557
|
+
throw new TypeError(
|
|
2558
|
+
'count must be non-negative integer ' +
|
|
2559
|
+
'or an unsigned integer ExternalLayout',
|
|
2560
|
+
);
|
|
2550
2561
|
}
|
|
2551
2562
|
let span = -1;
|
|
2552
|
-
if (
|
|
2553
|
-
&& (0 < elementLayout.span)) {
|
|
2563
|
+
if (!(count instanceof ExternalLayout) && 0 < elementLayout.span) {
|
|
2554
2564
|
span = count * elementLayout.span;
|
|
2555
2565
|
}
|
|
2556
2566
|
super(span, property);
|
|
@@ -2575,8 +2585,7 @@ class Sequence extends Layout {
|
|
|
2575
2585
|
}
|
|
2576
2586
|
if (0 < this.elementLayout.span) {
|
|
2577
2587
|
span = count * this.elementLayout.span;
|
|
2578
|
-
}
|
|
2579
|
-
else {
|
|
2588
|
+
} else {
|
|
2580
2589
|
let idx = 0;
|
|
2581
2590
|
while (idx < count) {
|
|
2582
2591
|
span += this.elementLayout.getSpan(b, offset + span);
|
|
@@ -2656,27 +2665,30 @@ Layout$1.Sequence = Sequence;
|
|
|
2656
2665
|
*/
|
|
2657
2666
|
class Structure extends Layout {
|
|
2658
2667
|
constructor(fields, property, decodePrefixes) {
|
|
2659
|
-
if (
|
|
2660
|
-
|
|
2668
|
+
if (
|
|
2669
|
+
!(
|
|
2670
|
+
Array.isArray(fields) &&
|
|
2671
|
+
fields.reduce((acc, v) => acc && v instanceof Layout, true)
|
|
2672
|
+
)
|
|
2673
|
+
) {
|
|
2661
2674
|
throw new TypeError('fields must be array of Layout instances');
|
|
2662
2675
|
}
|
|
2663
|
-
if (
|
|
2664
|
-
&& (undefined === decodePrefixes)) {
|
|
2676
|
+
if ('boolean' === typeof property && undefined === decodePrefixes) {
|
|
2665
2677
|
decodePrefixes = property;
|
|
2666
2678
|
property = undefined;
|
|
2667
2679
|
}
|
|
2668
2680
|
/* Verify absence of unnamed variable-length fields. */
|
|
2669
2681
|
for (const fd of fields) {
|
|
2670
|
-
if (
|
|
2671
|
-
|
|
2672
|
-
|
|
2682
|
+
if (0 > fd.span && undefined === fd.property) {
|
|
2683
|
+
throw new Error(
|
|
2684
|
+
'fields cannot contain unnamed variable-length layout',
|
|
2685
|
+
);
|
|
2673
2686
|
}
|
|
2674
2687
|
}
|
|
2675
2688
|
let span = -1;
|
|
2676
2689
|
try {
|
|
2677
2690
|
span = fields.reduce((span, fd) => span + fd.getSpan(), 0);
|
|
2678
|
-
}
|
|
2679
|
-
catch (e) {
|
|
2691
|
+
} catch (e) {
|
|
2680
2692
|
// ignore error
|
|
2681
2693
|
}
|
|
2682
2694
|
super(span, property);
|
|
@@ -2714,8 +2726,7 @@ class Structure extends Layout {
|
|
|
2714
2726
|
offset += fsp;
|
|
2715
2727
|
return span + fsp;
|
|
2716
2728
|
}, 0);
|
|
2717
|
-
}
|
|
2718
|
-
catch (e) {
|
|
2729
|
+
} catch (e) {
|
|
2719
2730
|
throw new RangeError('indeterminate span');
|
|
2720
2731
|
}
|
|
2721
2732
|
return span;
|
|
@@ -2729,8 +2740,7 @@ class Structure extends Layout {
|
|
|
2729
2740
|
dest[fd.property] = fd.decode(b, offset);
|
|
2730
2741
|
}
|
|
2731
2742
|
offset += fd.getSpan(b, offset);
|
|
2732
|
-
if (this.decodePrefixes
|
|
2733
|
-
&& (b.length === offset)) {
|
|
2743
|
+
if (this.decodePrefixes && b.length === offset) {
|
|
2734
2744
|
break;
|
|
2735
2745
|
}
|
|
2736
2746
|
}
|
|
@@ -2747,7 +2757,7 @@ class Structure extends Layout {
|
|
|
2747
2757
|
let lastWrote = 0;
|
|
2748
2758
|
for (const fd of this.fields) {
|
|
2749
2759
|
let span = fd.span;
|
|
2750
|
-
lastWrote =
|
|
2760
|
+
lastWrote = 0 < span ? span : 0;
|
|
2751
2761
|
if (undefined !== fd.property) {
|
|
2752
2762
|
const fv = src[fd.property];
|
|
2753
2763
|
if (undefined !== fv) {
|
|
@@ -2766,14 +2776,13 @@ class Structure extends Layout {
|
|
|
2766
2776
|
* item may have had a dynamic length and we don't want to include
|
|
2767
2777
|
* the padding between it and the end of the space reserved for
|
|
2768
2778
|
* it. */
|
|
2769
|
-
return
|
|
2779
|
+
return lastOffset + lastWrote - firstOffset;
|
|
2770
2780
|
}
|
|
2771
2781
|
/** @override */
|
|
2772
2782
|
fromArray(values) {
|
|
2773
2783
|
const dest = this.makeDestinationObject();
|
|
2774
2784
|
for (const fd of this.fields) {
|
|
2775
|
-
if (
|
|
2776
|
-
&& (0 < values.length)) {
|
|
2785
|
+
if (undefined !== fd.property && 0 < values.length) {
|
|
2777
2786
|
dest[fd.property] = values.shift();
|
|
2778
2787
|
}
|
|
2779
2788
|
}
|
|
@@ -2820,8 +2829,7 @@ class Structure extends Layout {
|
|
|
2820
2829
|
}
|
|
2821
2830
|
if (0 > fd.span) {
|
|
2822
2831
|
offset = -1;
|
|
2823
|
-
}
|
|
2824
|
-
else if (0 <= offset) {
|
|
2832
|
+
} else if (0 <= offset) {
|
|
2825
2833
|
offset += fd.span;
|
|
2826
2834
|
}
|
|
2827
2835
|
}
|
|
@@ -2886,9 +2894,10 @@ Layout$1.UnionDiscriminator = UnionDiscriminator;
|
|
|
2886
2894
|
*/
|
|
2887
2895
|
class UnionLayoutDiscriminator extends UnionDiscriminator {
|
|
2888
2896
|
constructor(layout, property) {
|
|
2889
|
-
if (!(
|
|
2890
|
-
|
|
2891
|
-
|
|
2897
|
+
if (!(layout instanceof ExternalLayout && layout.isCount())) {
|
|
2898
|
+
throw new TypeError(
|
|
2899
|
+
'layout must be an unsigned integer ExternalLayout',
|
|
2900
|
+
);
|
|
2892
2901
|
}
|
|
2893
2902
|
super(property || layout.property || 'variant');
|
|
2894
2903
|
/** The {@link ExternalLayout} used to access the discriminator
|
|
@@ -2967,26 +2976,24 @@ Layout$1.UnionLayoutDiscriminator = UnionLayoutDiscriminator;
|
|
|
2967
2976
|
class Union extends Layout {
|
|
2968
2977
|
constructor(discr, defaultLayout, property) {
|
|
2969
2978
|
let discriminator;
|
|
2970
|
-
if (
|
|
2971
|
-
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
else if (
|
|
2975
|
-
&& discr.isCount()) {
|
|
2979
|
+
if (discr instanceof UInt || discr instanceof UIntBE) {
|
|
2980
|
+
discriminator = new UnionLayoutDiscriminator(
|
|
2981
|
+
new OffsetLayout(discr),
|
|
2982
|
+
);
|
|
2983
|
+
} else if (discr instanceof ExternalLayout && discr.isCount()) {
|
|
2976
2984
|
discriminator = new UnionLayoutDiscriminator(discr);
|
|
2977
|
-
}
|
|
2978
|
-
|
|
2979
|
-
|
|
2980
|
-
|
|
2981
|
-
|
|
2982
|
-
else {
|
|
2985
|
+
} else if (!(discr instanceof UnionDiscriminator)) {
|
|
2986
|
+
throw new TypeError(
|
|
2987
|
+
'discr must be a UnionDiscriminator ' +
|
|
2988
|
+
'or an unsigned integer layout',
|
|
2989
|
+
);
|
|
2990
|
+
} else {
|
|
2983
2991
|
discriminator = discr;
|
|
2984
2992
|
}
|
|
2985
2993
|
if (undefined === defaultLayout) {
|
|
2986
2994
|
defaultLayout = null;
|
|
2987
2995
|
}
|
|
2988
|
-
if (!(
|
|
2989
|
-
|| (defaultLayout instanceof Layout))) {
|
|
2996
|
+
if (!(null === defaultLayout || defaultLayout instanceof Layout)) {
|
|
2990
2997
|
throw new TypeError('defaultLayout must be null or a Layout');
|
|
2991
2998
|
}
|
|
2992
2999
|
if (null !== defaultLayout) {
|
|
@@ -3004,8 +3011,10 @@ class Union extends Layout {
|
|
|
3004
3011
|
let span = -1;
|
|
3005
3012
|
if (defaultLayout) {
|
|
3006
3013
|
span = defaultLayout.span;
|
|
3007
|
-
if (
|
|
3008
|
-
|
|
3014
|
+
if (
|
|
3015
|
+
0 <= span &&
|
|
3016
|
+
(discr instanceof UInt || discr instanceof UIntBE)
|
|
3017
|
+
) {
|
|
3009
3018
|
span += discriminator.layout.span;
|
|
3010
3019
|
}
|
|
3011
3020
|
}
|
|
@@ -3024,8 +3033,8 @@ class Union extends Layout {
|
|
|
3024
3033
|
*
|
|
3025
3034
|
* If `false` the discriminator is obtained from somewhere
|
|
3026
3035
|
* else. */
|
|
3027
|
-
this.usesPrefixDiscriminator =
|
|
3028
|
-
||
|
|
3036
|
+
this.usesPrefixDiscriminator =
|
|
3037
|
+
discr instanceof UInt || discr instanceof UIntBE;
|
|
3029
3038
|
/** The layout for non-discriminator content when the value of the
|
|
3030
3039
|
* discriminator is not recognized.
|
|
3031
3040
|
*
|
|
@@ -3090,7 +3099,9 @@ class Union extends Layout {
|
|
|
3090
3099
|
* determine the span. */
|
|
3091
3100
|
const vlo = this.getVariant(b, offset);
|
|
3092
3101
|
if (!vlo) {
|
|
3093
|
-
throw new Error(
|
|
3102
|
+
throw new Error(
|
|
3103
|
+
'unable to determine span for unrecognized variant',
|
|
3104
|
+
);
|
|
3094
3105
|
}
|
|
3095
3106
|
return vlo.getSpan(b, offset);
|
|
3096
3107
|
}
|
|
@@ -3124,22 +3135,41 @@ class Union extends Layout {
|
|
|
3124
3135
|
* registered variant.
|
|
3125
3136
|
*/
|
|
3126
3137
|
defaultGetSourceVariant(src) {
|
|
3127
|
-
if (
|
|
3128
|
-
|
|
3129
|
-
|
|
3138
|
+
if (
|
|
3139
|
+
Object.prototype.hasOwnProperty.call(
|
|
3140
|
+
src,
|
|
3141
|
+
this.discriminator.property,
|
|
3142
|
+
)
|
|
3143
|
+
) {
|
|
3144
|
+
if (
|
|
3145
|
+
this.defaultLayout &&
|
|
3146
|
+
this.defaultLayout.property &&
|
|
3147
|
+
Object.prototype.hasOwnProperty.call(
|
|
3148
|
+
src,
|
|
3149
|
+
this.defaultLayout.property,
|
|
3150
|
+
)
|
|
3151
|
+
) {
|
|
3130
3152
|
return undefined;
|
|
3131
3153
|
}
|
|
3132
3154
|
const vlo = this.registry[src[this.discriminator.property]];
|
|
3133
|
-
if (
|
|
3134
|
-
&&
|
|
3135
|
-
|
|
3155
|
+
if (
|
|
3156
|
+
vlo &&
|
|
3157
|
+
(!vlo.layout ||
|
|
3158
|
+
(vlo.property &&
|
|
3159
|
+
Object.prototype.hasOwnProperty.call(
|
|
3160
|
+
src,
|
|
3161
|
+
vlo.property,
|
|
3162
|
+
)))
|
|
3163
|
+
) {
|
|
3136
3164
|
return vlo;
|
|
3137
3165
|
}
|
|
3138
|
-
}
|
|
3139
|
-
else {
|
|
3166
|
+
} else {
|
|
3140
3167
|
for (const tag in this.registry) {
|
|
3141
3168
|
const vlo = this.registry[tag];
|
|
3142
|
-
if (
|
|
3169
|
+
if (
|
|
3170
|
+
vlo.property &&
|
|
3171
|
+
Object.prototype.hasOwnProperty.call(src, vlo.property)
|
|
3172
|
+
) {
|
|
3143
3173
|
return vlo;
|
|
3144
3174
|
}
|
|
3145
3175
|
}
|
|
@@ -3167,9 +3197,11 @@ class Union extends Layout {
|
|
|
3167
3197
|
dest[dlo.property] = discr;
|
|
3168
3198
|
// defaultLayout.property can be undefined, but this is allowed by buffer-layout
|
|
3169
3199
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3170
|
-
dest[defaultLayout.property] = defaultLayout.decode(
|
|
3171
|
-
|
|
3172
|
-
|
|
3200
|
+
dest[defaultLayout.property] = defaultLayout.decode(
|
|
3201
|
+
b,
|
|
3202
|
+
offset + contentOffset,
|
|
3203
|
+
);
|
|
3204
|
+
} else {
|
|
3173
3205
|
dest = clo.decode(b, offset);
|
|
3174
3206
|
}
|
|
3175
3207
|
return dest;
|
|
@@ -3194,7 +3226,10 @@ class Union extends Layout {
|
|
|
3194
3226
|
dlo.encode(src[dlo.property], b, offset);
|
|
3195
3227
|
// clo.property is not undefined when vlo is undefined
|
|
3196
3228
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3197
|
-
return
|
|
3229
|
+
return (
|
|
3230
|
+
contentOffset +
|
|
3231
|
+
clo.encode(src[clo.property], b, offset + contentOffset)
|
|
3232
|
+
);
|
|
3198
3233
|
}
|
|
3199
3234
|
return vlo.encode(src, b, offset);
|
|
3200
3235
|
}
|
|
@@ -3234,8 +3269,7 @@ class Union extends Layout {
|
|
|
3234
3269
|
let variant;
|
|
3235
3270
|
if (vb instanceof Uint8Array) {
|
|
3236
3271
|
variant = this.discriminator.decode(vb, offset);
|
|
3237
|
-
}
|
|
3238
|
-
else {
|
|
3272
|
+
} else {
|
|
3239
3273
|
variant = vb;
|
|
3240
3274
|
}
|
|
3241
3275
|
return this.registry[variant];
|
|
@@ -3276,11 +3310,10 @@ class VariantLayout extends Layout {
|
|
|
3276
3310
|
if (!(union instanceof Union)) {
|
|
3277
3311
|
throw new TypeError('union must be a Union');
|
|
3278
3312
|
}
|
|
3279
|
-
if (
|
|
3313
|
+
if (!Number.isInteger(variant) || 0 > variant) {
|
|
3280
3314
|
throw new TypeError('variant must be a (non-negative) integer');
|
|
3281
3315
|
}
|
|
3282
|
-
if (
|
|
3283
|
-
&& (undefined === property)) {
|
|
3316
|
+
if ('string' === typeof layout && undefined === property) {
|
|
3284
3317
|
property = layout;
|
|
3285
3318
|
layout = null;
|
|
3286
3319
|
}
|
|
@@ -3288,10 +3321,14 @@ class VariantLayout extends Layout {
|
|
|
3288
3321
|
if (!(layout instanceof Layout)) {
|
|
3289
3322
|
throw new TypeError('layout must be a Layout');
|
|
3290
3323
|
}
|
|
3291
|
-
if (
|
|
3292
|
-
|
|
3293
|
-
|
|
3294
|
-
|
|
3324
|
+
if (
|
|
3325
|
+
null !== union.defaultLayout &&
|
|
3326
|
+
0 <= layout.span &&
|
|
3327
|
+
layout.span > union.defaultLayout.span
|
|
3328
|
+
) {
|
|
3329
|
+
throw new Error(
|
|
3330
|
+
'variant span exceeds span of containing union',
|
|
3331
|
+
);
|
|
3295
3332
|
}
|
|
3296
3333
|
if ('string' !== typeof property) {
|
|
3297
3334
|
throw new TypeError('variant must have a String property');
|
|
@@ -3300,7 +3337,7 @@ class VariantLayout extends Layout {
|
|
|
3300
3337
|
let span = union.span;
|
|
3301
3338
|
if (0 > union.span) {
|
|
3302
3339
|
span = layout ? layout.span : 0;
|
|
3303
|
-
if (
|
|
3340
|
+
if (0 <= span && union.usesPrefixDiscriminator) {
|
|
3304
3341
|
span += union.discriminator.layout.span;
|
|
3305
3342
|
}
|
|
3306
3343
|
}
|
|
@@ -3347,11 +3384,9 @@ class VariantLayout extends Layout {
|
|
|
3347
3384
|
}
|
|
3348
3385
|
if (this.layout) {
|
|
3349
3386
|
dest[this.property] = this.layout.decode(b, offset + contentOffset);
|
|
3350
|
-
}
|
|
3351
|
-
else if (this.property) {
|
|
3387
|
+
} else if (this.property) {
|
|
3352
3388
|
dest[this.property] = true;
|
|
3353
|
-
}
|
|
3354
|
-
else if (this.union.usesPrefixDiscriminator) {
|
|
3389
|
+
} else if (this.union.usesPrefixDiscriminator) {
|
|
3355
3390
|
dest[this.union.discriminator.property] = this.variant;
|
|
3356
3391
|
}
|
|
3357
3392
|
return dest;
|
|
@@ -3362,8 +3397,10 @@ class VariantLayout extends Layout {
|
|
|
3362
3397
|
if (this.union.usesPrefixDiscriminator) {
|
|
3363
3398
|
contentOffset = this.union.discriminator.layout.span;
|
|
3364
3399
|
}
|
|
3365
|
-
if (
|
|
3366
|
-
|
|
3400
|
+
if (
|
|
3401
|
+
this.layout &&
|
|
3402
|
+
!Object.prototype.hasOwnProperty.call(src, this.property)
|
|
3403
|
+
) {
|
|
3367
3404
|
throw new TypeError('variant lacks property ' + this.property);
|
|
3368
3405
|
}
|
|
3369
3406
|
this.union.discriminator.encode(this.variant, b, offset);
|
|
@@ -3371,8 +3408,7 @@ class VariantLayout extends Layout {
|
|
|
3371
3408
|
if (this.layout) {
|
|
3372
3409
|
this.layout.encode(src[this.property], b, offset + contentOffset);
|
|
3373
3410
|
span += this.layout.getSpan(b, offset + contentOffset);
|
|
3374
|
-
if (
|
|
3375
|
-
&& (span > this.union.span)) {
|
|
3411
|
+
if (0 <= this.union.span && span > this.union.span) {
|
|
3376
3412
|
throw new Error('encoded variant overruns containing union');
|
|
3377
3413
|
}
|
|
3378
3414
|
}
|
|
@@ -3433,12 +3469,10 @@ function fixBitwiseResult(v) {
|
|
|
3433
3469
|
*/
|
|
3434
3470
|
class BitStructure extends Layout {
|
|
3435
3471
|
constructor(word, msb, property) {
|
|
3436
|
-
if (!(
|
|
3437
|
-
|| (word instanceof UIntBE))) {
|
|
3472
|
+
if (!(word instanceof UInt || word instanceof UIntBE)) {
|
|
3438
3473
|
throw new TypeError('word must be a UInt or UIntBE layout');
|
|
3439
3474
|
}
|
|
3440
|
-
if (
|
|
3441
|
-
&& (undefined === property)) {
|
|
3475
|
+
if ('string' === typeof msb && undefined === property) {
|
|
3442
3476
|
property = msb;
|
|
3443
3477
|
msb = false;
|
|
3444
3478
|
}
|
|
@@ -3582,15 +3616,19 @@ class BitField {
|
|
|
3582
3616
|
if (!(container instanceof BitStructure)) {
|
|
3583
3617
|
throw new TypeError('container must be a BitStructure');
|
|
3584
3618
|
}
|
|
3585
|
-
if (
|
|
3619
|
+
if (!Number.isInteger(bits) || 0 >= bits) {
|
|
3586
3620
|
throw new TypeError('bits must be positive integer');
|
|
3587
3621
|
}
|
|
3588
3622
|
const totalBits = 8 * container.span;
|
|
3589
3623
|
const usedBits = container.fields.reduce((sum, fd) => sum + fd.bits, 0);
|
|
3590
|
-
if (
|
|
3591
|
-
throw new Error(
|
|
3592
|
-
|
|
3593
|
-
|
|
3624
|
+
if (bits + usedBits > totalBits) {
|
|
3625
|
+
throw new Error(
|
|
3626
|
+
'bits too long for span remainder (' +
|
|
3627
|
+
(totalBits - usedBits) +
|
|
3628
|
+
' of ' +
|
|
3629
|
+
totalBits +
|
|
3630
|
+
' remain)',
|
|
3631
|
+
);
|
|
3594
3632
|
}
|
|
3595
3633
|
/** The {@link BitStructure} instance to which this bit field
|
|
3596
3634
|
* belongs. */
|
|
@@ -3603,8 +3641,9 @@ class BitField {
|
|
|
3603
3641
|
* That is, it masks a value that has not yet been shifted into
|
|
3604
3642
|
* position within its containing packed integer. */
|
|
3605
3643
|
this.valueMask = (1 << bits) - 1;
|
|
3606
|
-
if (32 === bits) {
|
|
3607
|
-
|
|
3644
|
+
if (32 === bits) {
|
|
3645
|
+
// shifted value out of range
|
|
3646
|
+
this.valueMask = 0xffffffff;
|
|
3608
3647
|
}
|
|
3609
3648
|
/** The offset of the value within the containing packed unsigned
|
|
3610
3649
|
* integer. The least significant bit of the packed value is at
|
|
@@ -3642,16 +3681,22 @@ class BitField {
|
|
|
3642
3681
|
* **NOTE** This is not a specialization of {@link
|
|
3643
3682
|
* Layout#encode|Layout.encode} and there is no return value. */
|
|
3644
3683
|
encode(value) {
|
|
3645
|
-
if (
|
|
3646
|
-
|
|
3647
|
-
|
|
3648
|
-
|
|
3649
|
-
|
|
3684
|
+
if (
|
|
3685
|
+
'number' !== typeof value ||
|
|
3686
|
+
!Number.isInteger(value) ||
|
|
3687
|
+
value !== fixBitwiseResult(value & this.valueMask)
|
|
3688
|
+
) {
|
|
3689
|
+
throw new TypeError(
|
|
3690
|
+
nameWithProperty('BitField.encode', this) +
|
|
3691
|
+
' value must be integer not exceeding ' +
|
|
3692
|
+
this.valueMask,
|
|
3693
|
+
);
|
|
3650
3694
|
}
|
|
3651
3695
|
const word = this.container._packedGetValue();
|
|
3652
3696
|
const wordValue = fixBitwiseResult(value << this.start);
|
|
3653
|
-
this.container._packedSetValue(
|
|
3654
|
-
| wordValue
|
|
3697
|
+
this.container._packedSetValue(
|
|
3698
|
+
fixBitwiseResult(word & ~this.wordMask) | wordValue,
|
|
3699
|
+
);
|
|
3655
3700
|
}
|
|
3656
3701
|
}
|
|
3657
3702
|
Layout$1.BitField = BitField;
|
|
@@ -3709,10 +3754,16 @@ Layout$1.Boolean = Boolean;
|
|
|
3709
3754
|
*/
|
|
3710
3755
|
class Blob extends Layout {
|
|
3711
3756
|
constructor(length, property) {
|
|
3712
|
-
if (
|
|
3713
|
-
|
|
3714
|
-
|
|
3715
|
-
|
|
3757
|
+
if (
|
|
3758
|
+
!(
|
|
3759
|
+
(length instanceof ExternalLayout && length.isCount()) ||
|
|
3760
|
+
(Number.isInteger(length) && 0 <= length)
|
|
3761
|
+
)
|
|
3762
|
+
) {
|
|
3763
|
+
throw new TypeError(
|
|
3764
|
+
'length must be positive integer ' +
|
|
3765
|
+
'or an unsigned integer ExternalLayout',
|
|
3766
|
+
);
|
|
3716
3767
|
}
|
|
3717
3768
|
let span = -1;
|
|
3718
3769
|
if (!(length instanceof ExternalLayout)) {
|
|
@@ -3753,14 +3804,23 @@ class Blob extends Layout {
|
|
|
3753
3804
|
span = src.length;
|
|
3754
3805
|
}
|
|
3755
3806
|
if (!(src instanceof Uint8Array && span === src.length)) {
|
|
3756
|
-
throw new TypeError(
|
|
3757
|
-
|
|
3807
|
+
throw new TypeError(
|
|
3808
|
+
nameWithProperty('Blob.encode', this) +
|
|
3809
|
+
' requires (length ' +
|
|
3810
|
+
span +
|
|
3811
|
+
') Uint8Array as src',
|
|
3812
|
+
);
|
|
3758
3813
|
}
|
|
3759
|
-
if (
|
|
3814
|
+
if (offset + span > b.length) {
|
|
3760
3815
|
throw new RangeError('encoding overruns Uint8Array');
|
|
3761
3816
|
}
|
|
3762
3817
|
const srcBuffer = uint8ArrayToBuffer(src);
|
|
3763
|
-
uint8ArrayToBuffer(b).write(
|
|
3818
|
+
uint8ArrayToBuffer(b).write(
|
|
3819
|
+
srcBuffer.toString('hex'),
|
|
3820
|
+
offset,
|
|
3821
|
+
span,
|
|
3822
|
+
'hex',
|
|
3823
|
+
);
|
|
3764
3824
|
if (this.length instanceof ExternalLayout) {
|
|
3765
3825
|
this.length.encode(span, b, offset);
|
|
3766
3826
|
}
|
|
@@ -3789,7 +3849,7 @@ class CString extends Layout {
|
|
|
3789
3849
|
getSpan(b, offset = 0) {
|
|
3790
3850
|
checkUint8Array(b);
|
|
3791
3851
|
let idx = offset;
|
|
3792
|
-
while (
|
|
3852
|
+
while (idx < b.length && 0 !== b[idx]) {
|
|
3793
3853
|
idx += 1;
|
|
3794
3854
|
}
|
|
3795
3855
|
return 1 + idx - offset;
|
|
@@ -3797,7 +3857,9 @@ class CString extends Layout {
|
|
|
3797
3857
|
/** @override */
|
|
3798
3858
|
decode(b, offset = 0) {
|
|
3799
3859
|
const span = this.getSpan(b, offset);
|
|
3800
|
-
return uint8ArrayToBuffer(b)
|
|
3860
|
+
return uint8ArrayToBuffer(b)
|
|
3861
|
+
.slice(offset, offset + span - 1)
|
|
3862
|
+
.toString('utf-8');
|
|
3801
3863
|
}
|
|
3802
3864
|
/** @override */
|
|
3803
3865
|
encode(src, b, offset = 0) {
|
|
@@ -3809,7 +3871,7 @@ class CString extends Layout {
|
|
|
3809
3871
|
}
|
|
3810
3872
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3811
3873
|
const span = srcb.length;
|
|
3812
|
-
if (
|
|
3874
|
+
if (offset + span > b.length) {
|
|
3813
3875
|
throw new RangeError('encoding overruns Buffer');
|
|
3814
3876
|
}
|
|
3815
3877
|
const buffer = uint8ArrayToBuffer(b);
|
|
@@ -3840,14 +3902,13 @@ Layout$1.CString = CString;
|
|
|
3840
3902
|
*/
|
|
3841
3903
|
class UTF8 extends Layout {
|
|
3842
3904
|
constructor(maxSpan, property) {
|
|
3843
|
-
if (
|
|
3905
|
+
if ('string' === typeof maxSpan && undefined === property) {
|
|
3844
3906
|
property = maxSpan;
|
|
3845
3907
|
maxSpan = undefined;
|
|
3846
3908
|
}
|
|
3847
3909
|
if (undefined === maxSpan) {
|
|
3848
3910
|
maxSpan = -1;
|
|
3849
|
-
}
|
|
3850
|
-
else if (!Number.isInteger(maxSpan)) {
|
|
3911
|
+
} else if (!Number.isInteger(maxSpan)) {
|
|
3851
3912
|
throw new TypeError('maxSpan must be an integer');
|
|
3852
3913
|
}
|
|
3853
3914
|
super(-1, property);
|
|
@@ -3869,11 +3930,12 @@ class UTF8 extends Layout {
|
|
|
3869
3930
|
/** @override */
|
|
3870
3931
|
decode(b, offset = 0) {
|
|
3871
3932
|
const span = this.getSpan(b, offset);
|
|
3872
|
-
if (
|
|
3873
|
-
&& (this.maxSpan < span)) {
|
|
3933
|
+
if (0 <= this.maxSpan && this.maxSpan < span) {
|
|
3874
3934
|
throw new RangeError('text length exceeds maxSpan');
|
|
3875
3935
|
}
|
|
3876
|
-
return uint8ArrayToBuffer(b)
|
|
3936
|
+
return uint8ArrayToBuffer(b)
|
|
3937
|
+
.slice(offset, offset + span)
|
|
3938
|
+
.toString('utf-8');
|
|
3877
3939
|
}
|
|
3878
3940
|
/** @override */
|
|
3879
3941
|
encode(src, b, offset = 0) {
|
|
@@ -3885,11 +3947,10 @@ class UTF8 extends Layout {
|
|
|
3885
3947
|
}
|
|
3886
3948
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3887
3949
|
const span = srcb.length;
|
|
3888
|
-
if (
|
|
3889
|
-
&& (this.maxSpan < span)) {
|
|
3950
|
+
if (0 <= this.maxSpan && this.maxSpan < span) {
|
|
3890
3951
|
throw new RangeError('text length exceeds maxSpan');
|
|
3891
3952
|
}
|
|
3892
|
-
if (
|
|
3953
|
+
if (offset + span > b.length) {
|
|
3893
3954
|
throw new RangeError('encoding overruns Buffer');
|
|
3894
3955
|
}
|
|
3895
3956
|
srcb.copy(uint8ArrayToBuffer(b), offset);
|
|
@@ -3942,115 +4003,121 @@ class Constant extends Layout {
|
|
|
3942
4003
|
}
|
|
3943
4004
|
Layout$1.Constant = Constant;
|
|
3944
4005
|
/** Factory for {@link GreedyCount}. */
|
|
3945
|
-
Layout$1.greedy = (
|
|
4006
|
+
Layout$1.greedy = (elementSpan, property) =>
|
|
4007
|
+
new GreedyCount(elementSpan, property);
|
|
3946
4008
|
/** Factory for {@link OffsetLayout}. */
|
|
3947
|
-
Layout$1.offset = (
|
|
4009
|
+
Layout$1.offset = (layout, offset, property) =>
|
|
4010
|
+
new OffsetLayout(layout, offset, property);
|
|
3948
4011
|
/** Factory for {@link UInt|unsigned int layouts} spanning one
|
|
3949
4012
|
* byte. */
|
|
3950
|
-
var u8 = Layout$1.u8 =
|
|
4013
|
+
var u8 = (Layout$1.u8 = property => new UInt(1, property));
|
|
3951
4014
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3952
4015
|
* spanning two bytes. */
|
|
3953
|
-
Layout$1.u16 =
|
|
4016
|
+
Layout$1.u16 = property => new UInt(2, property);
|
|
3954
4017
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3955
4018
|
* spanning three bytes. */
|
|
3956
|
-
Layout$1.u24 =
|
|
4019
|
+
Layout$1.u24 = property => new UInt(3, property);
|
|
3957
4020
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3958
4021
|
* spanning four bytes. */
|
|
3959
|
-
var u32 = Layout$1.u32 =
|
|
4022
|
+
var u32 = (Layout$1.u32 = property => new UInt(4, property));
|
|
3960
4023
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3961
4024
|
* spanning five bytes. */
|
|
3962
|
-
Layout$1.u40 =
|
|
4025
|
+
Layout$1.u40 = property => new UInt(5, property);
|
|
3963
4026
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3964
4027
|
* spanning six bytes. */
|
|
3965
|
-
Layout$1.u48 =
|
|
4028
|
+
Layout$1.u48 = property => new UInt(6, property);
|
|
3966
4029
|
/** Factory for {@link NearUInt64|little-endian unsigned int
|
|
3967
4030
|
* layouts} interpreted as Numbers. */
|
|
3968
|
-
Layout$1.nu64 =
|
|
4031
|
+
Layout$1.nu64 = property => new NearUInt64(property);
|
|
3969
4032
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3970
4033
|
* spanning two bytes. */
|
|
3971
|
-
Layout$1.u16be =
|
|
4034
|
+
Layout$1.u16be = property => new UIntBE(2, property);
|
|
3972
4035
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3973
4036
|
* spanning three bytes. */
|
|
3974
|
-
Layout$1.u24be =
|
|
4037
|
+
Layout$1.u24be = property => new UIntBE(3, property);
|
|
3975
4038
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3976
4039
|
* spanning four bytes. */
|
|
3977
|
-
Layout$1.u32be =
|
|
4040
|
+
Layout$1.u32be = property => new UIntBE(4, property);
|
|
3978
4041
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3979
4042
|
* spanning five bytes. */
|
|
3980
|
-
Layout$1.u40be =
|
|
4043
|
+
Layout$1.u40be = property => new UIntBE(5, property);
|
|
3981
4044
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3982
4045
|
* spanning six bytes. */
|
|
3983
|
-
Layout$1.u48be =
|
|
4046
|
+
Layout$1.u48be = property => new UIntBE(6, property);
|
|
3984
4047
|
/** Factory for {@link NearUInt64BE|big-endian unsigned int
|
|
3985
4048
|
* layouts} interpreted as Numbers. */
|
|
3986
|
-
Layout$1.nu64be =
|
|
4049
|
+
Layout$1.nu64be = property => new NearUInt64BE(property);
|
|
3987
4050
|
/** Factory for {@link Int|signed int layouts} spanning one
|
|
3988
4051
|
* byte. */
|
|
3989
|
-
Layout$1.s8 =
|
|
4052
|
+
Layout$1.s8 = property => new Int(1, property);
|
|
3990
4053
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3991
4054
|
* spanning two bytes. */
|
|
3992
|
-
Layout$1.s16 =
|
|
4055
|
+
Layout$1.s16 = property => new Int(2, property);
|
|
3993
4056
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3994
4057
|
* spanning three bytes. */
|
|
3995
|
-
Layout$1.s24 =
|
|
4058
|
+
Layout$1.s24 = property => new Int(3, property);
|
|
3996
4059
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3997
4060
|
* spanning four bytes. */
|
|
3998
|
-
Layout$1.s32 =
|
|
4061
|
+
Layout$1.s32 = property => new Int(4, property);
|
|
3999
4062
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
4000
4063
|
* spanning five bytes. */
|
|
4001
|
-
Layout$1.s40 =
|
|
4064
|
+
Layout$1.s40 = property => new Int(5, property);
|
|
4002
4065
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
4003
4066
|
* spanning six bytes. */
|
|
4004
|
-
Layout$1.s48 =
|
|
4067
|
+
Layout$1.s48 = property => new Int(6, property);
|
|
4005
4068
|
/** Factory for {@link NearInt64|little-endian signed int layouts}
|
|
4006
4069
|
* interpreted as Numbers. */
|
|
4007
|
-
Layout$1.ns64 =
|
|
4070
|
+
Layout$1.ns64 = property => new NearInt64(property);
|
|
4008
4071
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4009
4072
|
* spanning two bytes. */
|
|
4010
|
-
Layout$1.s16be =
|
|
4073
|
+
Layout$1.s16be = property => new IntBE(2, property);
|
|
4011
4074
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4012
4075
|
* spanning three bytes. */
|
|
4013
|
-
Layout$1.s24be =
|
|
4076
|
+
Layout$1.s24be = property => new IntBE(3, property);
|
|
4014
4077
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4015
4078
|
* spanning four bytes. */
|
|
4016
|
-
Layout$1.s32be =
|
|
4079
|
+
Layout$1.s32be = property => new IntBE(4, property);
|
|
4017
4080
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4018
4081
|
* spanning five bytes. */
|
|
4019
|
-
Layout$1.s40be =
|
|
4082
|
+
Layout$1.s40be = property => new IntBE(5, property);
|
|
4020
4083
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4021
4084
|
* spanning six bytes. */
|
|
4022
|
-
Layout$1.s48be =
|
|
4085
|
+
Layout$1.s48be = property => new IntBE(6, property);
|
|
4023
4086
|
/** Factory for {@link NearInt64BE|big-endian signed int layouts}
|
|
4024
4087
|
* interpreted as Numbers. */
|
|
4025
|
-
Layout$1.ns64be =
|
|
4088
|
+
Layout$1.ns64be = property => new NearInt64BE(property);
|
|
4026
4089
|
/** Factory for {@link Float|little-endian 32-bit floating point} values. */
|
|
4027
|
-
Layout$1.f32 =
|
|
4090
|
+
Layout$1.f32 = property => new Float(property);
|
|
4028
4091
|
/** Factory for {@link FloatBE|big-endian 32-bit floating point} values. */
|
|
4029
|
-
Layout$1.f32be =
|
|
4092
|
+
Layout$1.f32be = property => new FloatBE(property);
|
|
4030
4093
|
/** Factory for {@link Double|little-endian 64-bit floating point} values. */
|
|
4031
|
-
Layout$1.f64 =
|
|
4094
|
+
Layout$1.f64 = property => new Double(property);
|
|
4032
4095
|
/** Factory for {@link DoubleBE|big-endian 64-bit floating point} values. */
|
|
4033
|
-
Layout$1.f64be =
|
|
4096
|
+
Layout$1.f64be = property => new DoubleBE(property);
|
|
4034
4097
|
/** Factory for {@link Structure} values. */
|
|
4035
|
-
var struct = Layout$1.struct = (
|
|
4098
|
+
var struct = (Layout$1.struct = (fields, property, decodePrefixes) =>
|
|
4099
|
+
new Structure(fields, property, decodePrefixes));
|
|
4036
4100
|
/** Factory for {@link BitStructure} values. */
|
|
4037
|
-
Layout$1.bits = (
|
|
4101
|
+
Layout$1.bits = (word, msb, property) => new BitStructure(word, msb, property);
|
|
4038
4102
|
/** Factory for {@link Sequence} values. */
|
|
4039
|
-
Layout$1.seq = (
|
|
4103
|
+
Layout$1.seq = (elementLayout, count, property) =>
|
|
4104
|
+
new Sequence(elementLayout, count, property);
|
|
4040
4105
|
/** Factory for {@link Union} values. */
|
|
4041
|
-
Layout$1.union = (
|
|
4106
|
+
Layout$1.union = (discr, defaultLayout, property) =>
|
|
4107
|
+
new Union(discr, defaultLayout, property);
|
|
4042
4108
|
/** Factory for {@link UnionLayoutDiscriminator} values. */
|
|
4043
|
-
Layout$1.unionLayoutDiscriminator = (
|
|
4109
|
+
Layout$1.unionLayoutDiscriminator = (layout, property) =>
|
|
4110
|
+
new UnionLayoutDiscriminator(layout, property);
|
|
4044
4111
|
/** Factory for {@link Blob} values. */
|
|
4045
|
-
var blob = Layout$1.blob = (
|
|
4112
|
+
var blob = (Layout$1.blob = (length, property) => new Blob(length, property));
|
|
4046
4113
|
/** Factory for {@link CString} values. */
|
|
4047
|
-
Layout$1.cstr =
|
|
4114
|
+
Layout$1.cstr = property => new CString(property);
|
|
4048
4115
|
/** Factory for {@link UTF8} values. */
|
|
4049
|
-
Layout$1.utf8 = (
|
|
4116
|
+
Layout$1.utf8 = (maxSpan, property) => new UTF8(maxSpan, property);
|
|
4050
4117
|
/** Factory for {@link Constant} values. */
|
|
4051
|
-
Layout$1.constant = (
|
|
4118
|
+
Layout$1.constant = (value, property) => new Constant(value, property);
|
|
4052
4119
|
|
|
4053
|
-
const encodeDecode =
|
|
4120
|
+
const encodeDecode = layout => {
|
|
4054
4121
|
const decode = layout.decode.bind(layout);
|
|
4055
4122
|
const encode = layout.encode.bind(layout);
|
|
4056
4123
|
return { decode, encode };
|
|
@@ -4058,7 +4125,7 @@ const encodeDecode = (layout) => {
|
|
|
4058
4125
|
|
|
4059
4126
|
var browser = {};
|
|
4060
4127
|
|
|
4061
|
-
Object.defineProperty(browser,
|
|
4128
|
+
Object.defineProperty(browser, '__esModule', { value: true });
|
|
4062
4129
|
/**
|
|
4063
4130
|
* Convert a little-endian buffer into a BigInt.
|
|
4064
4131
|
* @param buf The little-endian buffer to convert
|
|
@@ -4075,7 +4142,7 @@ function toBigIntLE(buf) {
|
|
|
4075
4142
|
return BigInt(`0x${hex}`);
|
|
4076
4143
|
}
|
|
4077
4144
|
}
|
|
4078
|
-
var toBigIntLE_1 = browser.toBigIntLE = toBigIntLE;
|
|
4145
|
+
var toBigIntLE_1 = (browser.toBigIntLE = toBigIntLE);
|
|
4079
4146
|
/**
|
|
4080
4147
|
* Convert a big-endian buffer into a BigInt
|
|
4081
4148
|
* @param buf The big-endian buffer to convert.
|
|
@@ -4100,12 +4167,15 @@ browser.toBigIntBE = toBigIntBE;
|
|
|
4100
4167
|
function toBufferLE(num, width) {
|
|
4101
4168
|
{
|
|
4102
4169
|
const hex = num.toString(16);
|
|
4103
|
-
const buffer = Buffer.from(
|
|
4170
|
+
const buffer = Buffer.from(
|
|
4171
|
+
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4172
|
+
'hex',
|
|
4173
|
+
);
|
|
4104
4174
|
buffer.reverse();
|
|
4105
4175
|
return buffer;
|
|
4106
4176
|
}
|
|
4107
4177
|
}
|
|
4108
|
-
var toBufferLE_1 = browser.toBufferLE = toBufferLE;
|
|
4178
|
+
var toBufferLE_1 = (browser.toBufferLE = toBufferLE);
|
|
4109
4179
|
/**
|
|
4110
4180
|
* Convert a BigInt to a big-endian buffer.
|
|
4111
4181
|
* @param num The BigInt to convert.
|
|
@@ -4115,12 +4185,15 @@ var toBufferLE_1 = browser.toBufferLE = toBufferLE;
|
|
|
4115
4185
|
function toBufferBE(num, width) {
|
|
4116
4186
|
{
|
|
4117
4187
|
const hex = num.toString(16);
|
|
4118
|
-
return Buffer.from(
|
|
4188
|
+
return Buffer.from(
|
|
4189
|
+
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4190
|
+
'hex',
|
|
4191
|
+
);
|
|
4119
4192
|
}
|
|
4120
4193
|
}
|
|
4121
4194
|
browser.toBufferBE = toBufferBE;
|
|
4122
4195
|
|
|
4123
|
-
const bigInt =
|
|
4196
|
+
const bigInt = length => property => {
|
|
4124
4197
|
const layout = blob(length, property);
|
|
4125
4198
|
const { encode, decode } = encodeDecode(layout);
|
|
4126
4199
|
const bigIntLayout = layout;
|
|
@@ -4136,7 +4209,7 @@ const bigInt = (length) => (property) => {
|
|
|
4136
4209
|
};
|
|
4137
4210
|
const u64 = bigInt(8);
|
|
4138
4211
|
|
|
4139
|
-
const bool =
|
|
4212
|
+
const bool = property => {
|
|
4140
4213
|
const layout = u8(property);
|
|
4141
4214
|
const { encode, decode } = encodeDecode(layout);
|
|
4142
4215
|
const boolLayout = layout;
|
|
@@ -4151,7 +4224,7 @@ const bool = (property) => {
|
|
|
4151
4224
|
return boolLayout;
|
|
4152
4225
|
};
|
|
4153
4226
|
|
|
4154
|
-
const publicKey =
|
|
4227
|
+
const publicKey = property => {
|
|
4155
4228
|
const layout = blob(32, property);
|
|
4156
4229
|
const { encode, decode } = encodeDecode(layout);
|
|
4157
4230
|
const publicKeyLayout = layout;
|
|
@@ -4225,71 +4298,112 @@ class TokenOwnerOffCurveError extends TokenError {
|
|
|
4225
4298
|
/** Instructions defined by the program */
|
|
4226
4299
|
var TokenInstruction;
|
|
4227
4300
|
(function (TokenInstruction) {
|
|
4228
|
-
TokenInstruction[TokenInstruction[
|
|
4229
|
-
|
|
4230
|
-
TokenInstruction[TokenInstruction[
|
|
4231
|
-
|
|
4232
|
-
TokenInstruction[TokenInstruction[
|
|
4233
|
-
|
|
4234
|
-
TokenInstruction[TokenInstruction[
|
|
4235
|
-
TokenInstruction[TokenInstruction[
|
|
4236
|
-
TokenInstruction[TokenInstruction[
|
|
4237
|
-
TokenInstruction[TokenInstruction[
|
|
4238
|
-
TokenInstruction[TokenInstruction[
|
|
4239
|
-
TokenInstruction[TokenInstruction[
|
|
4240
|
-
TokenInstruction[TokenInstruction[
|
|
4241
|
-
TokenInstruction[TokenInstruction[
|
|
4242
|
-
|
|
4243
|
-
TokenInstruction[TokenInstruction[
|
|
4244
|
-
TokenInstruction[TokenInstruction[
|
|
4245
|
-
|
|
4246
|
-
TokenInstruction[TokenInstruction[
|
|
4247
|
-
|
|
4248
|
-
TokenInstruction[TokenInstruction[
|
|
4249
|
-
|
|
4250
|
-
TokenInstruction[TokenInstruction[
|
|
4251
|
-
TokenInstruction[TokenInstruction[
|
|
4252
|
-
|
|
4253
|
-
TokenInstruction[TokenInstruction[
|
|
4254
|
-
TokenInstruction[TokenInstruction[
|
|
4255
|
-
|
|
4256
|
-
TokenInstruction[TokenInstruction[
|
|
4257
|
-
|
|
4258
|
-
TokenInstruction[TokenInstruction[
|
|
4259
|
-
|
|
4260
|
-
TokenInstruction[TokenInstruction[
|
|
4261
|
-
|
|
4262
|
-
TokenInstruction[TokenInstruction[
|
|
4263
|
-
|
|
4264
|
-
TokenInstruction[TokenInstruction[
|
|
4301
|
+
TokenInstruction[(TokenInstruction['InitializeMint'] = 0)] =
|
|
4302
|
+
'InitializeMint';
|
|
4303
|
+
TokenInstruction[(TokenInstruction['InitializeAccount'] = 1)] =
|
|
4304
|
+
'InitializeAccount';
|
|
4305
|
+
TokenInstruction[(TokenInstruction['InitializeMultisig'] = 2)] =
|
|
4306
|
+
'InitializeMultisig';
|
|
4307
|
+
TokenInstruction[(TokenInstruction['Transfer'] = 3)] = 'Transfer';
|
|
4308
|
+
TokenInstruction[(TokenInstruction['Approve'] = 4)] = 'Approve';
|
|
4309
|
+
TokenInstruction[(TokenInstruction['Revoke'] = 5)] = 'Revoke';
|
|
4310
|
+
TokenInstruction[(TokenInstruction['SetAuthority'] = 6)] = 'SetAuthority';
|
|
4311
|
+
TokenInstruction[(TokenInstruction['MintTo'] = 7)] = 'MintTo';
|
|
4312
|
+
TokenInstruction[(TokenInstruction['Burn'] = 8)] = 'Burn';
|
|
4313
|
+
TokenInstruction[(TokenInstruction['CloseAccount'] = 9)] = 'CloseAccount';
|
|
4314
|
+
TokenInstruction[(TokenInstruction['FreezeAccount'] = 10)] =
|
|
4315
|
+
'FreezeAccount';
|
|
4316
|
+
TokenInstruction[(TokenInstruction['ThawAccount'] = 11)] = 'ThawAccount';
|
|
4317
|
+
TokenInstruction[(TokenInstruction['TransferChecked'] = 12)] =
|
|
4318
|
+
'TransferChecked';
|
|
4319
|
+
TokenInstruction[(TokenInstruction['ApproveChecked'] = 13)] =
|
|
4320
|
+
'ApproveChecked';
|
|
4321
|
+
TokenInstruction[(TokenInstruction['MintToChecked'] = 14)] =
|
|
4322
|
+
'MintToChecked';
|
|
4323
|
+
TokenInstruction[(TokenInstruction['BurnChecked'] = 15)] = 'BurnChecked';
|
|
4324
|
+
TokenInstruction[(TokenInstruction['InitializeAccount2'] = 16)] =
|
|
4325
|
+
'InitializeAccount2';
|
|
4326
|
+
TokenInstruction[(TokenInstruction['SyncNative'] = 17)] = 'SyncNative';
|
|
4327
|
+
TokenInstruction[(TokenInstruction['InitializeAccount3'] = 18)] =
|
|
4328
|
+
'InitializeAccount3';
|
|
4329
|
+
TokenInstruction[(TokenInstruction['InitializeMultisig2'] = 19)] =
|
|
4330
|
+
'InitializeMultisig2';
|
|
4331
|
+
TokenInstruction[(TokenInstruction['InitializeMint2'] = 20)] =
|
|
4332
|
+
'InitializeMint2';
|
|
4333
|
+
TokenInstruction[(TokenInstruction['GetAccountDataSize'] = 21)] =
|
|
4334
|
+
'GetAccountDataSize';
|
|
4335
|
+
TokenInstruction[(TokenInstruction['InitializeImmutableOwner'] = 22)] =
|
|
4336
|
+
'InitializeImmutableOwner';
|
|
4337
|
+
TokenInstruction[(TokenInstruction['AmountToUiAmount'] = 23)] =
|
|
4338
|
+
'AmountToUiAmount';
|
|
4339
|
+
TokenInstruction[(TokenInstruction['UiAmountToAmount'] = 24)] =
|
|
4340
|
+
'UiAmountToAmount';
|
|
4341
|
+
TokenInstruction[(TokenInstruction['InitializeMintCloseAuthority'] = 25)] =
|
|
4342
|
+
'InitializeMintCloseAuthority';
|
|
4343
|
+
TokenInstruction[(TokenInstruction['TransferFeeExtension'] = 26)] =
|
|
4344
|
+
'TransferFeeExtension';
|
|
4345
|
+
TokenInstruction[(TokenInstruction['ConfidentialTransferExtension'] = 27)] =
|
|
4346
|
+
'ConfidentialTransferExtension';
|
|
4347
|
+
TokenInstruction[(TokenInstruction['DefaultAccountStateExtension'] = 28)] =
|
|
4348
|
+
'DefaultAccountStateExtension';
|
|
4349
|
+
TokenInstruction[(TokenInstruction['Reallocate'] = 29)] = 'Reallocate';
|
|
4350
|
+
TokenInstruction[(TokenInstruction['MemoTransferExtension'] = 30)] =
|
|
4351
|
+
'MemoTransferExtension';
|
|
4352
|
+
TokenInstruction[(TokenInstruction['CreateNativeMint'] = 31)] =
|
|
4353
|
+
'CreateNativeMint';
|
|
4354
|
+
TokenInstruction[(TokenInstruction['InitializeNonTransferableMint'] = 32)] =
|
|
4355
|
+
'InitializeNonTransferableMint';
|
|
4356
|
+
TokenInstruction[(TokenInstruction['InterestBearingMintExtension'] = 33)] =
|
|
4357
|
+
'InterestBearingMintExtension';
|
|
4358
|
+
TokenInstruction[(TokenInstruction['CpiGuardExtension'] = 34)] =
|
|
4359
|
+
'CpiGuardExtension';
|
|
4360
|
+
TokenInstruction[(TokenInstruction['InitializePermanentDelegate'] = 35)] =
|
|
4361
|
+
'InitializePermanentDelegate';
|
|
4362
|
+
TokenInstruction[(TokenInstruction['TransferHookExtension'] = 36)] =
|
|
4363
|
+
'TransferHookExtension';
|
|
4265
4364
|
// ConfidentialTransferFeeExtension = 37,
|
|
4266
4365
|
// WithdrawalExcessLamports = 38,
|
|
4267
|
-
TokenInstruction[TokenInstruction[
|
|
4366
|
+
TokenInstruction[(TokenInstruction['MetadataPointerExtension'] = 39)] =
|
|
4367
|
+
'MetadataPointerExtension';
|
|
4368
|
+
TokenInstruction[(TokenInstruction['GroupPointerExtension'] = 40)] =
|
|
4369
|
+
'GroupPointerExtension';
|
|
4370
|
+
TokenInstruction[(TokenInstruction['GroupMemberPointerExtension'] = 41)] =
|
|
4371
|
+
'GroupMemberPointerExtension';
|
|
4268
4372
|
})(TokenInstruction || (TokenInstruction = {}));
|
|
4269
4373
|
|
|
4270
4374
|
/** @internal */
|
|
4271
4375
|
function addSigners(keys, ownerOrAuthority, multiSigners) {
|
|
4272
4376
|
if (multiSigners.length) {
|
|
4273
|
-
keys.push({
|
|
4377
|
+
keys.push({
|
|
4378
|
+
pubkey: ownerOrAuthority,
|
|
4379
|
+
isSigner: false,
|
|
4380
|
+
isWritable: false,
|
|
4381
|
+
});
|
|
4274
4382
|
for (const signer of multiSigners) {
|
|
4275
4383
|
keys.push({
|
|
4276
|
-
pubkey:
|
|
4384
|
+
pubkey:
|
|
4385
|
+
signer instanceof web3_js.PublicKey
|
|
4386
|
+
? signer
|
|
4387
|
+
: signer.publicKey,
|
|
4277
4388
|
isSigner: true,
|
|
4278
4389
|
isWritable: false,
|
|
4279
4390
|
});
|
|
4280
4391
|
}
|
|
4281
|
-
}
|
|
4282
|
-
|
|
4283
|
-
|
|
4392
|
+
} else {
|
|
4393
|
+
keys.push({
|
|
4394
|
+
pubkey: ownerOrAuthority,
|
|
4395
|
+
isSigner: true,
|
|
4396
|
+
isWritable: false,
|
|
4397
|
+
});
|
|
4284
4398
|
}
|
|
4285
4399
|
return keys;
|
|
4286
4400
|
}
|
|
4287
4401
|
|
|
4288
4402
|
var AccountType;
|
|
4289
4403
|
(function (AccountType) {
|
|
4290
|
-
AccountType[AccountType[
|
|
4291
|
-
AccountType[AccountType[
|
|
4292
|
-
AccountType[AccountType[
|
|
4404
|
+
AccountType[(AccountType['Uninitialized'] = 0)] = 'Uninitialized';
|
|
4405
|
+
AccountType[(AccountType['Mint'] = 1)] = 'Mint';
|
|
4406
|
+
AccountType[(AccountType['Account'] = 2)] = 'Account';
|
|
4293
4407
|
})(AccountType || (AccountType = {}));
|
|
4294
4408
|
const ACCOUNT_TYPE_SIZE = 1;
|
|
4295
4409
|
|
|
@@ -4316,9 +4430,9 @@ const MULTISIG_SIZE = MultisigLayout.span;
|
|
|
4316
4430
|
/** Token account state as stored by the program */
|
|
4317
4431
|
var AccountState;
|
|
4318
4432
|
(function (AccountState) {
|
|
4319
|
-
AccountState[AccountState[
|
|
4320
|
-
AccountState[AccountState[
|
|
4321
|
-
AccountState[AccountState[
|
|
4433
|
+
AccountState[(AccountState['Uninitialized'] = 0)] = 'Uninitialized';
|
|
4434
|
+
AccountState[(AccountState['Initialized'] = 1)] = 'Initialized';
|
|
4435
|
+
AccountState[(AccountState['Frozen'] = 2)] = 'Frozen';
|
|
4322
4436
|
})(AccountState || (AccountState = {}));
|
|
4323
4437
|
/** Buffer layout for de/serializing a token account */
|
|
4324
4438
|
const AccountLayout = struct([
|
|
@@ -4346,7 +4460,12 @@ const ACCOUNT_SIZE = AccountLayout.span;
|
|
|
4346
4460
|
*
|
|
4347
4461
|
* @return Token account information
|
|
4348
4462
|
*/
|
|
4349
|
-
async function getAccount(
|
|
4463
|
+
async function getAccount(
|
|
4464
|
+
connection,
|
|
4465
|
+
address,
|
|
4466
|
+
commitment,
|
|
4467
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4468
|
+
) {
|
|
4350
4469
|
const info = await connection.getAccountInfo(address, commitment);
|
|
4351
4470
|
return unpackAccount(address, info, programId);
|
|
4352
4471
|
}
|
|
@@ -4360,8 +4479,7 @@ async function getAccount(connection, address, commitment, programId = TOKEN_PRO
|
|
|
4360
4479
|
* @return Unpacked token account
|
|
4361
4480
|
*/
|
|
4362
4481
|
function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
4363
|
-
if (!info)
|
|
4364
|
-
throw new TokenAccountNotFoundError();
|
|
4482
|
+
if (!info) throw new TokenAccountNotFoundError();
|
|
4365
4483
|
if (!info.owner.equals(programId))
|
|
4366
4484
|
throw new TokenInvalidAccountOwnerError();
|
|
4367
4485
|
if (info.data.length < ACCOUNT_SIZE)
|
|
@@ -4385,8 +4503,12 @@ function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
|
4385
4503
|
isInitialized: rawAccount.state !== AccountState.Uninitialized,
|
|
4386
4504
|
isFrozen: rawAccount.state === AccountState.Frozen,
|
|
4387
4505
|
isNative: !!rawAccount.isNativeOption,
|
|
4388
|
-
rentExemptReserve: rawAccount.isNativeOption
|
|
4389
|
-
|
|
4506
|
+
rentExemptReserve: rawAccount.isNativeOption
|
|
4507
|
+
? rawAccount.isNative
|
|
4508
|
+
: null,
|
|
4509
|
+
closeAuthority: rawAccount.closeAuthorityOption
|
|
4510
|
+
? rawAccount.closeAuthority
|
|
4511
|
+
: null,
|
|
4390
4512
|
tlvData,
|
|
4391
4513
|
};
|
|
4392
4514
|
}
|
|
@@ -4414,13 +4536,53 @@ const MINT_SIZE = MintLayout.span;
|
|
|
4414
4536
|
*
|
|
4415
4537
|
* @return Address of the associated token account
|
|
4416
4538
|
*/
|
|
4417
|
-
function getAssociatedTokenAddressSync(
|
|
4539
|
+
function getAssociatedTokenAddressSync(
|
|
4540
|
+
mint,
|
|
4541
|
+
owner,
|
|
4542
|
+
allowOwnerOffCurve = false,
|
|
4543
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4544
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4545
|
+
) {
|
|
4418
4546
|
if (!allowOwnerOffCurve && !web3_js.PublicKey.isOnCurve(owner.toBuffer()))
|
|
4419
4547
|
throw new TokenOwnerOffCurveError();
|
|
4420
|
-
const [address] = web3_js.PublicKey.findProgramAddressSync(
|
|
4548
|
+
const [address] = web3_js.PublicKey.findProgramAddressSync(
|
|
4549
|
+
[owner.toBuffer(), programId.toBuffer(), mint.toBuffer()],
|
|
4550
|
+
associatedTokenProgramId,
|
|
4551
|
+
);
|
|
4421
4552
|
return address;
|
|
4422
4553
|
}
|
|
4423
4554
|
|
|
4555
|
+
class COptionPublicKeyLayout extends Layout_2 {
|
|
4556
|
+
constructor(property) {
|
|
4557
|
+
super(-1, property);
|
|
4558
|
+
this.publicKeyLayout = publicKey();
|
|
4559
|
+
}
|
|
4560
|
+
decode(buffer, offset = 0) {
|
|
4561
|
+
const option = buffer[offset];
|
|
4562
|
+
if (option === 0) {
|
|
4563
|
+
return null;
|
|
4564
|
+
}
|
|
4565
|
+
return this.publicKeyLayout.decode(buffer, offset + 1);
|
|
4566
|
+
}
|
|
4567
|
+
encode(src, buffer, offset = 0) {
|
|
4568
|
+
if (src === null) {
|
|
4569
|
+
buffer[offset] = 0;
|
|
4570
|
+
return 1;
|
|
4571
|
+
} else {
|
|
4572
|
+
buffer[offset] = 1;
|
|
4573
|
+
this.publicKeyLayout.encode(src, buffer, offset + 1);
|
|
4574
|
+
return 33;
|
|
4575
|
+
}
|
|
4576
|
+
}
|
|
4577
|
+
getSpan(buffer, offset = 0) {
|
|
4578
|
+
if (buffer) {
|
|
4579
|
+
const option = buffer[offset];
|
|
4580
|
+
return option === 0 ? 1 : 1 + this.publicKeyLayout.span;
|
|
4581
|
+
}
|
|
4582
|
+
return 1 + this.publicKeyLayout.span;
|
|
4583
|
+
}
|
|
4584
|
+
}
|
|
4585
|
+
|
|
4424
4586
|
/**
|
|
4425
4587
|
* Construct a CreateAssociatedTokenAccount instruction
|
|
4426
4588
|
*
|
|
@@ -4433,16 +4595,43 @@ function getAssociatedTokenAddressSync(mint, owner, allowOwnerOffCurve = false,
|
|
|
4433
4595
|
*
|
|
4434
4596
|
* @return Instruction to add to a transaction
|
|
4435
4597
|
*/
|
|
4436
|
-
function createAssociatedTokenAccountInstruction(
|
|
4437
|
-
|
|
4598
|
+
function createAssociatedTokenAccountInstruction(
|
|
4599
|
+
payer,
|
|
4600
|
+
associatedToken,
|
|
4601
|
+
owner,
|
|
4602
|
+
mint,
|
|
4603
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4604
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4605
|
+
) {
|
|
4606
|
+
return buildAssociatedTokenAccountInstruction(
|
|
4607
|
+
payer,
|
|
4608
|
+
associatedToken,
|
|
4609
|
+
owner,
|
|
4610
|
+
mint,
|
|
4611
|
+
Buffer.alloc(0),
|
|
4612
|
+
programId,
|
|
4613
|
+
associatedTokenProgramId,
|
|
4614
|
+
);
|
|
4438
4615
|
}
|
|
4439
|
-
function buildAssociatedTokenAccountInstruction(
|
|
4616
|
+
function buildAssociatedTokenAccountInstruction(
|
|
4617
|
+
payer,
|
|
4618
|
+
associatedToken,
|
|
4619
|
+
owner,
|
|
4620
|
+
mint,
|
|
4621
|
+
instructionData,
|
|
4622
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4623
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4624
|
+
) {
|
|
4440
4625
|
const keys = [
|
|
4441
4626
|
{ pubkey: payer, isSigner: true, isWritable: true },
|
|
4442
4627
|
{ pubkey: associatedToken, isSigner: false, isWritable: true },
|
|
4443
4628
|
{ pubkey: owner, isSigner: false, isWritable: false },
|
|
4444
4629
|
{ pubkey: mint, isSigner: false, isWritable: false },
|
|
4445
|
-
{
|
|
4630
|
+
{
|
|
4631
|
+
pubkey: web3_js.SystemProgram.programId,
|
|
4632
|
+
isSigner: false,
|
|
4633
|
+
isWritable: false,
|
|
4634
|
+
},
|
|
4446
4635
|
{ pubkey: programId, isSigner: false, isWritable: false },
|
|
4447
4636
|
];
|
|
4448
4637
|
return new web3_js.TransactionInstruction({
|
|
@@ -4457,8 +4646,7 @@ const initializeMint2InstructionData = struct([
|
|
|
4457
4646
|
u8('instruction'),
|
|
4458
4647
|
u8('decimals'),
|
|
4459
4648
|
publicKey('mintAuthority'),
|
|
4460
|
-
|
|
4461
|
-
publicKey('freezeAuthority'),
|
|
4649
|
+
new COptionPublicKeyLayout('freezeAuthority'),
|
|
4462
4650
|
]);
|
|
4463
4651
|
/**
|
|
4464
4652
|
* Construct an InitializeMint2 instruction
|
|
@@ -4471,16 +4659,24 @@ const initializeMint2InstructionData = struct([
|
|
|
4471
4659
|
*
|
|
4472
4660
|
* @return Instruction to add to a transaction
|
|
4473
4661
|
*/
|
|
4474
|
-
function createInitializeMint2Instruction(
|
|
4662
|
+
function createInitializeMint2Instruction(
|
|
4663
|
+
mint,
|
|
4664
|
+
decimals,
|
|
4665
|
+
mintAuthority,
|
|
4666
|
+
freezeAuthority,
|
|
4667
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4668
|
+
) {
|
|
4475
4669
|
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];
|
|
4476
4670
|
const data = Buffer.alloc(initializeMint2InstructionData.span);
|
|
4477
|
-
initializeMint2InstructionData.encode(
|
|
4478
|
-
|
|
4479
|
-
|
|
4480
|
-
|
|
4481
|
-
|
|
4482
|
-
|
|
4483
|
-
|
|
4671
|
+
initializeMint2InstructionData.encode(
|
|
4672
|
+
{
|
|
4673
|
+
instruction: TokenInstruction.InitializeMint2,
|
|
4674
|
+
decimals,
|
|
4675
|
+
mintAuthority,
|
|
4676
|
+
freezeAuthority,
|
|
4677
|
+
},
|
|
4678
|
+
data,
|
|
4679
|
+
);
|
|
4484
4680
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4485
4681
|
}
|
|
4486
4682
|
|
|
@@ -4499,39 +4695,77 @@ function createInitializeMint2Instruction(mint, decimals, mintAuthority, freezeA
|
|
|
4499
4695
|
*
|
|
4500
4696
|
* @return Address of the new associated token account
|
|
4501
4697
|
*/
|
|
4502
|
-
async function getOrCreateAssociatedTokenAccount(
|
|
4503
|
-
|
|
4698
|
+
async function getOrCreateAssociatedTokenAccount(
|
|
4699
|
+
connection,
|
|
4700
|
+
payer,
|
|
4701
|
+
mint,
|
|
4702
|
+
owner,
|
|
4703
|
+
allowOwnerOffCurve = false,
|
|
4704
|
+
commitment,
|
|
4705
|
+
confirmOptions,
|
|
4706
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4707
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4708
|
+
) {
|
|
4709
|
+
const associatedToken = getAssociatedTokenAddressSync(
|
|
4710
|
+
mint,
|
|
4711
|
+
owner,
|
|
4712
|
+
allowOwnerOffCurve,
|
|
4713
|
+
programId,
|
|
4714
|
+
associatedTokenProgramId,
|
|
4715
|
+
);
|
|
4504
4716
|
// This is the optimal logic, considering TX fee, client-side computation, RPC roundtrips and guaranteed idempotent.
|
|
4505
4717
|
// Sadly we can't do this atomically.
|
|
4506
4718
|
let account;
|
|
4507
4719
|
try {
|
|
4508
|
-
account = await getAccount(
|
|
4509
|
-
|
|
4510
|
-
|
|
4720
|
+
account = await getAccount(
|
|
4721
|
+
connection,
|
|
4722
|
+
associatedToken,
|
|
4723
|
+
commitment,
|
|
4724
|
+
programId,
|
|
4725
|
+
);
|
|
4726
|
+
} catch (error) {
|
|
4511
4727
|
// TokenAccountNotFoundError can be possible if the associated address has already received some lamports,
|
|
4512
4728
|
// becoming a system account. Assuming program derived addressing is safe, this is the only case for the
|
|
4513
4729
|
// TokenInvalidAccountOwnerError in this code path.
|
|
4514
|
-
if (
|
|
4730
|
+
if (
|
|
4731
|
+
error instanceof TokenAccountNotFoundError ||
|
|
4732
|
+
error instanceof TokenInvalidAccountOwnerError
|
|
4733
|
+
) {
|
|
4515
4734
|
// As this isn't atomic, it's possible others can create associated accounts meanwhile.
|
|
4516
4735
|
try {
|
|
4517
|
-
const transaction = new web3_js.Transaction().add(
|
|
4518
|
-
|
|
4519
|
-
|
|
4520
|
-
|
|
4736
|
+
const transaction = new web3_js.Transaction().add(
|
|
4737
|
+
createAssociatedTokenAccountInstruction(
|
|
4738
|
+
payer.publicKey,
|
|
4739
|
+
associatedToken,
|
|
4740
|
+
owner,
|
|
4741
|
+
mint,
|
|
4742
|
+
programId,
|
|
4743
|
+
associatedTokenProgramId,
|
|
4744
|
+
),
|
|
4745
|
+
);
|
|
4746
|
+
await web3_js.sendAndConfirmTransaction(
|
|
4747
|
+
connection,
|
|
4748
|
+
transaction,
|
|
4749
|
+
[payer],
|
|
4750
|
+
confirmOptions,
|
|
4751
|
+
);
|
|
4752
|
+
} catch (error) {
|
|
4521
4753
|
// Ignore all errors; for now there is no API-compatible way to selectively ignore the expected
|
|
4522
4754
|
// instruction error if the associated account exists already.
|
|
4523
4755
|
}
|
|
4524
4756
|
// Now this should always succeed
|
|
4525
|
-
account = await getAccount(
|
|
4526
|
-
|
|
4527
|
-
|
|
4757
|
+
account = await getAccount(
|
|
4758
|
+
connection,
|
|
4759
|
+
associatedToken,
|
|
4760
|
+
commitment,
|
|
4761
|
+
programId,
|
|
4762
|
+
);
|
|
4763
|
+
} else {
|
|
4528
4764
|
throw error;
|
|
4529
4765
|
}
|
|
4530
4766
|
}
|
|
4531
|
-
if (!account.mint.equals(mint))
|
|
4532
|
-
|
|
4533
|
-
if (!account.owner.equals(owner))
|
|
4534
|
-
throw new TokenInvalidOwnerError();
|
|
4767
|
+
if (!account.mint.equals(mint)) throw new TokenInvalidMintError();
|
|
4768
|
+
if (!account.owner.equals(owner)) throw new TokenInvalidOwnerError();
|
|
4535
4769
|
return account;
|
|
4536
4770
|
}
|
|
4537
4771
|
|
|
@@ -4549,29 +4783,46 @@ const mintToInstructionData = struct([u8('instruction'), u64('amount')]);
|
|
|
4549
4783
|
*
|
|
4550
4784
|
* @return Instruction to add to a transaction
|
|
4551
4785
|
*/
|
|
4552
|
-
function createMintToInstruction(
|
|
4553
|
-
|
|
4554
|
-
|
|
4555
|
-
|
|
4556
|
-
|
|
4786
|
+
function createMintToInstruction(
|
|
4787
|
+
mint,
|
|
4788
|
+
destination,
|
|
4789
|
+
authority,
|
|
4790
|
+
amount,
|
|
4791
|
+
multiSigners = [],
|
|
4792
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4793
|
+
) {
|
|
4794
|
+
const keys = addSigners(
|
|
4795
|
+
[
|
|
4796
|
+
{ pubkey: mint, isSigner: false, isWritable: true },
|
|
4797
|
+
{ pubkey: destination, isSigner: false, isWritable: true },
|
|
4798
|
+
],
|
|
4799
|
+
authority,
|
|
4800
|
+
multiSigners,
|
|
4801
|
+
);
|
|
4557
4802
|
const data = Buffer.alloc(mintToInstructionData.span);
|
|
4558
|
-
mintToInstructionData.encode(
|
|
4559
|
-
|
|
4560
|
-
|
|
4561
|
-
|
|
4803
|
+
mintToInstructionData.encode(
|
|
4804
|
+
{
|
|
4805
|
+
instruction: TokenInstruction.MintTo,
|
|
4806
|
+
amount: BigInt(amount),
|
|
4807
|
+
},
|
|
4808
|
+
data,
|
|
4809
|
+
);
|
|
4562
4810
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4563
4811
|
}
|
|
4564
4812
|
|
|
4565
4813
|
/**
|
|
4566
4814
|
* Sum up the token amounts of the compressed token accounts
|
|
4567
4815
|
*/
|
|
4568
|
-
const sumUpTokenAmount =
|
|
4569
|
-
return accounts.reduce(
|
|
4816
|
+
const sumUpTokenAmount = accounts => {
|
|
4817
|
+
return accounts.reduce(
|
|
4818
|
+
(acc, account) => acc.add(account.parsed.amount),
|
|
4819
|
+
stateless_js.bn(0),
|
|
4820
|
+
);
|
|
4570
4821
|
};
|
|
4571
4822
|
/**
|
|
4572
4823
|
* Validate that all the compressed token accounts are owned by the same owner.
|
|
4573
4824
|
*/
|
|
4574
|
-
const validateSameTokenOwner =
|
|
4825
|
+
const validateSameTokenOwner = accounts => {
|
|
4575
4826
|
const owner = accounts[0].parsed.owner;
|
|
4576
4827
|
accounts.forEach(acc => {
|
|
4577
4828
|
if (!acc.parsed.owner.equals(owner)) {
|
|
@@ -4582,7 +4833,7 @@ const validateSameTokenOwner = (accounts) => {
|
|
|
4582
4833
|
/**
|
|
4583
4834
|
* Parse compressed token accounts to get the mint, current owner and delegate.
|
|
4584
4835
|
*/
|
|
4585
|
-
const parseTokenData =
|
|
4836
|
+
const parseTokenData = compressedTokenAccounts => {
|
|
4586
4837
|
const mint = compressedTokenAccounts[0].parsed.mint;
|
|
4587
4838
|
const currentOwner = compressedTokenAccounts[0].parsed.owner;
|
|
4588
4839
|
const delegate = compressedTokenAccounts[0].parsed.delegate;
|
|
@@ -4596,13 +4847,22 @@ const parseTokenData = (compressedTokenAccounts) => {
|
|
|
4596
4847
|
* @returns Output token data for the transfer
|
|
4597
4848
|
* instruction
|
|
4598
4849
|
*/
|
|
4599
|
-
function createTransferOutputState(
|
|
4850
|
+
function createTransferOutputState(
|
|
4851
|
+
inputCompressedTokenAccounts,
|
|
4852
|
+
toAddress,
|
|
4853
|
+
amount,
|
|
4854
|
+
) {
|
|
4600
4855
|
amount = stateless_js.bn(amount);
|
|
4601
4856
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4602
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
4857
|
+
const inputLamports = stateless_js.sumUpLamports(
|
|
4858
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4859
|
+
);
|
|
4603
4860
|
const changeAmount = inputAmount.sub(amount);
|
|
4604
4861
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4605
|
-
if (
|
|
4862
|
+
if (
|
|
4863
|
+
changeAmount.eq(stateless_js.bn(0)) &&
|
|
4864
|
+
inputLamports.eq(stateless_js.bn(0))
|
|
4865
|
+
) {
|
|
4606
4866
|
return [
|
|
4607
4867
|
{
|
|
4608
4868
|
owner: toAddress,
|
|
@@ -4613,7 +4873,9 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
|
|
|
4613
4873
|
];
|
|
4614
4874
|
}
|
|
4615
4875
|
/// validates token program
|
|
4616
|
-
stateless_js.validateSameOwner(
|
|
4876
|
+
stateless_js.validateSameOwner(
|
|
4877
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4878
|
+
);
|
|
4617
4879
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4618
4880
|
const outputCompressedAccounts = [
|
|
4619
4881
|
{
|
|
@@ -4640,15 +4902,22 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
|
|
|
4640
4902
|
*/
|
|
4641
4903
|
function createDecompressOutputState(inputCompressedTokenAccounts, amount) {
|
|
4642
4904
|
amount = stateless_js.bn(amount);
|
|
4643
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
4905
|
+
const inputLamports = stateless_js.sumUpLamports(
|
|
4906
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4907
|
+
);
|
|
4644
4908
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4645
4909
|
const changeAmount = inputAmount.sub(amount);
|
|
4646
4910
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4647
4911
|
/// lamports gets decompressed
|
|
4648
|
-
if (
|
|
4912
|
+
if (
|
|
4913
|
+
changeAmount.eq(stateless_js.bn(0)) &&
|
|
4914
|
+
inputLamports.eq(stateless_js.bn(0))
|
|
4915
|
+
) {
|
|
4649
4916
|
return [];
|
|
4650
4917
|
}
|
|
4651
|
-
stateless_js.validateSameOwner(
|
|
4918
|
+
stateless_js.validateSameOwner(
|
|
4919
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4920
|
+
);
|
|
4652
4921
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4653
4922
|
const tokenTransferOutputs = [
|
|
4654
4923
|
{
|
|
@@ -4664,7 +4933,7 @@ class CompressedTokenProgram {
|
|
|
4664
4933
|
/**
|
|
4665
4934
|
* @internal
|
|
4666
4935
|
*/
|
|
4667
|
-
constructor() {
|
|
4936
|
+
constructor() {}
|
|
4668
4937
|
/** @internal */
|
|
4669
4938
|
static get program() {
|
|
4670
4939
|
if (!this._program) {
|
|
@@ -4682,21 +4951,38 @@ class CompressedTokenProgram {
|
|
|
4682
4951
|
/// program only for serde and building instructions, not for
|
|
4683
4952
|
/// interacting with the network.
|
|
4684
4953
|
const mockKeypair = web3_js.Keypair.generate();
|
|
4685
|
-
const mockConnection = new web3_js.Connection(
|
|
4686
|
-
|
|
4954
|
+
const mockConnection = new web3_js.Connection(
|
|
4955
|
+
'http://127.0.0.1:8899',
|
|
4956
|
+
'confirmed',
|
|
4957
|
+
);
|
|
4958
|
+
const mockProvider = new anchor.AnchorProvider(
|
|
4959
|
+
mockConnection,
|
|
4960
|
+
stateless_js.useWallet(mockKeypair),
|
|
4961
|
+
stateless_js.confirmConfig,
|
|
4962
|
+
);
|
|
4687
4963
|
anchor.setProvider(mockProvider);
|
|
4688
|
-
this._program = new anchor.Program(
|
|
4964
|
+
this._program = new anchor.Program(
|
|
4965
|
+
IDL,
|
|
4966
|
+
this.programId,
|
|
4967
|
+
mockProvider,
|
|
4968
|
+
);
|
|
4689
4969
|
}
|
|
4690
4970
|
}
|
|
4691
4971
|
/** @internal */
|
|
4692
4972
|
static deriveTokenPoolPda(mint) {
|
|
4693
4973
|
const seeds = [POOL_SEED, mint.toBuffer()];
|
|
4694
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4974
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4975
|
+
seeds,
|
|
4976
|
+
this.programId,
|
|
4977
|
+
);
|
|
4695
4978
|
return address;
|
|
4696
4979
|
}
|
|
4697
4980
|
/** @internal */
|
|
4698
4981
|
static get deriveCpiAuthorityPda() {
|
|
4699
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4982
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
4983
|
+
[CPI_AUTHORITY_SEED],
|
|
4984
|
+
this.programId,
|
|
4985
|
+
);
|
|
4700
4986
|
return address;
|
|
4701
4987
|
}
|
|
4702
4988
|
/**
|
|
@@ -4705,14 +4991,21 @@ class CompressedTokenProgram {
|
|
|
4705
4991
|
static async createMint(params) {
|
|
4706
4992
|
const { mint, authority, feePayer, rentExemptBalance } = params;
|
|
4707
4993
|
/// Create and initialize SPL Mint account
|
|
4708
|
-
const createMintAccountInstruction =
|
|
4709
|
-
|
|
4710
|
-
|
|
4711
|
-
|
|
4712
|
-
|
|
4713
|
-
|
|
4714
|
-
|
|
4715
|
-
|
|
4994
|
+
const createMintAccountInstruction =
|
|
4995
|
+
web3_js.SystemProgram.createAccount({
|
|
4996
|
+
fromPubkey: feePayer,
|
|
4997
|
+
lamports: rentExemptBalance,
|
|
4998
|
+
newAccountPubkey: mint,
|
|
4999
|
+
programId: TOKEN_PROGRAM_ID,
|
|
5000
|
+
space: MINT_SIZE,
|
|
5001
|
+
});
|
|
5002
|
+
const initializeMintInstruction = createInitializeMint2Instruction(
|
|
5003
|
+
mint,
|
|
5004
|
+
params.decimals,
|
|
5005
|
+
authority,
|
|
5006
|
+
params.freezeAuthority,
|
|
5007
|
+
TOKEN_PROGRAM_ID,
|
|
5008
|
+
);
|
|
4716
5009
|
const ix = await this.createTokenPool({
|
|
4717
5010
|
feePayer,
|
|
4718
5011
|
mint,
|
|
@@ -4729,13 +5022,13 @@ class CompressedTokenProgram {
|
|
|
4729
5022
|
const ix = await this.program.methods
|
|
4730
5023
|
.createTokenPool()
|
|
4731
5024
|
.accounts({
|
|
4732
|
-
|
|
4733
|
-
|
|
4734
|
-
|
|
4735
|
-
|
|
4736
|
-
|
|
4737
|
-
|
|
4738
|
-
|
|
5025
|
+
mint,
|
|
5026
|
+
feePayer,
|
|
5027
|
+
tokenPoolPda,
|
|
5028
|
+
systemProgram: web3_js.SystemProgram.programId,
|
|
5029
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5030
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5031
|
+
})
|
|
4739
5032
|
.instruction();
|
|
4740
5033
|
return ix;
|
|
4741
5034
|
}
|
|
@@ -4744,31 +5037,41 @@ class CompressedTokenProgram {
|
|
|
4744
5037
|
*/
|
|
4745
5038
|
static async mintTo(params) {
|
|
4746
5039
|
const systemKeys = stateless_js.defaultStaticAccountsStruct();
|
|
4747
|
-
const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
|
|
5040
|
+
const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
|
|
5041
|
+
params;
|
|
4748
5042
|
const tokenPoolPda = this.deriveTokenPoolPda(mint);
|
|
4749
|
-
const amounts = stateless_js
|
|
5043
|
+
const amounts = stateless_js
|
|
5044
|
+
.toArray(amount)
|
|
5045
|
+
.map(amount => stateless_js.bn(amount));
|
|
4750
5046
|
const toPubkeys = stateless_js.toArray(toPubkey);
|
|
4751
5047
|
if (amounts.length !== toPubkeys.length) {
|
|
4752
|
-
throw new Error(
|
|
5048
|
+
throw new Error(
|
|
5049
|
+
'Amount and toPubkey arrays must have the same length',
|
|
5050
|
+
);
|
|
4753
5051
|
}
|
|
4754
5052
|
const instruction = await this.program.methods
|
|
4755
5053
|
.mintTo(toPubkeys, amounts, null)
|
|
4756
5054
|
.accounts({
|
|
4757
|
-
|
|
4758
|
-
|
|
4759
|
-
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
4764
|
-
|
|
4765
|
-
|
|
4766
|
-
|
|
4767
|
-
|
|
4768
|
-
|
|
4769
|
-
|
|
4770
|
-
|
|
4771
|
-
|
|
5055
|
+
feePayer,
|
|
5056
|
+
authority,
|
|
5057
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5058
|
+
mint,
|
|
5059
|
+
tokenPoolPda,
|
|
5060
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5061
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5062
|
+
registeredProgramPda: systemKeys.registeredProgramPda,
|
|
5063
|
+
noopProgram: systemKeys.noopProgram,
|
|
5064
|
+
accountCompressionAuthority:
|
|
5065
|
+
systemKeys.accountCompressionAuthority,
|
|
5066
|
+
accountCompressionProgram: systemKeys.accountCompressionProgram,
|
|
5067
|
+
merkleTree:
|
|
5068
|
+
merkleTree !== null && merkleTree !== void 0
|
|
5069
|
+
? merkleTree
|
|
5070
|
+
: stateless_js.defaultTestStateTreeAccounts()
|
|
5071
|
+
.merkleTree,
|
|
5072
|
+
selfProgram: this.programId,
|
|
5073
|
+
solPoolPda: null,
|
|
5074
|
+
})
|
|
4772
5075
|
.instruction();
|
|
4773
5076
|
return instruction;
|
|
4774
5077
|
}
|
|
@@ -4777,10 +5080,22 @@ class CompressedTokenProgram {
|
|
|
4777
5080
|
* Mint tokens from registed SPL mint account to a compressed account
|
|
4778
5081
|
*/
|
|
4779
5082
|
static async approveAndMintTo(params) {
|
|
4780
|
-
const {
|
|
5083
|
+
const {
|
|
5084
|
+
mint,
|
|
5085
|
+
feePayer,
|
|
5086
|
+
authorityTokenAccount,
|
|
5087
|
+
authority,
|
|
5088
|
+
merkleTree,
|
|
5089
|
+
toPubkey,
|
|
5090
|
+
} = params;
|
|
4781
5091
|
const amount = BigInt(params.amount.toString());
|
|
4782
5092
|
/// 1. Mint to existing ATA of mintAuthority.
|
|
4783
|
-
const splMintToInstruction = createMintToInstruction(
|
|
5093
|
+
const splMintToInstruction = createMintToInstruction(
|
|
5094
|
+
mint,
|
|
5095
|
+
authorityTokenAccount,
|
|
5096
|
+
authority,
|
|
5097
|
+
amount,
|
|
5098
|
+
);
|
|
4784
5099
|
/// 2. Compress from mint authority ATA to recipient compressed account
|
|
4785
5100
|
const compressInstruction = await this.compress({
|
|
4786
5101
|
payer: feePayer,
|
|
@@ -4797,15 +5112,33 @@ class CompressedTokenProgram {
|
|
|
4797
5112
|
* Construct transfer instruction for compressed tokens
|
|
4798
5113
|
*/
|
|
4799
5114
|
static async transfer(params) {
|
|
4800
|
-
const {
|
|
4801
|
-
|
|
4802
|
-
|
|
5115
|
+
const {
|
|
5116
|
+
payer,
|
|
5117
|
+
inputCompressedTokenAccounts,
|
|
5118
|
+
recentInputStateRootIndices,
|
|
5119
|
+
recentValidityProof,
|
|
5120
|
+
amount,
|
|
5121
|
+
outputStateTrees,
|
|
5122
|
+
toAddress,
|
|
5123
|
+
} = params;
|
|
5124
|
+
const tokenTransferOutputs = createTransferOutputState(
|
|
5125
|
+
inputCompressedTokenAccounts,
|
|
5126
|
+
toAddress,
|
|
5127
|
+
amount,
|
|
5128
|
+
);
|
|
5129
|
+
const {
|
|
5130
|
+
inputTokenDataWithContext,
|
|
5131
|
+
packedOutputTokenData,
|
|
5132
|
+
remainingAccountMetas,
|
|
5133
|
+
} = packCompressedTokenAccounts({
|
|
4803
5134
|
inputCompressedTokenAccounts,
|
|
4804
5135
|
outputStateTrees,
|
|
4805
5136
|
rootIndices: recentInputStateRootIndices,
|
|
4806
5137
|
tokenTransferOutputs,
|
|
4807
5138
|
});
|
|
4808
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5139
|
+
const { mint, currentOwner } = parseTokenData(
|
|
5140
|
+
inputCompressedTokenAccounts,
|
|
5141
|
+
);
|
|
4809
5142
|
const data = {
|
|
4810
5143
|
proof: recentValidityProof,
|
|
4811
5144
|
mint,
|
|
@@ -4817,24 +5150,32 @@ class CompressedTokenProgram {
|
|
|
4817
5150
|
cpiContext: null,
|
|
4818
5151
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4819
5152
|
};
|
|
4820
|
-
const encodedData = this.program.coder.types.encode(
|
|
4821
|
-
|
|
5153
|
+
const encodedData = this.program.coder.types.encode(
|
|
5154
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5155
|
+
data,
|
|
5156
|
+
);
|
|
5157
|
+
const {
|
|
5158
|
+
accountCompressionAuthority,
|
|
5159
|
+
noopProgram,
|
|
5160
|
+
registeredProgramPda,
|
|
5161
|
+
accountCompressionProgram,
|
|
5162
|
+
} = stateless_js.defaultStaticAccountsStruct();
|
|
4822
5163
|
const instruction = await this.program.methods
|
|
4823
5164
|
.transfer(encodedData)
|
|
4824
5165
|
.accounts({
|
|
4825
|
-
|
|
4826
|
-
|
|
4827
|
-
|
|
4828
|
-
|
|
4829
|
-
|
|
4830
|
-
|
|
4831
|
-
|
|
4832
|
-
|
|
4833
|
-
|
|
4834
|
-
|
|
4835
|
-
|
|
4836
|
-
|
|
4837
|
-
|
|
5166
|
+
feePayer: payer,
|
|
5167
|
+
authority: currentOwner,
|
|
5168
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5169
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5170
|
+
registeredProgramPda: registeredProgramPda,
|
|
5171
|
+
noopProgram: noopProgram,
|
|
5172
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
5173
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
5174
|
+
selfProgram: this.programId,
|
|
5175
|
+
tokenPoolPda: null,
|
|
5176
|
+
compressOrDecompressTokenAccount: null,
|
|
5177
|
+
tokenProgram: null,
|
|
5178
|
+
})
|
|
4838
5179
|
.remainingAccounts(remainingAccountMetas)
|
|
4839
5180
|
.instruction();
|
|
4840
5181
|
return instruction;
|
|
@@ -4843,12 +5184,14 @@ class CompressedTokenProgram {
|
|
|
4843
5184
|
* Create lookup table instructions for the token program's default accounts.
|
|
4844
5185
|
*/
|
|
4845
5186
|
static async createTokenProgramLookupTable(params) {
|
|
4846
|
-
const { authority, mints, recentSlot, payer, remainingAccounts } =
|
|
4847
|
-
|
|
4848
|
-
|
|
4849
|
-
|
|
4850
|
-
|
|
4851
|
-
|
|
5187
|
+
const { authority, mints, recentSlot, payer, remainingAccounts } =
|
|
5188
|
+
params;
|
|
5189
|
+
const [createInstruction, lookupTableAddress] =
|
|
5190
|
+
web3_js.AddressLookupTableProgram.createLookupTable({
|
|
5191
|
+
authority,
|
|
5192
|
+
payer: authority,
|
|
5193
|
+
recentSlot,
|
|
5194
|
+
});
|
|
4852
5195
|
let optionalMintKeys = [];
|
|
4853
5196
|
if (mints) {
|
|
4854
5197
|
optionalMintKeys = [
|
|
@@ -4856,31 +5199,35 @@ class CompressedTokenProgram {
|
|
|
4856
5199
|
...mints.map(mint => this.deriveTokenPoolPda(mint)),
|
|
4857
5200
|
];
|
|
4858
5201
|
}
|
|
4859
|
-
const extendInstruction =
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
lookupTable: lookupTableAddress,
|
|
4863
|
-
addresses: [
|
|
4864
|
-
this.deriveCpiAuthorityPda,
|
|
4865
|
-
stateless_js.LightSystemProgram.programId,
|
|
4866
|
-
stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
|
|
4867
|
-
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
4868
|
-
stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
|
|
4869
|
-
stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
|
|
4870
|
-
stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
4871
|
-
stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
|
|
4872
|
-
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
4873
|
-
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
4874
|
-
this.programId,
|
|
4875
|
-
TOKEN_PROGRAM_ID,
|
|
4876
|
-
web3_js.SystemProgram.programId,
|
|
4877
|
-
web3_js.ComputeBudgetProgram.programId,
|
|
4878
|
-
ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
5202
|
+
const extendInstruction =
|
|
5203
|
+
web3_js.AddressLookupTableProgram.extendLookupTable({
|
|
5204
|
+
payer,
|
|
4879
5205
|
authority,
|
|
4880
|
-
|
|
4881
|
-
|
|
4882
|
-
|
|
4883
|
-
|
|
5206
|
+
lookupTable: lookupTableAddress,
|
|
5207
|
+
addresses: [
|
|
5208
|
+
this.deriveCpiAuthorityPda,
|
|
5209
|
+
stateless_js.LightSystemProgram.programId,
|
|
5210
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5211
|
+
.registeredProgramPda,
|
|
5212
|
+
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
5213
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5214
|
+
.accountCompressionAuthority,
|
|
5215
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5216
|
+
.accountCompressionProgram,
|
|
5217
|
+
stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
5218
|
+
stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
|
|
5219
|
+
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
5220
|
+
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
5221
|
+
this.programId,
|
|
5222
|
+
TOKEN_PROGRAM_ID,
|
|
5223
|
+
authority,
|
|
5224
|
+
...optionalMintKeys,
|
|
5225
|
+
...(remainingAccounts !== null &&
|
|
5226
|
+
remainingAccounts !== void 0
|
|
5227
|
+
? remainingAccounts
|
|
5228
|
+
: []),
|
|
5229
|
+
],
|
|
5230
|
+
});
|
|
4884
5231
|
return {
|
|
4885
5232
|
instructions: [createInstruction, extendInstruction],
|
|
4886
5233
|
address: lookupTableAddress,
|
|
@@ -4891,14 +5238,19 @@ class CompressedTokenProgram {
|
|
|
4891
5238
|
* @returns compressInstruction
|
|
4892
5239
|
*/
|
|
4893
5240
|
static async compress(params) {
|
|
4894
|
-
const { payer, owner, source, toAddress, mint, outputStateTree } =
|
|
5241
|
+
const { payer, owner, source, toAddress, mint, outputStateTree } =
|
|
5242
|
+
params;
|
|
4895
5243
|
if (Array.isArray(params.amount) !== Array.isArray(params.toAddress)) {
|
|
4896
|
-
throw new Error(
|
|
5244
|
+
throw new Error(
|
|
5245
|
+
'Both amount and toAddress must be arrays or both must be single values',
|
|
5246
|
+
);
|
|
4897
5247
|
}
|
|
4898
5248
|
let tokenTransferOutputs;
|
|
4899
5249
|
if (Array.isArray(params.amount) && Array.isArray(params.toAddress)) {
|
|
4900
5250
|
if (params.amount.length !== params.toAddress.length) {
|
|
4901
|
-
throw new Error(
|
|
5251
|
+
throw new Error(
|
|
5252
|
+
'Amount and toAddress arrays must have the same length',
|
|
5253
|
+
);
|
|
4902
5254
|
}
|
|
4903
5255
|
tokenTransferOutputs = params.amount.map((amt, index) => {
|
|
4904
5256
|
const amount = stateless_js.bn(amt);
|
|
@@ -4909,8 +5261,7 @@ class CompressedTokenProgram {
|
|
|
4909
5261
|
tlv: null,
|
|
4910
5262
|
};
|
|
4911
5263
|
});
|
|
4912
|
-
}
|
|
4913
|
-
else {
|
|
5264
|
+
} else {
|
|
4914
5265
|
tokenTransferOutputs = [
|
|
4915
5266
|
{
|
|
4916
5267
|
owner: toAddress,
|
|
@@ -4920,7 +5271,11 @@ class CompressedTokenProgram {
|
|
|
4920
5271
|
},
|
|
4921
5272
|
];
|
|
4922
5273
|
}
|
|
4923
|
-
const {
|
|
5274
|
+
const {
|
|
5275
|
+
inputTokenDataWithContext,
|
|
5276
|
+
packedOutputTokenData,
|
|
5277
|
+
remainingAccountMetas,
|
|
5278
|
+
} = packCompressedTokenAccounts({
|
|
4924
5279
|
inputCompressedTokenAccounts: [],
|
|
4925
5280
|
outputStateTrees: outputStateTree,
|
|
4926
5281
|
rootIndices: [],
|
|
@@ -4934,30 +5289,40 @@ class CompressedTokenProgram {
|
|
|
4934
5289
|
outputCompressedAccounts: packedOutputTokenData,
|
|
4935
5290
|
compressOrDecompressAmount: Array.isArray(params.amount)
|
|
4936
5291
|
? params.amount
|
|
4937
|
-
|
|
4938
|
-
|
|
5292
|
+
.map(amt => new anchor.BN(amt))
|
|
5293
|
+
.reduce((sum, amt) => sum.add(amt), new anchor.BN(0))
|
|
4939
5294
|
: new anchor.BN(params.amount),
|
|
4940
5295
|
isCompress: true,
|
|
4941
5296
|
cpiContext: null,
|
|
4942
5297
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4943
5298
|
};
|
|
4944
|
-
const encodedData = this.program.coder.types.encode(
|
|
5299
|
+
const encodedData = this.program.coder.types.encode(
|
|
5300
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5301
|
+
data,
|
|
5302
|
+
);
|
|
4945
5303
|
const instruction = await this.program.methods
|
|
4946
5304
|
.transfer(encodedData)
|
|
4947
5305
|
.accounts({
|
|
4948
|
-
|
|
4949
|
-
|
|
4950
|
-
|
|
4951
|
-
|
|
4952
|
-
|
|
4953
|
-
|
|
4954
|
-
|
|
4955
|
-
|
|
4956
|
-
|
|
4957
|
-
|
|
4958
|
-
|
|
4959
|
-
|
|
4960
|
-
|
|
5306
|
+
feePayer: payer,
|
|
5307
|
+
authority: owner,
|
|
5308
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5309
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5310
|
+
registeredProgramPda:
|
|
5311
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5312
|
+
.registeredProgramPda,
|
|
5313
|
+
noopProgram:
|
|
5314
|
+
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
5315
|
+
accountCompressionAuthority:
|
|
5316
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5317
|
+
.accountCompressionAuthority,
|
|
5318
|
+
accountCompressionProgram:
|
|
5319
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5320
|
+
.accountCompressionProgram,
|
|
5321
|
+
selfProgram: this.programId,
|
|
5322
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5323
|
+
compressOrDecompressTokenAccount: source, // token
|
|
5324
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5325
|
+
})
|
|
4961
5326
|
.remainingAccounts(remainingAccountMetas)
|
|
4962
5327
|
.instruction();
|
|
4963
5328
|
return instruction;
|
|
@@ -4966,17 +5331,33 @@ class CompressedTokenProgram {
|
|
|
4966
5331
|
* Construct decompress instruction
|
|
4967
5332
|
*/
|
|
4968
5333
|
static async decompress(params) {
|
|
4969
|
-
const {
|
|
5334
|
+
const {
|
|
5335
|
+
payer,
|
|
5336
|
+
inputCompressedTokenAccounts,
|
|
5337
|
+
toAddress,
|
|
5338
|
+
outputStateTree,
|
|
5339
|
+
recentValidityProof,
|
|
5340
|
+
recentInputStateRootIndices,
|
|
5341
|
+
} = params;
|
|
4970
5342
|
const amount = stateless_js.bn(params.amount);
|
|
4971
|
-
const tokenTransferOutputs = createDecompressOutputState(
|
|
5343
|
+
const tokenTransferOutputs = createDecompressOutputState(
|
|
5344
|
+
inputCompressedTokenAccounts,
|
|
5345
|
+
amount,
|
|
5346
|
+
);
|
|
4972
5347
|
/// Pack
|
|
4973
|
-
const {
|
|
5348
|
+
const {
|
|
5349
|
+
inputTokenDataWithContext,
|
|
5350
|
+
packedOutputTokenData,
|
|
5351
|
+
remainingAccountMetas,
|
|
5352
|
+
} = packCompressedTokenAccounts({
|
|
4974
5353
|
inputCompressedTokenAccounts,
|
|
4975
5354
|
outputStateTrees: outputStateTree,
|
|
4976
5355
|
rootIndices: recentInputStateRootIndices,
|
|
4977
5356
|
tokenTransferOutputs: tokenTransferOutputs,
|
|
4978
5357
|
});
|
|
4979
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5358
|
+
const { mint, currentOwner } = parseTokenData(
|
|
5359
|
+
inputCompressedTokenAccounts,
|
|
5360
|
+
);
|
|
4980
5361
|
const data = {
|
|
4981
5362
|
proof: recentValidityProof,
|
|
4982
5363
|
mint,
|
|
@@ -4988,33 +5369,69 @@ class CompressedTokenProgram {
|
|
|
4988
5369
|
cpiContext: null,
|
|
4989
5370
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4990
5371
|
};
|
|
4991
|
-
const encodedData = this.program.coder.types.encode(
|
|
4992
|
-
|
|
5372
|
+
const encodedData = this.program.coder.types.encode(
|
|
5373
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5374
|
+
data,
|
|
5375
|
+
);
|
|
5376
|
+
const {
|
|
5377
|
+
accountCompressionAuthority,
|
|
5378
|
+
noopProgram,
|
|
5379
|
+
registeredProgramPda,
|
|
5380
|
+
accountCompressionProgram,
|
|
5381
|
+
} = stateless_js.defaultStaticAccountsStruct();
|
|
4993
5382
|
const instruction = await this.program.methods
|
|
4994
5383
|
.transfer(encodedData)
|
|
4995
5384
|
.accounts({
|
|
4996
|
-
|
|
4997
|
-
|
|
4998
|
-
|
|
4999
|
-
|
|
5000
|
-
|
|
5001
|
-
|
|
5002
|
-
|
|
5003
|
-
|
|
5004
|
-
|
|
5005
|
-
|
|
5006
|
-
|
|
5007
|
-
|
|
5008
|
-
|
|
5385
|
+
feePayer: payer,
|
|
5386
|
+
authority: currentOwner,
|
|
5387
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5388
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5389
|
+
registeredProgramPda: registeredProgramPda,
|
|
5390
|
+
noopProgram: noopProgram,
|
|
5391
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
5392
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
5393
|
+
selfProgram: this.programId,
|
|
5394
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5395
|
+
compressOrDecompressTokenAccount: toAddress,
|
|
5396
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5397
|
+
})
|
|
5009
5398
|
.remainingAccounts(remainingAccountMetas)
|
|
5010
5399
|
.instruction();
|
|
5011
5400
|
return instruction;
|
|
5012
5401
|
}
|
|
5402
|
+
static async mergeTokenAccounts(params) {
|
|
5403
|
+
const {
|
|
5404
|
+
payer,
|
|
5405
|
+
owner,
|
|
5406
|
+
inputCompressedTokenAccounts,
|
|
5407
|
+
outputStateTree,
|
|
5408
|
+
recentValidityProof,
|
|
5409
|
+
recentInputStateRootIndices,
|
|
5410
|
+
} = params;
|
|
5411
|
+
if (inputCompressedTokenAccounts.length > 3) {
|
|
5412
|
+
throw new Error('Cannot merge more than 3 token accounts at once');
|
|
5413
|
+
}
|
|
5414
|
+
const ix = await this.transfer({
|
|
5415
|
+
payer,
|
|
5416
|
+
inputCompressedTokenAccounts,
|
|
5417
|
+
toAddress: owner,
|
|
5418
|
+
amount: inputCompressedTokenAccounts.reduce(
|
|
5419
|
+
(sum, account) => sum.add(account.parsed.amount),
|
|
5420
|
+
new anchor.BN(0),
|
|
5421
|
+
),
|
|
5422
|
+
outputStateTrees: outputStateTree,
|
|
5423
|
+
recentInputStateRootIndices,
|
|
5424
|
+
recentValidityProof,
|
|
5425
|
+
});
|
|
5426
|
+
return [ix];
|
|
5427
|
+
}
|
|
5013
5428
|
}
|
|
5014
5429
|
/**
|
|
5015
5430
|
* Public key that identifies the CompressedPda program
|
|
5016
5431
|
*/
|
|
5017
|
-
CompressedTokenProgram.programId = new web3_js.PublicKey(
|
|
5432
|
+
CompressedTokenProgram.programId = new web3_js.PublicKey(
|
|
5433
|
+
'cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m',
|
|
5434
|
+
);
|
|
5018
5435
|
CompressedTokenProgram._program = null;
|
|
5019
5436
|
|
|
5020
5437
|
/**
|
|
@@ -5032,8 +5449,22 @@ CompressedTokenProgram._program = null;
|
|
|
5032
5449
|
*
|
|
5033
5450
|
* @return Signature of the confirmed transaction
|
|
5034
5451
|
*/
|
|
5035
|
-
async function approveAndMintTo(
|
|
5036
|
-
|
|
5452
|
+
async function approveAndMintTo(
|
|
5453
|
+
rpc,
|
|
5454
|
+
payer,
|
|
5455
|
+
mint,
|
|
5456
|
+
destination,
|
|
5457
|
+
authority,
|
|
5458
|
+
amount,
|
|
5459
|
+
merkleTree,
|
|
5460
|
+
confirmOptions,
|
|
5461
|
+
) {
|
|
5462
|
+
const authorityTokenAccount = await getOrCreateAssociatedTokenAccount(
|
|
5463
|
+
rpc,
|
|
5464
|
+
payer,
|
|
5465
|
+
mint,
|
|
5466
|
+
authority.publicKey,
|
|
5467
|
+
);
|
|
5037
5468
|
const ixs = await CompressedTokenProgram.approveAndMintTo({
|
|
5038
5469
|
feePayer: payer.publicKey,
|
|
5039
5470
|
mint,
|
|
@@ -5045,10 +5476,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
5045
5476
|
});
|
|
5046
5477
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5047
5478
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5048
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5049
|
-
|
|
5050
|
-
|
|
5051
|
-
|
|
5479
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5480
|
+
[
|
|
5481
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5482
|
+
units: 1000000,
|
|
5483
|
+
}),
|
|
5484
|
+
...ixs,
|
|
5485
|
+
],
|
|
5486
|
+
payer,
|
|
5487
|
+
blockhash,
|
|
5488
|
+
additionalSigners,
|
|
5489
|
+
);
|
|
5052
5490
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5053
5491
|
return txId;
|
|
5054
5492
|
}
|
|
@@ -5071,7 +5509,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
5071
5509
|
*
|
|
5072
5510
|
* @return Signature of the confirmed transaction
|
|
5073
5511
|
*/
|
|
5074
|
-
async function compress(
|
|
5512
|
+
async function compress(
|
|
5513
|
+
rpc,
|
|
5514
|
+
payer,
|
|
5515
|
+
mint,
|
|
5516
|
+
amount,
|
|
5517
|
+
owner,
|
|
5518
|
+
sourceTokenAccount,
|
|
5519
|
+
toAddress,
|
|
5520
|
+
merkleTree,
|
|
5521
|
+
confirmOptions,
|
|
5522
|
+
) {
|
|
5075
5523
|
const compressIx = await CompressedTokenProgram.compress({
|
|
5076
5524
|
payer: payer.publicKey,
|
|
5077
5525
|
owner: owner.publicKey,
|
|
@@ -5083,13 +5531,23 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
|
|
|
5083
5531
|
});
|
|
5084
5532
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5085
5533
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5086
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5087
|
-
|
|
5088
|
-
|
|
5089
|
-
|
|
5090
|
-
|
|
5091
|
-
|
|
5092
|
-
|
|
5534
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5535
|
+
[
|
|
5536
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5537
|
+
units: 1000000,
|
|
5538
|
+
}),
|
|
5539
|
+
compressIx,
|
|
5540
|
+
],
|
|
5541
|
+
payer,
|
|
5542
|
+
blockhashCtx.blockhash,
|
|
5543
|
+
additionalSigners,
|
|
5544
|
+
);
|
|
5545
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5546
|
+
rpc,
|
|
5547
|
+
signedTx,
|
|
5548
|
+
confirmOptions,
|
|
5549
|
+
blockhashCtx,
|
|
5550
|
+
);
|
|
5093
5551
|
return txId;
|
|
5094
5552
|
}
|
|
5095
5553
|
|
|
@@ -5110,15 +5568,33 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
|
|
|
5110
5568
|
*
|
|
5111
5569
|
* @return Signature of the confirmed transaction
|
|
5112
5570
|
*/
|
|
5113
|
-
async function transfer(
|
|
5114
|
-
|
|
5115
|
-
|
|
5571
|
+
async function transfer(
|
|
5572
|
+
rpc,
|
|
5573
|
+
payer,
|
|
5574
|
+
mint,
|
|
5575
|
+
amount,
|
|
5576
|
+
owner,
|
|
5577
|
+
toAddress,
|
|
5578
|
+
/// TODO: allow multiple
|
|
5579
|
+
merkleTree,
|
|
5580
|
+
confirmOptions,
|
|
5581
|
+
) {
|
|
5116
5582
|
amount = stateless_js.bn(amount);
|
|
5117
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5118
|
-
|
|
5119
|
-
|
|
5120
|
-
|
|
5121
|
-
|
|
5583
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5584
|
+
owner.publicKey,
|
|
5585
|
+
{
|
|
5586
|
+
mint,
|
|
5587
|
+
},
|
|
5588
|
+
);
|
|
5589
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5590
|
+
compressedTokenAccounts.items,
|
|
5591
|
+
amount,
|
|
5592
|
+
);
|
|
5593
|
+
const proof = await rpc.getValidityProof(
|
|
5594
|
+
inputAccounts.map(account =>
|
|
5595
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5596
|
+
),
|
|
5597
|
+
);
|
|
5122
5598
|
const ix = await CompressedTokenProgram.transfer({
|
|
5123
5599
|
payer: payer.publicKey,
|
|
5124
5600
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5130,8 +5606,22 @@ merkleTree, confirmOptions) {
|
|
|
5130
5606
|
});
|
|
5131
5607
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5132
5608
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5133
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5134
|
-
|
|
5609
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5610
|
+
[
|
|
5611
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5612
|
+
units: 1000000,
|
|
5613
|
+
}),
|
|
5614
|
+
ix,
|
|
5615
|
+
],
|
|
5616
|
+
payer,
|
|
5617
|
+
blockhash,
|
|
5618
|
+
additionalSigners,
|
|
5619
|
+
);
|
|
5620
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5621
|
+
rpc,
|
|
5622
|
+
signedTx,
|
|
5623
|
+
confirmOptions,
|
|
5624
|
+
);
|
|
5135
5625
|
return txId;
|
|
5136
5626
|
}
|
|
5137
5627
|
/**
|
|
@@ -5147,14 +5637,17 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5147
5637
|
const selectedAccounts = [];
|
|
5148
5638
|
accounts.sort((a, b) => b.parsed.amount.cmp(a.parsed.amount));
|
|
5149
5639
|
for (const account of accounts) {
|
|
5150
|
-
if (accumulatedAmount.gte(stateless_js.bn(transferAmount)))
|
|
5151
|
-
break;
|
|
5640
|
+
if (accumulatedAmount.gte(stateless_js.bn(transferAmount))) break;
|
|
5152
5641
|
accumulatedAmount = accumulatedAmount.add(account.parsed.amount);
|
|
5153
|
-
accumulatedLamports = accumulatedLamports.add(
|
|
5642
|
+
accumulatedLamports = accumulatedLamports.add(
|
|
5643
|
+
account.compressedAccount.lamports,
|
|
5644
|
+
);
|
|
5154
5645
|
selectedAccounts.push(account);
|
|
5155
5646
|
}
|
|
5156
5647
|
if (accumulatedAmount.lt(stateless_js.bn(transferAmount))) {
|
|
5157
|
-
throw new Error(
|
|
5648
|
+
throw new Error(
|
|
5649
|
+
`Not enough balance for transfer. Required: ${transferAmount.toString()}, available: ${accumulatedAmount.toString()}`,
|
|
5650
|
+
);
|
|
5158
5651
|
}
|
|
5159
5652
|
return [
|
|
5160
5653
|
selectedAccounts,
|
|
@@ -5181,16 +5674,34 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5181
5674
|
*
|
|
5182
5675
|
* @return Signature of the confirmed transaction
|
|
5183
5676
|
*/
|
|
5184
|
-
async function decompress(
|
|
5185
|
-
|
|
5186
|
-
|
|
5677
|
+
async function decompress(
|
|
5678
|
+
rpc,
|
|
5679
|
+
payer,
|
|
5680
|
+
mint,
|
|
5681
|
+
amount,
|
|
5682
|
+
owner,
|
|
5683
|
+
toAddress,
|
|
5684
|
+
/// TODO: allow multiple
|
|
5685
|
+
merkleTree,
|
|
5686
|
+
confirmOptions,
|
|
5687
|
+
) {
|
|
5187
5688
|
amount = stateless_js.bn(amount);
|
|
5188
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5189
|
-
|
|
5190
|
-
|
|
5689
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5690
|
+
owner.publicKey,
|
|
5691
|
+
{
|
|
5692
|
+
mint,
|
|
5693
|
+
},
|
|
5694
|
+
);
|
|
5191
5695
|
/// TODO: consider using a different selection algorithm
|
|
5192
|
-
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5193
|
-
|
|
5696
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5697
|
+
compressedTokenAccounts.items,
|
|
5698
|
+
amount,
|
|
5699
|
+
);
|
|
5700
|
+
const proof = await rpc.getValidityProof(
|
|
5701
|
+
inputAccounts.map(account =>
|
|
5702
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5703
|
+
),
|
|
5704
|
+
);
|
|
5194
5705
|
const ix = await CompressedTokenProgram.decompress({
|
|
5195
5706
|
payer: payer.publicKey,
|
|
5196
5707
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5202,8 +5713,22 @@ merkleTree, confirmOptions) {
|
|
|
5202
5713
|
});
|
|
5203
5714
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5204
5715
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5205
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5206
|
-
|
|
5716
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5717
|
+
[
|
|
5718
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5719
|
+
units: 1000000,
|
|
5720
|
+
}),
|
|
5721
|
+
ix,
|
|
5722
|
+
],
|
|
5723
|
+
payer,
|
|
5724
|
+
blockhash,
|
|
5725
|
+
additionalSigners,
|
|
5726
|
+
);
|
|
5727
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5728
|
+
rpc,
|
|
5729
|
+
signedTx,
|
|
5730
|
+
confirmOptions,
|
|
5731
|
+
);
|
|
5207
5732
|
return txId;
|
|
5208
5733
|
}
|
|
5209
5734
|
|
|
@@ -5219,8 +5744,16 @@ merkleTree, confirmOptions) {
|
|
|
5219
5744
|
*
|
|
5220
5745
|
* @return Address of the new mint and the transaction signature
|
|
5221
5746
|
*/
|
|
5222
|
-
async function createMint(
|
|
5223
|
-
|
|
5747
|
+
async function createMint(
|
|
5748
|
+
rpc,
|
|
5749
|
+
payer,
|
|
5750
|
+
mintAuthority,
|
|
5751
|
+
decimals,
|
|
5752
|
+
keypair = web3_js.Keypair.generate(),
|
|
5753
|
+
confirmOptions,
|
|
5754
|
+
) {
|
|
5755
|
+
const rentExemptBalance =
|
|
5756
|
+
await rpc.getMinimumBalanceForRentExemption(MINT_SIZE);
|
|
5224
5757
|
const ixs = await CompressedTokenProgram.createMint({
|
|
5225
5758
|
feePayer: payer.publicKey,
|
|
5226
5759
|
mint: keypair.publicKey,
|
|
@@ -5231,7 +5764,12 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5231
5764
|
});
|
|
5232
5765
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5233
5766
|
const additionalSigners = stateless_js.dedupeSigner(payer, [keypair]);
|
|
5234
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5767
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5768
|
+
ixs,
|
|
5769
|
+
payer,
|
|
5770
|
+
blockhash,
|
|
5771
|
+
additionalSigners,
|
|
5772
|
+
);
|
|
5235
5773
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5236
5774
|
return { mint: keypair.publicKey, transactionSignature: txId };
|
|
5237
5775
|
}
|
|
@@ -5253,7 +5791,16 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5253
5791
|
*
|
|
5254
5792
|
* @return Signature of the confirmed transaction
|
|
5255
5793
|
*/
|
|
5256
|
-
async function mintTo(
|
|
5794
|
+
async function mintTo(
|
|
5795
|
+
rpc,
|
|
5796
|
+
payer,
|
|
5797
|
+
mint,
|
|
5798
|
+
destination,
|
|
5799
|
+
authority,
|
|
5800
|
+
amount,
|
|
5801
|
+
merkleTree,
|
|
5802
|
+
confirmOptions,
|
|
5803
|
+
) {
|
|
5257
5804
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5258
5805
|
const ix = await CompressedTokenProgram.mintTo({
|
|
5259
5806
|
feePayer: payer.publicKey,
|
|
@@ -5264,11 +5811,98 @@ async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTr
|
|
|
5264
5811
|
merkleTree,
|
|
5265
5812
|
});
|
|
5266
5813
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5267
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5814
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5815
|
+
[
|
|
5816
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5817
|
+
units: 1000000,
|
|
5818
|
+
}),
|
|
5819
|
+
ix,
|
|
5820
|
+
],
|
|
5821
|
+
payer,
|
|
5822
|
+
blockhash,
|
|
5823
|
+
additionalSigners,
|
|
5824
|
+
);
|
|
5268
5825
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5269
5826
|
return txId;
|
|
5270
5827
|
}
|
|
5271
5828
|
|
|
5829
|
+
/**
|
|
5830
|
+
* Merge multiple compressed token accounts for a given mint into a single
|
|
5831
|
+
* account
|
|
5832
|
+
*
|
|
5833
|
+
* @param rpc RPC to use
|
|
5834
|
+
* @param payer Payer of the transaction fees
|
|
5835
|
+
* @param mint Public key of the token's mint
|
|
5836
|
+
* @param owner Owner of the token accounts to be merged
|
|
5837
|
+
* @param merkleTree Optional merkle tree for compressed tokens
|
|
5838
|
+
* @param confirmOptions Options for confirming the transaction
|
|
5839
|
+
*
|
|
5840
|
+
* @return Array of transaction signatures
|
|
5841
|
+
*/
|
|
5842
|
+
async function mergeTokenAccounts(
|
|
5843
|
+
rpc,
|
|
5844
|
+
payer,
|
|
5845
|
+
mint,
|
|
5846
|
+
owner,
|
|
5847
|
+
merkleTree,
|
|
5848
|
+
confirmOptions,
|
|
5849
|
+
) {
|
|
5850
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5851
|
+
owner.publicKey,
|
|
5852
|
+
{ mint },
|
|
5853
|
+
);
|
|
5854
|
+
if (compressedTokenAccounts.items.length === 0) {
|
|
5855
|
+
throw new Error(
|
|
5856
|
+
`No compressed token accounts found for mint ${mint.toBase58()}`,
|
|
5857
|
+
);
|
|
5858
|
+
}
|
|
5859
|
+
if (compressedTokenAccounts.items.length >= 6) {
|
|
5860
|
+
throw new Error(
|
|
5861
|
+
`Too many compressed token accounts used for mint ${mint.toBase58()}`,
|
|
5862
|
+
);
|
|
5863
|
+
}
|
|
5864
|
+
const instructions = [
|
|
5865
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
|
|
5866
|
+
];
|
|
5867
|
+
for (
|
|
5868
|
+
let i = 0;
|
|
5869
|
+
i < compressedTokenAccounts.items.slice(0, 6).length;
|
|
5870
|
+
i += 3
|
|
5871
|
+
) {
|
|
5872
|
+
const batch = compressedTokenAccounts.items.slice(i, i + 3);
|
|
5873
|
+
const proof = await rpc.getValidityProof(
|
|
5874
|
+
batch.map(account =>
|
|
5875
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5876
|
+
),
|
|
5877
|
+
);
|
|
5878
|
+
const batchInstructions =
|
|
5879
|
+
await CompressedTokenProgram.mergeTokenAccounts({
|
|
5880
|
+
payer: payer.publicKey,
|
|
5881
|
+
owner: owner.publicKey,
|
|
5882
|
+
mint,
|
|
5883
|
+
inputCompressedTokenAccounts: batch,
|
|
5884
|
+
outputStateTree: merkleTree,
|
|
5885
|
+
recentValidityProof: proof.compressedProof,
|
|
5886
|
+
recentInputStateRootIndices: proof.rootIndices,
|
|
5887
|
+
});
|
|
5888
|
+
instructions.push(...batchInstructions);
|
|
5889
|
+
}
|
|
5890
|
+
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5891
|
+
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5892
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5893
|
+
instructions,
|
|
5894
|
+
payer,
|
|
5895
|
+
blockhash,
|
|
5896
|
+
additionalSigners,
|
|
5897
|
+
);
|
|
5898
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5899
|
+
rpc,
|
|
5900
|
+
signedTx,
|
|
5901
|
+
confirmOptions,
|
|
5902
|
+
);
|
|
5903
|
+
return txId;
|
|
5904
|
+
}
|
|
5905
|
+
|
|
5272
5906
|
/**
|
|
5273
5907
|
* Register an existing mint with the CompressedToken program
|
|
5274
5908
|
*
|
|
@@ -5304,23 +5938,50 @@ async function createTokenPool(rpc, payer, mintAddress, confirmOptions) {
|
|
|
5304
5938
|
*
|
|
5305
5939
|
* @return Transaction signatures and the address of the created lookup table
|
|
5306
5940
|
*/
|
|
5307
|
-
async function createTokenProgramLookupTable(
|
|
5941
|
+
async function createTokenProgramLookupTable(
|
|
5942
|
+
rpc,
|
|
5943
|
+
payer,
|
|
5944
|
+
authority,
|
|
5945
|
+
mints,
|
|
5946
|
+
additionalAccounts,
|
|
5947
|
+
) {
|
|
5308
5948
|
const recentSlot = await rpc.getSlot('finalized');
|
|
5309
|
-
const { instructions, address } =
|
|
5310
|
-
|
|
5311
|
-
|
|
5312
|
-
|
|
5313
|
-
|
|
5314
|
-
|
|
5315
|
-
|
|
5949
|
+
const { instructions, address } =
|
|
5950
|
+
await CompressedTokenProgram.createTokenProgramLookupTable({
|
|
5951
|
+
payer: payer.publicKey,
|
|
5952
|
+
authority: authority.publicKey,
|
|
5953
|
+
mints,
|
|
5954
|
+
remainingAccounts: additionalAccounts,
|
|
5955
|
+
recentSlot,
|
|
5956
|
+
});
|
|
5316
5957
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5317
5958
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5318
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5959
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5960
|
+
[instructions[0]],
|
|
5961
|
+
payer,
|
|
5962
|
+
blockhashCtx.blockhash,
|
|
5963
|
+
additionalSigners,
|
|
5964
|
+
);
|
|
5319
5965
|
/// Must wait for the first instruction to be finalized.
|
|
5320
|
-
const txId = await stateless_js.sendAndConfirmTx(
|
|
5966
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5967
|
+
rpc,
|
|
5968
|
+
signedTx,
|
|
5969
|
+
{ commitment: 'finalized' },
|
|
5970
|
+
blockhashCtx,
|
|
5971
|
+
);
|
|
5321
5972
|
const blockhashCtx2 = await rpc.getLatestBlockhash();
|
|
5322
|
-
const signedTx2 = stateless_js.buildAndSignTx(
|
|
5323
|
-
|
|
5973
|
+
const signedTx2 = stateless_js.buildAndSignTx(
|
|
5974
|
+
[instructions[1]],
|
|
5975
|
+
payer,
|
|
5976
|
+
blockhashCtx2.blockhash,
|
|
5977
|
+
additionalSigners,
|
|
5978
|
+
);
|
|
5979
|
+
const txId2 = await stateless_js.sendAndConfirmTx(
|
|
5980
|
+
rpc,
|
|
5981
|
+
signedTx2,
|
|
5982
|
+
{ commitment: 'finalized' },
|
|
5983
|
+
blockhashCtx2,
|
|
5984
|
+
);
|
|
5324
5985
|
return { txIds: [txId, txId2], address };
|
|
5325
5986
|
}
|
|
5326
5987
|
|
|
@@ -5337,10 +5998,12 @@ exports.createTokenPool = createTokenPool;
|
|
|
5337
5998
|
exports.createTokenProgramLookupTable = createTokenProgramLookupTable;
|
|
5338
5999
|
exports.createTransferOutputState = createTransferOutputState;
|
|
5339
6000
|
exports.decompress = decompress;
|
|
6001
|
+
exports.mergeTokenAccounts = mergeTokenAccounts;
|
|
5340
6002
|
exports.mintTo = mintTo;
|
|
5341
6003
|
exports.packCompressedTokenAccounts = packCompressedTokenAccounts;
|
|
5342
6004
|
exports.parseTokenData = parseTokenData;
|
|
5343
|
-
exports.selectMinCompressedTokenAccountsForTransfer =
|
|
6005
|
+
exports.selectMinCompressedTokenAccountsForTransfer =
|
|
6006
|
+
selectMinCompressedTokenAccountsForTransfer;
|
|
5344
6007
|
exports.sumUpTokenAmount = sumUpTokenAmount;
|
|
5345
6008
|
exports.transfer = transfer;
|
|
5346
6009
|
exports.validateSameTokenOwner = validateSameTokenOwner;
|