@rhea-finance/cross-chain-aggregation-dex 0.1.4 → 0.1.6
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/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +98 -7
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +98 -7
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -90,6 +90,8 @@ interface BaseExecuteParams {
|
|
|
90
90
|
recipient: string;
|
|
91
91
|
depositAddress?: string;
|
|
92
92
|
deadline?: number;
|
|
93
|
+
/** Optional sender address (for native NEAR wrap operations) */
|
|
94
|
+
sender?: string;
|
|
93
95
|
}
|
|
94
96
|
/**
|
|
95
97
|
* Recipient execute parameters (V2 NEAR, EVM, etc.)
|
|
@@ -429,6 +431,10 @@ interface CompleteQuoteParams {
|
|
|
429
431
|
recipient: string;
|
|
430
432
|
refundTo?: string;
|
|
431
433
|
customRecipientMsg?: string;
|
|
434
|
+
appFees?: Array<{
|
|
435
|
+
recipient: string;
|
|
436
|
+
fee: number;
|
|
437
|
+
}>;
|
|
432
438
|
}
|
|
433
439
|
interface CompleteQuoteResult {
|
|
434
440
|
intents: {
|
package/dist/index.d.ts
CHANGED
|
@@ -90,6 +90,8 @@ interface BaseExecuteParams {
|
|
|
90
90
|
recipient: string;
|
|
91
91
|
depositAddress?: string;
|
|
92
92
|
deadline?: number;
|
|
93
|
+
/** Optional sender address (for native NEAR wrap operations) */
|
|
94
|
+
sender?: string;
|
|
93
95
|
}
|
|
94
96
|
/**
|
|
95
97
|
* Recipient execute parameters (V2 NEAR, EVM, etc.)
|
|
@@ -429,6 +431,10 @@ interface CompleteQuoteParams {
|
|
|
429
431
|
recipient: string;
|
|
430
432
|
refundTo?: string;
|
|
431
433
|
customRecipientMsg?: string;
|
|
434
|
+
appFees?: Array<{
|
|
435
|
+
recipient: string;
|
|
436
|
+
fee: number;
|
|
437
|
+
}>;
|
|
432
438
|
}
|
|
433
439
|
interface CompleteQuoteResult {
|
|
434
440
|
intents: {
|
package/dist/index.js
CHANGED
|
@@ -147,6 +147,13 @@ function convertSlippageToBasisPoints(slippage) {
|
|
|
147
147
|
}
|
|
148
148
|
function normalizeDestinationAsset(assetId, wrapNearContractId = "wrap.near") {
|
|
149
149
|
if (!assetId) return assetId;
|
|
150
|
+
if (assetId.startsWith("1cs_v1:")) {
|
|
151
|
+
const parts = assetId.split(":");
|
|
152
|
+
const nep141Index = parts.findIndex((p) => p === "nep141");
|
|
153
|
+
if (nep141Index >= 0 && nep141Index < parts.length - 1) {
|
|
154
|
+
return `nep141:${parts.slice(nep141Index + 1).join(":")}`;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
150
157
|
if (assetId.startsWith("nep141:") || assetId.startsWith("nep245:")) {
|
|
151
158
|
return assetId;
|
|
152
159
|
}
|
|
@@ -383,7 +390,43 @@ var NearSmartRouter = class {
|
|
|
383
390
|
};
|
|
384
391
|
}
|
|
385
392
|
const finalRecipient = depositAddress || recipient;
|
|
393
|
+
const sender = params.sender || finalRecipient;
|
|
386
394
|
const transactions = [];
|
|
395
|
+
const isNativeNear = quote.tokenIn.address === "near" || quote.tokenIn.address === this.wrapNearContractId && quote.tokenIn.symbol === "NEAR" || !quote.tokenIn.address && quote.tokenIn.symbol === "NEAR";
|
|
396
|
+
if (isNativeNear) {
|
|
397
|
+
let wrapNearStorageBalance = null;
|
|
398
|
+
try {
|
|
399
|
+
wrapNearStorageBalance = await this.nearChainAdapter.view({
|
|
400
|
+
contractId: this.wrapNearContractId,
|
|
401
|
+
methodName: "storage_balance_of",
|
|
402
|
+
args: {
|
|
403
|
+
account_id: sender
|
|
404
|
+
}
|
|
405
|
+
});
|
|
406
|
+
} catch (err) {
|
|
407
|
+
wrapNearStorageBalance = null;
|
|
408
|
+
}
|
|
409
|
+
if (!wrapNearStorageBalance) {
|
|
410
|
+
transactions.push({
|
|
411
|
+
contractId: this.wrapNearContractId,
|
|
412
|
+
methodName: "storage_deposit",
|
|
413
|
+
args: {
|
|
414
|
+
account_id: sender,
|
|
415
|
+
registration_only: true
|
|
416
|
+
},
|
|
417
|
+
gas: "50000000000000",
|
|
418
|
+
expandDeposit: "1250000000000000000000"
|
|
419
|
+
// 0.00125 NEAR
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
transactions.push({
|
|
423
|
+
contractId: this.wrapNearContractId,
|
|
424
|
+
methodName: "near_deposit",
|
|
425
|
+
args: {},
|
|
426
|
+
gas: "50000000000000",
|
|
427
|
+
expandDeposit: quote.amountIn
|
|
428
|
+
});
|
|
429
|
+
}
|
|
387
430
|
if (finalRecipient && quote.tokenOut?.address) {
|
|
388
431
|
let isRegistered = false;
|
|
389
432
|
try {
|
|
@@ -419,8 +462,9 @@ var NearSmartRouter = class {
|
|
|
419
462
|
if (finalRecipient) {
|
|
420
463
|
swapMsg.swap_out_recipient = finalRecipient;
|
|
421
464
|
}
|
|
465
|
+
const tokenInAddress = isNativeNear ? this.wrapNearContractId : quote.tokenIn.address;
|
|
422
466
|
transactions.push({
|
|
423
|
-
contractId:
|
|
467
|
+
contractId: tokenInAddress,
|
|
424
468
|
methodName: "ft_transfer_call",
|
|
425
469
|
args: {
|
|
426
470
|
receiver_id: this.refExchangeId,
|
|
@@ -798,6 +842,47 @@ var AggregateDexRouter = class {
|
|
|
798
842
|
expandDeposit: finalQuote.amountIn
|
|
799
843
|
});
|
|
800
844
|
}
|
|
845
|
+
const isWrappedNear = finalQuote.tokenIn.address === this.wrapNearContractId || finalQuote.tokenIn.symbol === "WNEAR" && finalQuote.tokenIn.address === this.wrapNearContractId;
|
|
846
|
+
if (isWrappedNear && !isNativeNear) {
|
|
847
|
+
let wNearBalance = "0";
|
|
848
|
+
try {
|
|
849
|
+
const wNearBalanceResult = await this.nearChainAdapter.view({
|
|
850
|
+
contractId: this.wrapNearContractId,
|
|
851
|
+
methodName: "ft_balance_of",
|
|
852
|
+
args: { account_id: sender }
|
|
853
|
+
});
|
|
854
|
+
wNearBalance = wNearBalanceResult || "0";
|
|
855
|
+
} catch (e) {
|
|
856
|
+
}
|
|
857
|
+
const requiredAmount = new Big3__default.default(finalQuote.amountIn);
|
|
858
|
+
const currentBalance = new Big3__default.default(wNearBalance);
|
|
859
|
+
if (currentBalance.lt(requiredAmount)) {
|
|
860
|
+
const amountToConvert = requiredAmount.minus(currentBalance);
|
|
861
|
+
const wrapNearStorageBalance = await getStorageBalance(
|
|
862
|
+
this.wrapNearContractId,
|
|
863
|
+
sender
|
|
864
|
+
).catch(() => null);
|
|
865
|
+
if (!wrapNearStorageBalance) {
|
|
866
|
+
transactions.push({
|
|
867
|
+
contractId: this.wrapNearContractId,
|
|
868
|
+
methodName: "storage_deposit",
|
|
869
|
+
args: {
|
|
870
|
+
account_id: sender,
|
|
871
|
+
registration_only: true
|
|
872
|
+
},
|
|
873
|
+
gas: "50000000000000",
|
|
874
|
+
expandDeposit: this.NEW_ACCOUNT_STORAGE_COST
|
|
875
|
+
});
|
|
876
|
+
}
|
|
877
|
+
transactions.push({
|
|
878
|
+
contractId: this.wrapNearContractId,
|
|
879
|
+
methodName: "near_deposit",
|
|
880
|
+
args: {},
|
|
881
|
+
gas: "50000000000000",
|
|
882
|
+
expandDeposit: amountToConvert.toFixed(0)
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
}
|
|
801
886
|
const tokensToCheck = dexs.length > 1 ? tokens : [finalQuote.tokenOut.address];
|
|
802
887
|
const tokenStorageBalances = await Promise.all(
|
|
803
888
|
tokensToCheck.map(
|
|
@@ -980,7 +1065,8 @@ async function completeQuote(params, config) {
|
|
|
980
1065
|
slippage,
|
|
981
1066
|
recipient,
|
|
982
1067
|
refundTo,
|
|
983
|
-
customRecipientMsg
|
|
1068
|
+
customRecipientMsg,
|
|
1069
|
+
appFees
|
|
984
1070
|
} = params;
|
|
985
1071
|
const {
|
|
986
1072
|
intentsQuotationAdapter,
|
|
@@ -1045,13 +1131,15 @@ async function completeQuote(params, config) {
|
|
|
1045
1131
|
const bluechipTokenConfig = bluechipKey && bluechipTokens[bluechipKey] || void 0;
|
|
1046
1132
|
const normalizedSourceAsset = bluechipTokenConfig?.assetId ? bluechipTokenConfig.assetId : `nep141:${bluechipToken.address}`;
|
|
1047
1133
|
let normalizedTargetAsset = targetToken.address;
|
|
1048
|
-
if (normalizedTargetAsset && !normalizedTargetAsset.startsWith("nep141:") && !normalizedTargetAsset.startsWith("nep245:") && normalizedTargetAsset.includes(".")) {
|
|
1134
|
+
if (normalizedTargetAsset?.startsWith("1cs_v1:")) ; else if (normalizedTargetAsset && !normalizedTargetAsset.startsWith("nep141:") && !normalizedTargetAsset.startsWith("nep245:") && normalizedTargetAsset.includes(".")) {
|
|
1049
1135
|
normalizedTargetAsset = `nep141:${normalizeTokenId(
|
|
1050
1136
|
normalizedTargetAsset,
|
|
1051
1137
|
wrapNearContractId
|
|
1052
1138
|
)}`;
|
|
1053
1139
|
}
|
|
1054
|
-
|
|
1140
|
+
if (!normalizedTargetAsset?.startsWith("1cs_v1:")) {
|
|
1141
|
+
normalizedTargetAsset = normalizeDestinationAsset(normalizedTargetAsset, wrapNearContractId) || normalizedTargetAsset;
|
|
1142
|
+
}
|
|
1055
1143
|
const slippageBps = convertSlippageToBasisPoints(slippage);
|
|
1056
1144
|
const intentsQuote = await intentsQuotationAdapter.quote({
|
|
1057
1145
|
originAsset: normalizedSourceAsset,
|
|
@@ -1061,7 +1149,8 @@ async function completeQuote(params, config) {
|
|
|
1061
1149
|
recipient,
|
|
1062
1150
|
slippageTolerance: slippageBps,
|
|
1063
1151
|
swapType: "FLEX_INPUT",
|
|
1064
|
-
...customRecipientMsg ? { customRecipientMsg } : {}
|
|
1152
|
+
...customRecipientMsg ? { customRecipientMsg } : {},
|
|
1153
|
+
...appFees ? { appFees } : {}
|
|
1065
1154
|
});
|
|
1066
1155
|
if (intentsQuote.quoteStatus !== "success") {
|
|
1067
1156
|
throw new Error("Failed to get quote");
|
|
@@ -1104,13 +1193,15 @@ async function completeQuote(params, config) {
|
|
|
1104
1193
|
}
|
|
1105
1194
|
}
|
|
1106
1195
|
let normalizedTargetAsset = targetToken.address;
|
|
1107
|
-
if (normalizedTargetAsset && !normalizedTargetAsset.startsWith("nep141:") && !normalizedTargetAsset.startsWith("nep245:") && normalizedTargetAsset.includes(".")) {
|
|
1196
|
+
if (normalizedTargetAsset?.startsWith("1cs_v1:")) ; else if (normalizedTargetAsset && !normalizedTargetAsset.startsWith("nep141:") && !normalizedTargetAsset.startsWith("nep245:") && normalizedTargetAsset.includes(".")) {
|
|
1108
1197
|
normalizedTargetAsset = `nep141:${normalizeTokenId(
|
|
1109
1198
|
normalizedTargetAsset,
|
|
1110
1199
|
wrapNearContractId
|
|
1111
1200
|
)}`;
|
|
1112
1201
|
}
|
|
1113
|
-
|
|
1202
|
+
if (!normalizedTargetAsset?.startsWith("1cs_v1:")) {
|
|
1203
|
+
normalizedTargetAsset = normalizeDestinationAsset(normalizedTargetAsset, wrapNearContractId) || normalizedTargetAsset;
|
|
1204
|
+
}
|
|
1114
1205
|
const slippageBps = convertSlippageToBasisPoints(slippage);
|
|
1115
1206
|
const intentsQuote = await intentsQuotationAdapter.quote({
|
|
1116
1207
|
originAsset: normalizedSourceAsset,
|