@avail-project/ca-common 1.0.0-beta.6 → 1.0.0-beta.8

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.
Files changed (41) hide show
  1. package/dist/cjs/_polyfill.js +147 -0
  2. package/dist/cjs/data/chaindata.js +43 -29
  3. package/dist/cjs/data/chainid.js +4 -2
  4. package/dist/cjs/index.js +4 -4
  5. package/dist/cjs/proto/client.js +48 -0
  6. package/dist/cjs/proto/definition.js +167 -448
  7. package/dist/cjs/proto/grpc.js +4 -9
  8. package/dist/cjs/rff/rff.js +0 -35
  9. package/dist/cjs/vaultcontracts/vaultcontracts.js +70 -29
  10. package/dist/cjs/xcs/autochoice.js +255 -0
  11. package/dist/esm/_polyfill.js +143 -0
  12. package/dist/esm/data/chaindata.js +43 -29
  13. package/dist/esm/data/chainid.js +4 -2
  14. package/dist/esm/index.js +13 -12
  15. package/dist/esm/proto/client.js +11 -0
  16. package/dist/esm/proto/definition.js +167 -448
  17. package/dist/esm/proto/grpc.js +4 -9
  18. package/dist/esm/rff/rff.js +0 -35
  19. package/dist/esm/vaultcontracts/vaultcontracts.js +70 -29
  20. package/dist/esm/xcs/autochoice.js +254 -0
  21. package/dist/types/_polyfill.d.ts +1 -0
  22. package/dist/types/index.d.ts +13 -12
  23. package/dist/types/proto/client.d.ts +2 -0
  24. package/dist/types/rff/rff.d.ts +0 -3
  25. package/dist/types/xcs/autochoice.d.ts +17 -0
  26. package/package.json +14 -8
  27. package/dist/cjs/fuelcontracts/ArcanaVault.js +0 -2407
  28. package/dist/cjs/fuelcontracts/ArcanaVaultFactory.js +0 -18
  29. package/dist/cjs/fuelcontracts/common.js +0 -3
  30. package/dist/cjs/fuelcontracts/index.js +0 -5
  31. package/dist/cjs/rff/fuel.js +0 -27
  32. package/dist/esm/fuelcontracts/ArcanaVault.js +0 -2402
  33. package/dist/esm/fuelcontracts/ArcanaVaultFactory.js +0 -14
  34. package/dist/esm/fuelcontracts/common.js +0 -2
  35. package/dist/esm/fuelcontracts/index.js +0 -2
  36. package/dist/esm/rff/fuel.js +0 -23
  37. package/dist/types/fuelcontracts/ArcanaVault.d.ts +0 -448
  38. package/dist/types/fuelcontracts/ArcanaVaultFactory.d.ts +0 -8
  39. package/dist/types/fuelcontracts/common.d.ts +0 -23
  40. package/dist/types/fuelcontracts/index.d.ts +0 -2
  41. package/dist/types/rff/fuel.d.ts +0 -4
@@ -1,6 +1,6 @@
1
1
  // Code generated by protoc-gen-ts_proto. DO NOT EDIT.
2
2
  // versions:
3
- // protoc-gen-ts_proto v2.7.5
3
+ // protoc-gen-ts_proto v2.8.3
4
4
  // protoc v6.32.1
5
5
  // source: grpc.proto
6
6
  /* eslint-disable */
@@ -259,19 +259,14 @@ export class GrpcWebImpl {
259
259
  unary(methodDesc, _request, metadata) {
260
260
  const request = { ..._request, ...methodDesc.requestType };
261
261
  const maybeCombinedMetadata = metadata && this.options.metadata
262
- ? new BrowserHeaders({
263
- ...this.options?.metadata.headersMap,
264
- ...metadata?.headersMap,
265
- })
266
- : (metadata ?? this.options.metadata);
262
+ ? new BrowserHeaders({ ...this.options?.metadata.headersMap, ...metadata?.headersMap })
263
+ : metadata ?? this.options.metadata;
267
264
  return new Promise((resolve, reject) => {
268
265
  grpc.unary(methodDesc, {
269
266
  request,
270
267
  host: this.host,
271
268
  metadata: maybeCombinedMetadata ?? {},
272
- ...(this.options.transport !== undefined
273
- ? { transport: this.options.transport }
274
- : {}),
269
+ ...(this.options.transport !== undefined ? { transport: this.options.transport } : {}),
275
270
  debug: this.options.debug ?? false,
276
271
  onEnd: function (response) {
277
272
  if (response.status === grpc.Code.OK) {
@@ -1,16 +1,12 @@
1
1
  import { bytesToBigInt } from "viem";
2
- import { BN } from "fuels";
3
2
  import { ezPadTo32Hex } from "../data";
4
- import { protobufUniverseToFuelUniverse } from "./fuel";
5
3
  export class OmniversalRFF {
6
4
  protobufRFF;
7
5
  evmRFF;
8
- fuelRFF;
9
6
  constructor(protobufRFF) {
10
7
  this.protobufRFF = protobufRFF;
11
8
  }
12
9
  asEVMRFF() {
13
- console.log("yge2i21ugeyu12igeui1g");
14
10
  if (this.evmRFF == null) {
15
11
  this.evmRFF = {
16
12
  sources: this.protobufRFF.sources.map((s) => ({
@@ -36,37 +32,6 @@ export class OmniversalRFF {
36
32
  }
37
33
  return this.evmRFF;
38
34
  }
39
- asFuelRFF() {
40
- if (this.fuelRFF == null) {
41
- this.fuelRFF = {
42
- sources: this.protobufRFF.sources.map((s) => ({
43
- universe: protobufUniverseToFuelUniverse(s.universe),
44
- chain_id: new BN(s.chainID),
45
- asset_id: {
46
- bits: ezPadTo32Hex(s.contractAddress),
47
- },
48
- value: new BN(s.value),
49
- })),
50
- destination_chain_id: new BN(this.protobufRFF.destinationChainID),
51
- destination_universe: protobufUniverseToFuelUniverse(this.protobufRFF.destinationUniverse),
52
- destinations: this.protobufRFF.destinations.map((d) => ({
53
- asset_id: {
54
- bits: ezPadTo32Hex(d.contractAddress),
55
- },
56
- value: new BN(d.value),
57
- })),
58
- expiry: new BN(this.protobufRFF.expiry.toBytesBE()),
59
- nonce: new BN(this.protobufRFF.nonce),
60
- parties: this.protobufRFF.signatureData.map((sd) => ({
61
- universe: protobufUniverseToFuelUniverse(sd.universe),
62
- address: {
63
- bits: ezPadTo32Hex(sd.address),
64
- },
65
- })),
66
- };
67
- }
68
- return this.fuelRFF;
69
- }
70
35
  asProtobufRFF() {
71
36
  return this.protobufRFF;
72
37
  }
@@ -14,31 +14,27 @@ const dataSets = new Map([
14
14
  [
15
15
  [
16
16
  new OmniversalChainID(Universe.ETHEREUM, 421614),
17
- "0xF0111EdE031a4377C34A4AD900f1E633E41055Dc",
17
+ "0xEFF0C81eC6D7c2a3B924e98B65303DDaa3030a81",
18
18
  ],
19
19
  [
20
20
  new OmniversalChainID(Universe.ETHEREUM, 11155420),
21
- "0xF0111EdE031a4377C34A4AD900f1E633E41055Dc",
21
+ "0xEFF0C81eC6D7c2a3B924e98B65303DDaa3030a81",
22
22
  ],
23
23
  [
24
24
  new OmniversalChainID(Universe.ETHEREUM, 80002),
25
- "0xF0111EdE031a4377C34A4AD900f1E633E41055Dc",
25
+ "0xEFF0C81eC6D7c2a3B924e98B65303DDaa3030a81",
26
26
  ],
27
27
  [
28
28
  new OmniversalChainID(Universe.ETHEREUM, 84532),
29
- "0xF0111EdE031a4377C34A4AD900f1E633E41055Dc",
29
+ "0xEFF0C81eC6D7c2a3B924e98B65303DDaa3030a81",
30
30
  ],
31
31
  [
32
32
  new OmniversalChainID(Universe.ETHEREUM, 11155111),
33
- "0xF0111EdE031a4377C34A4AD900f1E633E41055Dc",
33
+ "0xEFF0C81eC6D7c2a3B924e98B65303DDaa3030a81",
34
34
  ],
35
35
  [
36
36
  new OmniversalChainID(Universe.ETHEREUM, 10143),
37
- "0xF0111EdE031a4377C34A4AD900f1E633E41055Dc",
38
- ],
39
- [
40
- new OmniversalChainID(Universe.TRON, 2494104990n),
41
- "0x70f03baa2CD784447A4B133E41386562163209f8",
37
+ "0xEFF0C81eC6D7c2a3B924e98B65303DDaa3030a81",
42
38
  ],
43
39
  ],
44
40
  ],
@@ -47,31 +43,31 @@ const dataSets = new Map([
47
43
  [
48
44
  [
49
45
  new OmniversalChainID(Universe.ETHEREUM, 137),
50
- "0xB0BB1Ea8Eefb51BDA49631b09A350266e0F76EF3",
46
+ "0x111111eA4f8BdfB5AE22c37ebC3eE17b82072F57",
51
47
  ],
52
48
  [
53
49
  new OmniversalChainID(Universe.ETHEREUM, 10),
54
- "0xB0BB1Ea8Eefb51BDA49631b09A350266e0F76EF3",
50
+ "0x111111eA4f8BdfB5AE22c37ebC3eE17b82072F57",
55
51
  ],
56
52
  [
57
53
  new OmniversalChainID(Universe.ETHEREUM, 42161),
58
- "0xB0BB1Ea8Eefb51BDA49631b09A350266e0F76EF3",
54
+ "0x111111eA4f8BdfB5AE22c37ebC3eE17b82072F57",
59
55
  ],
60
56
  [
61
57
  new OmniversalChainID(Universe.ETHEREUM, 8453),
62
- "0xB0BB1Ea8Eefb51BDA49631b09A350266e0F76EF3",
58
+ "0x111111eA4f8BdfB5AE22c37ebC3eE17b82072F57",
63
59
  ],
64
60
  [
65
61
  new OmniversalChainID(Universe.ETHEREUM, 534352),
66
- "0xB0BB1Ea8Eefb51BDA49631b09A350266e0F76EF3",
62
+ "0x111111eA4f8BdfB5AE22c37ebC3eE17b82072F57",
67
63
  ],
68
64
  [
69
65
  new OmniversalChainID(Universe.ETHEREUM, 56),
70
- "0xB0BB1Ea8Eefb51BDA49631b09A350266e0F76EF3",
66
+ "0x111111eA4f8BdfB5AE22c37ebC3eE17b82072F57",
71
67
  ],
72
68
  [
73
69
  new OmniversalChainID(Universe.TRON, 728126428),
74
- "0x4ddDbe8D0D399B65E5898b6d2bC97Ae0683E8CB7",
70
+ "0x46de8c7e6f1da4dd851b62c20b78971f230fca5b",
75
71
  ],
76
72
  ],
77
73
  ],
@@ -80,39 +76,39 @@ const dataSets = new Map([
80
76
  [
81
77
  [
82
78
  new OmniversalChainID(Universe.ETHEREUM, 1),
83
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
79
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
84
80
  ],
85
81
  [
86
82
  new OmniversalChainID(Universe.ETHEREUM, 10),
87
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
83
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
88
84
  ],
89
85
  [
90
86
  new OmniversalChainID(Universe.ETHEREUM, 137),
91
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
87
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
92
88
  ],
93
89
  [
94
90
  new OmniversalChainID(Universe.ETHEREUM, 42161),
95
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
91
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
96
92
  ],
97
93
  [
98
94
  new OmniversalChainID(Universe.ETHEREUM, 534352),
99
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
95
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
100
96
  ],
101
97
  [
102
98
  new OmniversalChainID(Universe.ETHEREUM, 8453),
103
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
99
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
104
100
  ],
105
101
  [
106
102
  new OmniversalChainID(Universe.ETHEREUM, 43114),
107
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
103
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
108
104
  ],
109
105
  [
110
106
  new OmniversalChainID(Universe.ETHEREUM, 999),
111
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
107
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
112
108
  ],
113
109
  [
114
110
  new OmniversalChainID(Universe.ETHEREUM, 8217),
115
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
111
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
116
112
  ],
117
113
  [
118
114
  new OmniversalChainID(Universe.ETHEREUM, 50104),
@@ -120,11 +116,56 @@ const dataSets = new Map([
120
116
  ],
121
117
  [
122
118
  new OmniversalChainID(Universe.ETHEREUM, 56),
123
- "0xBADA557252D286e45a1AD73f32479062D4E2e86B",
119
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
120
+ ],
121
+ [
122
+ new OmniversalChainID(Universe.ETHEREUM, 143),
123
+ "0xC0DED5d7F424276c821AF21F68E1e663bC671C3D",
124
+ ],
125
+ ],
126
+ ],
127
+ [
128
+ Environment.JADE,
129
+ [
130
+ [
131
+ new OmniversalChainID(Universe.ETHEREUM, 1),
132
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
133
+ ],
134
+ [
135
+ new OmniversalChainID(Universe.ETHEREUM, 10),
136
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
137
+ ],
138
+ [
139
+ new OmniversalChainID(Universe.ETHEREUM, 137),
140
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
141
+ ],
142
+ [
143
+ new OmniversalChainID(Universe.ETHEREUM, 42161),
144
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
145
+ ],
146
+ [
147
+ new OmniversalChainID(Universe.ETHEREUM, 534352),
148
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
149
+ ],
150
+ [
151
+ new OmniversalChainID(Universe.ETHEREUM, 8453),
152
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
153
+ ],
154
+ [
155
+ new OmniversalChainID(Universe.ETHEREUM, 43114),
156
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
157
+ ],
158
+ [
159
+ new OmniversalChainID(Universe.ETHEREUM, 999),
160
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
124
161
  ],
125
162
  [
126
- new OmniversalChainID(Universe.FUEL, 9889),
127
- "0xe2586f908cc885e630cec6d8d578f02e6ade66983baf23f82757be502127dfb1",
163
+ new OmniversalChainID(Universe.ETHEREUM, 8217),
164
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
165
+ ],
166
+ [
167
+ new OmniversalChainID(Universe.ETHEREUM, 56),
168
+ "0x00000000ac0ac9d69424fA5Adc291D75Ec4a0F11",
128
169
  ],
129
170
  ],
130
171
  ],
@@ -56,6 +56,259 @@ export async function aggregateAggregators(requests, aggregators, mode) {
56
56
  }
57
57
  return final;
58
58
  }
59
+ /*
60
+ In original autoSelectSources:
61
+ Assets = [1 ETH, 1 COT, 1 ETH, 1 USDT, 1 COT]
62
+ Output = 4
63
+
64
+ First loop that just removes cot:
65
+ quoteAssets = [1 ETH, 1 ETH, 1 USDT]
66
+ Output = 4
67
+
68
+ Outside the function we can just remove all COT's and assume those as being used
69
+ but that's incorrect as we want to use assets in exact order as holdings array.
70
+ We can't remove only COT's that are going to be used because we don't know which ones are going
71
+ to get used.
72
+
73
+ Proposed solution:
74
+ It should actually use assets in exact order, so it cant be done outside the function
75
+ 1. The function has to keep an order of assets, separate out COT and non-COT assets.
76
+ 2. Get quote for non-COT assets, then loop over the original order
77
+ 3. Used either COT or quote depending on original order
78
+ 4. Send back quotes and COT's used
79
+
80
+ Alg:
81
+ Assets = [1 ETH, 1 COT, 1 ETH, 1 USDT, 1 COT]
82
+ Output = 4
83
+
84
+ 1. separate into two with indexes:
85
+ quotes = [(1 ETH, 0), (1 ETH, 2), (1 USDT, 3)]
86
+ cots = [(1 COT, 1), (1 COT, 4)]
87
+
88
+ 2. Get quotes using only quotes
89
+ 3. merge quote cots and sort by order
90
+ 4. loop assets (original order):
91
+ if a quote:
92
+ output = output - quote_output_amount
93
+ if a cot:
94
+ output = output - cot_amount
95
+ if output <= 0:
96
+ break
97
+ 5. return quotes and assets used.
98
+ */
99
+ export async function autoSelectSourcesV2(userAddress, holdings, outputRequired, aggregators, commonCurrencyID = CurrencyID.USDC) {
100
+ // Assumption: Holding is already sorted in usage priority
101
+ console.log("XCS | SSV2:", {
102
+ holdings,
103
+ outputRequired: outputRequired.toFixed(),
104
+ });
105
+ const fullLiquidationQuotes = [];
106
+ const cotList = [];
107
+ for (const [idx, holding] of holdings.entries()) {
108
+ const chain = ChaindataMap.get(holding.chainID);
109
+ if (chain == null) {
110
+ throw new AutoSelectionError("Chain not found");
111
+ }
112
+ const correspondingCurrency = chain.Currencies.find((cur) => cur.currencyID === commonCurrencyID);
113
+ if (correspondingCurrency == null) {
114
+ console.log("XCS | SS | Skipping because correspondingCurrency is null", {
115
+ chain,
116
+ correspondingCurrency,
117
+ });
118
+ continue;
119
+ }
120
+ if (Buffer.compare(holding.tokenAddress, correspondingCurrency.tokenAddress) === 0) {
121
+ const normalizedAmount = new Decimal(holding.amount).div(Decimal.pow(10, correspondingCurrency.decimals));
122
+ cotList.push({
123
+ amount: normalizedAmount,
124
+ idx,
125
+ chainID: holding.chainID,
126
+ currency: correspondingCurrency,
127
+ originalHolding: holding,
128
+ });
129
+ }
130
+ else {
131
+ fullLiquidationQuotes.push({
132
+ req: {
133
+ userAddress,
134
+ type: QuoteType.EXACT_IN,
135
+ chain: chain.ChainID,
136
+ inputToken: holding.tokenAddress,
137
+ inputAmount: holding.amount,
138
+ outputToken: correspondingCurrency.tokenAddress,
139
+ seriousness: QuoteSeriousness.PRICE_SURVEY,
140
+ },
141
+ originalHolding: holding,
142
+ cur: correspondingCurrency,
143
+ idx,
144
+ });
145
+ }
146
+ }
147
+ // Check if continuous COTs from the start can cover the entire requirement
148
+ // We can skip quoting unused holdings
149
+ if (cotList.length > 0 && cotList[0].idx === 0) {
150
+ let continuousCOTAmount = new Decimal(0);
151
+ let continuousCount = 0;
152
+ for (const cot of cotList) {
153
+ // only consecutive cots allowed, otherwise we need to go to quoting
154
+ if (cot.idx !== continuousCount)
155
+ break;
156
+ continuousCOTAmount = continuousCOTAmount.add(cot.amount);
157
+ continuousCount++;
158
+ if (continuousCOTAmount.gte(outputRequired)) {
159
+ console.log("XCS | SS | Continuous COTs can satisfy requirement, skipping quotes");
160
+ const usedCOTs = [];
161
+ let remainder = outputRequired;
162
+ for (let i = 0; i < continuousCount; i++) {
163
+ const cot = cotList[i];
164
+ const amountToUse = Decimal.min(remainder, cot.amount);
165
+ usedCOTs.push({
166
+ originalHolding: cot.originalHolding,
167
+ amountUsed: amountToUse,
168
+ idx: cot.idx,
169
+ cur: cot.currency,
170
+ });
171
+ remainder = remainder.minus(amountToUse);
172
+ if (remainder.lte(0))
173
+ break;
174
+ }
175
+ console.log("XCS | SS | Early return with continuous COTs:", {
176
+ cots: usedCOTs,
177
+ });
178
+ return { quotes: [], usedCOTs };
179
+ }
180
+ }
181
+ }
182
+ const processingQueue = [];
183
+ // Add COT holdings
184
+ for (const cot of cotList) {
185
+ processingQueue.push({
186
+ idx: cot.idx,
187
+ isCOT: true,
188
+ cotData: cot,
189
+ });
190
+ }
191
+ // Add non-COT holdings
192
+ for (let i = 0; i < fullLiquidationQuotes.length; i++) {
193
+ processingQueue.push({
194
+ idx: fullLiquidationQuotes[i].idx,
195
+ isCOT: false,
196
+ quoteData: fullLiquidationQuotes[i],
197
+ responseIdx: i,
198
+ });
199
+ }
200
+ // Sort by original index to maintain priority
201
+ processingQueue.sort((a, b) => a.idx - b.idx);
202
+ const responses = await aggregateAggregators(fullLiquidationQuotes.map((fq) => fq.req), aggregators, 0 /* AggregateAggregatorsMode.MaximizeOutput */);
203
+ console.log("XCS | SS | Responses:", responses);
204
+ const final = [];
205
+ const usedCOTs = [];
206
+ let remainder = outputRequired;
207
+ for (const item of processingQueue) {
208
+ if (remainder.lte(0)) {
209
+ break;
210
+ }
211
+ if (item.isCOT) {
212
+ // Process COT holding - direct usage, no quote
213
+ const { cotData } = item;
214
+ const amountToUse = Decimal.min(remainder, cotData.amount);
215
+ usedCOTs.push({
216
+ originalHolding: cotData.originalHolding,
217
+ amountUsed: amountToUse,
218
+ idx: cotData.idx,
219
+ cur: cotData.currency,
220
+ });
221
+ remainder = remainder.minus(amountToUse);
222
+ console.log("XCS | SS | Used COT", {
223
+ idx: cotData.idx,
224
+ amountToUse: amountToUse.toFixed(),
225
+ remainder: remainder.toFixed(),
226
+ });
227
+ }
228
+ else {
229
+ // Process non-COT holding - use existing quote logic
230
+ const { quoteData, responseIdx } = item;
231
+ const { quote: resp, aggregator: agg } = responses[responseIdx];
232
+ if (resp == null) {
233
+ continue;
234
+ }
235
+ console.log("XCS | SS | 1", {
236
+ i: responseIdx,
237
+ idx: quoteData.idx,
238
+ remainder,
239
+ q: quoteData,
240
+ resp,
241
+ agg,
242
+ });
243
+ const divisor = Decimal.pow(10, quoteData.cur.decimals);
244
+ const oamD = Decimal.div(resp.outputAmountMinimum, divisor);
245
+ if (oamD.gt(remainder)) {
246
+ const indicativePrice = Decimal.div(resp.inputAmount, resp.outputAmountMinimum);
247
+ const userBal = new Decimal(quoteData.originalHolding.amount);
248
+ // remainder is the output we want, so the input amount is remainder × indicativePrice
249
+ let expectedInput = Decimal.min(remainder.mul(divisor).mul(indicativePrice).mul(safetyMultiplier), userBal);
250
+ while (true) {
251
+ console.log("XCS | SS | 2⒜", {
252
+ indicativePrice,
253
+ expectedInput,
254
+ userBal,
255
+ });
256
+ const adequateQuoteResult = await aggregateAggregators([
257
+ {
258
+ ...quoteData.req,
259
+ seriousness: QuoteSeriousness.SERIOUS,
260
+ inputAmount: convertDecimalToBigInt(expectedInput),
261
+ },
262
+ ], aggregators, 0 /* AggregateAggregatorsMode.MaximizeOutput */);
263
+ if (adequateQuoteResult.length !== 1) {
264
+ throw new AutoSelectionError("???");
265
+ }
266
+ const adequateQuote = adequateQuoteResult[0];
267
+ if (adequateQuote.quote == null) {
268
+ throw new AutoSelectionError("Couldn't get buy quote");
269
+ }
270
+ console.log("XCS | SS | 2⒜⑴", {
271
+ adequateQuote,
272
+ });
273
+ const oam2D = Decimal.div(adequateQuote.quote.outputAmountMinimum, divisor);
274
+ if (oam2D.gte(remainder)) {
275
+ final.push({
276
+ ...quoteData,
277
+ quote: adequateQuote.quote,
278
+ agg: adequateQuote.aggregator,
279
+ });
280
+ remainder = remainder.minus(oam2D);
281
+ break;
282
+ }
283
+ else if (expectedInput.eq(userBal)) {
284
+ throw new AutoSelectionError("Holding was supposedly enough to meet the full requirement but ceased to be so subsequently");
285
+ }
286
+ else {
287
+ expectedInput = Decimal.min(expectedInput.mul(safetyMultiplier), userBal); // try again with higher amount
288
+ }
289
+ }
290
+ }
291
+ else {
292
+ console.log("XCS | SS | 2⒝", resp);
293
+ final.push({
294
+ ...quoteData,
295
+ quote: resp,
296
+ agg,
297
+ });
298
+ remainder = remainder.minus(convertBigIntToDecimal(resp.outputAmountMinimum).div(divisor));
299
+ }
300
+ }
301
+ }
302
+ console.log("XCS | SS | 3⒜", {
303
+ remainder,
304
+ final,
305
+ });
306
+ if (remainder.gt(0)) {
307
+ throw new AutoSelectionError("Failed to accumulate enough swaps to meet requirement");
308
+ }
309
+ console.log("XCS | SS | Final:", { quotes: final, cots: usedCOTs });
310
+ return { quotes: final, usedCOTs };
311
+ }
59
312
  export async function autoSelectSources(userAddress, holdings, outputRequired, aggregators, collectionFees, commonCurrencyID = CurrencyID.USDC) {
60
313
  console.log("XCS | SS | Holdings:", holdings);
61
314
  const groupedByChainID = groupBy(holdings, (h) => bytesToHex(h.chainID.toBytes()));
@@ -205,6 +458,7 @@ export async function determineDestinationSwaps(userAddress, chainID, requiremen
205
458
  if (COT == null) {
206
459
  throw new AutoSelectionError("COT not present on the destination chain");
207
460
  }
461
+ // FIXME: Replace with oracle usage - should reduce time.
208
462
  // what happens if we happen to sell the requirement for the COT, what would the amount be?
209
463
  const fullLiquidationQR = {
210
464
  type: QuoteType.EXACT_IN,
@@ -0,0 +1 @@
1
+ export declare function ensureBufferPolyfill(): void;
@@ -1,12 +1,13 @@
1
- export { ArcanaVault, ArcanaVaultFactory } from './fuelcontracts';
2
- export * from './proto/definition';
3
- export { QueryClientImpl, Query, GrpcWebError, GrpcWebImpl } from './proto/grpc';
4
- export * from './data';
5
- export * from './vaultcontracts';
6
- export * from './permitutils';
7
- export * from './xcs';
8
- export * from './cosmos';
9
- export * from './rff/rff';
10
- export * from './types';
11
- export * from './balances/ub-api';
12
- export * from './evmabi';
1
+ import "./_polyfill";
2
+ export * from "./proto/definition";
3
+ export { QueryClientImpl, Query, GrpcWebError, GrpcWebImpl, } from "./proto/grpc";
4
+ export { createGrpcWebImpl } from "./proto/client";
5
+ export * from "./data";
6
+ export * from "./vaultcontracts";
7
+ export * from "./permitutils";
8
+ export * from "./xcs";
9
+ export * from "./cosmos";
10
+ export * from "./rff/rff";
11
+ export * from "./types";
12
+ export * from "./balances/ub-api";
13
+ export * from "./evmabi";
@@ -0,0 +1,2 @@
1
+ import { GrpcWebImpl } from "./grpc";
2
+ export declare const createGrpcWebImpl: (url: string) => Promise<GrpcWebImpl>;
@@ -1,12 +1,9 @@
1
1
  import { RequestForFunds } from "../proto/definition";
2
2
  import { type EVMRFF } from "../vaultcontracts";
3
- import { RequestInput as FuelRFF } from "../fuelcontracts/ArcanaVault";
4
3
  export declare class OmniversalRFF {
5
4
  readonly protobufRFF: RequestForFunds;
6
5
  private evmRFF;
7
- private fuelRFF;
8
6
  constructor(protobufRFF: RequestForFunds);
9
7
  asEVMRFF(): EVMRFF;
10
- asFuelRFF(): FuelRFF;
11
8
  asProtobufRFF(): RequestForFunds;
12
9
  }
@@ -21,6 +21,23 @@ export declare function aggregateAggregators(requests: (QuoteRequestExactInput |
21
21
  quote: Quote | null;
22
22
  aggregator: Aggregator;
23
23
  }[]>;
24
+ export declare function autoSelectSourcesV2(userAddress: Bytes, holdings: Holding[], outputRequired: Decimal, aggregators: Aggregator[], commonCurrencyID?: CurrencyID): Promise<{
25
+ quotes: ({
26
+ req: QuoteRequestExactInput;
27
+ originalHolding: Holding;
28
+ cur: Currency;
29
+ idx: number;
30
+ } & {
31
+ quote: Quote;
32
+ agg: Aggregator;
33
+ })[];
34
+ usedCOTs: {
35
+ originalHolding: Holding;
36
+ amountUsed: Decimal;
37
+ idx: number;
38
+ cur: Currency;
39
+ }[];
40
+ }>;
24
41
  export declare function autoSelectSources(userAddress: Bytes, holdings: Holding[], outputRequired: Decimal, aggregators: Aggregator[], collectionFees: FixedFeeTuple[], commonCurrencyID?: CurrencyID): Promise<({
25
42
  req: QuoteRequestExactInput;
26
43
  cfee: bigint;
package/package.json CHANGED
@@ -1,20 +1,20 @@
1
1
  {
2
2
  "name": "@avail-project/ca-common",
3
- "version": "1.0.0-beta.6",
3
+ "version": "1.0.0-beta.8",
4
4
  "description": "common utilities for CA",
5
5
  "files": [
6
6
  "dist"
7
7
  ],
8
+ "sideEffects": [
9
+ "./src/_polyfill.ts",
10
+ "./dist/esm/_polyfill.js",
11
+ "./dist/cjs/_polyfill.js"
12
+ ],
8
13
  "exports": {
9
14
  ".": {
10
15
  "types": "./dist/types/index.d.ts",
11
16
  "require": "./dist/cjs/index.js",
12
17
  "import": "./dist/esm/index.js"
13
- },
14
- "./fuelcontracts": {
15
- "types": "./dist/types/fuelcontracts/index.d.ts",
16
- "require": "./dist/cjs/fuelcontracts/index.js",
17
- "import": "./dist/esm/fuelcontracts/index.js"
18
18
  }
19
19
  },
20
20
  "scripts": {
@@ -24,7 +24,9 @@
24
24
  "test": "echo \"Error: no test specified\" && exit 1",
25
25
  "lint": "eslint",
26
26
  "clean": "rimraf dist/",
27
- "prepublishOnly": "npm run build"
27
+ "prepublishOnly": "npm run build",
28
+ "release": "release-it",
29
+ "prepare": "npm run build"
28
30
  },
29
31
  "author": "",
30
32
  "license": "MIT",
@@ -39,6 +41,7 @@
39
41
  "globals": "^16.3.0",
40
42
  "npm-run-all": "^4.1.5",
41
43
  "prettier": "^3.6.2",
44
+ "release-it": "^19.2.3",
42
45
  "rimraf": "^6.0.1",
43
46
  "ts-proto": "^2.7.5",
44
47
  "typescript": "^5.8.3"
@@ -48,7 +51,6 @@
48
51
  "@cosmjs/stargate": "^0.34.0",
49
52
  "axios": "^1.10.0",
50
53
  "decimal.js": "^10.6.0",
51
- "fuels": "^0.101.1",
52
54
  "long": "^5.3.2",
53
55
  "msgpackr": "^1.11.4",
54
56
  "viem": "^2.31.7"
@@ -56,8 +58,12 @@
56
58
  "dependencies": {
57
59
  "@bufbuild/protobuf": "^2.6.0",
58
60
  "@improbable-eng/grpc-web": "^0.15.0",
61
+ "@improbable-eng/grpc-web-node-http-transport": "0.15.0",
59
62
  "browser-headers": "^0.4.1",
60
63
  "es-toolkit": "^1.39.7",
61
64
  "tslib": "^2.8.1"
65
+ },
66
+ "repository": {
67
+ "url": "https://github.com/availproject/ca-common"
62
68
  }
63
69
  }