@lightprotocol/compressed-token 0.12.0 → 0.13.1
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 +3051 -2074
- package/dist/cjs/browser/index.cjs.map +1 -1
- package/dist/cjs/node/index.cjs +1234 -489
- package/dist/cjs/node/index.cjs.map +1 -1
- package/dist/es/browser/index.js +2978 -2037
- package/dist/es/browser/index.js.map +1 -1
- package/dist/types/index.d.ts +296 -100
- package/package.json +8 -7
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: '1.
|
|
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
|
},
|
|
@@ -1379,7 +1383,8 @@ const IDL = {
|
|
|
1379
1383
|
name: 'outputCompressedAccounts',
|
|
1380
1384
|
type: {
|
|
1381
1385
|
vec: {
|
|
1382
|
-
defined:
|
|
1386
|
+
defined:
|
|
1387
|
+
'OutputCompressedAccountWithPackedContext',
|
|
1383
1388
|
},
|
|
1384
1389
|
},
|
|
1385
1390
|
},
|
|
@@ -1687,20 +1692,37 @@ const IDL = {
|
|
|
1687
1692
|
* Packs Compressed Token Accounts.
|
|
1688
1693
|
*/
|
|
1689
1694
|
function packCompressedTokenAccounts(params) {
|
|
1690
|
-
const {
|
|
1695
|
+
const {
|
|
1696
|
+
inputCompressedTokenAccounts,
|
|
1697
|
+
outputStateTrees,
|
|
1698
|
+
remainingAccounts = [],
|
|
1699
|
+
rootIndices,
|
|
1700
|
+
tokenTransferOutputs,
|
|
1701
|
+
} = params;
|
|
1691
1702
|
const _remainingAccounts = remainingAccounts.slice();
|
|
1692
1703
|
let delegateIndex = null;
|
|
1693
|
-
if (
|
|
1694
|
-
inputCompressedTokenAccounts
|
|
1695
|
-
|
|
1704
|
+
if (
|
|
1705
|
+
inputCompressedTokenAccounts.length > 0 &&
|
|
1706
|
+
inputCompressedTokenAccounts[0].parsed.delegate
|
|
1707
|
+
) {
|
|
1708
|
+
delegateIndex = stateless_js.getIndexOrAdd(
|
|
1709
|
+
_remainingAccounts,
|
|
1710
|
+
inputCompressedTokenAccounts[0].parsed.delegate,
|
|
1711
|
+
);
|
|
1696
1712
|
}
|
|
1697
1713
|
/// TODO: move pubkeyArray to remainingAccounts
|
|
1698
1714
|
/// Currently just packs 'delegate' to pubkeyArray
|
|
1699
1715
|
const packedInputTokenData = [];
|
|
1700
1716
|
/// pack inputs
|
|
1701
1717
|
inputCompressedTokenAccounts.forEach((account, index) => {
|
|
1702
|
-
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1703
|
-
|
|
1718
|
+
const merkleTreePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1719
|
+
_remainingAccounts,
|
|
1720
|
+
account.compressedAccount.merkleTree,
|
|
1721
|
+
);
|
|
1722
|
+
const nullifierQueuePubkeyIndex = stateless_js.getIndexOrAdd(
|
|
1723
|
+
_remainingAccounts,
|
|
1724
|
+
account.compressedAccount.nullifierQueue,
|
|
1725
|
+
);
|
|
1704
1726
|
packedInputTokenData.push({
|
|
1705
1727
|
amount: account.parsed.amount,
|
|
1706
1728
|
delegateIndex,
|
|
@@ -1718,15 +1740,27 @@ function packCompressedTokenAccounts(params) {
|
|
|
1718
1740
|
});
|
|
1719
1741
|
});
|
|
1720
1742
|
/// pack output state trees
|
|
1721
|
-
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
|
|
1743
|
+
const paddedOutputStateMerkleTrees = stateless_js.padOutputStateMerkleTrees(
|
|
1744
|
+
outputStateTrees,
|
|
1745
|
+
tokenTransferOutputs.length,
|
|
1746
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
1747
|
+
);
|
|
1722
1748
|
const packedOutputTokenData = [];
|
|
1723
1749
|
paddedOutputStateMerkleTrees.forEach((account, index) => {
|
|
1724
1750
|
var _a;
|
|
1725
|
-
const merkleTreeIndex = stateless_js.getIndexOrAdd(
|
|
1751
|
+
const merkleTreeIndex = stateless_js.getIndexOrAdd(
|
|
1752
|
+
_remainingAccounts,
|
|
1753
|
+
account,
|
|
1754
|
+
);
|
|
1726
1755
|
packedOutputTokenData.push({
|
|
1727
1756
|
owner: tokenTransferOutputs[index].owner,
|
|
1728
1757
|
amount: tokenTransferOutputs[index].amount,
|
|
1729
|
-
lamports: (
|
|
1758
|
+
lamports: (
|
|
1759
|
+
(_a = tokenTransferOutputs[index].lamports) === null ||
|
|
1760
|
+
_a === void 0
|
|
1761
|
+
? void 0
|
|
1762
|
+
: _a.eq(stateless_js.bn(0))
|
|
1763
|
+
)
|
|
1730
1764
|
? null
|
|
1731
1765
|
: tokenTransferOutputs[index].lamports,
|
|
1732
1766
|
merkleTreeIndex,
|
|
@@ -1734,7 +1768,7 @@ function packCompressedTokenAccounts(params) {
|
|
|
1734
1768
|
});
|
|
1735
1769
|
});
|
|
1736
1770
|
// to meta
|
|
1737
|
-
const remainingAccountMetas = _remainingAccounts.map(
|
|
1771
|
+
const remainingAccountMetas = _remainingAccounts.map(account => ({
|
|
1738
1772
|
pubkey: account,
|
|
1739
1773
|
isWritable: true,
|
|
1740
1774
|
isSigner: false,
|
|
@@ -1751,11 +1785,15 @@ const CPI_AUTHORITY_SEED = Buffer.from('cpi_authority');
|
|
|
1751
1785
|
const SPL_TOKEN_MINT_RENT_EXEMPT_BALANCE = 1461600;
|
|
1752
1786
|
|
|
1753
1787
|
/** Address of the SPL Token program */
|
|
1754
|
-
const TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1788
|
+
const TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1789
|
+
'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA',
|
|
1790
|
+
);
|
|
1755
1791
|
/** Address of the SPL Token 2022 program */
|
|
1756
1792
|
new web3_js.PublicKey('TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb');
|
|
1757
1793
|
/** Address of the SPL Associated Token Account program */
|
|
1758
|
-
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1794
|
+
const ASSOCIATED_TOKEN_PROGRAM_ID = new web3_js.PublicKey(
|
|
1795
|
+
'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL',
|
|
1796
|
+
);
|
|
1759
1797
|
/** Address of the special mint for wrapped native SOL in spl-token */
|
|
1760
1798
|
new web3_js.PublicKey('So11111111111111111111111111111111111111112');
|
|
1761
1799
|
/** Address of the special mint for wrapped native SOL in spl-token-2022 */
|
|
@@ -1785,9 +1823,88 @@ var Layout$1 = {};
|
|
|
1785
1823
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
1786
1824
|
* THE SOFTWARE.
|
|
1787
1825
|
*/
|
|
1788
|
-
Object.defineProperty(Layout$1,
|
|
1789
|
-
Layout$1.s16 =
|
|
1790
|
-
Layout$1.
|
|
1826
|
+
Object.defineProperty(Layout$1, '__esModule', { value: true });
|
|
1827
|
+
Layout$1.s16 =
|
|
1828
|
+
Layout$1.s8 =
|
|
1829
|
+
Layout$1.nu64be =
|
|
1830
|
+
Layout$1.u48be =
|
|
1831
|
+
Layout$1.u40be =
|
|
1832
|
+
Layout$1.u32be =
|
|
1833
|
+
Layout$1.u24be =
|
|
1834
|
+
Layout$1.u16be =
|
|
1835
|
+
Layout$1.nu64 =
|
|
1836
|
+
Layout$1.u48 =
|
|
1837
|
+
Layout$1.u40 =
|
|
1838
|
+
u32 =
|
|
1839
|
+
Layout$1.u32 =
|
|
1840
|
+
Layout$1.u24 =
|
|
1841
|
+
Layout$1.u16 =
|
|
1842
|
+
u8 =
|
|
1843
|
+
Layout$1.u8 =
|
|
1844
|
+
Layout$1.offset =
|
|
1845
|
+
Layout$1.greedy =
|
|
1846
|
+
Layout$1.Constant =
|
|
1847
|
+
Layout$1.UTF8 =
|
|
1848
|
+
Layout$1.CString =
|
|
1849
|
+
Layout$1.Blob =
|
|
1850
|
+
Layout$1.Boolean =
|
|
1851
|
+
Layout$1.BitField =
|
|
1852
|
+
Layout$1.BitStructure =
|
|
1853
|
+
Layout$1.VariantLayout =
|
|
1854
|
+
Layout$1.Union =
|
|
1855
|
+
Layout$1.UnionLayoutDiscriminator =
|
|
1856
|
+
Layout$1.UnionDiscriminator =
|
|
1857
|
+
Layout$1.Structure =
|
|
1858
|
+
Layout$1.Sequence =
|
|
1859
|
+
Layout$1.DoubleBE =
|
|
1860
|
+
Layout$1.Double =
|
|
1861
|
+
Layout$1.FloatBE =
|
|
1862
|
+
Layout$1.Float =
|
|
1863
|
+
Layout$1.NearInt64BE =
|
|
1864
|
+
Layout$1.NearInt64 =
|
|
1865
|
+
Layout$1.NearUInt64BE =
|
|
1866
|
+
Layout$1.NearUInt64 =
|
|
1867
|
+
Layout$1.IntBE =
|
|
1868
|
+
Layout$1.Int =
|
|
1869
|
+
Layout$1.UIntBE =
|
|
1870
|
+
Layout$1.UInt =
|
|
1871
|
+
Layout$1.OffsetLayout =
|
|
1872
|
+
Layout$1.GreedyCount =
|
|
1873
|
+
Layout$1.ExternalLayout =
|
|
1874
|
+
Layout$1.bindConstructorLayout =
|
|
1875
|
+
Layout$1.nameWithProperty =
|
|
1876
|
+
Layout_2 =
|
|
1877
|
+
Layout$1.Layout =
|
|
1878
|
+
Layout$1.uint8ArrayToBuffer =
|
|
1879
|
+
Layout$1.checkUint8Array =
|
|
1880
|
+
void 0;
|
|
1881
|
+
Layout$1.constant =
|
|
1882
|
+
Layout$1.utf8 =
|
|
1883
|
+
Layout$1.cstr =
|
|
1884
|
+
blob =
|
|
1885
|
+
Layout$1.blob =
|
|
1886
|
+
Layout$1.unionLayoutDiscriminator =
|
|
1887
|
+
Layout$1.union =
|
|
1888
|
+
Layout$1.seq =
|
|
1889
|
+
Layout$1.bits =
|
|
1890
|
+
struct =
|
|
1891
|
+
Layout$1.struct =
|
|
1892
|
+
Layout$1.f64be =
|
|
1893
|
+
Layout$1.f64 =
|
|
1894
|
+
Layout$1.f32be =
|
|
1895
|
+
Layout$1.f32 =
|
|
1896
|
+
Layout$1.ns64be =
|
|
1897
|
+
Layout$1.s48be =
|
|
1898
|
+
Layout$1.s40be =
|
|
1899
|
+
Layout$1.s32be =
|
|
1900
|
+
Layout$1.s24be =
|
|
1901
|
+
Layout$1.s16be =
|
|
1902
|
+
Layout$1.ns64 =
|
|
1903
|
+
Layout$1.s48 =
|
|
1904
|
+
Layout$1.s40 =
|
|
1905
|
+
Layout$1.s32 =
|
|
1906
|
+
Layout$1.s24 =
|
|
1907
|
+
void 0;
|
|
1791
1908
|
const buffer_1 = require$$0;
|
|
1792
1909
|
/* Check if a value is a Uint8Array.
|
|
1793
1910
|
*
|
|
@@ -1933,7 +2050,7 @@ class Layout {
|
|
|
1933
2050
|
return undefined;
|
|
1934
2051
|
}
|
|
1935
2052
|
}
|
|
1936
|
-
var Layout_2 = Layout$1.Layout = Layout;
|
|
2053
|
+
var Layout_2 = (Layout$1.Layout = Layout);
|
|
1937
2054
|
/* Provide text that carries a name (such as for a function that will
|
|
1938
2055
|
* be throwing an error) annotated with the property of a given layout
|
|
1939
2056
|
* (such as one for which the value was unacceptable).
|
|
@@ -1982,7 +2099,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
1982
2099
|
if (Object.prototype.hasOwnProperty.call(Class, 'layout_')) {
|
|
1983
2100
|
throw new Error('Class is already bound to a layout');
|
|
1984
2101
|
}
|
|
1985
|
-
if (!(layout &&
|
|
2102
|
+
if (!(layout && layout instanceof Layout)) {
|
|
1986
2103
|
throw new TypeError('layout must be a Layout');
|
|
1987
2104
|
}
|
|
1988
2105
|
if (Object.prototype.hasOwnProperty.call(layout, 'boundConstructor_')) {
|
|
@@ -1990,7 +2107,7 @@ function bindConstructorLayout(Class, layout) {
|
|
|
1990
2107
|
}
|
|
1991
2108
|
Class.layout_ = layout;
|
|
1992
2109
|
layout.boundConstructor_ = Class;
|
|
1993
|
-
layout.makeDestinationObject = (
|
|
2110
|
+
layout.makeDestinationObject = () => new Class();
|
|
1994
2111
|
Object.defineProperty(Class.prototype, 'encode', {
|
|
1995
2112
|
value(b, offset) {
|
|
1996
2113
|
return layout.encode(this, b, offset);
|
|
@@ -2060,7 +2177,7 @@ Layout$1.ExternalLayout = ExternalLayout;
|
|
|
2060
2177
|
*/
|
|
2061
2178
|
class GreedyCount extends ExternalLayout {
|
|
2062
2179
|
constructor(elementSpan = 1, property) {
|
|
2063
|
-
if (
|
|
2180
|
+
if (!Number.isInteger(elementSpan) || 0 >= elementSpan) {
|
|
2064
2181
|
throw new TypeError('elementSpan must be a (positive) integer');
|
|
2065
2182
|
}
|
|
2066
2183
|
super(-1, property);
|
|
@@ -2126,8 +2243,7 @@ class OffsetLayout extends ExternalLayout {
|
|
|
2126
2243
|
}
|
|
2127
2244
|
/** @override */
|
|
2128
2245
|
isCount() {
|
|
2129
|
-
return
|
|
2130
|
-
|| (this.layout instanceof UIntBE));
|
|
2246
|
+
return this.layout instanceof UInt || this.layout instanceof UIntBE;
|
|
2131
2247
|
}
|
|
2132
2248
|
/** @override */
|
|
2133
2249
|
decode(b, offset = 0) {
|
|
@@ -2280,7 +2396,7 @@ const V2E32 = Math.pow(2, 32);
|
|
|
2280
2396
|
* non-negative. */
|
|
2281
2397
|
function divmodInt64(src) {
|
|
2282
2398
|
const hi32 = Math.floor(src / V2E32);
|
|
2283
|
-
const lo32 = src -
|
|
2399
|
+
const lo32 = src - hi32 * V2E32;
|
|
2284
2400
|
return { hi32, lo32 };
|
|
2285
2401
|
}
|
|
2286
2402
|
/* Reconstruct Number from quotient and non-negative remainder */
|
|
@@ -2537,14 +2653,19 @@ class Sequence extends Layout {
|
|
|
2537
2653
|
if (!(elementLayout instanceof Layout)) {
|
|
2538
2654
|
throw new TypeError('elementLayout must be a Layout');
|
|
2539
2655
|
}
|
|
2540
|
-
if (
|
|
2541
|
-
|
|
2542
|
-
|
|
2543
|
-
|
|
2656
|
+
if (
|
|
2657
|
+
!(
|
|
2658
|
+
(count instanceof ExternalLayout && count.isCount()) ||
|
|
2659
|
+
(Number.isInteger(count) && 0 <= count)
|
|
2660
|
+
)
|
|
2661
|
+
) {
|
|
2662
|
+
throw new TypeError(
|
|
2663
|
+
'count must be non-negative integer ' +
|
|
2664
|
+
'or an unsigned integer ExternalLayout',
|
|
2665
|
+
);
|
|
2544
2666
|
}
|
|
2545
2667
|
let span = -1;
|
|
2546
|
-
if (
|
|
2547
|
-
&& (0 < elementLayout.span)) {
|
|
2668
|
+
if (!(count instanceof ExternalLayout) && 0 < elementLayout.span) {
|
|
2548
2669
|
span = count * elementLayout.span;
|
|
2549
2670
|
}
|
|
2550
2671
|
super(span, property);
|
|
@@ -2569,8 +2690,7 @@ class Sequence extends Layout {
|
|
|
2569
2690
|
}
|
|
2570
2691
|
if (0 < this.elementLayout.span) {
|
|
2571
2692
|
span = count * this.elementLayout.span;
|
|
2572
|
-
}
|
|
2573
|
-
else {
|
|
2693
|
+
} else {
|
|
2574
2694
|
let idx = 0;
|
|
2575
2695
|
while (idx < count) {
|
|
2576
2696
|
span += this.elementLayout.getSpan(b, offset + span);
|
|
@@ -2650,27 +2770,30 @@ Layout$1.Sequence = Sequence;
|
|
|
2650
2770
|
*/
|
|
2651
2771
|
class Structure extends Layout {
|
|
2652
2772
|
constructor(fields, property, decodePrefixes) {
|
|
2653
|
-
if (
|
|
2654
|
-
|
|
2773
|
+
if (
|
|
2774
|
+
!(
|
|
2775
|
+
Array.isArray(fields) &&
|
|
2776
|
+
fields.reduce((acc, v) => acc && v instanceof Layout, true)
|
|
2777
|
+
)
|
|
2778
|
+
) {
|
|
2655
2779
|
throw new TypeError('fields must be array of Layout instances');
|
|
2656
2780
|
}
|
|
2657
|
-
if (
|
|
2658
|
-
&& (undefined === decodePrefixes)) {
|
|
2781
|
+
if ('boolean' === typeof property && undefined === decodePrefixes) {
|
|
2659
2782
|
decodePrefixes = property;
|
|
2660
2783
|
property = undefined;
|
|
2661
2784
|
}
|
|
2662
2785
|
/* Verify absence of unnamed variable-length fields. */
|
|
2663
2786
|
for (const fd of fields) {
|
|
2664
|
-
if (
|
|
2665
|
-
|
|
2666
|
-
|
|
2787
|
+
if (0 > fd.span && undefined === fd.property) {
|
|
2788
|
+
throw new Error(
|
|
2789
|
+
'fields cannot contain unnamed variable-length layout',
|
|
2790
|
+
);
|
|
2667
2791
|
}
|
|
2668
2792
|
}
|
|
2669
2793
|
let span = -1;
|
|
2670
2794
|
try {
|
|
2671
2795
|
span = fields.reduce((span, fd) => span + fd.getSpan(), 0);
|
|
2672
|
-
}
|
|
2673
|
-
catch (e) {
|
|
2796
|
+
} catch (e) {
|
|
2674
2797
|
// ignore error
|
|
2675
2798
|
}
|
|
2676
2799
|
super(span, property);
|
|
@@ -2708,8 +2831,7 @@ class Structure extends Layout {
|
|
|
2708
2831
|
offset += fsp;
|
|
2709
2832
|
return span + fsp;
|
|
2710
2833
|
}, 0);
|
|
2711
|
-
}
|
|
2712
|
-
catch (e) {
|
|
2834
|
+
} catch (e) {
|
|
2713
2835
|
throw new RangeError('indeterminate span');
|
|
2714
2836
|
}
|
|
2715
2837
|
return span;
|
|
@@ -2723,8 +2845,7 @@ class Structure extends Layout {
|
|
|
2723
2845
|
dest[fd.property] = fd.decode(b, offset);
|
|
2724
2846
|
}
|
|
2725
2847
|
offset += fd.getSpan(b, offset);
|
|
2726
|
-
if (this.decodePrefixes
|
|
2727
|
-
&& (b.length === offset)) {
|
|
2848
|
+
if (this.decodePrefixes && b.length === offset) {
|
|
2728
2849
|
break;
|
|
2729
2850
|
}
|
|
2730
2851
|
}
|
|
@@ -2741,7 +2862,7 @@ class Structure extends Layout {
|
|
|
2741
2862
|
let lastWrote = 0;
|
|
2742
2863
|
for (const fd of this.fields) {
|
|
2743
2864
|
let span = fd.span;
|
|
2744
|
-
lastWrote =
|
|
2865
|
+
lastWrote = 0 < span ? span : 0;
|
|
2745
2866
|
if (undefined !== fd.property) {
|
|
2746
2867
|
const fv = src[fd.property];
|
|
2747
2868
|
if (undefined !== fv) {
|
|
@@ -2760,14 +2881,13 @@ class Structure extends Layout {
|
|
|
2760
2881
|
* item may have had a dynamic length and we don't want to include
|
|
2761
2882
|
* the padding between it and the end of the space reserved for
|
|
2762
2883
|
* it. */
|
|
2763
|
-
return
|
|
2884
|
+
return lastOffset + lastWrote - firstOffset;
|
|
2764
2885
|
}
|
|
2765
2886
|
/** @override */
|
|
2766
2887
|
fromArray(values) {
|
|
2767
2888
|
const dest = this.makeDestinationObject();
|
|
2768
2889
|
for (const fd of this.fields) {
|
|
2769
|
-
if (
|
|
2770
|
-
&& (0 < values.length)) {
|
|
2890
|
+
if (undefined !== fd.property && 0 < values.length) {
|
|
2771
2891
|
dest[fd.property] = values.shift();
|
|
2772
2892
|
}
|
|
2773
2893
|
}
|
|
@@ -2814,8 +2934,7 @@ class Structure extends Layout {
|
|
|
2814
2934
|
}
|
|
2815
2935
|
if (0 > fd.span) {
|
|
2816
2936
|
offset = -1;
|
|
2817
|
-
}
|
|
2818
|
-
else if (0 <= offset) {
|
|
2937
|
+
} else if (0 <= offset) {
|
|
2819
2938
|
offset += fd.span;
|
|
2820
2939
|
}
|
|
2821
2940
|
}
|
|
@@ -2880,9 +2999,10 @@ Layout$1.UnionDiscriminator = UnionDiscriminator;
|
|
|
2880
2999
|
*/
|
|
2881
3000
|
class UnionLayoutDiscriminator extends UnionDiscriminator {
|
|
2882
3001
|
constructor(layout, property) {
|
|
2883
|
-
if (!(
|
|
2884
|
-
|
|
2885
|
-
|
|
3002
|
+
if (!(layout instanceof ExternalLayout && layout.isCount())) {
|
|
3003
|
+
throw new TypeError(
|
|
3004
|
+
'layout must be an unsigned integer ExternalLayout',
|
|
3005
|
+
);
|
|
2886
3006
|
}
|
|
2887
3007
|
super(property || layout.property || 'variant');
|
|
2888
3008
|
/** The {@link ExternalLayout} used to access the discriminator
|
|
@@ -2961,26 +3081,24 @@ Layout$1.UnionLayoutDiscriminator = UnionLayoutDiscriminator;
|
|
|
2961
3081
|
class Union extends Layout {
|
|
2962
3082
|
constructor(discr, defaultLayout, property) {
|
|
2963
3083
|
let discriminator;
|
|
2964
|
-
if (
|
|
2965
|
-
|
|
2966
|
-
|
|
2967
|
-
|
|
2968
|
-
else if (
|
|
2969
|
-
&& discr.isCount()) {
|
|
3084
|
+
if (discr instanceof UInt || discr instanceof UIntBE) {
|
|
3085
|
+
discriminator = new UnionLayoutDiscriminator(
|
|
3086
|
+
new OffsetLayout(discr),
|
|
3087
|
+
);
|
|
3088
|
+
} else if (discr instanceof ExternalLayout && discr.isCount()) {
|
|
2970
3089
|
discriminator = new UnionLayoutDiscriminator(discr);
|
|
2971
|
-
}
|
|
2972
|
-
|
|
2973
|
-
|
|
2974
|
-
|
|
2975
|
-
|
|
2976
|
-
else {
|
|
3090
|
+
} else if (!(discr instanceof UnionDiscriminator)) {
|
|
3091
|
+
throw new TypeError(
|
|
3092
|
+
'discr must be a UnionDiscriminator ' +
|
|
3093
|
+
'or an unsigned integer layout',
|
|
3094
|
+
);
|
|
3095
|
+
} else {
|
|
2977
3096
|
discriminator = discr;
|
|
2978
3097
|
}
|
|
2979
3098
|
if (undefined === defaultLayout) {
|
|
2980
3099
|
defaultLayout = null;
|
|
2981
3100
|
}
|
|
2982
|
-
if (!(
|
|
2983
|
-
|| (defaultLayout instanceof Layout))) {
|
|
3101
|
+
if (!(null === defaultLayout || defaultLayout instanceof Layout)) {
|
|
2984
3102
|
throw new TypeError('defaultLayout must be null or a Layout');
|
|
2985
3103
|
}
|
|
2986
3104
|
if (null !== defaultLayout) {
|
|
@@ -2998,8 +3116,10 @@ class Union extends Layout {
|
|
|
2998
3116
|
let span = -1;
|
|
2999
3117
|
if (defaultLayout) {
|
|
3000
3118
|
span = defaultLayout.span;
|
|
3001
|
-
if (
|
|
3002
|
-
|
|
3119
|
+
if (
|
|
3120
|
+
0 <= span &&
|
|
3121
|
+
(discr instanceof UInt || discr instanceof UIntBE)
|
|
3122
|
+
) {
|
|
3003
3123
|
span += discriminator.layout.span;
|
|
3004
3124
|
}
|
|
3005
3125
|
}
|
|
@@ -3018,8 +3138,8 @@ class Union extends Layout {
|
|
|
3018
3138
|
*
|
|
3019
3139
|
* If `false` the discriminator is obtained from somewhere
|
|
3020
3140
|
* else. */
|
|
3021
|
-
this.usesPrefixDiscriminator =
|
|
3022
|
-
||
|
|
3141
|
+
this.usesPrefixDiscriminator =
|
|
3142
|
+
discr instanceof UInt || discr instanceof UIntBE;
|
|
3023
3143
|
/** The layout for non-discriminator content when the value of the
|
|
3024
3144
|
* discriminator is not recognized.
|
|
3025
3145
|
*
|
|
@@ -3084,7 +3204,9 @@ class Union extends Layout {
|
|
|
3084
3204
|
* determine the span. */
|
|
3085
3205
|
const vlo = this.getVariant(b, offset);
|
|
3086
3206
|
if (!vlo) {
|
|
3087
|
-
throw new Error(
|
|
3207
|
+
throw new Error(
|
|
3208
|
+
'unable to determine span for unrecognized variant',
|
|
3209
|
+
);
|
|
3088
3210
|
}
|
|
3089
3211
|
return vlo.getSpan(b, offset);
|
|
3090
3212
|
}
|
|
@@ -3118,22 +3240,41 @@ class Union extends Layout {
|
|
|
3118
3240
|
* registered variant.
|
|
3119
3241
|
*/
|
|
3120
3242
|
defaultGetSourceVariant(src) {
|
|
3121
|
-
if (
|
|
3122
|
-
|
|
3123
|
-
|
|
3243
|
+
if (
|
|
3244
|
+
Object.prototype.hasOwnProperty.call(
|
|
3245
|
+
src,
|
|
3246
|
+
this.discriminator.property,
|
|
3247
|
+
)
|
|
3248
|
+
) {
|
|
3249
|
+
if (
|
|
3250
|
+
this.defaultLayout &&
|
|
3251
|
+
this.defaultLayout.property &&
|
|
3252
|
+
Object.prototype.hasOwnProperty.call(
|
|
3253
|
+
src,
|
|
3254
|
+
this.defaultLayout.property,
|
|
3255
|
+
)
|
|
3256
|
+
) {
|
|
3124
3257
|
return undefined;
|
|
3125
3258
|
}
|
|
3126
3259
|
const vlo = this.registry[src[this.discriminator.property]];
|
|
3127
|
-
if (
|
|
3128
|
-
&&
|
|
3129
|
-
|
|
3260
|
+
if (
|
|
3261
|
+
vlo &&
|
|
3262
|
+
(!vlo.layout ||
|
|
3263
|
+
(vlo.property &&
|
|
3264
|
+
Object.prototype.hasOwnProperty.call(
|
|
3265
|
+
src,
|
|
3266
|
+
vlo.property,
|
|
3267
|
+
)))
|
|
3268
|
+
) {
|
|
3130
3269
|
return vlo;
|
|
3131
3270
|
}
|
|
3132
|
-
}
|
|
3133
|
-
else {
|
|
3271
|
+
} else {
|
|
3134
3272
|
for (const tag in this.registry) {
|
|
3135
3273
|
const vlo = this.registry[tag];
|
|
3136
|
-
if (
|
|
3274
|
+
if (
|
|
3275
|
+
vlo.property &&
|
|
3276
|
+
Object.prototype.hasOwnProperty.call(src, vlo.property)
|
|
3277
|
+
) {
|
|
3137
3278
|
return vlo;
|
|
3138
3279
|
}
|
|
3139
3280
|
}
|
|
@@ -3161,9 +3302,11 @@ class Union extends Layout {
|
|
|
3161
3302
|
dest[dlo.property] = discr;
|
|
3162
3303
|
// defaultLayout.property can be undefined, but this is allowed by buffer-layout
|
|
3163
3304
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3164
|
-
dest[defaultLayout.property] = defaultLayout.decode(
|
|
3165
|
-
|
|
3166
|
-
|
|
3305
|
+
dest[defaultLayout.property] = defaultLayout.decode(
|
|
3306
|
+
b,
|
|
3307
|
+
offset + contentOffset,
|
|
3308
|
+
);
|
|
3309
|
+
} else {
|
|
3167
3310
|
dest = clo.decode(b, offset);
|
|
3168
3311
|
}
|
|
3169
3312
|
return dest;
|
|
@@ -3188,7 +3331,10 @@ class Union extends Layout {
|
|
|
3188
3331
|
dlo.encode(src[dlo.property], b, offset);
|
|
3189
3332
|
// clo.property is not undefined when vlo is undefined
|
|
3190
3333
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
3191
|
-
return
|
|
3334
|
+
return (
|
|
3335
|
+
contentOffset +
|
|
3336
|
+
clo.encode(src[clo.property], b, offset + contentOffset)
|
|
3337
|
+
);
|
|
3192
3338
|
}
|
|
3193
3339
|
return vlo.encode(src, b, offset);
|
|
3194
3340
|
}
|
|
@@ -3228,8 +3374,7 @@ class Union extends Layout {
|
|
|
3228
3374
|
let variant;
|
|
3229
3375
|
if (vb instanceof Uint8Array) {
|
|
3230
3376
|
variant = this.discriminator.decode(vb, offset);
|
|
3231
|
-
}
|
|
3232
|
-
else {
|
|
3377
|
+
} else {
|
|
3233
3378
|
variant = vb;
|
|
3234
3379
|
}
|
|
3235
3380
|
return this.registry[variant];
|
|
@@ -3270,11 +3415,10 @@ class VariantLayout extends Layout {
|
|
|
3270
3415
|
if (!(union instanceof Union)) {
|
|
3271
3416
|
throw new TypeError('union must be a Union');
|
|
3272
3417
|
}
|
|
3273
|
-
if (
|
|
3418
|
+
if (!Number.isInteger(variant) || 0 > variant) {
|
|
3274
3419
|
throw new TypeError('variant must be a (non-negative) integer');
|
|
3275
3420
|
}
|
|
3276
|
-
if (
|
|
3277
|
-
&& (undefined === property)) {
|
|
3421
|
+
if ('string' === typeof layout && undefined === property) {
|
|
3278
3422
|
property = layout;
|
|
3279
3423
|
layout = null;
|
|
3280
3424
|
}
|
|
@@ -3282,10 +3426,14 @@ class VariantLayout extends Layout {
|
|
|
3282
3426
|
if (!(layout instanceof Layout)) {
|
|
3283
3427
|
throw new TypeError('layout must be a Layout');
|
|
3284
3428
|
}
|
|
3285
|
-
if (
|
|
3286
|
-
|
|
3287
|
-
|
|
3288
|
-
|
|
3429
|
+
if (
|
|
3430
|
+
null !== union.defaultLayout &&
|
|
3431
|
+
0 <= layout.span &&
|
|
3432
|
+
layout.span > union.defaultLayout.span
|
|
3433
|
+
) {
|
|
3434
|
+
throw new Error(
|
|
3435
|
+
'variant span exceeds span of containing union',
|
|
3436
|
+
);
|
|
3289
3437
|
}
|
|
3290
3438
|
if ('string' !== typeof property) {
|
|
3291
3439
|
throw new TypeError('variant must have a String property');
|
|
@@ -3294,7 +3442,7 @@ class VariantLayout extends Layout {
|
|
|
3294
3442
|
let span = union.span;
|
|
3295
3443
|
if (0 > union.span) {
|
|
3296
3444
|
span = layout ? layout.span : 0;
|
|
3297
|
-
if (
|
|
3445
|
+
if (0 <= span && union.usesPrefixDiscriminator) {
|
|
3298
3446
|
span += union.discriminator.layout.span;
|
|
3299
3447
|
}
|
|
3300
3448
|
}
|
|
@@ -3341,11 +3489,9 @@ class VariantLayout extends Layout {
|
|
|
3341
3489
|
}
|
|
3342
3490
|
if (this.layout) {
|
|
3343
3491
|
dest[this.property] = this.layout.decode(b, offset + contentOffset);
|
|
3344
|
-
}
|
|
3345
|
-
else if (this.property) {
|
|
3492
|
+
} else if (this.property) {
|
|
3346
3493
|
dest[this.property] = true;
|
|
3347
|
-
}
|
|
3348
|
-
else if (this.union.usesPrefixDiscriminator) {
|
|
3494
|
+
} else if (this.union.usesPrefixDiscriminator) {
|
|
3349
3495
|
dest[this.union.discriminator.property] = this.variant;
|
|
3350
3496
|
}
|
|
3351
3497
|
return dest;
|
|
@@ -3356,8 +3502,10 @@ class VariantLayout extends Layout {
|
|
|
3356
3502
|
if (this.union.usesPrefixDiscriminator) {
|
|
3357
3503
|
contentOffset = this.union.discriminator.layout.span;
|
|
3358
3504
|
}
|
|
3359
|
-
if (
|
|
3360
|
-
|
|
3505
|
+
if (
|
|
3506
|
+
this.layout &&
|
|
3507
|
+
!Object.prototype.hasOwnProperty.call(src, this.property)
|
|
3508
|
+
) {
|
|
3361
3509
|
throw new TypeError('variant lacks property ' + this.property);
|
|
3362
3510
|
}
|
|
3363
3511
|
this.union.discriminator.encode(this.variant, b, offset);
|
|
@@ -3365,8 +3513,7 @@ class VariantLayout extends Layout {
|
|
|
3365
3513
|
if (this.layout) {
|
|
3366
3514
|
this.layout.encode(src[this.property], b, offset + contentOffset);
|
|
3367
3515
|
span += this.layout.getSpan(b, offset + contentOffset);
|
|
3368
|
-
if (
|
|
3369
|
-
&& (span > this.union.span)) {
|
|
3516
|
+
if (0 <= this.union.span && span > this.union.span) {
|
|
3370
3517
|
throw new Error('encoded variant overruns containing union');
|
|
3371
3518
|
}
|
|
3372
3519
|
}
|
|
@@ -3427,12 +3574,10 @@ function fixBitwiseResult(v) {
|
|
|
3427
3574
|
*/
|
|
3428
3575
|
class BitStructure extends Layout {
|
|
3429
3576
|
constructor(word, msb, property) {
|
|
3430
|
-
if (!(
|
|
3431
|
-
|| (word instanceof UIntBE))) {
|
|
3577
|
+
if (!(word instanceof UInt || word instanceof UIntBE)) {
|
|
3432
3578
|
throw new TypeError('word must be a UInt or UIntBE layout');
|
|
3433
3579
|
}
|
|
3434
|
-
if (
|
|
3435
|
-
&& (undefined === property)) {
|
|
3580
|
+
if ('string' === typeof msb && undefined === property) {
|
|
3436
3581
|
property = msb;
|
|
3437
3582
|
msb = false;
|
|
3438
3583
|
}
|
|
@@ -3576,15 +3721,19 @@ class BitField {
|
|
|
3576
3721
|
if (!(container instanceof BitStructure)) {
|
|
3577
3722
|
throw new TypeError('container must be a BitStructure');
|
|
3578
3723
|
}
|
|
3579
|
-
if (
|
|
3724
|
+
if (!Number.isInteger(bits) || 0 >= bits) {
|
|
3580
3725
|
throw new TypeError('bits must be positive integer');
|
|
3581
3726
|
}
|
|
3582
3727
|
const totalBits = 8 * container.span;
|
|
3583
3728
|
const usedBits = container.fields.reduce((sum, fd) => sum + fd.bits, 0);
|
|
3584
|
-
if (
|
|
3585
|
-
throw new Error(
|
|
3586
|
-
|
|
3587
|
-
|
|
3729
|
+
if (bits + usedBits > totalBits) {
|
|
3730
|
+
throw new Error(
|
|
3731
|
+
'bits too long for span remainder (' +
|
|
3732
|
+
(totalBits - usedBits) +
|
|
3733
|
+
' of ' +
|
|
3734
|
+
totalBits +
|
|
3735
|
+
' remain)',
|
|
3736
|
+
);
|
|
3588
3737
|
}
|
|
3589
3738
|
/** The {@link BitStructure} instance to which this bit field
|
|
3590
3739
|
* belongs. */
|
|
@@ -3597,8 +3746,9 @@ class BitField {
|
|
|
3597
3746
|
* That is, it masks a value that has not yet been shifted into
|
|
3598
3747
|
* position within its containing packed integer. */
|
|
3599
3748
|
this.valueMask = (1 << bits) - 1;
|
|
3600
|
-
if (32 === bits) {
|
|
3601
|
-
|
|
3749
|
+
if (32 === bits) {
|
|
3750
|
+
// shifted value out of range
|
|
3751
|
+
this.valueMask = 0xffffffff;
|
|
3602
3752
|
}
|
|
3603
3753
|
/** The offset of the value within the containing packed unsigned
|
|
3604
3754
|
* integer. The least significant bit of the packed value is at
|
|
@@ -3636,16 +3786,22 @@ class BitField {
|
|
|
3636
3786
|
* **NOTE** This is not a specialization of {@link
|
|
3637
3787
|
* Layout#encode|Layout.encode} and there is no return value. */
|
|
3638
3788
|
encode(value) {
|
|
3639
|
-
if (
|
|
3640
|
-
|
|
3641
|
-
|
|
3642
|
-
|
|
3643
|
-
|
|
3789
|
+
if (
|
|
3790
|
+
'number' !== typeof value ||
|
|
3791
|
+
!Number.isInteger(value) ||
|
|
3792
|
+
value !== fixBitwiseResult(value & this.valueMask)
|
|
3793
|
+
) {
|
|
3794
|
+
throw new TypeError(
|
|
3795
|
+
nameWithProperty('BitField.encode', this) +
|
|
3796
|
+
' value must be integer not exceeding ' +
|
|
3797
|
+
this.valueMask,
|
|
3798
|
+
);
|
|
3644
3799
|
}
|
|
3645
3800
|
const word = this.container._packedGetValue();
|
|
3646
3801
|
const wordValue = fixBitwiseResult(value << this.start);
|
|
3647
|
-
this.container._packedSetValue(
|
|
3648
|
-
| wordValue
|
|
3802
|
+
this.container._packedSetValue(
|
|
3803
|
+
fixBitwiseResult(word & ~this.wordMask) | wordValue,
|
|
3804
|
+
);
|
|
3649
3805
|
}
|
|
3650
3806
|
}
|
|
3651
3807
|
Layout$1.BitField = BitField;
|
|
@@ -3703,10 +3859,16 @@ Layout$1.Boolean = Boolean;
|
|
|
3703
3859
|
*/
|
|
3704
3860
|
class Blob extends Layout {
|
|
3705
3861
|
constructor(length, property) {
|
|
3706
|
-
if (
|
|
3707
|
-
|
|
3708
|
-
|
|
3709
|
-
|
|
3862
|
+
if (
|
|
3863
|
+
!(
|
|
3864
|
+
(length instanceof ExternalLayout && length.isCount()) ||
|
|
3865
|
+
(Number.isInteger(length) && 0 <= length)
|
|
3866
|
+
)
|
|
3867
|
+
) {
|
|
3868
|
+
throw new TypeError(
|
|
3869
|
+
'length must be positive integer ' +
|
|
3870
|
+
'or an unsigned integer ExternalLayout',
|
|
3871
|
+
);
|
|
3710
3872
|
}
|
|
3711
3873
|
let span = -1;
|
|
3712
3874
|
if (!(length instanceof ExternalLayout)) {
|
|
@@ -3747,14 +3909,23 @@ class Blob extends Layout {
|
|
|
3747
3909
|
span = src.length;
|
|
3748
3910
|
}
|
|
3749
3911
|
if (!(src instanceof Uint8Array && span === src.length)) {
|
|
3750
|
-
throw new TypeError(
|
|
3751
|
-
|
|
3912
|
+
throw new TypeError(
|
|
3913
|
+
nameWithProperty('Blob.encode', this) +
|
|
3914
|
+
' requires (length ' +
|
|
3915
|
+
span +
|
|
3916
|
+
') Uint8Array as src',
|
|
3917
|
+
);
|
|
3752
3918
|
}
|
|
3753
|
-
if (
|
|
3919
|
+
if (offset + span > b.length) {
|
|
3754
3920
|
throw new RangeError('encoding overruns Uint8Array');
|
|
3755
3921
|
}
|
|
3756
3922
|
const srcBuffer = uint8ArrayToBuffer(src);
|
|
3757
|
-
uint8ArrayToBuffer(b).write(
|
|
3923
|
+
uint8ArrayToBuffer(b).write(
|
|
3924
|
+
srcBuffer.toString('hex'),
|
|
3925
|
+
offset,
|
|
3926
|
+
span,
|
|
3927
|
+
'hex',
|
|
3928
|
+
);
|
|
3758
3929
|
if (this.length instanceof ExternalLayout) {
|
|
3759
3930
|
this.length.encode(span, b, offset);
|
|
3760
3931
|
}
|
|
@@ -3783,7 +3954,7 @@ class CString extends Layout {
|
|
|
3783
3954
|
getSpan(b, offset = 0) {
|
|
3784
3955
|
checkUint8Array(b);
|
|
3785
3956
|
let idx = offset;
|
|
3786
|
-
while (
|
|
3957
|
+
while (idx < b.length && 0 !== b[idx]) {
|
|
3787
3958
|
idx += 1;
|
|
3788
3959
|
}
|
|
3789
3960
|
return 1 + idx - offset;
|
|
@@ -3791,7 +3962,9 @@ class CString extends Layout {
|
|
|
3791
3962
|
/** @override */
|
|
3792
3963
|
decode(b, offset = 0) {
|
|
3793
3964
|
const span = this.getSpan(b, offset);
|
|
3794
|
-
return uint8ArrayToBuffer(b)
|
|
3965
|
+
return uint8ArrayToBuffer(b)
|
|
3966
|
+
.slice(offset, offset + span - 1)
|
|
3967
|
+
.toString('utf-8');
|
|
3795
3968
|
}
|
|
3796
3969
|
/** @override */
|
|
3797
3970
|
encode(src, b, offset = 0) {
|
|
@@ -3803,7 +3976,7 @@ class CString extends Layout {
|
|
|
3803
3976
|
}
|
|
3804
3977
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3805
3978
|
const span = srcb.length;
|
|
3806
|
-
if (
|
|
3979
|
+
if (offset + span > b.length) {
|
|
3807
3980
|
throw new RangeError('encoding overruns Buffer');
|
|
3808
3981
|
}
|
|
3809
3982
|
const buffer = uint8ArrayToBuffer(b);
|
|
@@ -3834,14 +4007,13 @@ Layout$1.CString = CString;
|
|
|
3834
4007
|
*/
|
|
3835
4008
|
class UTF8 extends Layout {
|
|
3836
4009
|
constructor(maxSpan, property) {
|
|
3837
|
-
if (
|
|
4010
|
+
if ('string' === typeof maxSpan && undefined === property) {
|
|
3838
4011
|
property = maxSpan;
|
|
3839
4012
|
maxSpan = undefined;
|
|
3840
4013
|
}
|
|
3841
4014
|
if (undefined === maxSpan) {
|
|
3842
4015
|
maxSpan = -1;
|
|
3843
|
-
}
|
|
3844
|
-
else if (!Number.isInteger(maxSpan)) {
|
|
4016
|
+
} else if (!Number.isInteger(maxSpan)) {
|
|
3845
4017
|
throw new TypeError('maxSpan must be an integer');
|
|
3846
4018
|
}
|
|
3847
4019
|
super(-1, property);
|
|
@@ -3863,11 +4035,12 @@ class UTF8 extends Layout {
|
|
|
3863
4035
|
/** @override */
|
|
3864
4036
|
decode(b, offset = 0) {
|
|
3865
4037
|
const span = this.getSpan(b, offset);
|
|
3866
|
-
if (
|
|
3867
|
-
&& (this.maxSpan < span)) {
|
|
4038
|
+
if (0 <= this.maxSpan && this.maxSpan < span) {
|
|
3868
4039
|
throw new RangeError('text length exceeds maxSpan');
|
|
3869
4040
|
}
|
|
3870
|
-
return uint8ArrayToBuffer(b)
|
|
4041
|
+
return uint8ArrayToBuffer(b)
|
|
4042
|
+
.slice(offset, offset + span)
|
|
4043
|
+
.toString('utf-8');
|
|
3871
4044
|
}
|
|
3872
4045
|
/** @override */
|
|
3873
4046
|
encode(src, b, offset = 0) {
|
|
@@ -3879,11 +4052,10 @@ class UTF8 extends Layout {
|
|
|
3879
4052
|
}
|
|
3880
4053
|
const srcb = buffer_1.Buffer.from(src, 'utf8');
|
|
3881
4054
|
const span = srcb.length;
|
|
3882
|
-
if (
|
|
3883
|
-
&& (this.maxSpan < span)) {
|
|
4055
|
+
if (0 <= this.maxSpan && this.maxSpan < span) {
|
|
3884
4056
|
throw new RangeError('text length exceeds maxSpan');
|
|
3885
4057
|
}
|
|
3886
|
-
if (
|
|
4058
|
+
if (offset + span > b.length) {
|
|
3887
4059
|
throw new RangeError('encoding overruns Buffer');
|
|
3888
4060
|
}
|
|
3889
4061
|
srcb.copy(uint8ArrayToBuffer(b), offset);
|
|
@@ -3936,115 +4108,121 @@ class Constant extends Layout {
|
|
|
3936
4108
|
}
|
|
3937
4109
|
Layout$1.Constant = Constant;
|
|
3938
4110
|
/** Factory for {@link GreedyCount}. */
|
|
3939
|
-
Layout$1.greedy = (
|
|
4111
|
+
Layout$1.greedy = (elementSpan, property) =>
|
|
4112
|
+
new GreedyCount(elementSpan, property);
|
|
3940
4113
|
/** Factory for {@link OffsetLayout}. */
|
|
3941
|
-
Layout$1.offset = (
|
|
4114
|
+
Layout$1.offset = (layout, offset, property) =>
|
|
4115
|
+
new OffsetLayout(layout, offset, property);
|
|
3942
4116
|
/** Factory for {@link UInt|unsigned int layouts} spanning one
|
|
3943
4117
|
* byte. */
|
|
3944
|
-
var u8 = Layout$1.u8 =
|
|
4118
|
+
var u8 = (Layout$1.u8 = property => new UInt(1, property));
|
|
3945
4119
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3946
4120
|
* spanning two bytes. */
|
|
3947
|
-
Layout$1.u16 =
|
|
4121
|
+
Layout$1.u16 = property => new UInt(2, property);
|
|
3948
4122
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3949
4123
|
* spanning three bytes. */
|
|
3950
|
-
Layout$1.u24 =
|
|
4124
|
+
Layout$1.u24 = property => new UInt(3, property);
|
|
3951
4125
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3952
4126
|
* spanning four bytes. */
|
|
3953
|
-
var u32 = Layout$1.u32 =
|
|
4127
|
+
var u32 = (Layout$1.u32 = property => new UInt(4, property));
|
|
3954
4128
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3955
4129
|
* spanning five bytes. */
|
|
3956
|
-
Layout$1.u40 =
|
|
4130
|
+
Layout$1.u40 = property => new UInt(5, property);
|
|
3957
4131
|
/** Factory for {@link UInt|little-endian unsigned int layouts}
|
|
3958
4132
|
* spanning six bytes. */
|
|
3959
|
-
Layout$1.u48 =
|
|
4133
|
+
Layout$1.u48 = property => new UInt(6, property);
|
|
3960
4134
|
/** Factory for {@link NearUInt64|little-endian unsigned int
|
|
3961
4135
|
* layouts} interpreted as Numbers. */
|
|
3962
|
-
Layout$1.nu64 =
|
|
4136
|
+
Layout$1.nu64 = property => new NearUInt64(property);
|
|
3963
4137
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3964
4138
|
* spanning two bytes. */
|
|
3965
|
-
Layout$1.u16be =
|
|
4139
|
+
Layout$1.u16be = property => new UIntBE(2, property);
|
|
3966
4140
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3967
4141
|
* spanning three bytes. */
|
|
3968
|
-
Layout$1.u24be =
|
|
4142
|
+
Layout$1.u24be = property => new UIntBE(3, property);
|
|
3969
4143
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3970
4144
|
* spanning four bytes. */
|
|
3971
|
-
Layout$1.u32be =
|
|
4145
|
+
Layout$1.u32be = property => new UIntBE(4, property);
|
|
3972
4146
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3973
4147
|
* spanning five bytes. */
|
|
3974
|
-
Layout$1.u40be =
|
|
4148
|
+
Layout$1.u40be = property => new UIntBE(5, property);
|
|
3975
4149
|
/** Factory for {@link UInt|big-endian unsigned int layouts}
|
|
3976
4150
|
* spanning six bytes. */
|
|
3977
|
-
Layout$1.u48be =
|
|
4151
|
+
Layout$1.u48be = property => new UIntBE(6, property);
|
|
3978
4152
|
/** Factory for {@link NearUInt64BE|big-endian unsigned int
|
|
3979
4153
|
* layouts} interpreted as Numbers. */
|
|
3980
|
-
Layout$1.nu64be =
|
|
4154
|
+
Layout$1.nu64be = property => new NearUInt64BE(property);
|
|
3981
4155
|
/** Factory for {@link Int|signed int layouts} spanning one
|
|
3982
4156
|
* byte. */
|
|
3983
|
-
Layout$1.s8 =
|
|
4157
|
+
Layout$1.s8 = property => new Int(1, property);
|
|
3984
4158
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3985
4159
|
* spanning two bytes. */
|
|
3986
|
-
Layout$1.s16 =
|
|
4160
|
+
Layout$1.s16 = property => new Int(2, property);
|
|
3987
4161
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3988
4162
|
* spanning three bytes. */
|
|
3989
|
-
Layout$1.s24 =
|
|
4163
|
+
Layout$1.s24 = property => new Int(3, property);
|
|
3990
4164
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3991
4165
|
* spanning four bytes. */
|
|
3992
|
-
Layout$1.s32 =
|
|
4166
|
+
Layout$1.s32 = property => new Int(4, property);
|
|
3993
4167
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3994
4168
|
* spanning five bytes. */
|
|
3995
|
-
Layout$1.s40 =
|
|
4169
|
+
Layout$1.s40 = property => new Int(5, property);
|
|
3996
4170
|
/** Factory for {@link Int|little-endian signed int layouts}
|
|
3997
4171
|
* spanning six bytes. */
|
|
3998
|
-
Layout$1.s48 =
|
|
4172
|
+
Layout$1.s48 = property => new Int(6, property);
|
|
3999
4173
|
/** Factory for {@link NearInt64|little-endian signed int layouts}
|
|
4000
4174
|
* interpreted as Numbers. */
|
|
4001
|
-
Layout$1.ns64 =
|
|
4175
|
+
Layout$1.ns64 = property => new NearInt64(property);
|
|
4002
4176
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4003
4177
|
* spanning two bytes. */
|
|
4004
|
-
Layout$1.s16be =
|
|
4178
|
+
Layout$1.s16be = property => new IntBE(2, property);
|
|
4005
4179
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4006
4180
|
* spanning three bytes. */
|
|
4007
|
-
Layout$1.s24be =
|
|
4181
|
+
Layout$1.s24be = property => new IntBE(3, property);
|
|
4008
4182
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4009
4183
|
* spanning four bytes. */
|
|
4010
|
-
Layout$1.s32be =
|
|
4184
|
+
Layout$1.s32be = property => new IntBE(4, property);
|
|
4011
4185
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4012
4186
|
* spanning five bytes. */
|
|
4013
|
-
Layout$1.s40be =
|
|
4187
|
+
Layout$1.s40be = property => new IntBE(5, property);
|
|
4014
4188
|
/** Factory for {@link Int|big-endian signed int layouts}
|
|
4015
4189
|
* spanning six bytes. */
|
|
4016
|
-
Layout$1.s48be =
|
|
4190
|
+
Layout$1.s48be = property => new IntBE(6, property);
|
|
4017
4191
|
/** Factory for {@link NearInt64BE|big-endian signed int layouts}
|
|
4018
4192
|
* interpreted as Numbers. */
|
|
4019
|
-
Layout$1.ns64be =
|
|
4193
|
+
Layout$1.ns64be = property => new NearInt64BE(property);
|
|
4020
4194
|
/** Factory for {@link Float|little-endian 32-bit floating point} values. */
|
|
4021
|
-
Layout$1.f32 =
|
|
4195
|
+
Layout$1.f32 = property => new Float(property);
|
|
4022
4196
|
/** Factory for {@link FloatBE|big-endian 32-bit floating point} values. */
|
|
4023
|
-
Layout$1.f32be =
|
|
4197
|
+
Layout$1.f32be = property => new FloatBE(property);
|
|
4024
4198
|
/** Factory for {@link Double|little-endian 64-bit floating point} values. */
|
|
4025
|
-
Layout$1.f64 =
|
|
4199
|
+
Layout$1.f64 = property => new Double(property);
|
|
4026
4200
|
/** Factory for {@link DoubleBE|big-endian 64-bit floating point} values. */
|
|
4027
|
-
Layout$1.f64be =
|
|
4201
|
+
Layout$1.f64be = property => new DoubleBE(property);
|
|
4028
4202
|
/** Factory for {@link Structure} values. */
|
|
4029
|
-
var struct = Layout$1.struct = (
|
|
4203
|
+
var struct = (Layout$1.struct = (fields, property, decodePrefixes) =>
|
|
4204
|
+
new Structure(fields, property, decodePrefixes));
|
|
4030
4205
|
/** Factory for {@link BitStructure} values. */
|
|
4031
|
-
Layout$1.bits = (
|
|
4206
|
+
Layout$1.bits = (word, msb, property) => new BitStructure(word, msb, property);
|
|
4032
4207
|
/** Factory for {@link Sequence} values. */
|
|
4033
|
-
Layout$1.seq = (
|
|
4208
|
+
Layout$1.seq = (elementLayout, count, property) =>
|
|
4209
|
+
new Sequence(elementLayout, count, property);
|
|
4034
4210
|
/** Factory for {@link Union} values. */
|
|
4035
|
-
Layout$1.union = (
|
|
4211
|
+
Layout$1.union = (discr, defaultLayout, property) =>
|
|
4212
|
+
new Union(discr, defaultLayout, property);
|
|
4036
4213
|
/** Factory for {@link UnionLayoutDiscriminator} values. */
|
|
4037
|
-
Layout$1.unionLayoutDiscriminator = (
|
|
4214
|
+
Layout$1.unionLayoutDiscriminator = (layout, property) =>
|
|
4215
|
+
new UnionLayoutDiscriminator(layout, property);
|
|
4038
4216
|
/** Factory for {@link Blob} values. */
|
|
4039
|
-
var blob = Layout$1.blob = (
|
|
4217
|
+
var blob = (Layout$1.blob = (length, property) => new Blob(length, property));
|
|
4040
4218
|
/** Factory for {@link CString} values. */
|
|
4041
|
-
Layout$1.cstr =
|
|
4219
|
+
Layout$1.cstr = property => new CString(property);
|
|
4042
4220
|
/** Factory for {@link UTF8} values. */
|
|
4043
|
-
Layout$1.utf8 = (
|
|
4221
|
+
Layout$1.utf8 = (maxSpan, property) => new UTF8(maxSpan, property);
|
|
4044
4222
|
/** Factory for {@link Constant} values. */
|
|
4045
|
-
Layout$1.constant = (
|
|
4223
|
+
Layout$1.constant = (value, property) => new Constant(value, property);
|
|
4046
4224
|
|
|
4047
|
-
const encodeDecode =
|
|
4225
|
+
const encodeDecode = layout => {
|
|
4048
4226
|
const decode = layout.decode.bind(layout);
|
|
4049
4227
|
const encode = layout.encode.bind(layout);
|
|
4050
4228
|
return { decode, encode };
|
|
@@ -4052,7 +4230,7 @@ const encodeDecode = (layout) => {
|
|
|
4052
4230
|
|
|
4053
4231
|
var browser = {};
|
|
4054
4232
|
|
|
4055
|
-
Object.defineProperty(browser,
|
|
4233
|
+
Object.defineProperty(browser, '__esModule', { value: true });
|
|
4056
4234
|
/**
|
|
4057
4235
|
* Convert a little-endian buffer into a BigInt.
|
|
4058
4236
|
* @param buf The little-endian buffer to convert
|
|
@@ -4069,7 +4247,7 @@ function toBigIntLE(buf) {
|
|
|
4069
4247
|
return BigInt(`0x${hex}`);
|
|
4070
4248
|
}
|
|
4071
4249
|
}
|
|
4072
|
-
var toBigIntLE_1 = browser.toBigIntLE = toBigIntLE;
|
|
4250
|
+
var toBigIntLE_1 = (browser.toBigIntLE = toBigIntLE);
|
|
4073
4251
|
/**
|
|
4074
4252
|
* Convert a big-endian buffer into a BigInt
|
|
4075
4253
|
* @param buf The big-endian buffer to convert.
|
|
@@ -4094,12 +4272,15 @@ browser.toBigIntBE = toBigIntBE;
|
|
|
4094
4272
|
function toBufferLE(num, width) {
|
|
4095
4273
|
{
|
|
4096
4274
|
const hex = num.toString(16);
|
|
4097
|
-
const buffer = Buffer.from(
|
|
4275
|
+
const buffer = Buffer.from(
|
|
4276
|
+
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4277
|
+
'hex',
|
|
4278
|
+
);
|
|
4098
4279
|
buffer.reverse();
|
|
4099
4280
|
return buffer;
|
|
4100
4281
|
}
|
|
4101
4282
|
}
|
|
4102
|
-
var toBufferLE_1 = browser.toBufferLE = toBufferLE;
|
|
4283
|
+
var toBufferLE_1 = (browser.toBufferLE = toBufferLE);
|
|
4103
4284
|
/**
|
|
4104
4285
|
* Convert a BigInt to a big-endian buffer.
|
|
4105
4286
|
* @param num The BigInt to convert.
|
|
@@ -4109,12 +4290,15 @@ var toBufferLE_1 = browser.toBufferLE = toBufferLE;
|
|
|
4109
4290
|
function toBufferBE(num, width) {
|
|
4110
4291
|
{
|
|
4111
4292
|
const hex = num.toString(16);
|
|
4112
|
-
return Buffer.from(
|
|
4293
|
+
return Buffer.from(
|
|
4294
|
+
hex.padStart(width * 2, '0').slice(0, width * 2),
|
|
4295
|
+
'hex',
|
|
4296
|
+
);
|
|
4113
4297
|
}
|
|
4114
4298
|
}
|
|
4115
4299
|
browser.toBufferBE = toBufferBE;
|
|
4116
4300
|
|
|
4117
|
-
const bigInt =
|
|
4301
|
+
const bigInt = length => property => {
|
|
4118
4302
|
const layout = blob(length, property);
|
|
4119
4303
|
const { encode, decode } = encodeDecode(layout);
|
|
4120
4304
|
const bigIntLayout = layout;
|
|
@@ -4130,7 +4314,7 @@ const bigInt = (length) => (property) => {
|
|
|
4130
4314
|
};
|
|
4131
4315
|
const u64 = bigInt(8);
|
|
4132
4316
|
|
|
4133
|
-
const bool =
|
|
4317
|
+
const bool = property => {
|
|
4134
4318
|
const layout = u8(property);
|
|
4135
4319
|
const { encode, decode } = encodeDecode(layout);
|
|
4136
4320
|
const boolLayout = layout;
|
|
@@ -4145,7 +4329,7 @@ const bool = (property) => {
|
|
|
4145
4329
|
return boolLayout;
|
|
4146
4330
|
};
|
|
4147
4331
|
|
|
4148
|
-
const publicKey =
|
|
4332
|
+
const publicKey = property => {
|
|
4149
4333
|
const layout = blob(32, property);
|
|
4150
4334
|
const { encode, decode } = encodeDecode(layout);
|
|
4151
4335
|
const publicKeyLayout = layout;
|
|
@@ -4219,73 +4403,112 @@ class TokenOwnerOffCurveError extends TokenError {
|
|
|
4219
4403
|
/** Instructions defined by the program */
|
|
4220
4404
|
var TokenInstruction;
|
|
4221
4405
|
(function (TokenInstruction) {
|
|
4222
|
-
TokenInstruction[TokenInstruction[
|
|
4223
|
-
|
|
4224
|
-
TokenInstruction[TokenInstruction[
|
|
4225
|
-
|
|
4226
|
-
TokenInstruction[TokenInstruction[
|
|
4227
|
-
|
|
4228
|
-
TokenInstruction[TokenInstruction[
|
|
4229
|
-
TokenInstruction[TokenInstruction[
|
|
4230
|
-
TokenInstruction[TokenInstruction[
|
|
4231
|
-
TokenInstruction[TokenInstruction[
|
|
4232
|
-
TokenInstruction[TokenInstruction[
|
|
4233
|
-
TokenInstruction[TokenInstruction[
|
|
4234
|
-
TokenInstruction[TokenInstruction[
|
|
4235
|
-
TokenInstruction[TokenInstruction[
|
|
4236
|
-
|
|
4237
|
-
TokenInstruction[TokenInstruction[
|
|
4238
|
-
TokenInstruction[TokenInstruction[
|
|
4239
|
-
|
|
4240
|
-
TokenInstruction[TokenInstruction[
|
|
4241
|
-
|
|
4242
|
-
TokenInstruction[TokenInstruction[
|
|
4243
|
-
|
|
4244
|
-
TokenInstruction[TokenInstruction[
|
|
4245
|
-
TokenInstruction[TokenInstruction[
|
|
4246
|
-
|
|
4247
|
-
TokenInstruction[TokenInstruction[
|
|
4248
|
-
TokenInstruction[TokenInstruction[
|
|
4249
|
-
|
|
4250
|
-
TokenInstruction[TokenInstruction[
|
|
4251
|
-
|
|
4252
|
-
TokenInstruction[TokenInstruction[
|
|
4253
|
-
|
|
4254
|
-
TokenInstruction[TokenInstruction[
|
|
4255
|
-
|
|
4256
|
-
TokenInstruction[TokenInstruction[
|
|
4257
|
-
|
|
4258
|
-
TokenInstruction[TokenInstruction[
|
|
4406
|
+
TokenInstruction[(TokenInstruction['InitializeMint'] = 0)] =
|
|
4407
|
+
'InitializeMint';
|
|
4408
|
+
TokenInstruction[(TokenInstruction['InitializeAccount'] = 1)] =
|
|
4409
|
+
'InitializeAccount';
|
|
4410
|
+
TokenInstruction[(TokenInstruction['InitializeMultisig'] = 2)] =
|
|
4411
|
+
'InitializeMultisig';
|
|
4412
|
+
TokenInstruction[(TokenInstruction['Transfer'] = 3)] = 'Transfer';
|
|
4413
|
+
TokenInstruction[(TokenInstruction['Approve'] = 4)] = 'Approve';
|
|
4414
|
+
TokenInstruction[(TokenInstruction['Revoke'] = 5)] = 'Revoke';
|
|
4415
|
+
TokenInstruction[(TokenInstruction['SetAuthority'] = 6)] = 'SetAuthority';
|
|
4416
|
+
TokenInstruction[(TokenInstruction['MintTo'] = 7)] = 'MintTo';
|
|
4417
|
+
TokenInstruction[(TokenInstruction['Burn'] = 8)] = 'Burn';
|
|
4418
|
+
TokenInstruction[(TokenInstruction['CloseAccount'] = 9)] = 'CloseAccount';
|
|
4419
|
+
TokenInstruction[(TokenInstruction['FreezeAccount'] = 10)] =
|
|
4420
|
+
'FreezeAccount';
|
|
4421
|
+
TokenInstruction[(TokenInstruction['ThawAccount'] = 11)] = 'ThawAccount';
|
|
4422
|
+
TokenInstruction[(TokenInstruction['TransferChecked'] = 12)] =
|
|
4423
|
+
'TransferChecked';
|
|
4424
|
+
TokenInstruction[(TokenInstruction['ApproveChecked'] = 13)] =
|
|
4425
|
+
'ApproveChecked';
|
|
4426
|
+
TokenInstruction[(TokenInstruction['MintToChecked'] = 14)] =
|
|
4427
|
+
'MintToChecked';
|
|
4428
|
+
TokenInstruction[(TokenInstruction['BurnChecked'] = 15)] = 'BurnChecked';
|
|
4429
|
+
TokenInstruction[(TokenInstruction['InitializeAccount2'] = 16)] =
|
|
4430
|
+
'InitializeAccount2';
|
|
4431
|
+
TokenInstruction[(TokenInstruction['SyncNative'] = 17)] = 'SyncNative';
|
|
4432
|
+
TokenInstruction[(TokenInstruction['InitializeAccount3'] = 18)] =
|
|
4433
|
+
'InitializeAccount3';
|
|
4434
|
+
TokenInstruction[(TokenInstruction['InitializeMultisig2'] = 19)] =
|
|
4435
|
+
'InitializeMultisig2';
|
|
4436
|
+
TokenInstruction[(TokenInstruction['InitializeMint2'] = 20)] =
|
|
4437
|
+
'InitializeMint2';
|
|
4438
|
+
TokenInstruction[(TokenInstruction['GetAccountDataSize'] = 21)] =
|
|
4439
|
+
'GetAccountDataSize';
|
|
4440
|
+
TokenInstruction[(TokenInstruction['InitializeImmutableOwner'] = 22)] =
|
|
4441
|
+
'InitializeImmutableOwner';
|
|
4442
|
+
TokenInstruction[(TokenInstruction['AmountToUiAmount'] = 23)] =
|
|
4443
|
+
'AmountToUiAmount';
|
|
4444
|
+
TokenInstruction[(TokenInstruction['UiAmountToAmount'] = 24)] =
|
|
4445
|
+
'UiAmountToAmount';
|
|
4446
|
+
TokenInstruction[(TokenInstruction['InitializeMintCloseAuthority'] = 25)] =
|
|
4447
|
+
'InitializeMintCloseAuthority';
|
|
4448
|
+
TokenInstruction[(TokenInstruction['TransferFeeExtension'] = 26)] =
|
|
4449
|
+
'TransferFeeExtension';
|
|
4450
|
+
TokenInstruction[(TokenInstruction['ConfidentialTransferExtension'] = 27)] =
|
|
4451
|
+
'ConfidentialTransferExtension';
|
|
4452
|
+
TokenInstruction[(TokenInstruction['DefaultAccountStateExtension'] = 28)] =
|
|
4453
|
+
'DefaultAccountStateExtension';
|
|
4454
|
+
TokenInstruction[(TokenInstruction['Reallocate'] = 29)] = 'Reallocate';
|
|
4455
|
+
TokenInstruction[(TokenInstruction['MemoTransferExtension'] = 30)] =
|
|
4456
|
+
'MemoTransferExtension';
|
|
4457
|
+
TokenInstruction[(TokenInstruction['CreateNativeMint'] = 31)] =
|
|
4458
|
+
'CreateNativeMint';
|
|
4459
|
+
TokenInstruction[(TokenInstruction['InitializeNonTransferableMint'] = 32)] =
|
|
4460
|
+
'InitializeNonTransferableMint';
|
|
4461
|
+
TokenInstruction[(TokenInstruction['InterestBearingMintExtension'] = 33)] =
|
|
4462
|
+
'InterestBearingMintExtension';
|
|
4463
|
+
TokenInstruction[(TokenInstruction['CpiGuardExtension'] = 34)] =
|
|
4464
|
+
'CpiGuardExtension';
|
|
4465
|
+
TokenInstruction[(TokenInstruction['InitializePermanentDelegate'] = 35)] =
|
|
4466
|
+
'InitializePermanentDelegate';
|
|
4467
|
+
TokenInstruction[(TokenInstruction['TransferHookExtension'] = 36)] =
|
|
4468
|
+
'TransferHookExtension';
|
|
4259
4469
|
// ConfidentialTransferFeeExtension = 37,
|
|
4260
4470
|
// WithdrawalExcessLamports = 38,
|
|
4261
|
-
TokenInstruction[TokenInstruction[
|
|
4262
|
-
|
|
4263
|
-
TokenInstruction[TokenInstruction[
|
|
4471
|
+
TokenInstruction[(TokenInstruction['MetadataPointerExtension'] = 39)] =
|
|
4472
|
+
'MetadataPointerExtension';
|
|
4473
|
+
TokenInstruction[(TokenInstruction['GroupPointerExtension'] = 40)] =
|
|
4474
|
+
'GroupPointerExtension';
|
|
4475
|
+
TokenInstruction[(TokenInstruction['GroupMemberPointerExtension'] = 41)] =
|
|
4476
|
+
'GroupMemberPointerExtension';
|
|
4264
4477
|
})(TokenInstruction || (TokenInstruction = {}));
|
|
4265
4478
|
|
|
4266
4479
|
/** @internal */
|
|
4267
4480
|
function addSigners(keys, ownerOrAuthority, multiSigners) {
|
|
4268
4481
|
if (multiSigners.length) {
|
|
4269
|
-
keys.push({
|
|
4482
|
+
keys.push({
|
|
4483
|
+
pubkey: ownerOrAuthority,
|
|
4484
|
+
isSigner: false,
|
|
4485
|
+
isWritable: false,
|
|
4486
|
+
});
|
|
4270
4487
|
for (const signer of multiSigners) {
|
|
4271
4488
|
keys.push({
|
|
4272
|
-
pubkey:
|
|
4489
|
+
pubkey:
|
|
4490
|
+
signer instanceof web3_js.PublicKey
|
|
4491
|
+
? signer
|
|
4492
|
+
: signer.publicKey,
|
|
4273
4493
|
isSigner: true,
|
|
4274
4494
|
isWritable: false,
|
|
4275
4495
|
});
|
|
4276
4496
|
}
|
|
4277
|
-
}
|
|
4278
|
-
|
|
4279
|
-
|
|
4497
|
+
} else {
|
|
4498
|
+
keys.push({
|
|
4499
|
+
pubkey: ownerOrAuthority,
|
|
4500
|
+
isSigner: true,
|
|
4501
|
+
isWritable: false,
|
|
4502
|
+
});
|
|
4280
4503
|
}
|
|
4281
4504
|
return keys;
|
|
4282
4505
|
}
|
|
4283
4506
|
|
|
4284
4507
|
var AccountType;
|
|
4285
4508
|
(function (AccountType) {
|
|
4286
|
-
AccountType[AccountType[
|
|
4287
|
-
AccountType[AccountType[
|
|
4288
|
-
AccountType[AccountType[
|
|
4509
|
+
AccountType[(AccountType['Uninitialized'] = 0)] = 'Uninitialized';
|
|
4510
|
+
AccountType[(AccountType['Mint'] = 1)] = 'Mint';
|
|
4511
|
+
AccountType[(AccountType['Account'] = 2)] = 'Account';
|
|
4289
4512
|
})(AccountType || (AccountType = {}));
|
|
4290
4513
|
const ACCOUNT_TYPE_SIZE = 1;
|
|
4291
4514
|
|
|
@@ -4312,9 +4535,9 @@ const MULTISIG_SIZE = MultisigLayout.span;
|
|
|
4312
4535
|
/** Token account state as stored by the program */
|
|
4313
4536
|
var AccountState;
|
|
4314
4537
|
(function (AccountState) {
|
|
4315
|
-
AccountState[AccountState[
|
|
4316
|
-
AccountState[AccountState[
|
|
4317
|
-
AccountState[AccountState[
|
|
4538
|
+
AccountState[(AccountState['Uninitialized'] = 0)] = 'Uninitialized';
|
|
4539
|
+
AccountState[(AccountState['Initialized'] = 1)] = 'Initialized';
|
|
4540
|
+
AccountState[(AccountState['Frozen'] = 2)] = 'Frozen';
|
|
4318
4541
|
})(AccountState || (AccountState = {}));
|
|
4319
4542
|
/** Buffer layout for de/serializing a token account */
|
|
4320
4543
|
const AccountLayout = struct([
|
|
@@ -4342,7 +4565,12 @@ const ACCOUNT_SIZE = AccountLayout.span;
|
|
|
4342
4565
|
*
|
|
4343
4566
|
* @return Token account information
|
|
4344
4567
|
*/
|
|
4345
|
-
async function getAccount(
|
|
4568
|
+
async function getAccount(
|
|
4569
|
+
connection,
|
|
4570
|
+
address,
|
|
4571
|
+
commitment,
|
|
4572
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4573
|
+
) {
|
|
4346
4574
|
const info = await connection.getAccountInfo(address, commitment);
|
|
4347
4575
|
return unpackAccount(address, info, programId);
|
|
4348
4576
|
}
|
|
@@ -4356,8 +4584,7 @@ async function getAccount(connection, address, commitment, programId = TOKEN_PRO
|
|
|
4356
4584
|
* @return Unpacked token account
|
|
4357
4585
|
*/
|
|
4358
4586
|
function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
4359
|
-
if (!info)
|
|
4360
|
-
throw new TokenAccountNotFoundError();
|
|
4587
|
+
if (!info) throw new TokenAccountNotFoundError();
|
|
4361
4588
|
if (!info.owner.equals(programId))
|
|
4362
4589
|
throw new TokenInvalidAccountOwnerError();
|
|
4363
4590
|
if (info.data.length < ACCOUNT_SIZE)
|
|
@@ -4381,8 +4608,12 @@ function unpackAccount(address, info, programId = TOKEN_PROGRAM_ID) {
|
|
|
4381
4608
|
isInitialized: rawAccount.state !== AccountState.Uninitialized,
|
|
4382
4609
|
isFrozen: rawAccount.state === AccountState.Frozen,
|
|
4383
4610
|
isNative: !!rawAccount.isNativeOption,
|
|
4384
|
-
rentExemptReserve: rawAccount.isNativeOption
|
|
4385
|
-
|
|
4611
|
+
rentExemptReserve: rawAccount.isNativeOption
|
|
4612
|
+
? rawAccount.isNative
|
|
4613
|
+
: null,
|
|
4614
|
+
closeAuthority: rawAccount.closeAuthorityOption
|
|
4615
|
+
? rawAccount.closeAuthority
|
|
4616
|
+
: null,
|
|
4386
4617
|
tlvData,
|
|
4387
4618
|
};
|
|
4388
4619
|
}
|
|
@@ -4410,10 +4641,19 @@ const MINT_SIZE = MintLayout.span;
|
|
|
4410
4641
|
*
|
|
4411
4642
|
* @return Address of the associated token account
|
|
4412
4643
|
*/
|
|
4413
|
-
function getAssociatedTokenAddressSync(
|
|
4644
|
+
function getAssociatedTokenAddressSync(
|
|
4645
|
+
mint,
|
|
4646
|
+
owner,
|
|
4647
|
+
allowOwnerOffCurve = false,
|
|
4648
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4649
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4650
|
+
) {
|
|
4414
4651
|
if (!allowOwnerOffCurve && !web3_js.PublicKey.isOnCurve(owner.toBuffer()))
|
|
4415
4652
|
throw new TokenOwnerOffCurveError();
|
|
4416
|
-
const [address] = web3_js.PublicKey.findProgramAddressSync(
|
|
4653
|
+
const [address] = web3_js.PublicKey.findProgramAddressSync(
|
|
4654
|
+
[owner.toBuffer(), programId.toBuffer(), mint.toBuffer()],
|
|
4655
|
+
associatedTokenProgramId,
|
|
4656
|
+
);
|
|
4417
4657
|
return address;
|
|
4418
4658
|
}
|
|
4419
4659
|
|
|
@@ -4433,8 +4673,7 @@ class COptionPublicKeyLayout extends Layout_2 {
|
|
|
4433
4673
|
if (src === null) {
|
|
4434
4674
|
buffer[offset] = 0;
|
|
4435
4675
|
return 1;
|
|
4436
|
-
}
|
|
4437
|
-
else {
|
|
4676
|
+
} else {
|
|
4438
4677
|
buffer[offset] = 1;
|
|
4439
4678
|
this.publicKeyLayout.encode(src, buffer, offset + 1);
|
|
4440
4679
|
return 33;
|
|
@@ -4461,16 +4700,43 @@ class COptionPublicKeyLayout extends Layout_2 {
|
|
|
4461
4700
|
*
|
|
4462
4701
|
* @return Instruction to add to a transaction
|
|
4463
4702
|
*/
|
|
4464
|
-
function createAssociatedTokenAccountInstruction(
|
|
4465
|
-
|
|
4703
|
+
function createAssociatedTokenAccountInstruction(
|
|
4704
|
+
payer,
|
|
4705
|
+
associatedToken,
|
|
4706
|
+
owner,
|
|
4707
|
+
mint,
|
|
4708
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4709
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4710
|
+
) {
|
|
4711
|
+
return buildAssociatedTokenAccountInstruction(
|
|
4712
|
+
payer,
|
|
4713
|
+
associatedToken,
|
|
4714
|
+
owner,
|
|
4715
|
+
mint,
|
|
4716
|
+
Buffer.alloc(0),
|
|
4717
|
+
programId,
|
|
4718
|
+
associatedTokenProgramId,
|
|
4719
|
+
);
|
|
4466
4720
|
}
|
|
4467
|
-
function buildAssociatedTokenAccountInstruction(
|
|
4721
|
+
function buildAssociatedTokenAccountInstruction(
|
|
4722
|
+
payer,
|
|
4723
|
+
associatedToken,
|
|
4724
|
+
owner,
|
|
4725
|
+
mint,
|
|
4726
|
+
instructionData,
|
|
4727
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4728
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4729
|
+
) {
|
|
4468
4730
|
const keys = [
|
|
4469
4731
|
{ pubkey: payer, isSigner: true, isWritable: true },
|
|
4470
4732
|
{ pubkey: associatedToken, isSigner: false, isWritable: true },
|
|
4471
4733
|
{ pubkey: owner, isSigner: false, isWritable: false },
|
|
4472
4734
|
{ pubkey: mint, isSigner: false, isWritable: false },
|
|
4473
|
-
{
|
|
4735
|
+
{
|
|
4736
|
+
pubkey: web3_js.SystemProgram.programId,
|
|
4737
|
+
isSigner: false,
|
|
4738
|
+
isWritable: false,
|
|
4739
|
+
},
|
|
4474
4740
|
{ pubkey: programId, isSigner: false, isWritable: false },
|
|
4475
4741
|
];
|
|
4476
4742
|
return new web3_js.TransactionInstruction({
|
|
@@ -4498,15 +4764,24 @@ const initializeMint2InstructionData = struct([
|
|
|
4498
4764
|
*
|
|
4499
4765
|
* @return Instruction to add to a transaction
|
|
4500
4766
|
*/
|
|
4501
|
-
function createInitializeMint2Instruction(
|
|
4767
|
+
function createInitializeMint2Instruction(
|
|
4768
|
+
mint,
|
|
4769
|
+
decimals,
|
|
4770
|
+
mintAuthority,
|
|
4771
|
+
freezeAuthority,
|
|
4772
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4773
|
+
) {
|
|
4502
4774
|
const keys = [{ pubkey: mint, isSigner: false, isWritable: true }];
|
|
4503
4775
|
const data = Buffer.alloc(initializeMint2InstructionData.span);
|
|
4504
|
-
initializeMint2InstructionData.encode(
|
|
4505
|
-
|
|
4506
|
-
|
|
4507
|
-
|
|
4508
|
-
|
|
4509
|
-
|
|
4776
|
+
initializeMint2InstructionData.encode(
|
|
4777
|
+
{
|
|
4778
|
+
instruction: TokenInstruction.InitializeMint2,
|
|
4779
|
+
decimals,
|
|
4780
|
+
mintAuthority,
|
|
4781
|
+
freezeAuthority,
|
|
4782
|
+
},
|
|
4783
|
+
data,
|
|
4784
|
+
);
|
|
4510
4785
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4511
4786
|
}
|
|
4512
4787
|
|
|
@@ -4525,39 +4800,77 @@ function createInitializeMint2Instruction(mint, decimals, mintAuthority, freezeA
|
|
|
4525
4800
|
*
|
|
4526
4801
|
* @return Address of the new associated token account
|
|
4527
4802
|
*/
|
|
4528
|
-
async function getOrCreateAssociatedTokenAccount(
|
|
4529
|
-
|
|
4803
|
+
async function getOrCreateAssociatedTokenAccount(
|
|
4804
|
+
connection,
|
|
4805
|
+
payer,
|
|
4806
|
+
mint,
|
|
4807
|
+
owner,
|
|
4808
|
+
allowOwnerOffCurve = false,
|
|
4809
|
+
commitment,
|
|
4810
|
+
confirmOptions,
|
|
4811
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4812
|
+
associatedTokenProgramId = ASSOCIATED_TOKEN_PROGRAM_ID,
|
|
4813
|
+
) {
|
|
4814
|
+
const associatedToken = getAssociatedTokenAddressSync(
|
|
4815
|
+
mint,
|
|
4816
|
+
owner,
|
|
4817
|
+
allowOwnerOffCurve,
|
|
4818
|
+
programId,
|
|
4819
|
+
associatedTokenProgramId,
|
|
4820
|
+
);
|
|
4530
4821
|
// This is the optimal logic, considering TX fee, client-side computation, RPC roundtrips and guaranteed idempotent.
|
|
4531
4822
|
// Sadly we can't do this atomically.
|
|
4532
4823
|
let account;
|
|
4533
4824
|
try {
|
|
4534
|
-
account = await getAccount(
|
|
4535
|
-
|
|
4536
|
-
|
|
4825
|
+
account = await getAccount(
|
|
4826
|
+
connection,
|
|
4827
|
+
associatedToken,
|
|
4828
|
+
commitment,
|
|
4829
|
+
programId,
|
|
4830
|
+
);
|
|
4831
|
+
} catch (error) {
|
|
4537
4832
|
// TokenAccountNotFoundError can be possible if the associated address has already received some lamports,
|
|
4538
4833
|
// becoming a system account. Assuming program derived addressing is safe, this is the only case for the
|
|
4539
4834
|
// TokenInvalidAccountOwnerError in this code path.
|
|
4540
|
-
if (
|
|
4835
|
+
if (
|
|
4836
|
+
error instanceof TokenAccountNotFoundError ||
|
|
4837
|
+
error instanceof TokenInvalidAccountOwnerError
|
|
4838
|
+
) {
|
|
4541
4839
|
// As this isn't atomic, it's possible others can create associated accounts meanwhile.
|
|
4542
4840
|
try {
|
|
4543
|
-
const transaction = new web3_js.Transaction().add(
|
|
4544
|
-
|
|
4545
|
-
|
|
4546
|
-
|
|
4841
|
+
const transaction = new web3_js.Transaction().add(
|
|
4842
|
+
createAssociatedTokenAccountInstruction(
|
|
4843
|
+
payer.publicKey,
|
|
4844
|
+
associatedToken,
|
|
4845
|
+
owner,
|
|
4846
|
+
mint,
|
|
4847
|
+
programId,
|
|
4848
|
+
associatedTokenProgramId,
|
|
4849
|
+
),
|
|
4850
|
+
);
|
|
4851
|
+
await web3_js.sendAndConfirmTransaction(
|
|
4852
|
+
connection,
|
|
4853
|
+
transaction,
|
|
4854
|
+
[payer],
|
|
4855
|
+
confirmOptions,
|
|
4856
|
+
);
|
|
4857
|
+
} catch (error) {
|
|
4547
4858
|
// Ignore all errors; for now there is no API-compatible way to selectively ignore the expected
|
|
4548
4859
|
// instruction error if the associated account exists already.
|
|
4549
4860
|
}
|
|
4550
4861
|
// Now this should always succeed
|
|
4551
|
-
account = await getAccount(
|
|
4552
|
-
|
|
4553
|
-
|
|
4862
|
+
account = await getAccount(
|
|
4863
|
+
connection,
|
|
4864
|
+
associatedToken,
|
|
4865
|
+
commitment,
|
|
4866
|
+
programId,
|
|
4867
|
+
);
|
|
4868
|
+
} else {
|
|
4554
4869
|
throw error;
|
|
4555
4870
|
}
|
|
4556
4871
|
}
|
|
4557
|
-
if (!account.mint.equals(mint))
|
|
4558
|
-
|
|
4559
|
-
if (!account.owner.equals(owner))
|
|
4560
|
-
throw new TokenInvalidOwnerError();
|
|
4872
|
+
if (!account.mint.equals(mint)) throw new TokenInvalidMintError();
|
|
4873
|
+
if (!account.owner.equals(owner)) throw new TokenInvalidOwnerError();
|
|
4561
4874
|
return account;
|
|
4562
4875
|
}
|
|
4563
4876
|
|
|
@@ -4575,29 +4888,46 @@ const mintToInstructionData = struct([u8('instruction'), u64('amount')]);
|
|
|
4575
4888
|
*
|
|
4576
4889
|
* @return Instruction to add to a transaction
|
|
4577
4890
|
*/
|
|
4578
|
-
function createMintToInstruction(
|
|
4579
|
-
|
|
4580
|
-
|
|
4581
|
-
|
|
4582
|
-
|
|
4891
|
+
function createMintToInstruction(
|
|
4892
|
+
mint,
|
|
4893
|
+
destination,
|
|
4894
|
+
authority,
|
|
4895
|
+
amount,
|
|
4896
|
+
multiSigners = [],
|
|
4897
|
+
programId = TOKEN_PROGRAM_ID,
|
|
4898
|
+
) {
|
|
4899
|
+
const keys = addSigners(
|
|
4900
|
+
[
|
|
4901
|
+
{ pubkey: mint, isSigner: false, isWritable: true },
|
|
4902
|
+
{ pubkey: destination, isSigner: false, isWritable: true },
|
|
4903
|
+
],
|
|
4904
|
+
authority,
|
|
4905
|
+
multiSigners,
|
|
4906
|
+
);
|
|
4583
4907
|
const data = Buffer.alloc(mintToInstructionData.span);
|
|
4584
|
-
mintToInstructionData.encode(
|
|
4585
|
-
|
|
4586
|
-
|
|
4587
|
-
|
|
4908
|
+
mintToInstructionData.encode(
|
|
4909
|
+
{
|
|
4910
|
+
instruction: TokenInstruction.MintTo,
|
|
4911
|
+
amount: BigInt(amount),
|
|
4912
|
+
},
|
|
4913
|
+
data,
|
|
4914
|
+
);
|
|
4588
4915
|
return new web3_js.TransactionInstruction({ keys, programId, data });
|
|
4589
4916
|
}
|
|
4590
4917
|
|
|
4591
4918
|
/**
|
|
4592
4919
|
* Sum up the token amounts of the compressed token accounts
|
|
4593
4920
|
*/
|
|
4594
|
-
const sumUpTokenAmount =
|
|
4595
|
-
return accounts.reduce(
|
|
4921
|
+
const sumUpTokenAmount = accounts => {
|
|
4922
|
+
return accounts.reduce(
|
|
4923
|
+
(acc, account) => acc.add(account.parsed.amount),
|
|
4924
|
+
stateless_js.bn(0),
|
|
4925
|
+
);
|
|
4596
4926
|
};
|
|
4597
4927
|
/**
|
|
4598
4928
|
* Validate that all the compressed token accounts are owned by the same owner.
|
|
4599
4929
|
*/
|
|
4600
|
-
const validateSameTokenOwner =
|
|
4930
|
+
const validateSameTokenOwner = accounts => {
|
|
4601
4931
|
const owner = accounts[0].parsed.owner;
|
|
4602
4932
|
accounts.forEach(acc => {
|
|
4603
4933
|
if (!acc.parsed.owner.equals(owner)) {
|
|
@@ -4608,7 +4938,7 @@ const validateSameTokenOwner = (accounts) => {
|
|
|
4608
4938
|
/**
|
|
4609
4939
|
* Parse compressed token accounts to get the mint, current owner and delegate.
|
|
4610
4940
|
*/
|
|
4611
|
-
const parseTokenData =
|
|
4941
|
+
const parseTokenData = compressedTokenAccounts => {
|
|
4612
4942
|
const mint = compressedTokenAccounts[0].parsed.mint;
|
|
4613
4943
|
const currentOwner = compressedTokenAccounts[0].parsed.owner;
|
|
4614
4944
|
const delegate = compressedTokenAccounts[0].parsed.delegate;
|
|
@@ -4622,13 +4952,22 @@ const parseTokenData = (compressedTokenAccounts) => {
|
|
|
4622
4952
|
* @returns Output token data for the transfer
|
|
4623
4953
|
* instruction
|
|
4624
4954
|
*/
|
|
4625
|
-
function createTransferOutputState(
|
|
4955
|
+
function createTransferOutputState(
|
|
4956
|
+
inputCompressedTokenAccounts,
|
|
4957
|
+
toAddress,
|
|
4958
|
+
amount,
|
|
4959
|
+
) {
|
|
4626
4960
|
amount = stateless_js.bn(amount);
|
|
4627
4961
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4628
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
4962
|
+
const inputLamports = stateless_js.sumUpLamports(
|
|
4963
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4964
|
+
);
|
|
4629
4965
|
const changeAmount = inputAmount.sub(amount);
|
|
4630
4966
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4631
|
-
if (
|
|
4967
|
+
if (
|
|
4968
|
+
changeAmount.eq(stateless_js.bn(0)) &&
|
|
4969
|
+
inputLamports.eq(stateless_js.bn(0))
|
|
4970
|
+
) {
|
|
4632
4971
|
return [
|
|
4633
4972
|
{
|
|
4634
4973
|
owner: toAddress,
|
|
@@ -4639,7 +4978,9 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
|
|
|
4639
4978
|
];
|
|
4640
4979
|
}
|
|
4641
4980
|
/// validates token program
|
|
4642
|
-
stateless_js.validateSameOwner(
|
|
4981
|
+
stateless_js.validateSameOwner(
|
|
4982
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
4983
|
+
);
|
|
4643
4984
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4644
4985
|
const outputCompressedAccounts = [
|
|
4645
4986
|
{
|
|
@@ -4666,15 +5007,22 @@ function createTransferOutputState(inputCompressedTokenAccounts, toAddress, amou
|
|
|
4666
5007
|
*/
|
|
4667
5008
|
function createDecompressOutputState(inputCompressedTokenAccounts, amount) {
|
|
4668
5009
|
amount = stateless_js.bn(amount);
|
|
4669
|
-
const inputLamports = stateless_js.sumUpLamports(
|
|
5010
|
+
const inputLamports = stateless_js.sumUpLamports(
|
|
5011
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
5012
|
+
);
|
|
4670
5013
|
const inputAmount = sumUpTokenAmount(inputCompressedTokenAccounts);
|
|
4671
5014
|
const changeAmount = inputAmount.sub(amount);
|
|
4672
5015
|
stateless_js.validateSufficientBalance(changeAmount);
|
|
4673
5016
|
/// lamports gets decompressed
|
|
4674
|
-
if (
|
|
5017
|
+
if (
|
|
5018
|
+
changeAmount.eq(stateless_js.bn(0)) &&
|
|
5019
|
+
inputLamports.eq(stateless_js.bn(0))
|
|
5020
|
+
) {
|
|
4675
5021
|
return [];
|
|
4676
5022
|
}
|
|
4677
|
-
stateless_js.validateSameOwner(
|
|
5023
|
+
stateless_js.validateSameOwner(
|
|
5024
|
+
inputCompressedTokenAccounts.map(acc => acc.compressedAccount),
|
|
5025
|
+
);
|
|
4678
5026
|
validateSameTokenOwner(inputCompressedTokenAccounts);
|
|
4679
5027
|
const tokenTransferOutputs = [
|
|
4680
5028
|
{
|
|
@@ -4690,7 +5038,7 @@ class CompressedTokenProgram {
|
|
|
4690
5038
|
/**
|
|
4691
5039
|
* @internal
|
|
4692
5040
|
*/
|
|
4693
|
-
constructor() {
|
|
5041
|
+
constructor() {}
|
|
4694
5042
|
/** @internal */
|
|
4695
5043
|
static get program() {
|
|
4696
5044
|
if (!this._program) {
|
|
@@ -4708,21 +5056,38 @@ class CompressedTokenProgram {
|
|
|
4708
5056
|
/// program only for serde and building instructions, not for
|
|
4709
5057
|
/// interacting with the network.
|
|
4710
5058
|
const mockKeypair = web3_js.Keypair.generate();
|
|
4711
|
-
const mockConnection = new web3_js.Connection(
|
|
4712
|
-
|
|
5059
|
+
const mockConnection = new web3_js.Connection(
|
|
5060
|
+
'http://127.0.0.1:8899',
|
|
5061
|
+
'confirmed',
|
|
5062
|
+
);
|
|
5063
|
+
const mockProvider = new anchor.AnchorProvider(
|
|
5064
|
+
mockConnection,
|
|
5065
|
+
stateless_js.useWallet(mockKeypair),
|
|
5066
|
+
stateless_js.confirmConfig,
|
|
5067
|
+
);
|
|
4713
5068
|
anchor.setProvider(mockProvider);
|
|
4714
|
-
this._program = new anchor.Program(
|
|
5069
|
+
this._program = new anchor.Program(
|
|
5070
|
+
IDL,
|
|
5071
|
+
this.programId,
|
|
5072
|
+
mockProvider,
|
|
5073
|
+
);
|
|
4715
5074
|
}
|
|
4716
5075
|
}
|
|
4717
5076
|
/** @internal */
|
|
4718
5077
|
static deriveTokenPoolPda(mint) {
|
|
4719
5078
|
const seeds = [POOL_SEED, mint.toBuffer()];
|
|
4720
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
5079
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
5080
|
+
seeds,
|
|
5081
|
+
this.programId,
|
|
5082
|
+
);
|
|
4721
5083
|
return address;
|
|
4722
5084
|
}
|
|
4723
5085
|
/** @internal */
|
|
4724
5086
|
static get deriveCpiAuthorityPda() {
|
|
4725
|
-
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
5087
|
+
const [address, _] = web3_js.PublicKey.findProgramAddressSync(
|
|
5088
|
+
[CPI_AUTHORITY_SEED],
|
|
5089
|
+
this.programId,
|
|
5090
|
+
);
|
|
4726
5091
|
return address;
|
|
4727
5092
|
}
|
|
4728
5093
|
/**
|
|
@@ -4731,14 +5096,21 @@ class CompressedTokenProgram {
|
|
|
4731
5096
|
static async createMint(params) {
|
|
4732
5097
|
const { mint, authority, feePayer, rentExemptBalance } = params;
|
|
4733
5098
|
/// Create and initialize SPL Mint account
|
|
4734
|
-
const createMintAccountInstruction =
|
|
4735
|
-
|
|
4736
|
-
|
|
4737
|
-
|
|
4738
|
-
|
|
4739
|
-
|
|
4740
|
-
|
|
4741
|
-
|
|
5099
|
+
const createMintAccountInstruction =
|
|
5100
|
+
web3_js.SystemProgram.createAccount({
|
|
5101
|
+
fromPubkey: feePayer,
|
|
5102
|
+
lamports: rentExemptBalance,
|
|
5103
|
+
newAccountPubkey: mint,
|
|
5104
|
+
programId: TOKEN_PROGRAM_ID,
|
|
5105
|
+
space: MINT_SIZE,
|
|
5106
|
+
});
|
|
5107
|
+
const initializeMintInstruction = createInitializeMint2Instruction(
|
|
5108
|
+
mint,
|
|
5109
|
+
params.decimals,
|
|
5110
|
+
authority,
|
|
5111
|
+
params.freezeAuthority,
|
|
5112
|
+
TOKEN_PROGRAM_ID,
|
|
5113
|
+
);
|
|
4742
5114
|
const ix = await this.createTokenPool({
|
|
4743
5115
|
feePayer,
|
|
4744
5116
|
mint,
|
|
@@ -4755,13 +5127,13 @@ class CompressedTokenProgram {
|
|
|
4755
5127
|
const ix = await this.program.methods
|
|
4756
5128
|
.createTokenPool()
|
|
4757
5129
|
.accounts({
|
|
4758
|
-
|
|
4759
|
-
|
|
4760
|
-
|
|
4761
|
-
|
|
4762
|
-
|
|
4763
|
-
|
|
4764
|
-
|
|
5130
|
+
mint,
|
|
5131
|
+
feePayer,
|
|
5132
|
+
tokenPoolPda,
|
|
5133
|
+
systemProgram: web3_js.SystemProgram.programId,
|
|
5134
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5135
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5136
|
+
})
|
|
4765
5137
|
.instruction();
|
|
4766
5138
|
return ix;
|
|
4767
5139
|
}
|
|
@@ -4770,31 +5142,41 @@ class CompressedTokenProgram {
|
|
|
4770
5142
|
*/
|
|
4771
5143
|
static async mintTo(params) {
|
|
4772
5144
|
const systemKeys = stateless_js.defaultStaticAccountsStruct();
|
|
4773
|
-
const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
|
|
5145
|
+
const { mint, feePayer, authority, merkleTree, toPubkey, amount } =
|
|
5146
|
+
params;
|
|
4774
5147
|
const tokenPoolPda = this.deriveTokenPoolPda(mint);
|
|
4775
|
-
const amounts = stateless_js
|
|
5148
|
+
const amounts = stateless_js
|
|
5149
|
+
.toArray(amount)
|
|
5150
|
+
.map(amount => stateless_js.bn(amount));
|
|
4776
5151
|
const toPubkeys = stateless_js.toArray(toPubkey);
|
|
4777
5152
|
if (amounts.length !== toPubkeys.length) {
|
|
4778
|
-
throw new Error(
|
|
5153
|
+
throw new Error(
|
|
5154
|
+
'Amount and toPubkey arrays must have the same length',
|
|
5155
|
+
);
|
|
4779
5156
|
}
|
|
4780
5157
|
const instruction = await this.program.methods
|
|
4781
5158
|
.mintTo(toPubkeys, amounts, null)
|
|
4782
5159
|
.accounts({
|
|
4783
|
-
|
|
4784
|
-
|
|
4785
|
-
|
|
4786
|
-
|
|
4787
|
-
|
|
4788
|
-
|
|
4789
|
-
|
|
4790
|
-
|
|
4791
|
-
|
|
4792
|
-
|
|
4793
|
-
|
|
4794
|
-
|
|
4795
|
-
|
|
4796
|
-
|
|
4797
|
-
|
|
5160
|
+
feePayer,
|
|
5161
|
+
authority,
|
|
5162
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5163
|
+
mint,
|
|
5164
|
+
tokenPoolPda,
|
|
5165
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5166
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5167
|
+
registeredProgramPda: systemKeys.registeredProgramPda,
|
|
5168
|
+
noopProgram: systemKeys.noopProgram,
|
|
5169
|
+
accountCompressionAuthority:
|
|
5170
|
+
systemKeys.accountCompressionAuthority,
|
|
5171
|
+
accountCompressionProgram: systemKeys.accountCompressionProgram,
|
|
5172
|
+
merkleTree:
|
|
5173
|
+
merkleTree !== null && merkleTree !== void 0
|
|
5174
|
+
? merkleTree
|
|
5175
|
+
: stateless_js.defaultTestStateTreeAccounts()
|
|
5176
|
+
.merkleTree,
|
|
5177
|
+
selfProgram: this.programId,
|
|
5178
|
+
solPoolPda: null,
|
|
5179
|
+
})
|
|
4798
5180
|
.instruction();
|
|
4799
5181
|
return instruction;
|
|
4800
5182
|
}
|
|
@@ -4803,10 +5185,22 @@ class CompressedTokenProgram {
|
|
|
4803
5185
|
* Mint tokens from registed SPL mint account to a compressed account
|
|
4804
5186
|
*/
|
|
4805
5187
|
static async approveAndMintTo(params) {
|
|
4806
|
-
const {
|
|
5188
|
+
const {
|
|
5189
|
+
mint,
|
|
5190
|
+
feePayer,
|
|
5191
|
+
authorityTokenAccount,
|
|
5192
|
+
authority,
|
|
5193
|
+
merkleTree,
|
|
5194
|
+
toPubkey,
|
|
5195
|
+
} = params;
|
|
4807
5196
|
const amount = BigInt(params.amount.toString());
|
|
4808
5197
|
/// 1. Mint to existing ATA of mintAuthority.
|
|
4809
|
-
const splMintToInstruction = createMintToInstruction(
|
|
5198
|
+
const splMintToInstruction = createMintToInstruction(
|
|
5199
|
+
mint,
|
|
5200
|
+
authorityTokenAccount,
|
|
5201
|
+
authority,
|
|
5202
|
+
amount,
|
|
5203
|
+
);
|
|
4810
5204
|
/// 2. Compress from mint authority ATA to recipient compressed account
|
|
4811
5205
|
const compressInstruction = await this.compress({
|
|
4812
5206
|
payer: feePayer,
|
|
@@ -4823,15 +5217,33 @@ class CompressedTokenProgram {
|
|
|
4823
5217
|
* Construct transfer instruction for compressed tokens
|
|
4824
5218
|
*/
|
|
4825
5219
|
static async transfer(params) {
|
|
4826
|
-
const {
|
|
4827
|
-
|
|
4828
|
-
|
|
5220
|
+
const {
|
|
5221
|
+
payer,
|
|
5222
|
+
inputCompressedTokenAccounts,
|
|
5223
|
+
recentInputStateRootIndices,
|
|
5224
|
+
recentValidityProof,
|
|
5225
|
+
amount,
|
|
5226
|
+
outputStateTrees,
|
|
5227
|
+
toAddress,
|
|
5228
|
+
} = params;
|
|
5229
|
+
const tokenTransferOutputs = createTransferOutputState(
|
|
5230
|
+
inputCompressedTokenAccounts,
|
|
5231
|
+
toAddress,
|
|
5232
|
+
amount,
|
|
5233
|
+
);
|
|
5234
|
+
const {
|
|
5235
|
+
inputTokenDataWithContext,
|
|
5236
|
+
packedOutputTokenData,
|
|
5237
|
+
remainingAccountMetas,
|
|
5238
|
+
} = packCompressedTokenAccounts({
|
|
4829
5239
|
inputCompressedTokenAccounts,
|
|
4830
5240
|
outputStateTrees,
|
|
4831
5241
|
rootIndices: recentInputStateRootIndices,
|
|
4832
5242
|
tokenTransferOutputs,
|
|
4833
5243
|
});
|
|
4834
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5244
|
+
const { mint, currentOwner } = parseTokenData(
|
|
5245
|
+
inputCompressedTokenAccounts,
|
|
5246
|
+
);
|
|
4835
5247
|
const data = {
|
|
4836
5248
|
proof: recentValidityProof,
|
|
4837
5249
|
mint,
|
|
@@ -4843,24 +5255,32 @@ class CompressedTokenProgram {
|
|
|
4843
5255
|
cpiContext: null,
|
|
4844
5256
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4845
5257
|
};
|
|
4846
|
-
const encodedData = this.program.coder.types.encode(
|
|
4847
|
-
|
|
5258
|
+
const encodedData = this.program.coder.types.encode(
|
|
5259
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5260
|
+
data,
|
|
5261
|
+
);
|
|
5262
|
+
const {
|
|
5263
|
+
accountCompressionAuthority,
|
|
5264
|
+
noopProgram,
|
|
5265
|
+
registeredProgramPda,
|
|
5266
|
+
accountCompressionProgram,
|
|
5267
|
+
} = stateless_js.defaultStaticAccountsStruct();
|
|
4848
5268
|
const instruction = await this.program.methods
|
|
4849
5269
|
.transfer(encodedData)
|
|
4850
5270
|
.accounts({
|
|
4851
|
-
|
|
4852
|
-
|
|
4853
|
-
|
|
4854
|
-
|
|
4855
|
-
|
|
4856
|
-
|
|
4857
|
-
|
|
4858
|
-
|
|
4859
|
-
|
|
4860
|
-
|
|
4861
|
-
|
|
4862
|
-
|
|
4863
|
-
|
|
5271
|
+
feePayer: payer,
|
|
5272
|
+
authority: currentOwner,
|
|
5273
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5274
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5275
|
+
registeredProgramPda: registeredProgramPda,
|
|
5276
|
+
noopProgram: noopProgram,
|
|
5277
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
5278
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
5279
|
+
selfProgram: this.programId,
|
|
5280
|
+
tokenPoolPda: null,
|
|
5281
|
+
compressOrDecompressTokenAccount: null,
|
|
5282
|
+
tokenProgram: null,
|
|
5283
|
+
})
|
|
4864
5284
|
.remainingAccounts(remainingAccountMetas)
|
|
4865
5285
|
.instruction();
|
|
4866
5286
|
return instruction;
|
|
@@ -4869,12 +5289,14 @@ class CompressedTokenProgram {
|
|
|
4869
5289
|
* Create lookup table instructions for the token program's default accounts.
|
|
4870
5290
|
*/
|
|
4871
5291
|
static async createTokenProgramLookupTable(params) {
|
|
4872
|
-
const { authority, mints, recentSlot, payer, remainingAccounts } =
|
|
4873
|
-
|
|
4874
|
-
|
|
4875
|
-
|
|
4876
|
-
|
|
4877
|
-
|
|
5292
|
+
const { authority, mints, recentSlot, payer, remainingAccounts } =
|
|
5293
|
+
params;
|
|
5294
|
+
const [createInstruction, lookupTableAddress] =
|
|
5295
|
+
web3_js.AddressLookupTableProgram.createLookupTable({
|
|
5296
|
+
authority,
|
|
5297
|
+
payer: authority,
|
|
5298
|
+
recentSlot,
|
|
5299
|
+
});
|
|
4878
5300
|
let optionalMintKeys = [];
|
|
4879
5301
|
if (mints) {
|
|
4880
5302
|
optionalMintKeys = [
|
|
@@ -4882,28 +5304,35 @@ class CompressedTokenProgram {
|
|
|
4882
5304
|
...mints.map(mint => this.deriveTokenPoolPda(mint)),
|
|
4883
5305
|
];
|
|
4884
5306
|
}
|
|
4885
|
-
const extendInstruction =
|
|
4886
|
-
|
|
4887
|
-
|
|
4888
|
-
lookupTable: lookupTableAddress,
|
|
4889
|
-
addresses: [
|
|
4890
|
-
this.deriveCpiAuthorityPda,
|
|
4891
|
-
stateless_js.LightSystemProgram.programId,
|
|
4892
|
-
stateless_js.defaultStaticAccountsStruct().registeredProgramPda,
|
|
4893
|
-
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
4894
|
-
stateless_js.defaultStaticAccountsStruct().accountCompressionAuthority,
|
|
4895
|
-
stateless_js.defaultStaticAccountsStruct().accountCompressionProgram,
|
|
4896
|
-
stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
4897
|
-
stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
|
|
4898
|
-
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
4899
|
-
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
4900
|
-
this.programId,
|
|
4901
|
-
TOKEN_PROGRAM_ID,
|
|
5307
|
+
const extendInstruction =
|
|
5308
|
+
web3_js.AddressLookupTableProgram.extendLookupTable({
|
|
5309
|
+
payer,
|
|
4902
5310
|
authority,
|
|
4903
|
-
|
|
4904
|
-
|
|
4905
|
-
|
|
4906
|
-
|
|
5311
|
+
lookupTable: lookupTableAddress,
|
|
5312
|
+
addresses: [
|
|
5313
|
+
this.deriveCpiAuthorityPda,
|
|
5314
|
+
stateless_js.LightSystemProgram.programId,
|
|
5315
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5316
|
+
.registeredProgramPda,
|
|
5317
|
+
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
5318
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5319
|
+
.accountCompressionAuthority,
|
|
5320
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5321
|
+
.accountCompressionProgram,
|
|
5322
|
+
stateless_js.defaultTestStateTreeAccounts().merkleTree,
|
|
5323
|
+
stateless_js.defaultTestStateTreeAccounts().nullifierQueue,
|
|
5324
|
+
stateless_js.defaultTestStateTreeAccounts().addressTree,
|
|
5325
|
+
stateless_js.defaultTestStateTreeAccounts().addressQueue,
|
|
5326
|
+
this.programId,
|
|
5327
|
+
TOKEN_PROGRAM_ID,
|
|
5328
|
+
authority,
|
|
5329
|
+
...optionalMintKeys,
|
|
5330
|
+
...(remainingAccounts !== null &&
|
|
5331
|
+
remainingAccounts !== void 0
|
|
5332
|
+
? remainingAccounts
|
|
5333
|
+
: []),
|
|
5334
|
+
],
|
|
5335
|
+
});
|
|
4907
5336
|
return {
|
|
4908
5337
|
instructions: [createInstruction, extendInstruction],
|
|
4909
5338
|
address: lookupTableAddress,
|
|
@@ -4914,14 +5343,19 @@ class CompressedTokenProgram {
|
|
|
4914
5343
|
* @returns compressInstruction
|
|
4915
5344
|
*/
|
|
4916
5345
|
static async compress(params) {
|
|
4917
|
-
const { payer, owner, source, toAddress, mint, outputStateTree } =
|
|
5346
|
+
const { payer, owner, source, toAddress, mint, outputStateTree } =
|
|
5347
|
+
params;
|
|
4918
5348
|
if (Array.isArray(params.amount) !== Array.isArray(params.toAddress)) {
|
|
4919
|
-
throw new Error(
|
|
5349
|
+
throw new Error(
|
|
5350
|
+
'Both amount and toAddress must be arrays or both must be single values',
|
|
5351
|
+
);
|
|
4920
5352
|
}
|
|
4921
5353
|
let tokenTransferOutputs;
|
|
4922
5354
|
if (Array.isArray(params.amount) && Array.isArray(params.toAddress)) {
|
|
4923
5355
|
if (params.amount.length !== params.toAddress.length) {
|
|
4924
|
-
throw new Error(
|
|
5356
|
+
throw new Error(
|
|
5357
|
+
'Amount and toAddress arrays must have the same length',
|
|
5358
|
+
);
|
|
4925
5359
|
}
|
|
4926
5360
|
tokenTransferOutputs = params.amount.map((amt, index) => {
|
|
4927
5361
|
const amount = stateless_js.bn(amt);
|
|
@@ -4932,8 +5366,7 @@ class CompressedTokenProgram {
|
|
|
4932
5366
|
tlv: null,
|
|
4933
5367
|
};
|
|
4934
5368
|
});
|
|
4935
|
-
}
|
|
4936
|
-
else {
|
|
5369
|
+
} else {
|
|
4937
5370
|
tokenTransferOutputs = [
|
|
4938
5371
|
{
|
|
4939
5372
|
owner: toAddress,
|
|
@@ -4943,7 +5376,11 @@ class CompressedTokenProgram {
|
|
|
4943
5376
|
},
|
|
4944
5377
|
];
|
|
4945
5378
|
}
|
|
4946
|
-
const {
|
|
5379
|
+
const {
|
|
5380
|
+
inputTokenDataWithContext,
|
|
5381
|
+
packedOutputTokenData,
|
|
5382
|
+
remainingAccountMetas,
|
|
5383
|
+
} = packCompressedTokenAccounts({
|
|
4947
5384
|
inputCompressedTokenAccounts: [],
|
|
4948
5385
|
outputStateTrees: outputStateTree,
|
|
4949
5386
|
rootIndices: [],
|
|
@@ -4957,30 +5394,40 @@ class CompressedTokenProgram {
|
|
|
4957
5394
|
outputCompressedAccounts: packedOutputTokenData,
|
|
4958
5395
|
compressOrDecompressAmount: Array.isArray(params.amount)
|
|
4959
5396
|
? params.amount
|
|
4960
|
-
|
|
4961
|
-
|
|
5397
|
+
.map(amt => new anchor.BN(amt))
|
|
5398
|
+
.reduce((sum, amt) => sum.add(amt), new anchor.BN(0))
|
|
4962
5399
|
: new anchor.BN(params.amount),
|
|
4963
5400
|
isCompress: true,
|
|
4964
5401
|
cpiContext: null,
|
|
4965
5402
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
4966
5403
|
};
|
|
4967
|
-
const encodedData = this.program.coder.types.encode(
|
|
5404
|
+
const encodedData = this.program.coder.types.encode(
|
|
5405
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5406
|
+
data,
|
|
5407
|
+
);
|
|
4968
5408
|
const instruction = await this.program.methods
|
|
4969
5409
|
.transfer(encodedData)
|
|
4970
5410
|
.accounts({
|
|
4971
|
-
|
|
4972
|
-
|
|
4973
|
-
|
|
4974
|
-
|
|
4975
|
-
|
|
4976
|
-
|
|
4977
|
-
|
|
4978
|
-
|
|
4979
|
-
|
|
4980
|
-
|
|
4981
|
-
|
|
4982
|
-
|
|
4983
|
-
|
|
5411
|
+
feePayer: payer,
|
|
5412
|
+
authority: owner,
|
|
5413
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5414
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5415
|
+
registeredProgramPda:
|
|
5416
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5417
|
+
.registeredProgramPda,
|
|
5418
|
+
noopProgram:
|
|
5419
|
+
stateless_js.defaultStaticAccountsStruct().noopProgram,
|
|
5420
|
+
accountCompressionAuthority:
|
|
5421
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5422
|
+
.accountCompressionAuthority,
|
|
5423
|
+
accountCompressionProgram:
|
|
5424
|
+
stateless_js.defaultStaticAccountsStruct()
|
|
5425
|
+
.accountCompressionProgram,
|
|
5426
|
+
selfProgram: this.programId,
|
|
5427
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5428
|
+
compressOrDecompressTokenAccount: source, // token
|
|
5429
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5430
|
+
})
|
|
4984
5431
|
.remainingAccounts(remainingAccountMetas)
|
|
4985
5432
|
.instruction();
|
|
4986
5433
|
return instruction;
|
|
@@ -4989,17 +5436,33 @@ class CompressedTokenProgram {
|
|
|
4989
5436
|
* Construct decompress instruction
|
|
4990
5437
|
*/
|
|
4991
5438
|
static async decompress(params) {
|
|
4992
|
-
const {
|
|
5439
|
+
const {
|
|
5440
|
+
payer,
|
|
5441
|
+
inputCompressedTokenAccounts,
|
|
5442
|
+
toAddress,
|
|
5443
|
+
outputStateTree,
|
|
5444
|
+
recentValidityProof,
|
|
5445
|
+
recentInputStateRootIndices,
|
|
5446
|
+
} = params;
|
|
4993
5447
|
const amount = stateless_js.bn(params.amount);
|
|
4994
|
-
const tokenTransferOutputs = createDecompressOutputState(
|
|
5448
|
+
const tokenTransferOutputs = createDecompressOutputState(
|
|
5449
|
+
inputCompressedTokenAccounts,
|
|
5450
|
+
amount,
|
|
5451
|
+
);
|
|
4995
5452
|
/// Pack
|
|
4996
|
-
const {
|
|
5453
|
+
const {
|
|
5454
|
+
inputTokenDataWithContext,
|
|
5455
|
+
packedOutputTokenData,
|
|
5456
|
+
remainingAccountMetas,
|
|
5457
|
+
} = packCompressedTokenAccounts({
|
|
4997
5458
|
inputCompressedTokenAccounts,
|
|
4998
5459
|
outputStateTrees: outputStateTree,
|
|
4999
5460
|
rootIndices: recentInputStateRootIndices,
|
|
5000
5461
|
tokenTransferOutputs: tokenTransferOutputs,
|
|
5001
5462
|
});
|
|
5002
|
-
const { mint, currentOwner } = parseTokenData(
|
|
5463
|
+
const { mint, currentOwner } = parseTokenData(
|
|
5464
|
+
inputCompressedTokenAccounts,
|
|
5465
|
+
);
|
|
5003
5466
|
const data = {
|
|
5004
5467
|
proof: recentValidityProof,
|
|
5005
5468
|
mint,
|
|
@@ -5011,33 +5474,69 @@ class CompressedTokenProgram {
|
|
|
5011
5474
|
cpiContext: null,
|
|
5012
5475
|
lamportsChangeAccountMerkleTreeIndex: null,
|
|
5013
5476
|
};
|
|
5014
|
-
const encodedData = this.program.coder.types.encode(
|
|
5015
|
-
|
|
5477
|
+
const encodedData = this.program.coder.types.encode(
|
|
5478
|
+
'CompressedTokenInstructionDataTransfer',
|
|
5479
|
+
data,
|
|
5480
|
+
);
|
|
5481
|
+
const {
|
|
5482
|
+
accountCompressionAuthority,
|
|
5483
|
+
noopProgram,
|
|
5484
|
+
registeredProgramPda,
|
|
5485
|
+
accountCompressionProgram,
|
|
5486
|
+
} = stateless_js.defaultStaticAccountsStruct();
|
|
5016
5487
|
const instruction = await this.program.methods
|
|
5017
5488
|
.transfer(encodedData)
|
|
5018
5489
|
.accounts({
|
|
5019
|
-
|
|
5020
|
-
|
|
5021
|
-
|
|
5022
|
-
|
|
5023
|
-
|
|
5024
|
-
|
|
5025
|
-
|
|
5026
|
-
|
|
5027
|
-
|
|
5028
|
-
|
|
5029
|
-
|
|
5030
|
-
|
|
5031
|
-
|
|
5490
|
+
feePayer: payer,
|
|
5491
|
+
authority: currentOwner,
|
|
5492
|
+
cpiAuthorityPda: this.deriveCpiAuthorityPda,
|
|
5493
|
+
lightSystemProgram: stateless_js.LightSystemProgram.programId,
|
|
5494
|
+
registeredProgramPda: registeredProgramPda,
|
|
5495
|
+
noopProgram: noopProgram,
|
|
5496
|
+
accountCompressionAuthority: accountCompressionAuthority,
|
|
5497
|
+
accountCompressionProgram: accountCompressionProgram,
|
|
5498
|
+
selfProgram: this.programId,
|
|
5499
|
+
tokenPoolPda: this.deriveTokenPoolPda(mint),
|
|
5500
|
+
compressOrDecompressTokenAccount: toAddress,
|
|
5501
|
+
tokenProgram: TOKEN_PROGRAM_ID,
|
|
5502
|
+
})
|
|
5032
5503
|
.remainingAccounts(remainingAccountMetas)
|
|
5033
5504
|
.instruction();
|
|
5034
5505
|
return instruction;
|
|
5035
5506
|
}
|
|
5507
|
+
static async mergeTokenAccounts(params) {
|
|
5508
|
+
const {
|
|
5509
|
+
payer,
|
|
5510
|
+
owner,
|
|
5511
|
+
inputCompressedTokenAccounts,
|
|
5512
|
+
outputStateTree,
|
|
5513
|
+
recentValidityProof,
|
|
5514
|
+
recentInputStateRootIndices,
|
|
5515
|
+
} = params;
|
|
5516
|
+
if (inputCompressedTokenAccounts.length > 3) {
|
|
5517
|
+
throw new Error('Cannot merge more than 3 token accounts at once');
|
|
5518
|
+
}
|
|
5519
|
+
const ix = await this.transfer({
|
|
5520
|
+
payer,
|
|
5521
|
+
inputCompressedTokenAccounts,
|
|
5522
|
+
toAddress: owner,
|
|
5523
|
+
amount: inputCompressedTokenAccounts.reduce(
|
|
5524
|
+
(sum, account) => sum.add(account.parsed.amount),
|
|
5525
|
+
new anchor.BN(0),
|
|
5526
|
+
),
|
|
5527
|
+
outputStateTrees: outputStateTree,
|
|
5528
|
+
recentInputStateRootIndices,
|
|
5529
|
+
recentValidityProof,
|
|
5530
|
+
});
|
|
5531
|
+
return [ix];
|
|
5532
|
+
}
|
|
5036
5533
|
}
|
|
5037
5534
|
/**
|
|
5038
5535
|
* Public key that identifies the CompressedPda program
|
|
5039
5536
|
*/
|
|
5040
|
-
CompressedTokenProgram.programId = new web3_js.PublicKey(
|
|
5537
|
+
CompressedTokenProgram.programId = new web3_js.PublicKey(
|
|
5538
|
+
'cTokenmWW8bLPjZEBAUgYy3zKxQZW6VKi7bqNFEVv3m',
|
|
5539
|
+
);
|
|
5041
5540
|
CompressedTokenProgram._program = null;
|
|
5042
5541
|
|
|
5043
5542
|
/**
|
|
@@ -5055,8 +5554,22 @@ CompressedTokenProgram._program = null;
|
|
|
5055
5554
|
*
|
|
5056
5555
|
* @return Signature of the confirmed transaction
|
|
5057
5556
|
*/
|
|
5058
|
-
async function approveAndMintTo(
|
|
5059
|
-
|
|
5557
|
+
async function approveAndMintTo(
|
|
5558
|
+
rpc,
|
|
5559
|
+
payer,
|
|
5560
|
+
mint,
|
|
5561
|
+
destination,
|
|
5562
|
+
authority,
|
|
5563
|
+
amount,
|
|
5564
|
+
merkleTree,
|
|
5565
|
+
confirmOptions,
|
|
5566
|
+
) {
|
|
5567
|
+
const authorityTokenAccount = await getOrCreateAssociatedTokenAccount(
|
|
5568
|
+
rpc,
|
|
5569
|
+
payer,
|
|
5570
|
+
mint,
|
|
5571
|
+
authority.publicKey,
|
|
5572
|
+
);
|
|
5060
5573
|
const ixs = await CompressedTokenProgram.approveAndMintTo({
|
|
5061
5574
|
feePayer: payer.publicKey,
|
|
5062
5575
|
mint,
|
|
@@ -5068,10 +5581,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
5068
5581
|
});
|
|
5069
5582
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5070
5583
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5071
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5072
|
-
|
|
5073
|
-
|
|
5074
|
-
|
|
5584
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5585
|
+
[
|
|
5586
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5587
|
+
units: 1000000,
|
|
5588
|
+
}),
|
|
5589
|
+
...ixs,
|
|
5590
|
+
],
|
|
5591
|
+
payer,
|
|
5592
|
+
blockhash,
|
|
5593
|
+
additionalSigners,
|
|
5594
|
+
);
|
|
5075
5595
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5076
5596
|
return txId;
|
|
5077
5597
|
}
|
|
@@ -5094,7 +5614,17 @@ async function approveAndMintTo(rpc, payer, mint, destination, authority, amount
|
|
|
5094
5614
|
*
|
|
5095
5615
|
* @return Signature of the confirmed transaction
|
|
5096
5616
|
*/
|
|
5097
|
-
async function compress(
|
|
5617
|
+
async function compress(
|
|
5618
|
+
rpc,
|
|
5619
|
+
payer,
|
|
5620
|
+
mint,
|
|
5621
|
+
amount,
|
|
5622
|
+
owner,
|
|
5623
|
+
sourceTokenAccount,
|
|
5624
|
+
toAddress,
|
|
5625
|
+
merkleTree,
|
|
5626
|
+
confirmOptions,
|
|
5627
|
+
) {
|
|
5098
5628
|
const compressIx = await CompressedTokenProgram.compress({
|
|
5099
5629
|
payer: payer.publicKey,
|
|
5100
5630
|
owner: owner.publicKey,
|
|
@@ -5106,13 +5636,23 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
|
|
|
5106
5636
|
});
|
|
5107
5637
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5108
5638
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5109
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5110
|
-
|
|
5111
|
-
|
|
5112
|
-
|
|
5113
|
-
|
|
5114
|
-
|
|
5115
|
-
|
|
5639
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5640
|
+
[
|
|
5641
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5642
|
+
units: 1000000,
|
|
5643
|
+
}),
|
|
5644
|
+
compressIx,
|
|
5645
|
+
],
|
|
5646
|
+
payer,
|
|
5647
|
+
blockhashCtx.blockhash,
|
|
5648
|
+
additionalSigners,
|
|
5649
|
+
);
|
|
5650
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5651
|
+
rpc,
|
|
5652
|
+
signedTx,
|
|
5653
|
+
confirmOptions,
|
|
5654
|
+
blockhashCtx,
|
|
5655
|
+
);
|
|
5116
5656
|
return txId;
|
|
5117
5657
|
}
|
|
5118
5658
|
|
|
@@ -5133,15 +5673,33 @@ async function compress(rpc, payer, mint, amount, owner, sourceTokenAccount, toA
|
|
|
5133
5673
|
*
|
|
5134
5674
|
* @return Signature of the confirmed transaction
|
|
5135
5675
|
*/
|
|
5136
|
-
async function transfer(
|
|
5137
|
-
|
|
5138
|
-
|
|
5676
|
+
async function transfer(
|
|
5677
|
+
rpc,
|
|
5678
|
+
payer,
|
|
5679
|
+
mint,
|
|
5680
|
+
amount,
|
|
5681
|
+
owner,
|
|
5682
|
+
toAddress,
|
|
5683
|
+
/// TODO: allow multiple
|
|
5684
|
+
merkleTree,
|
|
5685
|
+
confirmOptions,
|
|
5686
|
+
) {
|
|
5139
5687
|
amount = stateless_js.bn(amount);
|
|
5140
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5141
|
-
|
|
5142
|
-
|
|
5143
|
-
|
|
5144
|
-
|
|
5688
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5689
|
+
owner.publicKey,
|
|
5690
|
+
{
|
|
5691
|
+
mint,
|
|
5692
|
+
},
|
|
5693
|
+
);
|
|
5694
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5695
|
+
compressedTokenAccounts.items,
|
|
5696
|
+
amount,
|
|
5697
|
+
);
|
|
5698
|
+
const proof = await rpc.getValidityProof(
|
|
5699
|
+
inputAccounts.map(account =>
|
|
5700
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5701
|
+
),
|
|
5702
|
+
);
|
|
5145
5703
|
const ix = await CompressedTokenProgram.transfer({
|
|
5146
5704
|
payer: payer.publicKey,
|
|
5147
5705
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5153,8 +5711,22 @@ merkleTree, confirmOptions) {
|
|
|
5153
5711
|
});
|
|
5154
5712
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5155
5713
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5156
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5157
|
-
|
|
5714
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5715
|
+
[
|
|
5716
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5717
|
+
units: 1000000,
|
|
5718
|
+
}),
|
|
5719
|
+
ix,
|
|
5720
|
+
],
|
|
5721
|
+
payer,
|
|
5722
|
+
blockhash,
|
|
5723
|
+
additionalSigners,
|
|
5724
|
+
);
|
|
5725
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5726
|
+
rpc,
|
|
5727
|
+
signedTx,
|
|
5728
|
+
confirmOptions,
|
|
5729
|
+
);
|
|
5158
5730
|
return txId;
|
|
5159
5731
|
}
|
|
5160
5732
|
/**
|
|
@@ -5170,14 +5742,17 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5170
5742
|
const selectedAccounts = [];
|
|
5171
5743
|
accounts.sort((a, b) => b.parsed.amount.cmp(a.parsed.amount));
|
|
5172
5744
|
for (const account of accounts) {
|
|
5173
|
-
if (accumulatedAmount.gte(stateless_js.bn(transferAmount)))
|
|
5174
|
-
break;
|
|
5745
|
+
if (accumulatedAmount.gte(stateless_js.bn(transferAmount))) break;
|
|
5175
5746
|
accumulatedAmount = accumulatedAmount.add(account.parsed.amount);
|
|
5176
|
-
accumulatedLamports = accumulatedLamports.add(
|
|
5747
|
+
accumulatedLamports = accumulatedLamports.add(
|
|
5748
|
+
account.compressedAccount.lamports,
|
|
5749
|
+
);
|
|
5177
5750
|
selectedAccounts.push(account);
|
|
5178
5751
|
}
|
|
5179
5752
|
if (accumulatedAmount.lt(stateless_js.bn(transferAmount))) {
|
|
5180
|
-
throw new Error(
|
|
5753
|
+
throw new Error(
|
|
5754
|
+
`Not enough balance for transfer. Required: ${transferAmount.toString()}, available: ${accumulatedAmount.toString()}`,
|
|
5755
|
+
);
|
|
5181
5756
|
}
|
|
5182
5757
|
return [
|
|
5183
5758
|
selectedAccounts,
|
|
@@ -5204,16 +5779,34 @@ function selectMinCompressedTokenAccountsForTransfer(accounts, transferAmount) {
|
|
|
5204
5779
|
*
|
|
5205
5780
|
* @return Signature of the confirmed transaction
|
|
5206
5781
|
*/
|
|
5207
|
-
async function decompress(
|
|
5208
|
-
|
|
5209
|
-
|
|
5782
|
+
async function decompress(
|
|
5783
|
+
rpc,
|
|
5784
|
+
payer,
|
|
5785
|
+
mint,
|
|
5786
|
+
amount,
|
|
5787
|
+
owner,
|
|
5788
|
+
toAddress,
|
|
5789
|
+
/// TODO: allow multiple
|
|
5790
|
+
merkleTree,
|
|
5791
|
+
confirmOptions,
|
|
5792
|
+
) {
|
|
5210
5793
|
amount = stateless_js.bn(amount);
|
|
5211
|
-
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5212
|
-
|
|
5213
|
-
|
|
5794
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5795
|
+
owner.publicKey,
|
|
5796
|
+
{
|
|
5797
|
+
mint,
|
|
5798
|
+
},
|
|
5799
|
+
);
|
|
5214
5800
|
/// TODO: consider using a different selection algorithm
|
|
5215
|
-
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5216
|
-
|
|
5801
|
+
const [inputAccounts] = selectMinCompressedTokenAccountsForTransfer(
|
|
5802
|
+
compressedTokenAccounts.items,
|
|
5803
|
+
amount,
|
|
5804
|
+
);
|
|
5805
|
+
const proof = await rpc.getValidityProof(
|
|
5806
|
+
inputAccounts.map(account =>
|
|
5807
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5808
|
+
),
|
|
5809
|
+
);
|
|
5217
5810
|
const ix = await CompressedTokenProgram.decompress({
|
|
5218
5811
|
payer: payer.publicKey,
|
|
5219
5812
|
inputCompressedTokenAccounts: inputAccounts,
|
|
@@ -5225,8 +5818,22 @@ merkleTree, confirmOptions) {
|
|
|
5225
5818
|
});
|
|
5226
5819
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5227
5820
|
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5228
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
5229
|
-
|
|
5821
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5822
|
+
[
|
|
5823
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5824
|
+
units: 1000000,
|
|
5825
|
+
}),
|
|
5826
|
+
ix,
|
|
5827
|
+
],
|
|
5828
|
+
payer,
|
|
5829
|
+
blockhash,
|
|
5830
|
+
additionalSigners,
|
|
5831
|
+
);
|
|
5832
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
5833
|
+
rpc,
|
|
5834
|
+
signedTx,
|
|
5835
|
+
confirmOptions,
|
|
5836
|
+
);
|
|
5230
5837
|
return txId;
|
|
5231
5838
|
}
|
|
5232
5839
|
|
|
@@ -5242,8 +5849,16 @@ merkleTree, confirmOptions) {
|
|
|
5242
5849
|
*
|
|
5243
5850
|
* @return Address of the new mint and the transaction signature
|
|
5244
5851
|
*/
|
|
5245
|
-
async function createMint(
|
|
5246
|
-
|
|
5852
|
+
async function createMint(
|
|
5853
|
+
rpc,
|
|
5854
|
+
payer,
|
|
5855
|
+
mintAuthority,
|
|
5856
|
+
decimals,
|
|
5857
|
+
keypair = web3_js.Keypair.generate(),
|
|
5858
|
+
confirmOptions,
|
|
5859
|
+
) {
|
|
5860
|
+
const rentExemptBalance =
|
|
5861
|
+
await rpc.getMinimumBalanceForRentExemption(MINT_SIZE);
|
|
5247
5862
|
const ixs = await CompressedTokenProgram.createMint({
|
|
5248
5863
|
feePayer: payer.publicKey,
|
|
5249
5864
|
mint: keypair.publicKey,
|
|
@@ -5254,7 +5869,12 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5254
5869
|
});
|
|
5255
5870
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5256
5871
|
const additionalSigners = stateless_js.dedupeSigner(payer, [keypair]);
|
|
5257
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5872
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5873
|
+
ixs,
|
|
5874
|
+
payer,
|
|
5875
|
+
blockhash,
|
|
5876
|
+
additionalSigners,
|
|
5877
|
+
);
|
|
5258
5878
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5259
5879
|
return { mint: keypair.publicKey, transactionSignature: txId };
|
|
5260
5880
|
}
|
|
@@ -5276,7 +5896,16 @@ async function createMint(rpc, payer, mintAuthority, decimals, keypair = web3_js
|
|
|
5276
5896
|
*
|
|
5277
5897
|
* @return Signature of the confirmed transaction
|
|
5278
5898
|
*/
|
|
5279
|
-
async function mintTo(
|
|
5899
|
+
async function mintTo(
|
|
5900
|
+
rpc,
|
|
5901
|
+
payer,
|
|
5902
|
+
mint,
|
|
5903
|
+
destination,
|
|
5904
|
+
authority,
|
|
5905
|
+
amount,
|
|
5906
|
+
merkleTree,
|
|
5907
|
+
confirmOptions,
|
|
5908
|
+
) {
|
|
5280
5909
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5281
5910
|
const ix = await CompressedTokenProgram.mintTo({
|
|
5282
5911
|
feePayer: payer.publicKey,
|
|
@@ -5287,11 +5916,98 @@ async function mintTo(rpc, payer, mint, destination, authority, amount, merkleTr
|
|
|
5287
5916
|
merkleTree,
|
|
5288
5917
|
});
|
|
5289
5918
|
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5290
|
-
const tx = stateless_js.buildAndSignTx(
|
|
5919
|
+
const tx = stateless_js.buildAndSignTx(
|
|
5920
|
+
[
|
|
5921
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({
|
|
5922
|
+
units: 1000000,
|
|
5923
|
+
}),
|
|
5924
|
+
ix,
|
|
5925
|
+
],
|
|
5926
|
+
payer,
|
|
5927
|
+
blockhash,
|
|
5928
|
+
additionalSigners,
|
|
5929
|
+
);
|
|
5291
5930
|
const txId = await stateless_js.sendAndConfirmTx(rpc, tx, confirmOptions);
|
|
5292
5931
|
return txId;
|
|
5293
5932
|
}
|
|
5294
5933
|
|
|
5934
|
+
/**
|
|
5935
|
+
* Merge multiple compressed token accounts for a given mint into a single
|
|
5936
|
+
* account
|
|
5937
|
+
*
|
|
5938
|
+
* @param rpc RPC to use
|
|
5939
|
+
* @param payer Payer of the transaction fees
|
|
5940
|
+
* @param mint Public key of the token's mint
|
|
5941
|
+
* @param owner Owner of the token accounts to be merged
|
|
5942
|
+
* @param merkleTree Optional merkle tree for compressed tokens
|
|
5943
|
+
* @param confirmOptions Options for confirming the transaction
|
|
5944
|
+
*
|
|
5945
|
+
* @return Array of transaction signatures
|
|
5946
|
+
*/
|
|
5947
|
+
async function mergeTokenAccounts(
|
|
5948
|
+
rpc,
|
|
5949
|
+
payer,
|
|
5950
|
+
mint,
|
|
5951
|
+
owner,
|
|
5952
|
+
merkleTree,
|
|
5953
|
+
confirmOptions,
|
|
5954
|
+
) {
|
|
5955
|
+
const compressedTokenAccounts = await rpc.getCompressedTokenAccountsByOwner(
|
|
5956
|
+
owner.publicKey,
|
|
5957
|
+
{ mint },
|
|
5958
|
+
);
|
|
5959
|
+
if (compressedTokenAccounts.items.length === 0) {
|
|
5960
|
+
throw new Error(
|
|
5961
|
+
`No compressed token accounts found for mint ${mint.toBase58()}`,
|
|
5962
|
+
);
|
|
5963
|
+
}
|
|
5964
|
+
if (compressedTokenAccounts.items.length >= 6) {
|
|
5965
|
+
throw new Error(
|
|
5966
|
+
`Too many compressed token accounts used for mint ${mint.toBase58()}`,
|
|
5967
|
+
);
|
|
5968
|
+
}
|
|
5969
|
+
const instructions = [
|
|
5970
|
+
web3_js.ComputeBudgetProgram.setComputeUnitLimit({ units: 1000000 }),
|
|
5971
|
+
];
|
|
5972
|
+
for (
|
|
5973
|
+
let i = 0;
|
|
5974
|
+
i < compressedTokenAccounts.items.slice(0, 6).length;
|
|
5975
|
+
i += 3
|
|
5976
|
+
) {
|
|
5977
|
+
const batch = compressedTokenAccounts.items.slice(i, i + 3);
|
|
5978
|
+
const proof = await rpc.getValidityProof(
|
|
5979
|
+
batch.map(account =>
|
|
5980
|
+
stateless_js.bn(account.compressedAccount.hash),
|
|
5981
|
+
),
|
|
5982
|
+
);
|
|
5983
|
+
const batchInstructions =
|
|
5984
|
+
await CompressedTokenProgram.mergeTokenAccounts({
|
|
5985
|
+
payer: payer.publicKey,
|
|
5986
|
+
owner: owner.publicKey,
|
|
5987
|
+
mint,
|
|
5988
|
+
inputCompressedTokenAccounts: batch,
|
|
5989
|
+
outputStateTree: merkleTree,
|
|
5990
|
+
recentValidityProof: proof.compressedProof,
|
|
5991
|
+
recentInputStateRootIndices: proof.rootIndices,
|
|
5992
|
+
});
|
|
5993
|
+
instructions.push(...batchInstructions);
|
|
5994
|
+
}
|
|
5995
|
+
const { blockhash } = await rpc.getLatestBlockhash();
|
|
5996
|
+
const additionalSigners = stateless_js.dedupeSigner(payer, [owner]);
|
|
5997
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
5998
|
+
instructions,
|
|
5999
|
+
payer,
|
|
6000
|
+
blockhash,
|
|
6001
|
+
additionalSigners,
|
|
6002
|
+
);
|
|
6003
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
6004
|
+
rpc,
|
|
6005
|
+
signedTx,
|
|
6006
|
+
confirmOptions,
|
|
6007
|
+
);
|
|
6008
|
+
return txId;
|
|
6009
|
+
}
|
|
6010
|
+
|
|
5295
6011
|
/**
|
|
5296
6012
|
* Register an existing mint with the CompressedToken program
|
|
5297
6013
|
*
|
|
@@ -5327,23 +6043,50 @@ async function createTokenPool(rpc, payer, mintAddress, confirmOptions) {
|
|
|
5327
6043
|
*
|
|
5328
6044
|
* @return Transaction signatures and the address of the created lookup table
|
|
5329
6045
|
*/
|
|
5330
|
-
async function createTokenProgramLookupTable(
|
|
6046
|
+
async function createTokenProgramLookupTable(
|
|
6047
|
+
rpc,
|
|
6048
|
+
payer,
|
|
6049
|
+
authority,
|
|
6050
|
+
mints,
|
|
6051
|
+
additionalAccounts,
|
|
6052
|
+
) {
|
|
5331
6053
|
const recentSlot = await rpc.getSlot('finalized');
|
|
5332
|
-
const { instructions, address } =
|
|
5333
|
-
|
|
5334
|
-
|
|
5335
|
-
|
|
5336
|
-
|
|
5337
|
-
|
|
5338
|
-
|
|
6054
|
+
const { instructions, address } =
|
|
6055
|
+
await CompressedTokenProgram.createTokenProgramLookupTable({
|
|
6056
|
+
payer: payer.publicKey,
|
|
6057
|
+
authority: authority.publicKey,
|
|
6058
|
+
mints,
|
|
6059
|
+
remainingAccounts: additionalAccounts,
|
|
6060
|
+
recentSlot,
|
|
6061
|
+
});
|
|
5339
6062
|
const additionalSigners = stateless_js.dedupeSigner(payer, [authority]);
|
|
5340
6063
|
const blockhashCtx = await rpc.getLatestBlockhash();
|
|
5341
|
-
const signedTx = stateless_js.buildAndSignTx(
|
|
6064
|
+
const signedTx = stateless_js.buildAndSignTx(
|
|
6065
|
+
[instructions[0]],
|
|
6066
|
+
payer,
|
|
6067
|
+
blockhashCtx.blockhash,
|
|
6068
|
+
additionalSigners,
|
|
6069
|
+
);
|
|
5342
6070
|
/// Must wait for the first instruction to be finalized.
|
|
5343
|
-
const txId = await stateless_js.sendAndConfirmTx(
|
|
6071
|
+
const txId = await stateless_js.sendAndConfirmTx(
|
|
6072
|
+
rpc,
|
|
6073
|
+
signedTx,
|
|
6074
|
+
{ commitment: 'finalized' },
|
|
6075
|
+
blockhashCtx,
|
|
6076
|
+
);
|
|
5344
6077
|
const blockhashCtx2 = await rpc.getLatestBlockhash();
|
|
5345
|
-
const signedTx2 = stateless_js.buildAndSignTx(
|
|
5346
|
-
|
|
6078
|
+
const signedTx2 = stateless_js.buildAndSignTx(
|
|
6079
|
+
[instructions[1]],
|
|
6080
|
+
payer,
|
|
6081
|
+
blockhashCtx2.blockhash,
|
|
6082
|
+
additionalSigners,
|
|
6083
|
+
);
|
|
6084
|
+
const txId2 = await stateless_js.sendAndConfirmTx(
|
|
6085
|
+
rpc,
|
|
6086
|
+
signedTx2,
|
|
6087
|
+
{ commitment: 'finalized' },
|
|
6088
|
+
blockhashCtx2,
|
|
6089
|
+
);
|
|
5347
6090
|
return { txIds: [txId, txId2], address };
|
|
5348
6091
|
}
|
|
5349
6092
|
|
|
@@ -5360,10 +6103,12 @@ exports.createTokenPool = createTokenPool;
|
|
|
5360
6103
|
exports.createTokenProgramLookupTable = createTokenProgramLookupTable;
|
|
5361
6104
|
exports.createTransferOutputState = createTransferOutputState;
|
|
5362
6105
|
exports.decompress = decompress;
|
|
6106
|
+
exports.mergeTokenAccounts = mergeTokenAccounts;
|
|
5363
6107
|
exports.mintTo = mintTo;
|
|
5364
6108
|
exports.packCompressedTokenAccounts = packCompressedTokenAccounts;
|
|
5365
6109
|
exports.parseTokenData = parseTokenData;
|
|
5366
|
-
exports.selectMinCompressedTokenAccountsForTransfer =
|
|
6110
|
+
exports.selectMinCompressedTokenAccountsForTransfer =
|
|
6111
|
+
selectMinCompressedTokenAccountsForTransfer;
|
|
5367
6112
|
exports.sumUpTokenAmount = sumUpTokenAmount;
|
|
5368
6113
|
exports.transfer = transfer;
|
|
5369
6114
|
exports.validateSameTokenOwner = validateSameTokenOwner;
|