@the-situation/utils 0.5.0-alpha.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/abi-encode.d.ts +185 -0
- package/dist/abi-encode.d.ts.map +1 -0
- package/dist/abi-encode.js +278 -0
- package/dist/abi-encode.js.map +1 -0
- package/dist/address.d.ts +29 -0
- package/dist/address.d.ts.map +1 -0
- package/dist/address.js +38 -0
- package/dist/address.js.map +1 -0
- package/dist/call-format.d.ts +61 -0
- package/dist/call-format.d.ts.map +1 -0
- package/dist/call-format.js +77 -0
- package/dist/call-format.js.map +1 -0
- package/dist/cumulative.d.ts +77 -0
- package/dist/cumulative.d.ts.map +1 -0
- package/dist/cumulative.js +102 -0
- package/dist/cumulative.js.map +1 -0
- package/dist/decimal-scale.d.ts +63 -0
- package/dist/decimal-scale.d.ts.map +1 -0
- package/dist/decimal-scale.js +108 -0
- package/dist/decimal-scale.js.map +1 -0
- package/dist/erc20.d.ts +26 -0
- package/dist/erc20.d.ts.map +1 -0
- package/dist/erc20.js +41 -0
- package/dist/erc20.js.map +1 -0
- package/dist/hints.d.ts +78 -0
- package/dist/hints.d.ts.map +1 -0
- package/dist/hints.js +118 -0
- package/dist/hints.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +15 -0
- package/dist/index.js.map +1 -0
- package/dist/math.d.ts +39 -0
- package/dist/math.d.ts.map +1 -0
- package/dist/math.js +105 -0
- package/dist/math.js.map +1 -0
- package/dist/normalize.d.ts +47 -0
- package/dist/normalize.d.ts.map +1 -0
- package/dist/normalize.js +115 -0
- package/dist/normalize.js.map +1 -0
- package/package.json +31 -0
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ABI-aware encoding utilities for Starknet calldata.
|
|
3
|
+
*
|
|
4
|
+
* This module provides utilities that convert SDK types to the format expected
|
|
5
|
+
* by starknet.js CallData.compile() with ABI awareness and supports single-type
|
|
6
|
+
* encode/decode via parseCalldataField.
|
|
7
|
+
*
|
|
8
|
+
* Using ABI-aware encoding avoids the zero-dropping serialization bug that
|
|
9
|
+
* occurs with manual array flattening.
|
|
10
|
+
*
|
|
11
|
+
* ## Why ABI-Aware Encoding?
|
|
12
|
+
*
|
|
13
|
+
* The starknet.js library has a serialization bug that can drop zero values (0n)
|
|
14
|
+
* at certain positions when using manual array flattening. By using CallData.compile()
|
|
15
|
+
* with method names and structured objects, starknet.js correctly handles the
|
|
16
|
+
* serialization through ABI resolution.
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
import { type Abi } from 'starknet';
|
|
21
|
+
import type { NormalDistributionRaw, NormalSqrtHintsRaw, SQ128x128Raw } from '@the-situation/core';
|
|
22
|
+
/**
|
|
23
|
+
* SQ128x128 in ABI format with snake_case field names.
|
|
24
|
+
*
|
|
25
|
+
* Field names must match the Cairo struct exactly:
|
|
26
|
+
* - limb0, limb1, limb2, limb3 (magnitude limbs)
|
|
27
|
+
* - neg (sign flag)
|
|
28
|
+
*/
|
|
29
|
+
export interface AbiSQ128x128 {
|
|
30
|
+
readonly limb0: bigint;
|
|
31
|
+
readonly limb1: bigint;
|
|
32
|
+
readonly limb2: bigint;
|
|
33
|
+
readonly limb3: bigint;
|
|
34
|
+
readonly neg: boolean;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Normal distribution in ABI format.
|
|
38
|
+
*/
|
|
39
|
+
export interface AbiNormalDistribution {
|
|
40
|
+
readonly mean: AbiSQ128x128;
|
|
41
|
+
readonly variance: AbiSQ128x128;
|
|
42
|
+
readonly sigma: AbiSQ128x128;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Hints in ABI format with snake_case field names.
|
|
46
|
+
*/
|
|
47
|
+
export interface AbiNormalSqrtHints {
|
|
48
|
+
readonly l2_norm_denom: AbiSQ128x128;
|
|
49
|
+
readonly backing_denom: AbiSQ128x128;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Converts SDK SQ128x128Raw type to ABI format.
|
|
53
|
+
*
|
|
54
|
+
* The SDK type and ABI type have the same field names, but this function
|
|
55
|
+
* provides a clear conversion point and type safety.
|
|
56
|
+
*
|
|
57
|
+
* @param raw - SDK SQ128x128Raw value
|
|
58
|
+
* @returns ABI-compatible SQ128x128
|
|
59
|
+
*/
|
|
60
|
+
export declare function toAbiSQ128x128(raw: SQ128x128Raw): AbiSQ128x128;
|
|
61
|
+
/**
|
|
62
|
+
* Converts SDK NormalDistributionRaw to ABI format.
|
|
63
|
+
*
|
|
64
|
+
* @param raw - SDK distribution value
|
|
65
|
+
* @returns ABI-compatible distribution
|
|
66
|
+
*/
|
|
67
|
+
export declare function toAbiDistribution(raw: NormalDistributionRaw): AbiNormalDistribution;
|
|
68
|
+
/**
|
|
69
|
+
* Converts SDK hints to ABI format with snake_case field names.
|
|
70
|
+
*
|
|
71
|
+
* Cairo uses snake_case for struct field names:
|
|
72
|
+
* - l2_norm_denom
|
|
73
|
+
* - backing_denom
|
|
74
|
+
*
|
|
75
|
+
* @param hints - SDK hints in ABI shape
|
|
76
|
+
* @returns ABI-compatible hints with snake_case field names
|
|
77
|
+
*/
|
|
78
|
+
export declare function toAbiHints(hints: NormalSqrtHintsRaw): AbiNormalSqrtHints;
|
|
79
|
+
/**
|
|
80
|
+
* Encodes a single ABI type using starknet.js parseCalldataField.
|
|
81
|
+
*
|
|
82
|
+
* This is useful when you need type-level encoding without a function context.
|
|
83
|
+
*/
|
|
84
|
+
export declare function encodeAbiType<T>(abi: Abi, typeName: string, value: T): string[];
|
|
85
|
+
/**
|
|
86
|
+
* Decodes a single ABI type using a synthetic ABI function.
|
|
87
|
+
*
|
|
88
|
+
* @param abi - Contract ABI that defines the type
|
|
89
|
+
* @param typeName - Fully-qualified ABI type name
|
|
90
|
+
* @param calldata - Encoded calldata for the type
|
|
91
|
+
*/
|
|
92
|
+
export declare function decodeAbiType<T>(abi: Abi, typeName: string, calldata: readonly (string | bigint)[]): T;
|
|
93
|
+
/**
|
|
94
|
+
* Encodes SQ128x128Raw using ABI type metadata.
|
|
95
|
+
*/
|
|
96
|
+
export declare function encodeSQ128x128RawType(abi: Abi, value: SQ128x128Raw): string[];
|
|
97
|
+
/**
|
|
98
|
+
* Decodes SQ128x128Raw using ABI type metadata.
|
|
99
|
+
*/
|
|
100
|
+
export declare function decodeSQ128x128RawType(abi: Abi, calldata: readonly (string | bigint)[]): SQ128x128Raw;
|
|
101
|
+
/**
|
|
102
|
+
* Encodes NormalDistributionRaw using ABI type metadata.
|
|
103
|
+
*/
|
|
104
|
+
export declare function encodeNormalDistributionRawType(abi: Abi, value: NormalDistributionRaw): string[];
|
|
105
|
+
/**
|
|
106
|
+
* Decodes NormalDistributionRaw using ABI type metadata.
|
|
107
|
+
*/
|
|
108
|
+
export declare function decodeNormalDistributionRawType(abi: Abi, calldata: readonly (string | bigint)[]): NormalDistributionRaw;
|
|
109
|
+
/**
|
|
110
|
+
* Encodes NormalSqrtHintsRaw using ABI type metadata.
|
|
111
|
+
*/
|
|
112
|
+
export declare function encodeNormalSqrtHintsRawType(abi: Abi, value: NormalSqrtHintsRaw): string[];
|
|
113
|
+
/**
|
|
114
|
+
* Decodes NormalSqrtHintsRaw using ABI type metadata.
|
|
115
|
+
*/
|
|
116
|
+
export declare function decodeNormalSqrtHintsRawType(abi: Abi, calldata: readonly (string | bigint)[]): NormalSqrtHintsRaw;
|
|
117
|
+
/**
|
|
118
|
+
* Encoder class for trade calldata using ABI-aware encoding.
|
|
119
|
+
*
|
|
120
|
+
* This class wraps starknet.js CallData with the AMM contract ABI to provide
|
|
121
|
+
* proper struct encoding for trade operations.
|
|
122
|
+
*
|
|
123
|
+
* @example
|
|
124
|
+
* ```typescript
|
|
125
|
+
* const encoder = new TradeCalldataEncoder(ammAbi);
|
|
126
|
+
* const calldata = encoder.encodeExecuteTrade(
|
|
127
|
+
* { mean: ..., variance: ..., sigma: ... },
|
|
128
|
+
* xStar,
|
|
129
|
+
* collateral,
|
|
130
|
+
* { l2_norm_denom: ..., backing_denom: ... }
|
|
131
|
+
* );
|
|
132
|
+
* ```
|
|
133
|
+
*/
|
|
134
|
+
export declare class TradeCalldataEncoder {
|
|
135
|
+
private readonly callData;
|
|
136
|
+
constructor(abi: Abi);
|
|
137
|
+
/**
|
|
138
|
+
* Encodes execute_trade calldata using ABI-aware encoding.
|
|
139
|
+
*
|
|
140
|
+
* The execute_trade function signature is:
|
|
141
|
+
* ```cairo
|
|
142
|
+
* fn execute_trade(
|
|
143
|
+
* ref self: ContractState,
|
|
144
|
+
* candidate: NormalDistribution,
|
|
145
|
+
* x_star: SQ128x128,
|
|
146
|
+
* supplied_collateral: SQ128x128,
|
|
147
|
+
* candidate_hints: NormalSqrtHints,
|
|
148
|
+
* ) -> TradeExecution
|
|
149
|
+
* ```
|
|
150
|
+
*
|
|
151
|
+
* @param candidate - The candidate distribution (mean, variance, sigma)
|
|
152
|
+
* @param xStar - The x* value for collateral computation
|
|
153
|
+
* @param suppliedCollateral - The collateral to supply
|
|
154
|
+
* @param hints - Precomputed hints for the candidate distribution
|
|
155
|
+
* @returns Encoded calldata as string array
|
|
156
|
+
*/
|
|
157
|
+
encodeExecuteTrade(candidate: NormalDistributionRaw, xStar: SQ128x128Raw, suppliedCollateral: SQ128x128Raw, hints: NormalSqrtHintsRaw): string[];
|
|
158
|
+
/**
|
|
159
|
+
* Encodes close_position calldata using ABI-aware encoding.
|
|
160
|
+
*
|
|
161
|
+
* The close_position function signature is:
|
|
162
|
+
* ```cairo
|
|
163
|
+
* fn close_position(
|
|
164
|
+
* ref self: ContractState,
|
|
165
|
+
* x_star: SQ128x128,
|
|
166
|
+
* additional_collateral: SQ128x128,
|
|
167
|
+
* target_hints: NormalSqrtHints,
|
|
168
|
+
* ) -> PositionCloseResult
|
|
169
|
+
* ```
|
|
170
|
+
*
|
|
171
|
+
* @param xStar - The x* value for collateral computation
|
|
172
|
+
* @param additionalCollateral - Additional collateral to supply
|
|
173
|
+
* @param targetHints - Hints for the target (flat) distribution
|
|
174
|
+
* @returns Encoded calldata as string array
|
|
175
|
+
*/
|
|
176
|
+
encodeClosePosition(xStar: SQ128x128Raw, additionalCollateral: SQ128x128Raw, targetHints: NormalSqrtHintsRaw): string[];
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Creates a TradeCalldataEncoder for the given ABI.
|
|
180
|
+
*
|
|
181
|
+
* @param abi - The AMM contract ABI
|
|
182
|
+
* @returns A new TradeCalldataEncoder instance
|
|
183
|
+
*/
|
|
184
|
+
export declare function createTradeEncoder(abi: Abi): TradeCalldataEncoder;
|
|
185
|
+
//# sourceMappingURL=abi-encode.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi-encode.d.ts","sourceRoot":"","sources":["../src/abi-encode.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EACL,KAAK,GAAG,EAMT,MAAM,UAAU,CAAC;AAClB,OAAO,KAAK,EAAE,qBAAqB,EAAE,kBAAkB,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAEnG;;;;;;GAMG;AACH,MAAM,WAAW,YAAY;IAC3B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,GAAG,EAAE,OAAO,CAAC;CACvB;AAED;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,QAAQ,CAAC,IAAI,EAAE,YAAY,CAAC;IAC5B,QAAQ,CAAC,QAAQ,EAAE,YAAY,CAAC;IAChC,QAAQ,CAAC,KAAK,EAAE,YAAY,CAAC;CAC9B;AAED;;GAEG;AACH,MAAM,WAAW,kBAAkB;IAEjC,QAAQ,CAAC,aAAa,EA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M,EAAE,CAE1F;AAED;;GAEG;AACH,wBAAgB,4BAA4B,CAC1C,GAAG,EAAE,GAAG,EACR,QAAQ,EAAE,SAAS,CAAC,MAAM,GAAG,MAAM,CAAC,EAAE,GACrC,kBAAkB,CAEpB;AAED;;;;;;;;;;;;;;;;GAgBG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAAW;gBAExB,GAAG,EAAE,GAAG;IAIpB;;;;;;;;;;;;;;;;;;;OAmBG;IACH,kBAAkB,CAChB,SAAS,EAAE,qBAAqB,EAChC,KAAK,EAAE,YAAY,EACnB,kBAAkB,EAAE,YAAY,EAChC,KAAK,EAAE,kBAAkB,GACxB,MAAM,EAAE;IAYX;;;;;;;;;;;;;;;;;OAiBG;IACH,mBAAmB,CACjB,KAAK,EAAE,YAAY,EACnB,oBAAoB,EAAE,YAAY,EAClC,WAAW,EAAE,kBAAkB,GAC9B,MAAM,EAAE;CAUZ;AAED;;;;;GAKG;AACH,wBAAgB,kBAAkB,CAAC,GAAG,EAAE,GAAG,GAAG,oBAAoB,CAEjE"}
|
|
@@ -0,0 +1,278 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ABI-aware encoding utilities for Starknet calldata.
|
|
3
|
+
*
|
|
4
|
+
* This module provides utilities that convert SDK types to the format expected
|
|
5
|
+
* by starknet.js CallData.compile() with ABI awareness and supports single-type
|
|
6
|
+
* encode/decode via parseCalldataField.
|
|
7
|
+
*
|
|
8
|
+
* Using ABI-aware encoding avoids the zero-dropping serialization bug that
|
|
9
|
+
* occurs with manual array flattening.
|
|
10
|
+
*
|
|
11
|
+
* ## Why ABI-Aware Encoding?
|
|
12
|
+
*
|
|
13
|
+
* The starknet.js library has a serialization bug that can drop zero values (0n)
|
|
14
|
+
* at certain positions when using manual array flattening. By using CallData.compile()
|
|
15
|
+
* with method names and structured objects, starknet.js correctly handles the
|
|
16
|
+
* serialization through ABI resolution.
|
|
17
|
+
*
|
|
18
|
+
* @module
|
|
19
|
+
*/
|
|
20
|
+
import { CallData, createAbiParser, hdParsingStrategy, parseCalldataField, } from 'starknet';
|
|
21
|
+
/**
|
|
22
|
+
* Converts SDK SQ128x128Raw type to ABI format.
|
|
23
|
+
*
|
|
24
|
+
* The SDK type and ABI type have the same field names, but this function
|
|
25
|
+
* provides a clear conversion point and type safety.
|
|
26
|
+
*
|
|
27
|
+
* @param raw - SDK SQ128x128Raw value
|
|
28
|
+
* @returns ABI-compatible SQ128x128
|
|
29
|
+
*/
|
|
30
|
+
export function toAbiSQ128x128(raw) {
|
|
31
|
+
return {
|
|
32
|
+
limb0: raw.limb0,
|
|
33
|
+
limb1: raw.limb1,
|
|
34
|
+
limb2: raw.limb2,
|
|
35
|
+
limb3: raw.limb3,
|
|
36
|
+
neg: raw.neg,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Converts SDK NormalDistributionRaw to ABI format.
|
|
41
|
+
*
|
|
42
|
+
* @param raw - SDK distribution value
|
|
43
|
+
* @returns ABI-compatible distribution
|
|
44
|
+
*/
|
|
45
|
+
export function toAbiDistribution(raw) {
|
|
46
|
+
return {
|
|
47
|
+
mean: toAbiSQ128x128(raw.mean),
|
|
48
|
+
variance: toAbiSQ128x128(raw.variance),
|
|
49
|
+
sigma: toAbiSQ128x128(raw.sigma),
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Converts SDK hints to ABI format with snake_case field names.
|
|
54
|
+
*
|
|
55
|
+
* Cairo uses snake_case for struct field names:
|
|
56
|
+
* - l2_norm_denom
|
|
57
|
+
* - backing_denom
|
|
58
|
+
*
|
|
59
|
+
* @param hints - SDK hints in ABI shape
|
|
60
|
+
* @returns ABI-compatible hints with snake_case field names
|
|
61
|
+
*/
|
|
62
|
+
export function toAbiHints(hints) {
|
|
63
|
+
return {
|
|
64
|
+
// biome-ignore lint/style/useNamingConvention: Cairo uses snake_case
|
|
65
|
+
l2_norm_denom: toAbiSQ128x128(hints.l2_norm_denom),
|
|
66
|
+
// biome-ignore lint/style/useNamingConvention: Cairo uses snake_case
|
|
67
|
+
backing_denom: toAbiSQ128x128(hints.backing_denom),
|
|
68
|
+
};
|
|
69
|
+
}
|
|
70
|
+
// ============================================================================
|
|
71
|
+
// Type Codec (ABI-aware single-type encode/decode)
|
|
72
|
+
// ============================================================================
|
|
73
|
+
const SQ128X128_RAW_ABI_TYPE = 'the_situation::types::sq128::types::SQ128x128Raw';
|
|
74
|
+
const NORMAL_DISTRIBUTION_RAW_ABI_TYPE = 'the_situation::types::normal_distribution::NormalDistributionRaw';
|
|
75
|
+
const NORMAL_SQRT_HINTS_RAW_ABI_TYPE = 'the_situation::onchain::common::NormalSqrtHintsRaw';
|
|
76
|
+
function findAbiTypeEntry(abi, typeName) {
|
|
77
|
+
const entry = abi.find((abiItem) => {
|
|
78
|
+
if (!abiItem || typeof abiItem !== 'object') {
|
|
79
|
+
return false;
|
|
80
|
+
}
|
|
81
|
+
const item = abiItem;
|
|
82
|
+
return item.name === typeName && (item.type === 'struct' || item.type === 'enum');
|
|
83
|
+
});
|
|
84
|
+
if (!entry) {
|
|
85
|
+
throw new Error(`ABI type not found: ${typeName}`);
|
|
86
|
+
}
|
|
87
|
+
return entry;
|
|
88
|
+
}
|
|
89
|
+
function getDecodeFunctionName(abi) {
|
|
90
|
+
const baseName = '__type_decode__';
|
|
91
|
+
let name = baseName;
|
|
92
|
+
let suffix = 0;
|
|
93
|
+
while (abi.some((abiItem) => {
|
|
94
|
+
if (!abiItem || typeof abiItem !== 'object') {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
const item = abiItem;
|
|
98
|
+
return item.name === name;
|
|
99
|
+
})) {
|
|
100
|
+
suffix += 1;
|
|
101
|
+
name = `${baseName}${suffix}`;
|
|
102
|
+
}
|
|
103
|
+
return name;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Encodes a single ABI type using starknet.js parseCalldataField.
|
|
107
|
+
*
|
|
108
|
+
* This is useful when you need type-level encoding without a function context.
|
|
109
|
+
*/
|
|
110
|
+
export function encodeAbiType(abi, typeName, value) {
|
|
111
|
+
const typeEntry = findAbiTypeEntry(abi, typeName);
|
|
112
|
+
const structs = CallData.getAbiStruct(abi);
|
|
113
|
+
const enums = CallData.getAbiEnum(abi);
|
|
114
|
+
const parser = createAbiParser(abi, hdParsingStrategy);
|
|
115
|
+
const argsIterator = [value][Symbol.iterator]();
|
|
116
|
+
const inputAbi = { name: typeEntry.type, type: typeEntry.name };
|
|
117
|
+
const encoded = parseCalldataField({
|
|
118
|
+
argsIterator,
|
|
119
|
+
input: inputAbi,
|
|
120
|
+
structs,
|
|
121
|
+
enums,
|
|
122
|
+
parser,
|
|
123
|
+
});
|
|
124
|
+
return Array.isArray(encoded) ? encoded : [encoded];
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Decodes a single ABI type using a synthetic ABI function.
|
|
128
|
+
*
|
|
129
|
+
* @param abi - Contract ABI that defines the type
|
|
130
|
+
* @param typeName - Fully-qualified ABI type name
|
|
131
|
+
* @param calldata - Encoded calldata for the type
|
|
132
|
+
*/
|
|
133
|
+
export function decodeAbiType(abi, typeName, calldata) {
|
|
134
|
+
const typeEntry = findAbiTypeEntry(abi, typeName);
|
|
135
|
+
const methodName = getDecodeFunctionName(abi);
|
|
136
|
+
const decodeAbi = [
|
|
137
|
+
...abi,
|
|
138
|
+
{
|
|
139
|
+
type: 'function',
|
|
140
|
+
name: methodName,
|
|
141
|
+
inputs: [],
|
|
142
|
+
outputs: [{ name: 'value', type: typeEntry.name }],
|
|
143
|
+
state_mutability: 'view',
|
|
144
|
+
},
|
|
145
|
+
];
|
|
146
|
+
const callData = new CallData(decodeAbi, hdParsingStrategy);
|
|
147
|
+
const response = calldata.map((value) => value.toString());
|
|
148
|
+
const parsed = callData.parse(methodName, response);
|
|
149
|
+
return parsed.value;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Encodes SQ128x128Raw using ABI type metadata.
|
|
153
|
+
*/
|
|
154
|
+
export function encodeSQ128x128RawType(abi, value) {
|
|
155
|
+
return encodeAbiType(abi, SQ128X128_RAW_ABI_TYPE, value);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Decodes SQ128x128Raw using ABI type metadata.
|
|
159
|
+
*/
|
|
160
|
+
export function decodeSQ128x128RawType(abi, calldata) {
|
|
161
|
+
return decodeAbiType(abi, SQ128X128_RAW_ABI_TYPE, calldata);
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Encodes NormalDistributionRaw using ABI type metadata.
|
|
165
|
+
*/
|
|
166
|
+
export function encodeNormalDistributionRawType(abi, value) {
|
|
167
|
+
return encodeAbiType(abi, NORMAL_DISTRIBUTION_RAW_ABI_TYPE, value);
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Decodes NormalDistributionRaw using ABI type metadata.
|
|
171
|
+
*/
|
|
172
|
+
export function decodeNormalDistributionRawType(abi, calldata) {
|
|
173
|
+
return decodeAbiType(abi, NORMAL_DISTRIBUTION_RAW_ABI_TYPE, calldata);
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Encodes NormalSqrtHintsRaw using ABI type metadata.
|
|
177
|
+
*/
|
|
178
|
+
export function encodeNormalSqrtHintsRawType(abi, value) {
|
|
179
|
+
return encodeAbiType(abi, NORMAL_SQRT_HINTS_RAW_ABI_TYPE, value);
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Decodes NormalSqrtHintsRaw using ABI type metadata.
|
|
183
|
+
*/
|
|
184
|
+
export function decodeNormalSqrtHintsRawType(abi, calldata) {
|
|
185
|
+
return decodeAbiType(abi, NORMAL_SQRT_HINTS_RAW_ABI_TYPE, calldata);
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* Encoder class for trade calldata using ABI-aware encoding.
|
|
189
|
+
*
|
|
190
|
+
* This class wraps starknet.js CallData with the AMM contract ABI to provide
|
|
191
|
+
* proper struct encoding for trade operations.
|
|
192
|
+
*
|
|
193
|
+
* @example
|
|
194
|
+
* ```typescript
|
|
195
|
+
* const encoder = new TradeCalldataEncoder(ammAbi);
|
|
196
|
+
* const calldata = encoder.encodeExecuteTrade(
|
|
197
|
+
* { mean: ..., variance: ..., sigma: ... },
|
|
198
|
+
* xStar,
|
|
199
|
+
* collateral,
|
|
200
|
+
* { l2_norm_denom: ..., backing_denom: ... }
|
|
201
|
+
* );
|
|
202
|
+
* ```
|
|
203
|
+
*/
|
|
204
|
+
export class TradeCalldataEncoder {
|
|
205
|
+
callData;
|
|
206
|
+
constructor(abi) {
|
|
207
|
+
this.callData = new CallData(abi);
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Encodes execute_trade calldata using ABI-aware encoding.
|
|
211
|
+
*
|
|
212
|
+
* The execute_trade function signature is:
|
|
213
|
+
* ```cairo
|
|
214
|
+
* fn execute_trade(
|
|
215
|
+
* ref self: ContractState,
|
|
216
|
+
* candidate: NormalDistribution,
|
|
217
|
+
* x_star: SQ128x128,
|
|
218
|
+
* supplied_collateral: SQ128x128,
|
|
219
|
+
* candidate_hints: NormalSqrtHints,
|
|
220
|
+
* ) -> TradeExecution
|
|
221
|
+
* ```
|
|
222
|
+
*
|
|
223
|
+
* @param candidate - The candidate distribution (mean, variance, sigma)
|
|
224
|
+
* @param xStar - The x* value for collateral computation
|
|
225
|
+
* @param suppliedCollateral - The collateral to supply
|
|
226
|
+
* @param hints - Precomputed hints for the candidate distribution
|
|
227
|
+
* @returns Encoded calldata as string array
|
|
228
|
+
*/
|
|
229
|
+
encodeExecuteTrade(candidate, xStar, suppliedCollateral, hints) {
|
|
230
|
+
return this.callData.compile('execute_trade', {
|
|
231
|
+
candidate: toAbiDistribution(candidate),
|
|
232
|
+
// biome-ignore lint/style/useNamingConvention: Cairo uses snake_case
|
|
233
|
+
x_star: toAbiSQ128x128(xStar),
|
|
234
|
+
// biome-ignore lint/style/useNamingConvention: Cairo uses snake_case
|
|
235
|
+
supplied_collateral: toAbiSQ128x128(suppliedCollateral),
|
|
236
|
+
// biome-ignore lint/style/useNamingConvention: Cairo uses snake_case
|
|
237
|
+
candidate_hints: toAbiHints(hints),
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Encodes close_position calldata using ABI-aware encoding.
|
|
242
|
+
*
|
|
243
|
+
* The close_position function signature is:
|
|
244
|
+
* ```cairo
|
|
245
|
+
* fn close_position(
|
|
246
|
+
* ref self: ContractState,
|
|
247
|
+
* x_star: SQ128x128,
|
|
248
|
+
* additional_collateral: SQ128x128,
|
|
249
|
+
* target_hints: NormalSqrtHints,
|
|
250
|
+
* ) -> PositionCloseResult
|
|
251
|
+
* ```
|
|
252
|
+
*
|
|
253
|
+
* @param xStar - The x* value for collateral computation
|
|
254
|
+
* @param additionalCollateral - Additional collateral to supply
|
|
255
|
+
* @param targetHints - Hints for the target (flat) distribution
|
|
256
|
+
* @returns Encoded calldata as string array
|
|
257
|
+
*/
|
|
258
|
+
encodeClosePosition(xStar, additionalCollateral, targetHints) {
|
|
259
|
+
return this.callData.compile('close_position', {
|
|
260
|
+
// biome-ignore lint/style/useNamingConvention: Cairo uses snake_case
|
|
261
|
+
x_star: toAbiSQ128x128(xStar),
|
|
262
|
+
// biome-ignore lint/style/useNamingConvention: Cairo uses snake_case
|
|
263
|
+
additional_collateral: toAbiSQ128x128(additionalCollateral),
|
|
264
|
+
// biome-ignore lint/style/useNamingConvention: Cairo uses snake_case
|
|
265
|
+
target_hints: toAbiHints(targetHints),
|
|
266
|
+
});
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
/**
|
|
270
|
+
* Creates a TradeCalldataEncoder for the given ABI.
|
|
271
|
+
*
|
|
272
|
+
* @param abi - The AMM contract ABI
|
|
273
|
+
* @returns A new TradeCalldataEncoder instance
|
|
274
|
+
*/
|
|
275
|
+
export function createTradeEncoder(abi) {
|
|
276
|
+
return new TradeCalldataEncoder(abi);
|
|
277
|
+
}
|
|
278
|
+
//# sourceMappingURL=abi-encode.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"abi-encode.js","sourceRoot":"","sources":["../src/abi-encode.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAEH,OAAO,EAGL,QAAQ,EACR,eAAe,EACf,iBAAiB,EACjB,kBAAkB,GACnB,MAAM,UAAU,CAAC;AAqClB;;;;;;;;GAQG;AACH,MAAM,UAAU,cAAc,CAAC,GAAiB;IAC9C,OAAO;QACL,KAAK,EAAE,GAAG,CAAC,KAAK;QAChB,KAAK,EAAE,GAAG,CAAC,KAAK;QAChB,KAAK,EAAE,GAAG,CAAC,KAAK;QAChB,KAAK,EAAE,GAAG,CAAC,KAAK;QAChB,GAAG,EAAE,GAAG,CAAC,GAAG;KACb,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,GAA0B;IAC1D,OAAO;QACL,IAAI,EAAE,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC;QAC9B,QAAQ,EAAE,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC;QACtC,KAAK,EAAE,cAAc,CAAC,GAAG,CAAC,KAAK,CAAC;KACjC,CAAC;AACJ,CAAC;AAED;;;;;;;;;GASG;AACH,MAAM,UAAU,UAAU,CAAC,KAAyB;IAClD,OAAO;QACL,qEAAqE;QACrE,aAAa,EAAE,cAAc,CAAC,KAAK,CAAC,aAAa,CAAC;QAClD,qEAAqE;QACrE,aAAa,EAAE,cAAc,CAAC,KAAK,CAAC,aAAa,CAAC;KACnD,CAAC;AACJ,CAAC;AAED,+EAA+E;AAC/E,mDAAmD;AACnD,+EAA+E;AAE/E,MAAM,sBAAsB,GAAG,kDAAkD,CAAC;AAClF,MAAM,gCAAgC,GACpC,kEAAkE,CAAC;AACrE,MAAM,8BAA8B,GAAG,oDAAoD,CAAC;AAE5F,SAAS,gBAA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,QAAQ,CAAC,GAAG,CAAC,CAAC;IACpC,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,kBAAkB,CAChB,SAAgC,EAChC,KAAmB,EACnB,kBAAgC,EAChC,KAAyB;QAEzB,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,eAAe,EAAE;YAC5C,SAAS,EAAE,iBAAiB,CAAC,SAAS,CAAC;YACvC,qEAAqE;YACrE,MAAM,EAAE,cAAc,CAAC,KAAK,CAAC;YAC7B,qEAAqE;YACrE,mBAAmB,EAAE,cAAc,CAAC,kBAAkB,CAAC;YACvD,qEAAqE;YACrE,eAAe,EAAE,UAAU,CAAC,KAAK,CAAC;SACnC,CAAC,CAAC;IACL,CAAC;IAED;;;;;;;;;;;;;;;;;OAiBG;IACH,mBAAmB,CACjB,KAAmB,EACnB,oBAAkC,EAClC,WAA+B;QAE/B,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,CAAC,gBAAgB,EAAE;YAC7C,qEAAqE;YACrE,MAAM,EAAE,cAAc,CAAC,KAAK,CAAC;YAC7B,qEAAqE;YACrE,qBAAqB,EAAE,cAAc,CAAC,oBAAoB,CAAC;YAC3D,qEAAqE;YACrE,YAAY,EAAE,UAAU,CAAC,WAAW,CAAC;SACtC,CAAC,CAAC;IACL,CAAC;CACF;AAED;;;;;GAKG;AACH,MAAM,UAAU,kBAAkB,CAAC,GAAQ;IACzC,OAAO,IAAI,oBAAoB,CAAC,GAAG,CAAC,CAAC;AACvC,CAAC"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Address formatting utilities.
|
|
3
|
+
*
|
|
4
|
+
* Ensures consistent hex format for all Starknet addresses.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
/**
|
|
9
|
+
* Converts any address format to hex string.
|
|
10
|
+
*
|
|
11
|
+
* Handles:
|
|
12
|
+
* - Hex strings (0x...) - returned as-is
|
|
13
|
+
* - Decimal strings - converted to hex
|
|
14
|
+
* - BigInt values - converted to hex
|
|
15
|
+
*
|
|
16
|
+
* @param address - Address in any format
|
|
17
|
+
* @returns Address as hex string (0x prefixed)
|
|
18
|
+
*
|
|
19
|
+
* @example
|
|
20
|
+
* ```typescript
|
|
21
|
+
* toHexAddress('0x049d36570d4e46f48e99674bd3fcc84644ddd6b9')
|
|
22
|
+
* // => '0x049d36570d4e46f48e99674bd3fcc84644ddd6b9'
|
|
23
|
+
*
|
|
24
|
+
* toHexAddress('2009894490435840142178903814830')
|
|
25
|
+
* // => '0x049d36570d4e46f48e99674bd3fcc84644ddd6b9'
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
export declare function toHexAddress(address: string | bigint): string;
|
|
29
|
+
//# sourceMappingURL=address.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address.d.ts","sourceRoot":"","sources":["../src/address.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAIH;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,MAAM,GAAG,MAAM,GAAG,MAAM,CAQ7D"}
|
package/dist/address.js
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Address formatting utilities.
|
|
3
|
+
*
|
|
4
|
+
* Ensures consistent hex format for all Starknet addresses.
|
|
5
|
+
*
|
|
6
|
+
* @module
|
|
7
|
+
*/
|
|
8
|
+
import { num } from 'starknet';
|
|
9
|
+
/**
|
|
10
|
+
* Converts any address format to hex string.
|
|
11
|
+
*
|
|
12
|
+
* Handles:
|
|
13
|
+
* - Hex strings (0x...) - returned as-is
|
|
14
|
+
* - Decimal strings - converted to hex
|
|
15
|
+
* - BigInt values - converted to hex
|
|
16
|
+
*
|
|
17
|
+
* @param address - Address in any format
|
|
18
|
+
* @returns Address as hex string (0x prefixed)
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```typescript
|
|
22
|
+
* toHexAddress('0x049d36570d4e46f48e99674bd3fcc84644ddd6b9')
|
|
23
|
+
* // => '0x049d36570d4e46f48e99674bd3fcc84644ddd6b9'
|
|
24
|
+
*
|
|
25
|
+
* toHexAddress('2009894490435840142178903814830')
|
|
26
|
+
* // => '0x049d36570d4e46f48e99674bd3fcc84644ddd6b9'
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export function toHexAddress(address) {
|
|
30
|
+
if (typeof address === 'bigint') {
|
|
31
|
+
return num.toHex(address);
|
|
32
|
+
}
|
|
33
|
+
if (address.startsWith('0x')) {
|
|
34
|
+
return address;
|
|
35
|
+
}
|
|
36
|
+
return num.toHex(address);
|
|
37
|
+
}
|
|
38
|
+
//# sourceMappingURL=address.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"address.js","sourceRoot":"","sources":["../src/address.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AAEH,OAAO,EAAE,GAAG,EAAE,MAAM,UAAU,CAAC;AAE/B;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,UAAU,YAAY,CAAC,OAAwB;IACnD,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE,CAAC;QAChC,OAAO,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;IAC5B,CAAC;IACD,IAAI,OAAO,CAAC,UAAU,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7B,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,OAAO,GAAG,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC;AAC5B,CAAC"}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Call format conversion utilities for wallet connector compatibility.
|
|
3
|
+
*
|
|
4
|
+
* starknet.js uses camelCase (`contractAddress`, `entrypoint`) but the
|
|
5
|
+
* Starknet JSON-RPC spec uses snake_case (`contract_address`, `entry_point`).
|
|
6
|
+
* Some wallet connectors (e.g., starknetkit) validate against the RPC format.
|
|
7
|
+
*
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
import { type Call } from 'starknet';
|
|
11
|
+
/**
|
|
12
|
+
* Call object in snake_case format (JSON-RPC spec format).
|
|
13
|
+
*
|
|
14
|
+
* Used by some wallet connectors like starknetkit that validate
|
|
15
|
+
* against the raw RPC format rather than starknet.js types.
|
|
16
|
+
*/
|
|
17
|
+
export interface CallRpc {
|
|
18
|
+
contract_address: string;
|
|
19
|
+
entry_point: string;
|
|
20
|
+
calldata: string[];
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Converts a starknet.js Call to snake_case RPC format.
|
|
24
|
+
*
|
|
25
|
+
* Also normalizes all addresses and calldata values to hex format
|
|
26
|
+
* for maximum wallet connector compatibility.
|
|
27
|
+
*
|
|
28
|
+
* Use this when your wallet connector (e.g., starknetkit) expects
|
|
29
|
+
* snake_case field names instead of starknet.js camelCase format.
|
|
30
|
+
*
|
|
31
|
+
* @param call - starknet.js Call object (camelCase)
|
|
32
|
+
* @returns Call in RPC format (snake_case) with hex-formatted values
|
|
33
|
+
*
|
|
34
|
+
* @example
|
|
35
|
+
* ```typescript
|
|
36
|
+
* import { toRpcCall } from '@the-situation/sdk';
|
|
37
|
+
*
|
|
38
|
+
* const trade = await market.prepareTrade({ targetMean: 105 });
|
|
39
|
+
* const rpcCalls = trade.calls.map(toRpcCall);
|
|
40
|
+
* await account.execute(rpcCalls);
|
|
41
|
+
* ```
|
|
42
|
+
*/
|
|
43
|
+
export declare function toRpcCall(call: Call): CallRpc;
|
|
44
|
+
/**
|
|
45
|
+
* Converts an array of starknet.js Calls to snake_case RPC format.
|
|
46
|
+
*
|
|
47
|
+
* Convenience wrapper for `calls.map(toRpcCall)`.
|
|
48
|
+
*
|
|
49
|
+
* @param calls - Array of starknet.js Call objects (camelCase)
|
|
50
|
+
* @returns Array of Calls in RPC format (snake_case)
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```typescript
|
|
54
|
+
* import { toRpcCalls } from '@the-situation/sdk';
|
|
55
|
+
*
|
|
56
|
+
* const trade = await market.prepareTrade({ targetMean: 105 });
|
|
57
|
+
* await account.execute(toRpcCalls(trade.calls));
|
|
58
|
+
* ```
|
|
59
|
+
*/
|
|
60
|
+
export declare function toRpcCalls(calls: Call[]): CallRpc[];
|
|
61
|
+
//# sourceMappingURL=call-format.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"call-format.d.ts","sourceRoot":"","sources":["../src/call-format.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAEH,OAAO,EAAE,KAAK,IAAI,EAAO,MAAM,UAAU,CAAC;AAG1C;;;;;GAKG;AACH,MAAM,WAAW,OAAO;IACtB,gBAAgB,EAAE,MAAM,CAAC;IACzB,WAAW,EAAE,MAAM,CAAC;IACpB,QAAQ,EAAE,MAAM,EAAE,CAAC;CACpB;AAmBD;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,SAAS,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAM7C;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,UAAU,CAAC,KAAK,EAAE,IAAI,EAAE,GAAG,OAAO,EAAE,CAEnD"}
|