warthog-ts 0.1.21
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/LICENSE +21 -0
- package/README.md +300 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +9 -0
- package/dist/index.js.map +1 -0
- package/dist/src/tests/account.test.d.ts +2 -0
- package/dist/src/tests/account.test.d.ts.map +1 -0
- package/dist/src/tests/account.test.js +32 -0
- package/dist/src/tests/account.test.js.map +1 -0
- package/dist/src/tests/funds.test.d.ts +2 -0
- package/dist/src/tests/funds.test.d.ts.map +1 -0
- package/dist/src/tests/funds.test.js +161 -0
- package/dist/src/tests/funds.test.js.map +1 -0
- package/dist/src/tests/hdwallet.test.d.ts +2 -0
- package/dist/src/tests/hdwallet.test.d.ts.map +1 -0
- package/dist/src/tests/hdwallet.test.js +26 -0
- package/dist/src/tests/hdwallet.test.js.map +1 -0
- package/dist/src/tests/price.test.d.ts +2 -0
- package/dist/src/tests/price.test.d.ts.map +1 -0
- package/dist/src/tests/price.test.js +40 -0
- package/dist/src/tests/price.test.js.map +1 -0
- package/dist/src/types/Account.d.ts +44 -0
- package/dist/src/types/Account.d.ts.map +1 -0
- package/dist/src/types/Account.js +74 -0
- package/dist/src/types/Account.js.map +1 -0
- package/dist/src/types/Address.d.ts +24 -0
- package/dist/src/types/Address.d.ts.map +1 -0
- package/dist/src/types/Address.js +56 -0
- package/dist/src/types/Address.js.map +1 -0
- package/dist/src/types/Api.d.ts +90 -0
- package/dist/src/types/Api.d.ts.map +1 -0
- package/dist/src/types/Api.js +99 -0
- package/dist/src/types/Api.js.map +1 -0
- package/dist/src/types/Funds.d.ts +175 -0
- package/dist/src/types/Funds.d.ts.map +1 -0
- package/dist/src/types/Funds.js +329 -0
- package/dist/src/types/Funds.js.map +1 -0
- package/dist/src/types/HDWallet.d.ts +29 -0
- package/dist/src/types/HDWallet.d.ts.map +1 -0
- package/dist/src/types/HDWallet.js +40 -0
- package/dist/src/types/HDWallet.js.map +1 -0
- package/dist/src/types/NonceId.d.ts +26 -0
- package/dist/src/types/NonceId.d.ts.map +1 -0
- package/dist/src/types/NonceId.js +37 -0
- package/dist/src/types/NonceId.js.map +1 -0
- package/dist/src/types/Price.d.ts +93 -0
- package/dist/src/types/Price.d.ts.map +1 -0
- package/dist/src/types/Price.js +170 -0
- package/dist/src/types/Price.js.map +1 -0
- package/dist/src/types/TransactionContext.d.ts +135 -0
- package/dist/src/types/TransactionContext.d.ts.map +1 -0
- package/dist/src/types/TransactionContext.js +314 -0
- package/dist/src/types/TransactionContext.js.map +1 -0
- package/dist/src/util/frexp.d.ts +2 -0
- package/dist/src/util/frexp.d.ts.map +1 -0
- package/dist/src/util/frexp.js +68 -0
- package/dist/src/util/frexp.js.map +1 -0
- package/package.json +40 -0
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
const MIN_MANTISSA = 0x8000; // 0x8000 - minimum normalized mantissa (high bit set)
|
|
2
|
+
const MAX_MANTISSA = 0xFFFF; // 0xFFFF - maximum mantissa (16 bits)
|
|
3
|
+
const MAX_EXPONENT = 0xFF; // 0xFF - maximum exponent (8 bits)
|
|
4
|
+
import { frexp } from '../util/frexp';
|
|
5
|
+
import { TokenDecimals } from './Funds';
|
|
6
|
+
export { TokenDecimals };
|
|
7
|
+
/**
|
|
8
|
+
* Represents a swap price in normalized mantissa/exponent format.
|
|
9
|
+
* - Mantissa: 16 bits, must be in range [0x8000, 0xFFFF] (high bit set for normalization)
|
|
10
|
+
* - Exponent: 8 bits, stored with offset +63 internally to map to range [0, 127]
|
|
11
|
+
* The price is interpreted as a qutient of the base and quote amount *integers*, i.e. when
|
|
12
|
+
* creating or printing this price representation, one must account for the number of
|
|
13
|
+
* decimals of the involved base asset traded.
|
|
14
|
+
*
|
|
15
|
+
* Used in limit swap transactions.
|
|
16
|
+
*/
|
|
17
|
+
export class Price {
|
|
18
|
+
constructor(mantissa, // 16 bits
|
|
19
|
+
exponent // 8 bits
|
|
20
|
+
) {
|
|
21
|
+
this.mantissa = mantissa;
|
|
22
|
+
this.exponent = exponent;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Check if a mantissa is valid (normalized, high bit set).
|
|
26
|
+
* @param m - Mantissa value to check
|
|
27
|
+
* @returns true if in range [0x8000, 0xFFFF]
|
|
28
|
+
*/
|
|
29
|
+
static isMantissa(m) {
|
|
30
|
+
// Must have high bit set (normalized): 0x8000 <= m <= 0xFFFF
|
|
31
|
+
return m >= MIN_MANTISSA && m <= MAX_MANTISSA;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Check if a internal exponent representation is valid.
|
|
35
|
+
* @param e - Exponent value to check
|
|
36
|
+
* @returns true if in range [0, 127]
|
|
37
|
+
*/
|
|
38
|
+
static isExponent(e) {
|
|
39
|
+
return e >= 0 && e < 128;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Get the maximum possible price.
|
|
43
|
+
* @returns Price with maximum mantissa (0xFFFF) and exponent (127)
|
|
44
|
+
*/
|
|
45
|
+
static max() {
|
|
46
|
+
// Returns 0xFFFF mantissa with 0x7F exponent
|
|
47
|
+
return new Price(0xFFFF, 127);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Convert stored internal exponent to base-2 exponent.
|
|
51
|
+
* @returns Base-2 exponent (exponent - 63)
|
|
52
|
+
*/
|
|
53
|
+
exponentBase2() {
|
|
54
|
+
return this.exponent - 63;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Get the base-2 exponent if mantissa is not considered as
|
|
58
|
+
* a fraction but as an integer.
|
|
59
|
+
* @returns Base-2 exponent for mantissa
|
|
60
|
+
*/
|
|
61
|
+
mantissaExponent2() {
|
|
62
|
+
return this.exponentBase2() - 16;
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Convert price to raw double (without decimals adjustment).
|
|
66
|
+
* @returns Price as double (mantissa * 2^exponent2)
|
|
67
|
+
*/
|
|
68
|
+
toDoubleRaw() {
|
|
69
|
+
// Equivalent to std::ldexp(m, e2) = m * 2^e2
|
|
70
|
+
return this.mantissa * Math.pow(2, this.mantissaExponent2());
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Convert price to double corrected for asset decimals. In Warthog, every
|
|
74
|
+
* market is with respect to WART as quote asset, which always has 8 decimals
|
|
75
|
+
* so we only need to pass the base, i.e. asset decimals.
|
|
76
|
+
* @param dec - Number decimal places of the traded asset
|
|
77
|
+
* @returns Price as double with decimal places adjustment applied
|
|
78
|
+
*/
|
|
79
|
+
toDoubleAdjusted(dec) {
|
|
80
|
+
// Compute double price respecting the asset decimals
|
|
81
|
+
const b10e = this.base10_decimals_exponent(dec);
|
|
82
|
+
return this.toDoubleRaw() * Math.pow(10, -b10e);
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Calculate base-10 exponent for decimals adjustment.
|
|
86
|
+
* @param dec - Number of decimals of the traded asset
|
|
87
|
+
* @returns Difference between WART decimals (8) and asset decimals
|
|
88
|
+
*/
|
|
89
|
+
base10_decimals_exponent(dec) {
|
|
90
|
+
return TokenDecimals.WART.decimals - dec.decimals;
|
|
91
|
+
}
|
|
92
|
+
/**
|
|
93
|
+
* Create Price from raw mantissa and exponent values.
|
|
94
|
+
* @param mantissa - 16-bit mantissa (must be normalized: 0x8000-0xFFFF)
|
|
95
|
+
* @param exponent - Raw exponent before +63 adjustment (0-127)
|
|
96
|
+
* @returns Price instance if valid, null otherwise
|
|
97
|
+
*/
|
|
98
|
+
static fromMantissaExponent(mantissa, exponent) {
|
|
99
|
+
exponent += 63;
|
|
100
|
+
if (!Price.isExponent(exponent) || !Price.isMantissa(mantissa)) {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
return new Price(mantissa, exponent);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Create Price from a number and given decimals of the base asset.
|
|
107
|
+
* This is the recommended factory method for users.
|
|
108
|
+
* @param d - Price as decimal number
|
|
109
|
+
* @param baseDecimals - Number of decimals of the traded asset
|
|
110
|
+
* @param ceil - If true, round up; otherwise round down
|
|
111
|
+
* @returns Price instance if valid, null otherwise
|
|
112
|
+
*/
|
|
113
|
+
static fromNumberDecimals(d, baseDecimals, ceil = false) {
|
|
114
|
+
const adjusted = d * Math.pow(10, 8 - baseDecimals.decimals);
|
|
115
|
+
return Price.fromDoubleInternal(adjusted, ceil);
|
|
116
|
+
}
|
|
117
|
+
// This factory method should not be used by library users as it may be easily misused.
|
|
118
|
+
// Warthog needs to specify the price in normalized form and this normalization depends
|
|
119
|
+
// on the intended price AND the decimals of the asset that is traded.
|
|
120
|
+
// Warthog prices are with respect to the ratio of the raw 64 bit unsinged amounts of
|
|
121
|
+
// WART and the traded token. Therefore, to normalize the price properly, users should
|
|
122
|
+
// call the `fromNumberDecimals` method.
|
|
123
|
+
static fromDoubleInternal(d, ceil = false) {
|
|
124
|
+
if (d <= 0 || !Number.isFinite(d)) {
|
|
125
|
+
return null;
|
|
126
|
+
}
|
|
127
|
+
const [mantissa, exponent] = frexp(d);
|
|
128
|
+
let mantissa32 = Math.floor(mantissa * 65536);
|
|
129
|
+
let exp = exponent;
|
|
130
|
+
const exact = (mantissa * 65536) === mantissa32;
|
|
131
|
+
if (ceil && !exact) {
|
|
132
|
+
mantissa32 += 1;
|
|
133
|
+
if (mantissa32 >= 65536) { // carry
|
|
134
|
+
mantissa32 >>= 1;
|
|
135
|
+
exp += 1;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return Price.fromMantissaExponent(mantissa32, exp);
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Convert price to 6-character hex string for transaction.
|
|
142
|
+
* @returns Hex string (4 chars mantissa + 2 chars exponent)
|
|
143
|
+
*/
|
|
144
|
+
toHex() {
|
|
145
|
+
const mantissaHex = this.mantissa.toString(16).padStart(4, '0');
|
|
146
|
+
const exponentHex = this.exponent.toString(16).padStart(2, '0');
|
|
147
|
+
return mantissaHex + exponentHex;
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* Parse price from 6-character hex string.
|
|
151
|
+
* @param hex - 6-character hex string (4 chars mantissa + 2 chars exponent)
|
|
152
|
+
* @returns Price instance if valid, null otherwise
|
|
153
|
+
*/
|
|
154
|
+
static fromHex(hex) {
|
|
155
|
+
if (hex.length !== 6) {
|
|
156
|
+
return null;
|
|
157
|
+
}
|
|
158
|
+
const parsed = parseInt(hex, 16);
|
|
159
|
+
if (isNaN(parsed)) {
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
const mantissa = parsed >> 8;
|
|
163
|
+
const exponent = parsed & 0xFF;
|
|
164
|
+
if (mantissa > MAX_MANTISSA || exponent > MAX_EXPONENT) {
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
return new Price(mantissa, exponent);
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
//# sourceMappingURL=Price.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Price.js","sourceRoot":"","sources":["../../../src/types/Price.ts"],"names":[],"mappings":"AAAA,MAAM,YAAY,GAAG,MAAM,CAAC,CAAC,sDAAsD;AACnF,MAAM,YAAY,GAAG,MAAM,CAAC,CAAC,sCAAsC;AACnE,MAAM,YAAY,GAAG,IAAI,CAAC,CAAG,mCAAmC;AAEhE,OAAO,EAAE,KAAK,EAAE,MAAM,eAAe,CAAC;AACtC,OAAO,EAAE,aAAa,EAAE,MAAM,SAAS,CAAC;AACxC,OAAO,EAAE,aAAa,EAAE,CAAC;AAEzB;;;;;;;;;GASG;AACH,MAAM,OAAO,KAAK;IACd,YACoB,QAAgB,EAAG,UAAU;IAC7B,QAAgB,CAAI,SAAS;;QAD7B,aAAQ,GAAR,QAAQ,CAAQ;QAChB,aAAQ,GAAR,QAAQ,CAAQ;IACjC,CAAC;IAEJ;;;;OAIG;IACK,MAAM,CAAC,UAAU,CAAC,CAA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,UAAU,EAAE,GAAG,CAAC,CAAC;IACvD,CAAC;IAED;;;OAGG;IACI,KAAK;QACR,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAChE,MAAM,WAAW,GAAG,IAAI,CAAC,QAAQ,CAAC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC;QAChE,OAAO,WAAW,GAAG,WAAW,CAAC;IACrC,CAAC;IAED;;;;OAIG;IACI,MAAM,CAAC,OAAO,CAAC,GAAW;QAC7B,IAAI,GAAG,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACnB,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,MAAM,MAAM,GAAG,QAAQ,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC;QACjC,IAAI,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC;YAChB,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,CAAC;QAC7B,MAAM,QAAQ,GAAG,MAAM,GAAG,IAAI,CAAC;QAE/B,IAAI,QAAQ,GAAG,YAAY,IAAI,QAAQ,GAAG,YAAY,EAAE,CAAC;YACrD,OAAO,IAAI,CAAC;QAChB,CAAC;QAED,OAAO,IAAI,KAAK,CAAC,QAAQ,EAAE,QAAQ,CAAC,CAAC;IACzC,CAAC;CACJ"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { Account } from './Account';
|
|
2
|
+
import { Address } from './Address';
|
|
3
|
+
import { NonceId } from './NonceId';
|
|
4
|
+
import { Price } from './Price';
|
|
5
|
+
import { Funds, Liquidity, RoundedFee, TokenDecimals, Wart } from './Funds';
|
|
6
|
+
export interface ChainPin {
|
|
7
|
+
pinHash: string;
|
|
8
|
+
pinHeight: number;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* JSON representation of a signed transaction for submission to Warthog nodes.
|
|
12
|
+
*/
|
|
13
|
+
export interface TransactionJson extends Record<string, unknown> {
|
|
14
|
+
type: string;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Transaction builder for creating and signing Warthog transactions.
|
|
18
|
+
* Obtained via WarthogApi.createTransactionContext().
|
|
19
|
+
*
|
|
20
|
+
* All properties (chainPin, fee, nonceId) can be modified. When reusing this
|
|
21
|
+
* context for multiple transactions, you MUST change the nonceId for each new
|
|
22
|
+
* transaction to prevent nonce collisions. The chainPin should typically remain
|
|
23
|
+
* unchanged unless you need to refresh it from the network.
|
|
24
|
+
*/
|
|
25
|
+
export declare class TransactionContext {
|
|
26
|
+
chainPin: ChainPin;
|
|
27
|
+
fee: RoundedFee;
|
|
28
|
+
nonceId: NonceId;
|
|
29
|
+
/**
|
|
30
|
+
* Create a new transaction context.
|
|
31
|
+
* @param chainPin - Chain pin data from the network (can be refreshed)
|
|
32
|
+
* @param fee - Transaction fee (can be modified for priority transactions)
|
|
33
|
+
* @param nonceId - Unique nonce (must be changed for each new transaction)
|
|
34
|
+
*/
|
|
35
|
+
constructor(chainPin: ChainPin, fee: RoundedFee, nonceId: NonceId);
|
|
36
|
+
/**
|
|
37
|
+
* Create a WART native token transfer transaction.
|
|
38
|
+
* @param account - Account signing the transaction
|
|
39
|
+
* @param toAddr - Recipient address
|
|
40
|
+
* @param wart - Amount in WART (E8)
|
|
41
|
+
* @returns Signed transaction JSON
|
|
42
|
+
*/
|
|
43
|
+
transferWart(account: Account, toAddr: Address, wart: Wart): TransactionJson;
|
|
44
|
+
/**
|
|
45
|
+
* Create an asset transfer transaction.
|
|
46
|
+
* @param account - Account signing the transaction
|
|
47
|
+
* @param assetHash - Asset hash as hex string
|
|
48
|
+
* @param toAddr - Recipient address
|
|
49
|
+
* @param amount - Amount in token units
|
|
50
|
+
* @returns Signed transaction JSON
|
|
51
|
+
*/
|
|
52
|
+
transferAsset(account: Account, assetHash: string, toAddr: Address, amount: Funds): TransactionJson;
|
|
53
|
+
/**
|
|
54
|
+
* Create a liquidity transfer transaction.
|
|
55
|
+
* @param account - Account signing the transaction
|
|
56
|
+
* @param assetHash - Asset hash as hex string
|
|
57
|
+
* @param toAddr - Recipient address
|
|
58
|
+
* @param units - Liquidity units to transfer
|
|
59
|
+
* @returns Signed transaction JSON
|
|
60
|
+
*/
|
|
61
|
+
transferLiquidity(account: Account, assetHash: string, toAddr: Address, units: Liquidity): TransactionJson;
|
|
62
|
+
/**
|
|
63
|
+
* Internal token transfer implementation.
|
|
64
|
+
* @param account - Account signing the transaction
|
|
65
|
+
* @param assetHash - Asset hash as hex string
|
|
66
|
+
* @param isLiquidity - Whether this transfer is for an asset's liquidity or the asset itself
|
|
67
|
+
* @param toAddr - Recipient address
|
|
68
|
+
* @param amountE8 - Amount in E8
|
|
69
|
+
* @returns Signed transaction JSON
|
|
70
|
+
*/
|
|
71
|
+
private tokenTransferInternal;
|
|
72
|
+
/**
|
|
73
|
+
* Create a limit buy transaction (buy token with WART).
|
|
74
|
+
* @param account - Account signing the transaction
|
|
75
|
+
* @param assetHash - Asset hash as hex string
|
|
76
|
+
* @param wartAmount - WART amount to spend
|
|
77
|
+
* @param limit - Limit price as Price object
|
|
78
|
+
* @returns Signed transaction JSON
|
|
79
|
+
*/
|
|
80
|
+
buy(account: Account, assetHash: string, wartAmount: Wart, limit: Price): TransactionJson;
|
|
81
|
+
/**
|
|
82
|
+
* Create a limit sell transaction (sell token for WART).
|
|
83
|
+
* @param account - Account signing the transaction
|
|
84
|
+
* @param assetHash - Asset hash as hex string
|
|
85
|
+
* @param tokenAmount - Token amount to sell
|
|
86
|
+
* @param limit - Limit price as Price object
|
|
87
|
+
* @returns Signed transaction JSON
|
|
88
|
+
*/
|
|
89
|
+
sell(account: Account, assetHash: string, tokenAmount: Funds, limit: Price): TransactionJson;
|
|
90
|
+
/**
|
|
91
|
+
* Internal limit swap implementation.
|
|
92
|
+
* @param account - Account signing the transaction
|
|
93
|
+
* @param assetHash - Asset hash as hex string
|
|
94
|
+
* @param isBuy - True to buy token with WART, false to sell token for WART
|
|
95
|
+
* @param amountE8 - Amount in E8 (token units for sell, WART E8 for buy)
|
|
96
|
+
* @param limit - Limit price as Price object
|
|
97
|
+
* @returns Signed transaction JSON
|
|
98
|
+
*/
|
|
99
|
+
private limitSwapInternal;
|
|
100
|
+
/**
|
|
101
|
+
* Create a liquidity deposit transaction (add tokens + WART to liquidity pool).
|
|
102
|
+
* @param account - Account signing the transaction
|
|
103
|
+
* @param assetHash - Asset hash as hex string
|
|
104
|
+
* @param tokenAmount - Token amount to deposit
|
|
105
|
+
* @param wart - WART amount to deposit
|
|
106
|
+
* @returns Signed transaction JSON
|
|
107
|
+
*/
|
|
108
|
+
depositLiquidity(account: Account, assetHash: string, tokenAmount: Funds, wart: Wart): TransactionJson;
|
|
109
|
+
/**
|
|
110
|
+
* Create a liquidity withdrawal transaction (remove tokens + WART from liquidity pool).
|
|
111
|
+
* @param account - Account signing the transaction
|
|
112
|
+
* @param assetHash - Asset hash as hex string
|
|
113
|
+
* @param units - Liquidity units to withdraw
|
|
114
|
+
* @returns Signed transaction JSON
|
|
115
|
+
*/
|
|
116
|
+
withdrawLiquidity(account: Account, assetHash: string, units: Liquidity): TransactionJson;
|
|
117
|
+
/**
|
|
118
|
+
* Create a cancelTransaction transaction (cancel a pending limit order).
|
|
119
|
+
* @param account - Account signing the transaction
|
|
120
|
+
* @param cancelHeight - Block height at which the order was placed
|
|
121
|
+
* @param cancelNonceId - NonceId of the order to cancel
|
|
122
|
+
* @returns Signed transaction JSON
|
|
123
|
+
*/
|
|
124
|
+
cancelTransaction(account: Account, cancelHeight: number, cancelNonceId: NonceId): TransactionJson;
|
|
125
|
+
/**
|
|
126
|
+
* Create an asset creation transaction (create a new token).
|
|
127
|
+
* @param account - Account signing the transaction
|
|
128
|
+
* @param totalSupply - Total supply in token units
|
|
129
|
+
* @param decimals - Token decimal places (0-18)
|
|
130
|
+
* @param name - Token name (max 5 ASCII characters)
|
|
131
|
+
* @returns Signed transaction JSON
|
|
132
|
+
*/
|
|
133
|
+
createAssets(account: Account, totalSupply: Funds, decimals: TokenDecimals, name: string): TransactionJson;
|
|
134
|
+
}
|
|
135
|
+
//# sourceMappingURL=TransactionContext.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TransactionContext.d.ts","sourceRoot":"","sources":["../../../src/types/TransactionContext.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAK5E,MAAM,WAAW,QAAQ;IACrB,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;CACrB;AAED;;GAEG;AACH,MAAM,WAAW,eAAgB,SAAQ,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;IAC5D,IAAI,EAAE,MAAM,CAAC;CAChB;AAED;;;;;;;;GAQG;AACH,qBAAa,kBAAkB;IAQhB,QAAQ,EAAE,QAAQ;IAClB,GAAG,EAAE,UAAU;IACf,OAAO,EAAE,OAAO;IAT3B;;;;;OAKG;gBAEQ,QAAQ,EAAE,QAAQ,EAClB,GAAG,EAAE,UAAU,EACf,OAAO,EAAE,OAAO;IAG3B;;;;;;OAMG;IACH,YAAY,CAAC,OAAO,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,GAAG,eAAe;IAwB5E;;;;;;;OAOG;IACH,aAAa,CACT,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,OAAO,EACf,MAAM,EAAE,KAAK,GACd,eAAe;IAIlB;;;;;;;OAOG;IACH,iBAAiB,CACb,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,MAAM,EACjB,MAAM,EAAE,OAAO,EACf,KAAK,EAAE,SAAS,GACjB,eAAe;IAIlB;;;;;;;;OAQG;IACH,OAAO,CAAC,qBAAqB;IAkC7B;;;;;;;OAOG;IACH,GAAG,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,UAAU,EAAE,IAAI,EAAE,KAAK,EAAE,KAAK,GAAG,eAAe;IAIzF;;;;;;;OAOG;IACH,IAAI,CAAC,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK,EAAE,KAAK,GAAG,eAAe;IAI5F;;;;;;;;OAQG;IACH,OAAO,CAAC,iBAAiB;IAkCzB;;;;;;;OAOG;IACH,gBAAgB,CACZ,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,MAAM,EACjB,WAAW,EAAE,KAAK,EAClB,IAAI,EAAE,IAAI,GACX,eAAe;IA0BlB;;;;;;OAMG;IACH,iBAAiB,CACb,OAAO,EAAE,OAAO,EAChB,SAAS,EAAE,MAAM,EACjB,KAAK,EAAE,SAAS,GACjB,eAAe;IAwBlB;;;;;;OAMG;IACH,iBAAiB,CACb,OAAO,EAAE,OAAO,EAChB,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,OAAO,GACvB,eAAe;IAwBlB;;;;;;;OAOG;IACH,YAAY,CACR,OAAO,EAAE,OAAO,EAChB,WAAW,EAAE,KAAK,EAClB,QAAQ,EAAE,aAAa,EACvB,IAAI,EAAE,MAAM,GACb,eAAe;CA2BrB"}
|
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
import { createHash } from 'crypto';
|
|
2
|
+
import { Account } from './Account';
|
|
3
|
+
import { Address } from './Address';
|
|
4
|
+
import { NonceId } from './NonceId';
|
|
5
|
+
import { Price } from './Price';
|
|
6
|
+
import { Funds, Liquidity, RoundedFee, TokenDecimals, Wart } from './Funds';
|
|
7
|
+
const UINT32_BE_BYTES = 4;
|
|
8
|
+
const UINT64_BE_BYTES = 8;
|
|
9
|
+
/**
|
|
10
|
+
* Transaction builder for creating and signing Warthog transactions.
|
|
11
|
+
* Obtained via WarthogApi.createTransactionContext().
|
|
12
|
+
*
|
|
13
|
+
* All properties (chainPin, fee, nonceId) can be modified. When reusing this
|
|
14
|
+
* context for multiple transactions, you MUST change the nonceId for each new
|
|
15
|
+
* transaction to prevent nonce collisions. The chainPin should typically remain
|
|
16
|
+
* unchanged unless you need to refresh it from the network.
|
|
17
|
+
*/
|
|
18
|
+
export class TransactionContext {
|
|
19
|
+
/**
|
|
20
|
+
* Create a new transaction context.
|
|
21
|
+
* @param chainPin - Chain pin data from the network (can be refreshed)
|
|
22
|
+
* @param fee - Transaction fee (can be modified for priority transactions)
|
|
23
|
+
* @param nonceId - Unique nonce (must be changed for each new transaction)
|
|
24
|
+
*/
|
|
25
|
+
constructor(chainPin, fee, nonceId) {
|
|
26
|
+
this.chainPin = chainPin;
|
|
27
|
+
this.fee = fee;
|
|
28
|
+
this.nonceId = nonceId;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Create a WART native token transfer transaction.
|
|
32
|
+
* @param account - Account signing the transaction
|
|
33
|
+
* @param toAddr - Recipient address
|
|
34
|
+
* @param wart - Amount in WART (E8)
|
|
35
|
+
* @returns Signed transaction JSON
|
|
36
|
+
*/
|
|
37
|
+
transferWart(account, toAddr, wart) {
|
|
38
|
+
const binary = Buffer.concat([
|
|
39
|
+
hashToBytes(this.chainPin.pinHash),
|
|
40
|
+
uint32BE(this.chainPin.pinHeight),
|
|
41
|
+
uint32BE(this.nonceId.value),
|
|
42
|
+
Buffer.alloc(3),
|
|
43
|
+
uint64BE(this.fee.E8),
|
|
44
|
+
addressToBytes(toAddr.hex),
|
|
45
|
+
uint64BE(wart.E8),
|
|
46
|
+
]);
|
|
47
|
+
const hash = createHash('sha256').update(binary).digest('hex');
|
|
48
|
+
const sig = account.sign(hash);
|
|
49
|
+
return {
|
|
50
|
+
type: 'wartTransfer',
|
|
51
|
+
pinHeight: this.chainPin.pinHeight,
|
|
52
|
+
nonceId: this.nonceId.value,
|
|
53
|
+
feeE8: this.fee.E8,
|
|
54
|
+
toAddr: toAddr.hex,
|
|
55
|
+
wartE8: wart.E8,
|
|
56
|
+
signature65: sig.signature,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Create an asset transfer transaction.
|
|
61
|
+
* @param account - Account signing the transaction
|
|
62
|
+
* @param assetHash - Asset hash as hex string
|
|
63
|
+
* @param toAddr - Recipient address
|
|
64
|
+
* @param amount - Amount in token units
|
|
65
|
+
* @returns Signed transaction JSON
|
|
66
|
+
*/
|
|
67
|
+
transferAsset(account, assetHash, toAddr, amount) {
|
|
68
|
+
return this.tokenTransferInternal(account, assetHash, false, toAddr, amount.amount);
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Create a liquidity transfer transaction.
|
|
72
|
+
* @param account - Account signing the transaction
|
|
73
|
+
* @param assetHash - Asset hash as hex string
|
|
74
|
+
* @param toAddr - Recipient address
|
|
75
|
+
* @param units - Liquidity units to transfer
|
|
76
|
+
* @returns Signed transaction JSON
|
|
77
|
+
*/
|
|
78
|
+
transferLiquidity(account, assetHash, toAddr, units) {
|
|
79
|
+
return this.tokenTransferInternal(account, assetHash, true, toAddr, units.E8);
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Internal token transfer implementation.
|
|
83
|
+
* @param account - Account signing the transaction
|
|
84
|
+
* @param assetHash - Asset hash as hex string
|
|
85
|
+
* @param isLiquidity - Whether this transfer is for an asset's liquidity or the asset itself
|
|
86
|
+
* @param toAddr - Recipient address
|
|
87
|
+
* @param amountE8 - Amount in E8
|
|
88
|
+
* @returns Signed transaction JSON
|
|
89
|
+
*/
|
|
90
|
+
tokenTransferInternal(account, assetHash, isLiquidity, toAddr, amountE8) {
|
|
91
|
+
const binary = Buffer.concat([
|
|
92
|
+
hashToBytes(this.chainPin.pinHash),
|
|
93
|
+
uint32BE(this.chainPin.pinHeight),
|
|
94
|
+
uint32BE(this.nonceId.value),
|
|
95
|
+
Buffer.alloc(3),
|
|
96
|
+
uint64BE(this.fee.E8),
|
|
97
|
+
hashToBytes(assetHash),
|
|
98
|
+
Buffer.from([isLiquidity ? 1 : 0]),
|
|
99
|
+
addressToBytes(toAddr.hex),
|
|
100
|
+
uint64BE(amountE8),
|
|
101
|
+
]);
|
|
102
|
+
const hash = createHash('sha256').update(binary).digest('hex');
|
|
103
|
+
const sig = account.sign(hash);
|
|
104
|
+
return {
|
|
105
|
+
type: 'tokenTransfer',
|
|
106
|
+
pinHeight: this.chainPin.pinHeight,
|
|
107
|
+
nonceId: this.nonceId.value,
|
|
108
|
+
feeE8: this.fee.E8,
|
|
109
|
+
assetHash,
|
|
110
|
+
isLiquidity,
|
|
111
|
+
toAddr: toAddr.hex,
|
|
112
|
+
amountU64: amountE8,
|
|
113
|
+
signature65: sig.signature,
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Create a limit buy transaction (buy token with WART).
|
|
118
|
+
* @param account - Account signing the transaction
|
|
119
|
+
* @param assetHash - Asset hash as hex string
|
|
120
|
+
* @param wartAmount - WART amount to spend
|
|
121
|
+
* @param limit - Limit price as Price object
|
|
122
|
+
* @returns Signed transaction JSON
|
|
123
|
+
*/
|
|
124
|
+
buy(account, assetHash, wartAmount, limit) {
|
|
125
|
+
return this.limitSwapInternal(account, assetHash, true, wartAmount.E8, limit);
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Create a limit sell transaction (sell token for WART).
|
|
129
|
+
* @param account - Account signing the transaction
|
|
130
|
+
* @param assetHash - Asset hash as hex string
|
|
131
|
+
* @param tokenAmount - Token amount to sell
|
|
132
|
+
* @param limit - Limit price as Price object
|
|
133
|
+
* @returns Signed transaction JSON
|
|
134
|
+
*/
|
|
135
|
+
sell(account, assetHash, tokenAmount, limit) {
|
|
136
|
+
return this.limitSwapInternal(account, assetHash, false, tokenAmount.amount, limit);
|
|
137
|
+
}
|
|
138
|
+
/**
|
|
139
|
+
* Internal limit swap implementation.
|
|
140
|
+
* @param account - Account signing the transaction
|
|
141
|
+
* @param assetHash - Asset hash as hex string
|
|
142
|
+
* @param isBuy - True to buy token with WART, false to sell token for WART
|
|
143
|
+
* @param amountE8 - Amount in E8 (token units for sell, WART E8 for buy)
|
|
144
|
+
* @param limit - Limit price as Price object
|
|
145
|
+
* @returns Signed transaction JSON
|
|
146
|
+
*/
|
|
147
|
+
limitSwapInternal(account, assetHash, isBuy, amountE8, limit) {
|
|
148
|
+
const binary = Buffer.concat([
|
|
149
|
+
hashToBytes(this.chainPin.pinHash),
|
|
150
|
+
uint32BE(this.chainPin.pinHeight),
|
|
151
|
+
uint32BE(this.nonceId.value),
|
|
152
|
+
Buffer.alloc(3),
|
|
153
|
+
uint64BE(this.fee.E8),
|
|
154
|
+
hashToBytes(assetHash),
|
|
155
|
+
Buffer.from([isBuy ? 1 : 0]),
|
|
156
|
+
uint64BE(amountE8),
|
|
157
|
+
Buffer.from(limit.toHex(), 'hex'),
|
|
158
|
+
]);
|
|
159
|
+
const hash = createHash('sha256').update(binary).digest('hex');
|
|
160
|
+
const sig = account.sign(hash);
|
|
161
|
+
return {
|
|
162
|
+
type: 'limitSwap',
|
|
163
|
+
pinHeight: this.chainPin.pinHeight,
|
|
164
|
+
nonceId: this.nonceId.value,
|
|
165
|
+
feeE8: this.fee.E8,
|
|
166
|
+
assetHash,
|
|
167
|
+
isBuy,
|
|
168
|
+
amountU64: amountE8,
|
|
169
|
+
limit: limit.toHex(),
|
|
170
|
+
signature65: sig.signature,
|
|
171
|
+
};
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Create a liquidity deposit transaction (add tokens + WART to liquidity pool).
|
|
175
|
+
* @param account - Account signing the transaction
|
|
176
|
+
* @param assetHash - Asset hash as hex string
|
|
177
|
+
* @param tokenAmount - Token amount to deposit
|
|
178
|
+
* @param wart - WART amount to deposit
|
|
179
|
+
* @returns Signed transaction JSON
|
|
180
|
+
*/
|
|
181
|
+
depositLiquidity(account, assetHash, tokenAmount, wart) {
|
|
182
|
+
const binary = Buffer.concat([
|
|
183
|
+
hashToBytes(this.chainPin.pinHash),
|
|
184
|
+
uint32BE(this.chainPin.pinHeight),
|
|
185
|
+
uint32BE(this.nonceId.value),
|
|
186
|
+
Buffer.alloc(3),
|
|
187
|
+
uint64BE(this.fee.E8),
|
|
188
|
+
hashToBytes(assetHash),
|
|
189
|
+
uint64BE(tokenAmount.amount),
|
|
190
|
+
uint64BE(wart.E8),
|
|
191
|
+
]);
|
|
192
|
+
const hash = createHash('sha256').update(binary).digest('hex');
|
|
193
|
+
const sig = account.sign(hash);
|
|
194
|
+
return {
|
|
195
|
+
type: 'liquidityDeposit',
|
|
196
|
+
pinHeight: this.chainPin.pinHeight,
|
|
197
|
+
nonceId: this.nonceId.value,
|
|
198
|
+
feeE8: this.fee.E8,
|
|
199
|
+
assetHash,
|
|
200
|
+
amountU64: tokenAmount.amount,
|
|
201
|
+
wartE8: wart.E8,
|
|
202
|
+
signature65: sig.signature,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Create a liquidity withdrawal transaction (remove tokens + WART from liquidity pool).
|
|
207
|
+
* @param account - Account signing the transaction
|
|
208
|
+
* @param assetHash - Asset hash as hex string
|
|
209
|
+
* @param units - Liquidity units to withdraw
|
|
210
|
+
* @returns Signed transaction JSON
|
|
211
|
+
*/
|
|
212
|
+
withdrawLiquidity(account, assetHash, units) {
|
|
213
|
+
const binary = Buffer.concat([
|
|
214
|
+
hashToBytes(this.chainPin.pinHash),
|
|
215
|
+
uint32BE(this.chainPin.pinHeight),
|
|
216
|
+
uint32BE(this.nonceId.value),
|
|
217
|
+
Buffer.alloc(3),
|
|
218
|
+
uint64BE(this.fee.E8),
|
|
219
|
+
hashToBytes(assetHash),
|
|
220
|
+
uint64BE(units.E8),
|
|
221
|
+
]);
|
|
222
|
+
const hash = createHash('sha256').update(binary).digest('hex');
|
|
223
|
+
const sig = account.sign(hash);
|
|
224
|
+
return {
|
|
225
|
+
type: 'liquidityWithdrawal',
|
|
226
|
+
pinHeight: this.chainPin.pinHeight,
|
|
227
|
+
nonceId: this.nonceId.value,
|
|
228
|
+
feeE8: this.fee.E8,
|
|
229
|
+
assetHash,
|
|
230
|
+
amountE8: units.E8,
|
|
231
|
+
signature65: sig.signature,
|
|
232
|
+
};
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Create a cancelTransaction transaction (cancel a pending limit order).
|
|
236
|
+
* @param account - Account signing the transaction
|
|
237
|
+
* @param cancelHeight - Block height at which the order was placed
|
|
238
|
+
* @param cancelNonceId - NonceId of the order to cancel
|
|
239
|
+
* @returns Signed transaction JSON
|
|
240
|
+
*/
|
|
241
|
+
cancelTransaction(account, cancelHeight, cancelNonceId) {
|
|
242
|
+
const binary = Buffer.concat([
|
|
243
|
+
hashToBytes(this.chainPin.pinHash),
|
|
244
|
+
uint32BE(this.chainPin.pinHeight),
|
|
245
|
+
uint32BE(this.nonceId.value),
|
|
246
|
+
Buffer.alloc(3),
|
|
247
|
+
uint64BE(this.fee.E8),
|
|
248
|
+
uint32BE(cancelHeight),
|
|
249
|
+
uint32BE(cancelNonceId.value),
|
|
250
|
+
]);
|
|
251
|
+
const hash = createHash('sha256').update(binary).digest('hex');
|
|
252
|
+
const sig = account.sign(hash);
|
|
253
|
+
return {
|
|
254
|
+
type: 'cancelation',
|
|
255
|
+
pinHeight: this.chainPin.pinHeight,
|
|
256
|
+
nonceId: this.nonceId.value,
|
|
257
|
+
feeE8: this.fee.E8,
|
|
258
|
+
cancelHeight,
|
|
259
|
+
cancelNonceId: cancelNonceId.value,
|
|
260
|
+
signature65: sig.signature,
|
|
261
|
+
};
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Create an asset creation transaction (create a new token).
|
|
265
|
+
* @param account - Account signing the transaction
|
|
266
|
+
* @param totalSupply - Total supply in token units
|
|
267
|
+
* @param decimals - Token decimal places (0-18)
|
|
268
|
+
* @param name - Token name (max 5 ASCII characters)
|
|
269
|
+
* @returns Signed transaction JSON
|
|
270
|
+
*/
|
|
271
|
+
createAssets(account, totalSupply, decimals, name) {
|
|
272
|
+
const nameBuffer = Buffer.alloc(5);
|
|
273
|
+
nameBuffer.write(name, 'ascii');
|
|
274
|
+
const binary = Buffer.concat([
|
|
275
|
+
hashToBytes(this.chainPin.pinHash),
|
|
276
|
+
uint32BE(this.chainPin.pinHeight),
|
|
277
|
+
uint32BE(this.nonceId.value),
|
|
278
|
+
Buffer.alloc(3),
|
|
279
|
+
uint64BE(this.fee.E8),
|
|
280
|
+
uint64BE(totalSupply.amount),
|
|
281
|
+
Buffer.from([decimals.decimals]),
|
|
282
|
+
nameBuffer,
|
|
283
|
+
]);
|
|
284
|
+
const hash = createHash('sha256').update(binary).digest('hex');
|
|
285
|
+
const sig = account.sign(hash);
|
|
286
|
+
return {
|
|
287
|
+
type: 'assetCreation',
|
|
288
|
+
pinHeight: this.chainPin.pinHeight,
|
|
289
|
+
nonceId: this.nonceId.value,
|
|
290
|
+
feeE8: this.fee.E8,
|
|
291
|
+
supplyU64: totalSupply.amount,
|
|
292
|
+
decimals: decimals.decimals,
|
|
293
|
+
name,
|
|
294
|
+
signature65: sig.signature,
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
function uint32BE(value) {
|
|
299
|
+
const buf = Buffer.alloc(UINT32_BE_BYTES);
|
|
300
|
+
buf.writeUInt32BE(value, 0);
|
|
301
|
+
return buf;
|
|
302
|
+
}
|
|
303
|
+
function uint64BE(value) {
|
|
304
|
+
const buf = Buffer.alloc(UINT64_BE_BYTES);
|
|
305
|
+
buf.writeBigUInt64BE(value, 0);
|
|
306
|
+
return buf;
|
|
307
|
+
}
|
|
308
|
+
function addressToBytes(address) {
|
|
309
|
+
return Buffer.from(address.slice(0, 40), 'hex');
|
|
310
|
+
}
|
|
311
|
+
function hashToBytes(hash) {
|
|
312
|
+
return Buffer.from(hash, 'hex');
|
|
313
|
+
}
|
|
314
|
+
//# sourceMappingURL=TransactionContext.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"TransactionContext.js","sourceRoot":"","sources":["../../../src/types/TransactionContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,QAAQ,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,EAAE,KAAK,EAAE,MAAM,SAAS,CAAC;AAChC,OAAO,EAAE,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,SAAS,CAAC;AAE5E,MAAM,eAAe,GAAG,CAAC,CAAC;AAC1B,MAAM,eAAe,GAAG,CAAC,CAAC;AAc1B;;;;;;;;GAQG;AACH,MAAM,OAAO,kBAAkB;IAC3B;;;;;OAKG;IACH,YACW,QAAkB,EAClB,GAAe,EACf,OAAgB;QAFhB,aAAQ,GAAR,QAAQ,CAAU;QAClB,QAAG,GAAH,GAAG,CAAY;QACf,YAAO,GAAP,OAAO,CAAS;IACxB,CAAC;IAEJ;;;;;;OAMG;IACH,YAAY,CAAC,OAAgB,EAAE,MAAe,EAAE,IAAU;QACtD,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;YACzB,WAAW,CAAC,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC;YAClC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC;YACjC,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,KAAK,CA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