@sodax/sdk 1.0.1-beta-rc2 → 1.0.1-beta
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.cjs +115 -47
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +8 -7
- package/dist/index.d.ts +8 -7
- package/dist/index.mjs +115 -47
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.cts
CHANGED
|
@@ -6387,7 +6387,7 @@ type MoneyMarketSupplyParams = {
|
|
|
6387
6387
|
amount: bigint;
|
|
6388
6388
|
action: 'supply';
|
|
6389
6389
|
toChainId?: SpokeChainId;
|
|
6390
|
-
toAddress?:
|
|
6390
|
+
toAddress?: string;
|
|
6391
6391
|
};
|
|
6392
6392
|
/**
|
|
6393
6393
|
* Parameters for a Money Market borrow operation.
|
|
@@ -6405,7 +6405,7 @@ type MoneyMarketBorrowParams = {
|
|
|
6405
6405
|
amount: bigint;
|
|
6406
6406
|
action: 'borrow';
|
|
6407
6407
|
toChainId?: SpokeChainId;
|
|
6408
|
-
toAddress?:
|
|
6408
|
+
toAddress?: string;
|
|
6409
6409
|
};
|
|
6410
6410
|
/**
|
|
6411
6411
|
* Parameters for a Money Market withdraw operation.
|
|
@@ -6423,7 +6423,7 @@ type MoneyMarketWithdrawParams = {
|
|
|
6423
6423
|
amount: bigint;
|
|
6424
6424
|
action: 'withdraw';
|
|
6425
6425
|
toChainId?: SpokeChainId;
|
|
6426
|
-
toAddress?:
|
|
6426
|
+
toAddress?: string;
|
|
6427
6427
|
};
|
|
6428
6428
|
/**
|
|
6429
6429
|
* Parameters for a Money Market repay operation.
|
|
@@ -6441,7 +6441,7 @@ type MoneyMarketRepayParams = {
|
|
|
6441
6441
|
amount: bigint;
|
|
6442
6442
|
action: 'repay';
|
|
6443
6443
|
toChainId?: SpokeChainId;
|
|
6444
|
-
toAddress?:
|
|
6444
|
+
toAddress?: string;
|
|
6445
6445
|
};
|
|
6446
6446
|
type MoneyMarketParams = MoneyMarketSupplyParams | MoneyMarketBorrowParams | MoneyMarketWithdrawParams | MoneyMarketRepayParams;
|
|
6447
6447
|
type MoneyMarketUnknownErrorCode = 'SUPPLY_UNKNOWN_ERROR' | 'BORROW_UNKNOWN_ERROR' | 'WITHDRAW_UNKNOWN_ERROR' | 'REPAY_UNKNOWN_ERROR';
|
|
@@ -8717,7 +8717,7 @@ declare class SonicSpokeService {
|
|
|
8717
8717
|
* @param configService - The config service
|
|
8718
8718
|
* @returns BorrowInfo containing variable debt token address and vault address
|
|
8719
8719
|
*/
|
|
8720
|
-
static getBorrowInfo(token: Address$1, amount: bigint, chainId: SpokeChainId, dataService: MoneyMarketDataService, configService: ConfigService): Promise<BorrowInfo>;
|
|
8720
|
+
static getBorrowInfo(token: Address$1, amount: bigint, chainId: SpokeChainId, dataService: MoneyMarketDataService, configService: ConfigService, moneyMarketConfig: MoneyMarketServiceConfig): Promise<BorrowInfo>;
|
|
8721
8721
|
/**
|
|
8722
8722
|
* Check if the user has approved the withdrawal of tokens from the spoke chain using the Sonic wallet abstraction.
|
|
8723
8723
|
* @param from - The address of the user on the spoke chain
|
|
@@ -9534,13 +9534,14 @@ declare class ConfigService {
|
|
|
9534
9534
|
getHubAssets(): GetHubAssetsApiResponse;
|
|
9535
9535
|
getRelayChainIdMap(): GetRelayChainIdMapApiResponse;
|
|
9536
9536
|
getMoneyMarketTokens(): GetMoneyMarketTokensApiResponse;
|
|
9537
|
+
getMoneyMarketToken(chainId: SpokeChainId, token: string): Token | undefined;
|
|
9537
9538
|
getMoneyMarketReserveAssets(): GetMoneyMarketReserveAssetsApiResponse;
|
|
9538
9539
|
getHubAssetInfo(chainId: SpokeChainId, asset: OriginalAssetAddress): HubAssetInfo | undefined;
|
|
9539
9540
|
isValidOriginalAssetAddress(chainId: SpokeChainId, asset: OriginalAssetAddress): boolean;
|
|
9540
9541
|
getOriginalAssetAddress(chainId: SpokeChainId, hubAsset: Address): OriginalAssetAddress | undefined;
|
|
9541
9542
|
isValidHubAsset(hubAsset: Address): boolean;
|
|
9542
|
-
isValidSodaVaultAsset(vault:
|
|
9543
|
-
isValidVault(vault:
|
|
9543
|
+
isValidSodaVaultAsset(vault: string): boolean;
|
|
9544
|
+
isValidVault(vault: string | Token): boolean;
|
|
9544
9545
|
isValidChainHubAsset(chainId: SpokeChainId, hubAsset: Address): boolean;
|
|
9545
9546
|
isValidSpokeChainId(chainId: SpokeChainId): boolean;
|
|
9546
9547
|
isValidIntentRelayChainId(chainId: bigint): boolean;
|
package/dist/index.d.ts
CHANGED
|
@@ -6387,7 +6387,7 @@ type MoneyMarketSupplyParams = {
|
|
|
6387
6387
|
amount: bigint;
|
|
6388
6388
|
action: 'supply';
|
|
6389
6389
|
toChainId?: SpokeChainId;
|
|
6390
|
-
toAddress?:
|
|
6390
|
+
toAddress?: string;
|
|
6391
6391
|
};
|
|
6392
6392
|
/**
|
|
6393
6393
|
* Parameters for a Money Market borrow operation.
|
|
@@ -6405,7 +6405,7 @@ type MoneyMarketBorrowParams = {
|
|
|
6405
6405
|
amount: bigint;
|
|
6406
6406
|
action: 'borrow';
|
|
6407
6407
|
toChainId?: SpokeChainId;
|
|
6408
|
-
toAddress?:
|
|
6408
|
+
toAddress?: string;
|
|
6409
6409
|
};
|
|
6410
6410
|
/**
|
|
6411
6411
|
* Parameters for a Money Market withdraw operation.
|
|
@@ -6423,7 +6423,7 @@ type MoneyMarketWithdrawParams = {
|
|
|
6423
6423
|
amount: bigint;
|
|
6424
6424
|
action: 'withdraw';
|
|
6425
6425
|
toChainId?: SpokeChainId;
|
|
6426
|
-
toAddress?:
|
|
6426
|
+
toAddress?: string;
|
|
6427
6427
|
};
|
|
6428
6428
|
/**
|
|
6429
6429
|
* Parameters for a Money Market repay operation.
|
|
@@ -6441,7 +6441,7 @@ type MoneyMarketRepayParams = {
|
|
|
6441
6441
|
amount: bigint;
|
|
6442
6442
|
action: 'repay';
|
|
6443
6443
|
toChainId?: SpokeChainId;
|
|
6444
|
-
toAddress?:
|
|
6444
|
+
toAddress?: string;
|
|
6445
6445
|
};
|
|
6446
6446
|
type MoneyMarketParams = MoneyMarketSupplyParams | MoneyMarketBorrowParams | MoneyMarketWithdrawParams | MoneyMarketRepayParams;
|
|
6447
6447
|
type MoneyMarketUnknownErrorCode = 'SUPPLY_UNKNOWN_ERROR' | 'BORROW_UNKNOWN_ERROR' | 'WITHDRAW_UNKNOWN_ERROR' | 'REPAY_UNKNOWN_ERROR';
|
|
@@ -8717,7 +8717,7 @@ declare class SonicSpokeService {
|
|
|
8717
8717
|
* @param configService - The config service
|
|
8718
8718
|
* @returns BorrowInfo containing variable debt token address and vault address
|
|
8719
8719
|
*/
|
|
8720
|
-
static getBorrowInfo(token: Address$1, amount: bigint, chainId: SpokeChainId, dataService: MoneyMarketDataService, configService: ConfigService): Promise<BorrowInfo>;
|
|
8720
|
+
static getBorrowInfo(token: Address$1, amount: bigint, chainId: SpokeChainId, dataService: MoneyMarketDataService, configService: ConfigService, moneyMarketConfig: MoneyMarketServiceConfig): Promise<BorrowInfo>;
|
|
8721
8721
|
/**
|
|
8722
8722
|
* Check if the user has approved the withdrawal of tokens from the spoke chain using the Sonic wallet abstraction.
|
|
8723
8723
|
* @param from - The address of the user on the spoke chain
|
|
@@ -9534,13 +9534,14 @@ declare class ConfigService {
|
|
|
9534
9534
|
getHubAssets(): GetHubAssetsApiResponse;
|
|
9535
9535
|
getRelayChainIdMap(): GetRelayChainIdMapApiResponse;
|
|
9536
9536
|
getMoneyMarketTokens(): GetMoneyMarketTokensApiResponse;
|
|
9537
|
+
getMoneyMarketToken(chainId: SpokeChainId, token: string): Token | undefined;
|
|
9537
9538
|
getMoneyMarketReserveAssets(): GetMoneyMarketReserveAssetsApiResponse;
|
|
9538
9539
|
getHubAssetInfo(chainId: SpokeChainId, asset: OriginalAssetAddress): HubAssetInfo | undefined;
|
|
9539
9540
|
isValidOriginalAssetAddress(chainId: SpokeChainId, asset: OriginalAssetAddress): boolean;
|
|
9540
9541
|
getOriginalAssetAddress(chainId: SpokeChainId, hubAsset: Address): OriginalAssetAddress | undefined;
|
|
9541
9542
|
isValidHubAsset(hubAsset: Address): boolean;
|
|
9542
|
-
isValidSodaVaultAsset(vault:
|
|
9543
|
-
isValidVault(vault:
|
|
9543
|
+
isValidSodaVaultAsset(vault: string): boolean;
|
|
9544
|
+
isValidVault(vault: string | Token): boolean;
|
|
9544
9545
|
isValidChainHubAsset(chainId: SpokeChainId, hubAsset: Address): boolean;
|
|
9545
9546
|
isValidSpokeChainId(chainId: SpokeChainId): boolean;
|
|
9546
9547
|
isValidIntentRelayChainId(chainId: bigint): boolean;
|
package/dist/index.mjs
CHANGED
|
@@ -6905,6 +6905,11 @@ var ConfigService = class {
|
|
|
6905
6905
|
getMoneyMarketTokens() {
|
|
6906
6906
|
return this.sodaxConfig.supportedMoneyMarketTokens;
|
|
6907
6907
|
}
|
|
6908
|
+
getMoneyMarketToken(chainId, token) {
|
|
6909
|
+
return this.sodaxConfig.supportedMoneyMarketTokens[chainId].find(
|
|
6910
|
+
(t) => t.address.toLowerCase() === token.toLowerCase()
|
|
6911
|
+
);
|
|
6912
|
+
}
|
|
6908
6913
|
getMoneyMarketReserveAssets() {
|
|
6909
6914
|
return this.sodaxConfig.supportedMoneyMarketReserveAssets;
|
|
6910
6915
|
}
|
|
@@ -6924,7 +6929,10 @@ var ConfigService = class {
|
|
|
6924
6929
|
return this.supportedSodaVaultAssetsSet.has(vault.toLowerCase());
|
|
6925
6930
|
}
|
|
6926
6931
|
isValidVault(vault) {
|
|
6927
|
-
|
|
6932
|
+
if (typeof vault === "string") {
|
|
6933
|
+
return this.isValidSodaVaultAsset(vault);
|
|
6934
|
+
}
|
|
6935
|
+
return this.isValidSodaVaultAsset(vault.address);
|
|
6928
6936
|
}
|
|
6929
6937
|
isValidChainHubAsset(chainId, hubAsset) {
|
|
6930
6938
|
return this.chainIdToHubAssetsMap.get(chainId)?.has(hubAsset.toLowerCase()) ?? false;
|
|
@@ -13346,7 +13354,7 @@ var SonicSpokeService = class _SonicSpokeService {
|
|
|
13346
13354
|
const aTokenAddress = (await dataService.getReserveData(vaultAddress)).aTokenAddress;
|
|
13347
13355
|
return {
|
|
13348
13356
|
aTokenAddress,
|
|
13349
|
-
aTokenAmount: amount,
|
|
13357
|
+
aTokenAmount: EvmVaultTokenService.translateIncomingDecimals(assetConfig.decimal, amount),
|
|
13350
13358
|
token
|
|
13351
13359
|
};
|
|
13352
13360
|
}
|
|
@@ -13359,12 +13367,15 @@ var SonicSpokeService = class _SonicSpokeService {
|
|
|
13359
13367
|
* @param configService - The config service
|
|
13360
13368
|
* @returns BorrowInfo containing variable debt token address and vault address
|
|
13361
13369
|
*/
|
|
13362
|
-
static async getBorrowInfo(token, amount, chainId, dataService, configService) {
|
|
13370
|
+
static async getBorrowInfo(token, amount, chainId, dataService, configService, moneyMarketConfig) {
|
|
13363
13371
|
const assetConfig = configService.getHubAssetInfo(chainId, token);
|
|
13364
13372
|
if (!assetConfig) {
|
|
13365
13373
|
throw new Error("[SonicSpokeService.getBorrowInfo] Hub asset not found");
|
|
13366
13374
|
}
|
|
13367
|
-
|
|
13375
|
+
let vaultAddress = assetConfig.vault;
|
|
13376
|
+
if (moneyMarketConfig.bnUSDVault.toLowerCase() === vaultAddress.toLowerCase()) {
|
|
13377
|
+
vaultAddress = moneyMarketConfig.bnUSD;
|
|
13378
|
+
}
|
|
13368
13379
|
const reserveData = await dataService.getReserveData(vaultAddress);
|
|
13369
13380
|
const variableDebtTokenAddress = reserveData.variableDebtTokenAddress;
|
|
13370
13381
|
return {
|
|
@@ -13576,10 +13587,18 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
13576
13587
|
try {
|
|
13577
13588
|
invariant6(params.amount > 0n, "Amount must be greater than 0");
|
|
13578
13589
|
invariant6(params.token.length > 0, "Token is required");
|
|
13579
|
-
|
|
13580
|
-
|
|
13581
|
-
|
|
13582
|
-
|
|
13590
|
+
if (params.action === "withdraw" || params.action === "borrow") {
|
|
13591
|
+
const toChainId = params.toChainId ?? spokeProvider.chainConfig.chain.id;
|
|
13592
|
+
invariant6(
|
|
13593
|
+
this.configService.isMoneyMarketSupportedToken(toChainId, params.token),
|
|
13594
|
+
`Unsupported spoke chain (${toChainId}) token: ${params.token}`
|
|
13595
|
+
);
|
|
13596
|
+
} else {
|
|
13597
|
+
invariant6(
|
|
13598
|
+
this.configService.isMoneyMarketSupportedToken(spokeProvider.chainConfig.chain.id, params.token),
|
|
13599
|
+
`Unsupported spoke chain (${spokeProvider.chainConfig.chain.id}) token: ${params.token}`
|
|
13600
|
+
);
|
|
13601
|
+
}
|
|
13583
13602
|
const walletAddress = await spokeProvider.walletProvider.getWalletAddress();
|
|
13584
13603
|
if (spokeProvider instanceof StellarSpokeProvider && (params.action === "supply" || params.action === "repay")) {
|
|
13585
13604
|
return {
|
|
@@ -13617,7 +13636,8 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
13617
13636
|
params.amount,
|
|
13618
13637
|
params.toChainId ?? spokeProvider.chainConfig.chain.id,
|
|
13619
13638
|
this.data,
|
|
13620
|
-
this.configService
|
|
13639
|
+
this.configService,
|
|
13640
|
+
this.config
|
|
13621
13641
|
);
|
|
13622
13642
|
return await SonicSpokeService.isBorrowApproved(
|
|
13623
13643
|
walletAddress,
|
|
@@ -13681,10 +13701,18 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
13681
13701
|
try {
|
|
13682
13702
|
invariant6(params.amount > 0n, "Amount must be greater than 0");
|
|
13683
13703
|
invariant6(params.token.length > 0, "Token is required");
|
|
13684
|
-
|
|
13685
|
-
|
|
13686
|
-
|
|
13687
|
-
|
|
13704
|
+
if (params.action === "withdraw" || params.action === "borrow") {
|
|
13705
|
+
const toChainId = params.toChainId ?? spokeProvider.chainConfig.chain.id;
|
|
13706
|
+
invariant6(
|
|
13707
|
+
this.configService.isMoneyMarketSupportedToken(toChainId, params.token),
|
|
13708
|
+
`Unsupported spoke chain (${toChainId}) token: ${params.token}`
|
|
13709
|
+
);
|
|
13710
|
+
} else {
|
|
13711
|
+
invariant6(
|
|
13712
|
+
this.configService.isMoneyMarketSupportedToken(spokeProvider.chainConfig.chain.id, params.token),
|
|
13713
|
+
`Unsupported spoke chain (${spokeProvider.chainConfig.chain.id}) token: ${params.token}`
|
|
13714
|
+
);
|
|
13715
|
+
}
|
|
13688
13716
|
const walletAddress = await spokeProvider.walletProvider.getWalletAddress();
|
|
13689
13717
|
if (spokeProvider instanceof StellarSpokeProvider) {
|
|
13690
13718
|
invariant6(
|
|
@@ -13746,7 +13774,8 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
13746
13774
|
params.amount,
|
|
13747
13775
|
params?.toChainId ?? spokeProvider.chainConfig.chain.id,
|
|
13748
13776
|
this.data,
|
|
13749
|
-
this.configService
|
|
13777
|
+
this.configService,
|
|
13778
|
+
this.config
|
|
13750
13779
|
);
|
|
13751
13780
|
const result = await SonicSpokeService.approveBorrow(
|
|
13752
13781
|
walletAddress,
|
|
@@ -13835,7 +13864,7 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
13835
13864
|
};
|
|
13836
13865
|
}
|
|
13837
13866
|
let intentTxHash = null;
|
|
13838
|
-
if (spokeProvider.chainConfig.chain.id !==
|
|
13867
|
+
if (spokeProvider.chainConfig.chain.id !== this.hubProvider.chainConfig.chain.id) {
|
|
13839
13868
|
const packetResult = await relayTxAndWaitPacket(
|
|
13840
13869
|
txResult.value,
|
|
13841
13870
|
spokeProvider instanceof SolanaSpokeProvider ? txResult.data : void 0,
|
|
@@ -14000,10 +14029,8 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
14000
14029
|
}
|
|
14001
14030
|
};
|
|
14002
14031
|
}
|
|
14003
|
-
const fromChainId = spokeProvider.chainConfig.chain.id;
|
|
14004
|
-
const toChainId = params.toChainId ?? fromChainId;
|
|
14005
14032
|
let intentTxHash = null;
|
|
14006
|
-
if (toChainId !==
|
|
14033
|
+
if (spokeProvider.chainConfig.chain.id !== this.hubProvider.chainConfig.chain.id || params.toChainId && params.toAddress && params.toChainId !== this.hubProvider.chainConfig.chain.id) {
|
|
14007
14034
|
const packetResult = await relayTxAndWaitPacket(
|
|
14008
14035
|
txResult.value,
|
|
14009
14036
|
spokeProvider instanceof SolanaSpokeProvider ? txResult.data : void 0,
|
|
@@ -14079,13 +14106,11 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
14079
14106
|
const fromAddress = await spokeProvider.walletProvider.getWalletAddress();
|
|
14080
14107
|
const toChainId = params.toChainId ?? fromChainId;
|
|
14081
14108
|
const toAddress = params.toAddress ?? fromAddress;
|
|
14082
|
-
|
|
14083
|
-
|
|
14084
|
-
`Unsupported spoke chain (${toChainId}) token: ${params.token}`
|
|
14085
|
-
);
|
|
14109
|
+
const dstToken = this.configService.getMoneyMarketToken(toChainId, params.token);
|
|
14110
|
+
invariant6(dstToken, `Money market token not found for spoke chain (${toChainId}) token: ${params.token}`);
|
|
14086
14111
|
const encodedToAddress = encodeAddress(toChainId, toAddress);
|
|
14087
14112
|
const fromHubWallet = await deriveUserWalletAddress(this.hubProvider, fromChainId, fromAddress);
|
|
14088
|
-
const data = this.buildBorrowData(fromHubWallet, encodedToAddress,
|
|
14113
|
+
const data = this.buildBorrowData(fromHubWallet, encodedToAddress, dstToken.address, params.amount, toChainId);
|
|
14089
14114
|
let txResult;
|
|
14090
14115
|
if (fromChainId === this.hubProvider.chainConfig.chain.id && isSonicSpokeProviderType(spokeProvider)) {
|
|
14091
14116
|
txResult = await SonicSpokeService.callWallet(data, spokeProvider, raw);
|
|
@@ -14148,10 +14173,8 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
14148
14173
|
}
|
|
14149
14174
|
};
|
|
14150
14175
|
}
|
|
14151
|
-
const fromChainId = spokeProvider.chainConfig.chain.id;
|
|
14152
|
-
const toChainId = params.toChainId ?? fromChainId;
|
|
14153
14176
|
let intentTxHash = null;
|
|
14154
|
-
if (toChainId !==
|
|
14177
|
+
if (spokeProvider.chainConfig.chain.id !== this.hubProvider.chainConfig.chain.id || params.toChainId && params.toAddress && params.toChainId !== this.hubProvider.chainConfig.chain.id && params.toAddress !== this.hubProvider.chainConfig.addresses.walletRouter) {
|
|
14155
14178
|
const packetResult = await relayTxAndWaitPacket(
|
|
14156
14179
|
txResult.value,
|
|
14157
14180
|
spokeProvider instanceof SolanaSpokeProvider ? txResult.data : void 0,
|
|
@@ -14312,7 +14335,7 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
14312
14335
|
};
|
|
14313
14336
|
}
|
|
14314
14337
|
let intentTxHash = null;
|
|
14315
|
-
if (spokeProvider.chainConfig.chain.id !==
|
|
14338
|
+
if (spokeProvider.chainConfig.chain.id !== this.hubProvider.chainConfig.chain.id) {
|
|
14316
14339
|
const packetResult = await relayTxAndWaitPacket(
|
|
14317
14340
|
txResult.value,
|
|
14318
14341
|
spokeProvider instanceof SolanaSpokeProvider ? txResult.data : void 0,
|
|
@@ -14431,8 +14454,10 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
14431
14454
|
const fromHubAsset = this.configService.getHubAssetInfo(fromChainId, fromToken);
|
|
14432
14455
|
invariant6(fromHubAsset, `hub asset not found for source chain token (token): ${fromToken}`);
|
|
14433
14456
|
const lendingPool = this.config.lendingPool;
|
|
14434
|
-
|
|
14435
|
-
|
|
14457
|
+
if (!this.configService.isValidVault(fromHubAsset.asset)) {
|
|
14458
|
+
calls.push(Erc20Service.encodeApprove(fromHubAsset.asset, fromHubAsset.vault, amount));
|
|
14459
|
+
calls.push(EvmVaultTokenService.encodeDeposit(fromHubAsset.vault, fromHubAsset.asset, amount));
|
|
14460
|
+
}
|
|
14436
14461
|
const translatedAmount = EvmVaultTokenService.translateIncomingDecimals(fromHubAsset.decimal, amount);
|
|
14437
14462
|
calls.push(Erc20Service.encodeApprove(fromHubAsset.vault, lendingPool, translatedAmount));
|
|
14438
14463
|
calls.push(
|
|
@@ -14454,29 +14479,44 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
14454
14479
|
*/
|
|
14455
14480
|
buildBorrowData(fromHubAddress, toAddress, toToken, amount, toChainId) {
|
|
14456
14481
|
const toHubAsset = this.configService.getHubAssetInfo(toChainId, toToken);
|
|
14482
|
+
const dstToken = this.configService.getMoneyMarketToken(toChainId, toToken);
|
|
14457
14483
|
invariant6(toHubAsset, `hub asset not found for target chain token (toToken): ${toToken}`);
|
|
14484
|
+
invariant6(dstToken, `Money market token not found for spoke chain (${toChainId}) token: ${toToken}`);
|
|
14458
14485
|
const assetAddress = toHubAsset.asset;
|
|
14459
14486
|
const vaultAddress = toHubAsset.vault;
|
|
14460
14487
|
const bnUSDVault = this.config.bnUSDVault;
|
|
14461
14488
|
const bnUSD = this.config.bnUSD;
|
|
14462
|
-
const
|
|
14489
|
+
const translatedInAmount = EvmVaultTokenService.translateIncomingDecimals(toHubAsset.decimal, amount);
|
|
14490
|
+
const feeAmount = calculateFeeAmount(translatedInAmount, this.config.partnerFee);
|
|
14463
14491
|
const calls = [];
|
|
14464
|
-
if (bnUSDVault
|
|
14492
|
+
if (bnUSDVault.toLowerCase() === vaultAddress.toLowerCase()) {
|
|
14465
14493
|
calls.push(
|
|
14466
14494
|
_MoneyMarketService.encodeBorrow(
|
|
14467
|
-
{
|
|
14495
|
+
{
|
|
14496
|
+
asset: bnUSD,
|
|
14497
|
+
amount: translatedInAmount,
|
|
14498
|
+
interestRateMode: 2n,
|
|
14499
|
+
referralCode: 0,
|
|
14500
|
+
onBehalfOf: fromHubAddress
|
|
14501
|
+
},
|
|
14468
14502
|
this.config.lendingPool
|
|
14469
14503
|
)
|
|
14470
14504
|
);
|
|
14471
|
-
calls.push(Erc20Service.encodeApprove(bnUSD, bnUSDVault,
|
|
14472
|
-
calls.push(EvmVaultTokenService.encodeDeposit(bnUSDVault, bnUSD,
|
|
14505
|
+
calls.push(Erc20Service.encodeApprove(bnUSD, bnUSDVault, translatedInAmount));
|
|
14506
|
+
calls.push(EvmVaultTokenService.encodeDeposit(bnUSDVault, bnUSD, translatedInAmount));
|
|
14473
14507
|
if (this.config.partnerFee && feeAmount) {
|
|
14474
14508
|
calls.push(Erc20Service.encodeTransfer(bnUSDVault, this.config.partnerFee.address, feeAmount));
|
|
14475
14509
|
}
|
|
14476
14510
|
} else {
|
|
14477
14511
|
calls.push(
|
|
14478
14512
|
_MoneyMarketService.encodeBorrow(
|
|
14479
|
-
{
|
|
14513
|
+
{
|
|
14514
|
+
asset: vaultAddress,
|
|
14515
|
+
amount: translatedInAmount,
|
|
14516
|
+
interestRateMode: 2n,
|
|
14517
|
+
referralCode: 0,
|
|
14518
|
+
onBehalfOf: fromHubAddress
|
|
14519
|
+
},
|
|
14480
14520
|
this.config.lendingPool
|
|
14481
14521
|
)
|
|
14482
14522
|
);
|
|
@@ -14484,8 +14524,21 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
14484
14524
|
calls.push(Erc20Service.encodeTransfer(vaultAddress, this.config.partnerFee.address, feeAmount));
|
|
14485
14525
|
}
|
|
14486
14526
|
}
|
|
14487
|
-
|
|
14488
|
-
|
|
14527
|
+
if (toToken.toLowerCase() !== vaultAddress.toLowerCase()) {
|
|
14528
|
+
calls.push(EvmVaultTokenService.encodeWithdraw(vaultAddress, assetAddress, translatedInAmount - feeAmount));
|
|
14529
|
+
}
|
|
14530
|
+
let translatedAmountOut;
|
|
14531
|
+
if (this.configService.isValidVault(toToken)) {
|
|
14532
|
+
translatedAmountOut = EvmVaultTokenService.translateOutgoingDecimals(
|
|
14533
|
+
toHubAsset.decimal,
|
|
14534
|
+
translatedInAmount - feeAmount
|
|
14535
|
+
);
|
|
14536
|
+
} else {
|
|
14537
|
+
translatedAmountOut = EvmVaultTokenService.translateOutgoingDecimals(
|
|
14538
|
+
dstToken.decimals,
|
|
14539
|
+
translatedInAmount - feeAmount
|
|
14540
|
+
);
|
|
14541
|
+
}
|
|
14489
14542
|
if (toChainId === this.hubProvider.chainConfig.chain.id) {
|
|
14490
14543
|
if (assetAddress.toLowerCase() === this.configService.spokeChainConfig[toChainId].addresses.wrappedSonic.toLowerCase()) {
|
|
14491
14544
|
const withdrawToCall = {
|
|
@@ -14525,17 +14578,27 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
14525
14578
|
buildWithdrawData(fromHubAddress, toAddress, toToken, amount, toChainId) {
|
|
14526
14579
|
const calls = [];
|
|
14527
14580
|
const toHubAsset = this.configService.getHubAssetInfo(toChainId, toToken);
|
|
14581
|
+
const dstToken = this.configService.getMoneyMarketToken(toChainId, toToken);
|
|
14528
14582
|
invariant6(toHubAsset, `hub asset not found for target chain token (toToken): ${toToken}`);
|
|
14583
|
+
invariant6(dstToken, `Money market token not found for spoke chain (${toChainId}) token: ${toToken}`);
|
|
14529
14584
|
const assetAddress = toHubAsset.asset;
|
|
14530
14585
|
const vaultAddress = toHubAsset.vault;
|
|
14586
|
+
const translatedInAmount = EvmVaultTokenService.translateIncomingDecimals(toHubAsset.decimal, amount);
|
|
14531
14587
|
calls.push(
|
|
14532
14588
|
_MoneyMarketService.encodeWithdraw(
|
|
14533
|
-
{ asset: vaultAddress, amount, to: fromHubAddress },
|
|
14589
|
+
{ asset: vaultAddress, amount: translatedInAmount, to: fromHubAddress },
|
|
14534
14590
|
this.config.lendingPool
|
|
14535
14591
|
)
|
|
14536
14592
|
);
|
|
14537
|
-
|
|
14538
|
-
|
|
14593
|
+
if (!this.configService.isValidVault(toToken)) {
|
|
14594
|
+
calls.push(EvmVaultTokenService.encodeWithdraw(vaultAddress, assetAddress, translatedInAmount));
|
|
14595
|
+
}
|
|
14596
|
+
let translatedAmountOut;
|
|
14597
|
+
if (this.configService.isValidVault(toToken)) {
|
|
14598
|
+
translatedAmountOut = EvmVaultTokenService.translateOutgoingDecimals(toHubAsset.decimal, translatedInAmount);
|
|
14599
|
+
} else {
|
|
14600
|
+
translatedAmountOut = EvmVaultTokenService.translateOutgoingDecimals(dstToken.decimals, translatedInAmount);
|
|
14601
|
+
}
|
|
14539
14602
|
if (toChainId === this.hubProvider.chainConfig.chain.id) {
|
|
14540
14603
|
if (assetAddress.toLowerCase() === this.configService.spokeChainConfig[toChainId].addresses.wrappedSonic.toLowerCase()) {
|
|
14541
14604
|
const withdrawToCall = {
|
|
@@ -14579,18 +14642,23 @@ var MoneyMarketService = class _MoneyMarketService {
|
|
|
14579
14642
|
const vaultAddress = fromHubAsset.vault;
|
|
14580
14643
|
const bnUSDVault = this.config.bnUSDVault;
|
|
14581
14644
|
const bnUSD = this.config.bnUSD;
|
|
14582
|
-
|
|
14583
|
-
calls.push(EvmVaultTokenService.encodeDeposit(vaultAddress, assetAddress, amount));
|
|
14584
|
-
const translatedAmount = EvmVaultTokenService.translateIncomingDecimals(fromHubAsset.decimal, amount);
|
|
14645
|
+
const translatedAmountIn = EvmVaultTokenService.translateIncomingDecimals(fromHubAsset.decimal, amount);
|
|
14585
14646
|
let repayToken = vaultAddress;
|
|
14586
|
-
if (bnUSDVault
|
|
14647
|
+
if (bnUSDVault.toLowerCase() === vaultAddress.toLowerCase()) {
|
|
14587
14648
|
repayToken = bnUSD;
|
|
14588
|
-
|
|
14649
|
+
if (assetAddress.toLowerCase() !== bnUSDVault.toLowerCase()) {
|
|
14650
|
+
calls.push(Erc20Service.encodeApprove(assetAddress, vaultAddress, translatedAmountIn));
|
|
14651
|
+
calls.push(EvmVaultTokenService.encodeDeposit(vaultAddress, assetAddress, translatedAmountIn));
|
|
14652
|
+
}
|
|
14653
|
+
calls.push(EvmVaultTokenService.encodeWithdraw(bnUSDVault, bnUSD, translatedAmountIn));
|
|
14654
|
+
} else {
|
|
14655
|
+
calls.push(Erc20Service.encodeApprove(assetAddress, vaultAddress, amount));
|
|
14656
|
+
calls.push(EvmVaultTokenService.encodeDeposit(vaultAddress, assetAddress, amount));
|
|
14589
14657
|
}
|
|
14590
|
-
calls.push(Erc20Service.encodeApprove(repayToken, this.config.lendingPool,
|
|
14658
|
+
calls.push(Erc20Service.encodeApprove(repayToken, this.config.lendingPool, translatedAmountIn));
|
|
14591
14659
|
calls.push(
|
|
14592
14660
|
_MoneyMarketService.encodeRepay(
|
|
14593
|
-
{ asset: repayToken, amount:
|
|
14661
|
+
{ asset: repayToken, amount: translatedAmountIn, interestRateMode: 2n, onBehalfOf: toHubAddress },
|
|
14594
14662
|
this.config.lendingPool
|
|
14595
14663
|
)
|
|
14596
14664
|
);
|