@tcswap/plugins 4.2.15
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/chainflip/index.cjs +4 -0
- package/dist/chainflip/index.cjs.map +11 -0
- package/dist/chainflip/index.js +4 -0
- package/dist/chainflip/index.js.map +11 -0
- package/dist/chunk-vzvcxw96.js +5 -0
- package/dist/chunk-vzvcxw96.js.map +10 -0
- package/dist/chunk-zweecvg1.js +4 -0
- package/dist/chunk-zweecvg1.js.map +10 -0
- package/dist/evm/index.cjs +4 -0
- package/dist/evm/index.cjs.map +10 -0
- package/dist/evm/index.js +4 -0
- package/dist/evm/index.js.map +10 -0
- package/dist/garden/index.cjs +4 -0
- package/dist/garden/index.cjs.map +10 -0
- package/dist/garden/index.js +4 -0
- package/dist/garden/index.js.map +10 -0
- package/dist/index.cjs +4 -0
- package/dist/index.cjs.map +10 -0
- package/dist/index.js +4 -0
- package/dist/index.js.map +10 -0
- package/dist/near/index.cjs +4 -0
- package/dist/near/index.cjs.map +11 -0
- package/dist/near/index.js +4 -0
- package/dist/near/index.js.map +11 -0
- package/dist/radix/index.cjs +4 -0
- package/dist/radix/index.cjs.map +10 -0
- package/dist/radix/index.js +4 -0
- package/dist/radix/index.js.map +10 -0
- package/dist/solana/index.cjs +4 -0
- package/dist/solana/index.cjs.map +10 -0
- package/dist/solana/index.js +4 -0
- package/dist/solana/index.js.map +10 -0
- package/dist/thorchain/index.cjs +4 -0
- package/dist/thorchain/index.cjs.map +11 -0
- package/dist/thorchain/index.js +4 -0
- package/dist/thorchain/index.js.map +11 -0
- package/dist/types/chainflip/broker.d.ts +23 -0
- package/dist/types/chainflip/broker.d.ts.map +1 -0
- package/dist/types/chainflip/index.d.ts +4 -0
- package/dist/types/chainflip/index.d.ts.map +1 -0
- package/dist/types/chainflip/plugin.d.ts +13 -0
- package/dist/types/chainflip/plugin.d.ts.map +1 -0
- package/dist/types/chainflip/types.d.ts +41 -0
- package/dist/types/chainflip/types.d.ts.map +1 -0
- package/dist/types/evm/index.d.ts +21 -0
- package/dist/types/evm/index.d.ts.map +1 -0
- package/dist/types/garden/index.d.ts +2 -0
- package/dist/types/garden/index.d.ts.map +1 -0
- package/dist/types/garden/plugin.d.ts +21 -0
- package/dist/types/garden/plugin.d.ts.map +1 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/near/index.d.ts +3 -0
- package/dist/types/near/index.d.ts.map +1 -0
- package/dist/types/near/nearNames.d.ts +3 -0
- package/dist/types/near/nearNames.d.ts.map +1 -0
- package/dist/types/near/plugin.d.ts +22 -0
- package/dist/types/near/plugin.d.ts.map +1 -0
- package/dist/types/near/types.d.ts +12 -0
- package/dist/types/near/types.d.ts.map +1 -0
- package/dist/types/radix/index.d.ts +13 -0
- package/dist/types/radix/index.d.ts.map +1 -0
- package/dist/types/solana/index.d.ts +2 -0
- package/dist/types/solana/index.d.ts.map +1 -0
- package/dist/types/solana/plugin.d.ts +13 -0
- package/dist/types/solana/plugin.d.ts.map +1 -0
- package/dist/types/thorchain/index.d.ts +4 -0
- package/dist/types/thorchain/index.d.ts.map +1 -0
- package/dist/types/thorchain/plugin.d.ts +110 -0
- package/dist/types/thorchain/plugin.d.ts.map +1 -0
- package/dist/types/thorchain/shared.d.ts +21 -0
- package/dist/types/thorchain/shared.d.ts.map +1 -0
- package/dist/types/thorchain/types.d.ts +64 -0
- package/dist/types/thorchain/types.d.ts.map +1 -0
- package/dist/types/types.d.ts +19 -0
- package/dist/types/types.d.ts.map +1 -0
- package/dist/types/utils.d.ts +20 -0
- package/dist/types/utils.d.ts.map +1 -0
- package/package.json +88 -0
- package/src/chainflip/broker.ts +116 -0
- package/src/chainflip/index.ts +3 -0
- package/src/chainflip/plugin.ts +56 -0
- package/src/chainflip/types.ts +37 -0
- package/src/evm/index.ts +49 -0
- package/src/garden/index.ts +1 -0
- package/src/garden/plugin.ts +50 -0
- package/src/index.ts +41 -0
- package/src/near/index.ts +2 -0
- package/src/near/nearNames.ts +37 -0
- package/src/near/plugin.ts +214 -0
- package/src/near/types.ts +9 -0
- package/src/radix/index.ts +28 -0
- package/src/solana/index.ts +1 -0
- package/src/solana/plugin.ts +30 -0
- package/src/thorchain/index.ts +3 -0
- package/src/thorchain/plugin.ts +491 -0
- package/src/thorchain/shared.ts +17 -0
- package/src/thorchain/types.ts +59 -0
- package/src/types.ts +26 -0
- package/src/utils.ts +46 -0
|
@@ -0,0 +1,491 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
ApproveMode,
|
|
7
|
+
type ApproveReturnType,
|
|
8
|
+
AssetValue,
|
|
9
|
+
Chain,
|
|
10
|
+
type ErrorKeys,
|
|
11
|
+
type EVMChain,
|
|
12
|
+
EVMChains,
|
|
13
|
+
FeeOption,
|
|
14
|
+
getChainConfig,
|
|
15
|
+
getMemoForDeposit,
|
|
16
|
+
getMemoForLeaveAndBond,
|
|
17
|
+
getMemoForNamePreferredAssetRegister,
|
|
18
|
+
getMemoForNameRegister,
|
|
19
|
+
getMemoForTcyClaim,
|
|
20
|
+
getMemoForTcyStake,
|
|
21
|
+
getMemoForUnbond,
|
|
22
|
+
getMemoForWithdraw,
|
|
23
|
+
getMinAmountByChain,
|
|
24
|
+
MemoType,
|
|
25
|
+
ProviderName,
|
|
26
|
+
type SwapParams,
|
|
27
|
+
type TCLikeChain,
|
|
28
|
+
USwapError,
|
|
29
|
+
wrapWithThrow,
|
|
30
|
+
} from "@tcswap/helpers";
|
|
31
|
+
import { type InboundAddressesItem, type QuoteResponseRoute, type THORNodeType, USwapApi } from "@tcswap/helpers/api";
|
|
32
|
+
import {
|
|
33
|
+
MayaArbitrumVaultAbi,
|
|
34
|
+
MayaEthereumVaultAbi,
|
|
35
|
+
TCAvalancheDepositABI,
|
|
36
|
+
TCBaseDepositABI,
|
|
37
|
+
TCBscDepositABI,
|
|
38
|
+
TCEthereumVaultAbi,
|
|
39
|
+
} from "@tcswap/helpers/contracts";
|
|
40
|
+
import type { SwapKitPluginParams } from "../types";
|
|
41
|
+
import { createPlugin } from "../utils";
|
|
42
|
+
import { prepareTxParams, validateAddressType } from "./shared";
|
|
43
|
+
import type {
|
|
44
|
+
AddLiquidityParams,
|
|
45
|
+
AddLiquidityPartParams,
|
|
46
|
+
CoreTxParams,
|
|
47
|
+
CreateLiquidityParams,
|
|
48
|
+
NodeActionParams,
|
|
49
|
+
RegisterThornameParams,
|
|
50
|
+
WithdrawParams,
|
|
51
|
+
} from "./types";
|
|
52
|
+
|
|
53
|
+
const gasFeeMultiplier: Record<FeeOption, number> = {
|
|
54
|
+
[FeeOption.Average]: 1.2,
|
|
55
|
+
[FeeOption.Fast]: 1.5,
|
|
56
|
+
[FeeOption.Fastest]: 2,
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const TCSpecificAbi = {
|
|
60
|
+
[Chain.Avalanche]: TCAvalancheDepositABI,
|
|
61
|
+
[Chain.Base]: TCBaseDepositABI,
|
|
62
|
+
[Chain.BinanceSmartChain]: TCBscDepositABI,
|
|
63
|
+
[Chain.Ethereum]: TCEthereumVaultAbi,
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const MayaSpecificAbi = { [Chain.Arbitrum]: MayaArbitrumVaultAbi, [Chain.Ethereum]: MayaEthereumVaultAbi };
|
|
67
|
+
|
|
68
|
+
export const ThorchainPlugin = createPlugin({
|
|
69
|
+
methods: createTCBasedPlugin(Chain.THORChain),
|
|
70
|
+
name: "thorchain",
|
|
71
|
+
properties: { supportedUSwapProviders: [ProviderName.THORCHAIN, ProviderName.THORCHAIN_STREAMING] as const },
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
export const MayachainPlugin = createPlugin({
|
|
75
|
+
methods: createTCBasedPlugin(Chain.Maya),
|
|
76
|
+
name: "mayachain",
|
|
77
|
+
properties: { supportedUSwapProviders: [ProviderName.MAYACHAIN, ProviderName.MAYACHAIN_STREAMING] as const },
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
function getInboundDataFunction(type?: THORNodeType) {
|
|
81
|
+
return async function getInboundDataByChain<T extends Chain>(chain: T) {
|
|
82
|
+
if ((type === "thorchain" && chain === Chain.THORChain) || (type === "mayachain" && chain === Chain.Maya)) {
|
|
83
|
+
return {
|
|
84
|
+
address: "",
|
|
85
|
+
chain,
|
|
86
|
+
dust_threshold: "0",
|
|
87
|
+
gas_rate: "0",
|
|
88
|
+
halted: false,
|
|
89
|
+
router: "",
|
|
90
|
+
} as InboundAddressesItem;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const inboundData = await USwapApi.thornode.getInboundAddresses(type);
|
|
94
|
+
const chainAddressData = inboundData.find((item) => item.chain === chain);
|
|
95
|
+
|
|
96
|
+
if (!chainAddressData) throw new USwapError("core_inbound_data_not_found");
|
|
97
|
+
if (chainAddressData?.halted) throw new USwapError("core_chain_halted");
|
|
98
|
+
|
|
99
|
+
return chainAddressData;
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function createTCBasedPlugin<T extends TCLikeChain>(pluginChain: T) {
|
|
104
|
+
return function plugin({ getWallet }: SwapKitPluginParams) {
|
|
105
|
+
const pluginType = pluginChain === Chain.Maya ? "mayachain" : "thorchain";
|
|
106
|
+
const getInboundDataByChain = getInboundDataFunction(pluginType);
|
|
107
|
+
|
|
108
|
+
async function approve<T extends ApproveMode>({
|
|
109
|
+
assetValue,
|
|
110
|
+
type = "checkOnly" as T,
|
|
111
|
+
}: {
|
|
112
|
+
type: T;
|
|
113
|
+
assetValue: AssetValue;
|
|
114
|
+
}) {
|
|
115
|
+
const router = (await getInboundDataByChain(assetValue.chain)).router as string;
|
|
116
|
+
|
|
117
|
+
const chain = assetValue.chain as EVMChain;
|
|
118
|
+
|
|
119
|
+
const isEVMChain = EVMChains.includes(chain as EVMChain);
|
|
120
|
+
const isNativeEVM = isEVMChain && assetValue.isGasAsset;
|
|
121
|
+
|
|
122
|
+
if (isNativeEVM || !isEVMChain || assetValue.isSynthetic) {
|
|
123
|
+
return Promise.resolve(type === "checkOnly" ? true : "approved") as ApproveReturnType<T>;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const wallet = getWallet(chain);
|
|
127
|
+
|
|
128
|
+
if (!wallet) {
|
|
129
|
+
throw new USwapError("core_wallet_connection_not_found");
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const walletAction = type === "checkOnly" ? wallet.isApproved : wallet.approve;
|
|
133
|
+
|
|
134
|
+
if (!(assetValue.address && wallet.address)) {
|
|
135
|
+
throw new USwapError("core_approve_asset_address_or_from_not_found");
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
return walletAction({
|
|
139
|
+
amount: assetValue.getBaseValue("bigint"),
|
|
140
|
+
assetAddress: assetValue.address,
|
|
141
|
+
from: wallet.address,
|
|
142
|
+
spenderAddress: router,
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO
|
|
147
|
+
async function deposit({ assetValue, recipient, router, ...rest }: CoreTxParams & { router?: string }) {
|
|
148
|
+
const abis = pluginType === "thorchain" ? TCSpecificAbi : MayaSpecificAbi;
|
|
149
|
+
const { chain, symbol, ticker } = assetValue;
|
|
150
|
+
|
|
151
|
+
const wallet = getWallet(chain);
|
|
152
|
+
if (!wallet) {
|
|
153
|
+
throw new USwapError("core_wallet_connection_not_found");
|
|
154
|
+
}
|
|
155
|
+
const { address } = wallet;
|
|
156
|
+
const isAddressValidated = validateAddressType({ address, chain });
|
|
157
|
+
if (!isAddressValidated) {
|
|
158
|
+
throw new USwapError("core_transaction_invalid_sender_address");
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const params = prepareTxParams({ assetValue, from: address, recipient, router, ...rest });
|
|
162
|
+
|
|
163
|
+
try {
|
|
164
|
+
const abi = abis?.[chain as keyof typeof abis];
|
|
165
|
+
|
|
166
|
+
if (!abi) {
|
|
167
|
+
const wallet = getWallet(chain as TCLikeChain);
|
|
168
|
+
const shouldDeposit = pluginChain === chain && recipient === "";
|
|
169
|
+
// @Towan: Is that the same action? :)
|
|
170
|
+
return shouldDeposit ? wallet.deposit(params) : wallet.transfer(params);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const { getChecksumAddressFromAsset } = await import("@tcswap/toolboxes/evm");
|
|
174
|
+
const wallet = getWallet(chain as EVMChain);
|
|
175
|
+
|
|
176
|
+
return wallet.call<string>({
|
|
177
|
+
abi,
|
|
178
|
+
contractAddress: router || ((await getInboundDataByChain(chain)).router as string),
|
|
179
|
+
funcName: "depositWithExpiry",
|
|
180
|
+
funcParams: [
|
|
181
|
+
recipient,
|
|
182
|
+
getChecksumAddressFromAsset({ chain, symbol, ticker }, chain as EVMChain),
|
|
183
|
+
assetValue.getBaseValue("string"),
|
|
184
|
+
params.memo,
|
|
185
|
+
rest.expiration || Number.parseInt(`${(Date.now() + 15 * 60 * 1000) / 1000}`, 10),
|
|
186
|
+
],
|
|
187
|
+
txOverrides: {
|
|
188
|
+
from: params.from,
|
|
189
|
+
value: assetValue.isGasAsset ? assetValue.getBaseValue("bigint") : undefined,
|
|
190
|
+
},
|
|
191
|
+
});
|
|
192
|
+
} catch (error) {
|
|
193
|
+
const errorMessage =
|
|
194
|
+
// @ts-expect-error Fine to use error as string
|
|
195
|
+
typeof error === "string" ? error.toLowerCase() : error?.message.toLowerCase();
|
|
196
|
+
const isInsufficientFunds = errorMessage?.includes("insufficient funds");
|
|
197
|
+
const isGas = errorMessage?.includes("gas");
|
|
198
|
+
const isServer = errorMessage?.includes("server");
|
|
199
|
+
const isUserRejected = errorMessage?.includes("user rejected");
|
|
200
|
+
const errorKey: ErrorKeys = isInsufficientFunds
|
|
201
|
+
? "core_transaction_deposit_insufficient_funds_error"
|
|
202
|
+
: isGas
|
|
203
|
+
? "core_transaction_deposit_gas_error"
|
|
204
|
+
: isServer
|
|
205
|
+
? "core_transaction_deposit_server_error"
|
|
206
|
+
: isUserRejected
|
|
207
|
+
? "core_transaction_user_rejected"
|
|
208
|
+
: "core_transaction_deposit_error";
|
|
209
|
+
|
|
210
|
+
throw new USwapError(errorKey, error);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
async function depositToProtocol({ memo, assetValue }: { assetValue: AssetValue; memo: string }) {
|
|
215
|
+
const mimir = await USwapApi.thornode.getMimirInfo(pluginType);
|
|
216
|
+
|
|
217
|
+
// check if trading is halted or not
|
|
218
|
+
if (mimir.HALTCHAINGLOBAL >= 1 || mimir.HALTTHORCHAIN >= 1) {
|
|
219
|
+
throw new USwapError("thorchain_chain_halted");
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
return deposit({ assetValue, memo, recipient: "" });
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
async function depositToPool({
|
|
226
|
+
assetValue,
|
|
227
|
+
memo,
|
|
228
|
+
feeOptionKey = FeeOption.Fast,
|
|
229
|
+
}: {
|
|
230
|
+
assetValue: AssetValue;
|
|
231
|
+
memo: string;
|
|
232
|
+
feeOptionKey?: FeeOption;
|
|
233
|
+
}) {
|
|
234
|
+
const { gas_rate = "0", router, address: poolAddress } = await getInboundDataByChain(assetValue.chain);
|
|
235
|
+
|
|
236
|
+
return deposit({
|
|
237
|
+
assetValue,
|
|
238
|
+
feeRate: Number.parseInt(gas_rate, 10) * gasFeeMultiplier[feeOptionKey],
|
|
239
|
+
memo,
|
|
240
|
+
recipient: poolAddress,
|
|
241
|
+
router,
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
function approveAssetValue({ assetValue }: { assetValue: AssetValue }) {
|
|
246
|
+
return approve({ assetValue, type: ApproveMode.Approve });
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
function isAssetValueApproved({ assetValue }: { assetValue: AssetValue }) {
|
|
250
|
+
return approve({ assetValue, type: ApproveMode.CheckOnly });
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function registerName({ assetValue, ...params }: RegisterThornameParams) {
|
|
254
|
+
return depositToProtocol({ assetValue, memo: getMemoForNameRegister(params) });
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
function registerPreferredAsset({
|
|
258
|
+
assetValue,
|
|
259
|
+
payoutAddress,
|
|
260
|
+
name,
|
|
261
|
+
ownerAddress,
|
|
262
|
+
}: {
|
|
263
|
+
assetValue: AssetValue;
|
|
264
|
+
payoutAddress?: string;
|
|
265
|
+
name: string;
|
|
266
|
+
ownerAddress: string;
|
|
267
|
+
}) {
|
|
268
|
+
const payout = payoutAddress || getWallet(assetValue.chain)?.address;
|
|
269
|
+
|
|
270
|
+
if (!payout) {
|
|
271
|
+
throw new USwapError("thorchain_preferred_asset_payout_required");
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return depositToProtocol({
|
|
275
|
+
assetValue: AssetValue.from({ chain: pluginChain }),
|
|
276
|
+
memo: getMemoForNamePreferredAssetRegister({
|
|
277
|
+
asset: assetValue.toString(),
|
|
278
|
+
chain: assetValue.chain,
|
|
279
|
+
name,
|
|
280
|
+
owner: ownerAddress,
|
|
281
|
+
payout,
|
|
282
|
+
}),
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function nodeAction({ type, assetValue, address }: NodeActionParams) {
|
|
287
|
+
const memo =
|
|
288
|
+
type === MemoType.UNBOND
|
|
289
|
+
? getMemoForUnbond({ address, unbondAmount: assetValue.getBaseValue("number") })
|
|
290
|
+
: getMemoForLeaveAndBond({ address, type });
|
|
291
|
+
|
|
292
|
+
const assetToTransfer = type === MemoType.BOND ? assetValue : getMinAmountByChain(pluginChain);
|
|
293
|
+
return depositToProtocol({ assetValue: assetToTransfer, memo });
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
async function createLiquidity({ baseAssetValue, assetValue }: CreateLiquidityParams) {
|
|
297
|
+
if (baseAssetValue.lte(0) || assetValue.lte(0)) {
|
|
298
|
+
throw new USwapError("core_transaction_create_liquidity_invalid_params");
|
|
299
|
+
}
|
|
300
|
+
|
|
301
|
+
const assetAddress = getWallet(assetValue.chain).address;
|
|
302
|
+
const baseAssetAddress = getWallet(pluginChain).address;
|
|
303
|
+
|
|
304
|
+
const baseAssetTx = await wrapWithThrow(() => {
|
|
305
|
+
return depositToPool({
|
|
306
|
+
assetValue: baseAssetValue,
|
|
307
|
+
memo: getMemoForDeposit({ ...assetValue, address: assetAddress }),
|
|
308
|
+
});
|
|
309
|
+
}, "core_transaction_create_liquidity_base_error");
|
|
310
|
+
|
|
311
|
+
const assetTx = await wrapWithThrow(() => {
|
|
312
|
+
return depositToPool({ assetValue, memo: getMemoForDeposit({ ...assetValue, address: baseAssetAddress }) });
|
|
313
|
+
}, "core_transaction_create_liquidity_asset_error");
|
|
314
|
+
|
|
315
|
+
return { assetTx, baseAssetTx };
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
function addLiquidityPart({ assetValue, poolAddress, address, symmetric }: AddLiquidityPartParams) {
|
|
319
|
+
if (symmetric && !address) {
|
|
320
|
+
throw new USwapError("core_transaction_add_liquidity_invalid_params");
|
|
321
|
+
}
|
|
322
|
+
const memo = getMemoForDeposit({
|
|
323
|
+
address: symmetric ? address : "",
|
|
324
|
+
chain: poolAddress.split(".")[0] as Chain,
|
|
325
|
+
symbol: poolAddress.split(".")[1] as string,
|
|
326
|
+
});
|
|
327
|
+
|
|
328
|
+
return depositToPool({ assetValue, memo });
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO
|
|
332
|
+
async function addLiquidity({
|
|
333
|
+
baseAssetValue,
|
|
334
|
+
assetValue,
|
|
335
|
+
baseAssetAddr,
|
|
336
|
+
assetAddr,
|
|
337
|
+
isPendingSymmAsset,
|
|
338
|
+
mode = "sym",
|
|
339
|
+
}: AddLiquidityParams) {
|
|
340
|
+
const { chain, symbol } = assetValue;
|
|
341
|
+
const isSym = mode === "sym";
|
|
342
|
+
const baseTransfer = baseAssetValue?.gt(0) && (isSym || mode === "baseAsset");
|
|
343
|
+
const assetTransfer = assetValue?.gt(0) && (isSym || mode === "asset");
|
|
344
|
+
const includeBaseAddress = isPendingSymmAsset || baseTransfer;
|
|
345
|
+
const baseAssetWalletAddress = getWallet(pluginChain).address;
|
|
346
|
+
|
|
347
|
+
const baseAddress = includeBaseAddress ? baseAssetAddr || baseAssetWalletAddress : "";
|
|
348
|
+
const assetAddress = isSym || mode === "asset" ? assetAddr || getWallet(chain).address : "";
|
|
349
|
+
|
|
350
|
+
if (!(baseTransfer || assetTransfer)) {
|
|
351
|
+
throw new USwapError("core_transaction_add_liquidity_invalid_params");
|
|
352
|
+
}
|
|
353
|
+
if (includeBaseAddress && !baseAddress) {
|
|
354
|
+
throw new USwapError("core_transaction_add_liquidity_base_address");
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
const baseAssetTx =
|
|
358
|
+
baseTransfer && baseAssetValue
|
|
359
|
+
? await wrapWithThrow(() => {
|
|
360
|
+
return depositToPool({
|
|
361
|
+
assetValue: baseAssetValue,
|
|
362
|
+
memo: getMemoForDeposit({ address: assetAddress, chain, symbol }),
|
|
363
|
+
});
|
|
364
|
+
}, "core_transaction_add_liquidity_base_error")
|
|
365
|
+
: undefined;
|
|
366
|
+
|
|
367
|
+
const assetTx =
|
|
368
|
+
assetTransfer && assetValue
|
|
369
|
+
? await wrapWithThrow(() => {
|
|
370
|
+
return depositToPool({ assetValue, memo: getMemoForDeposit({ address: baseAddress, chain, symbol }) });
|
|
371
|
+
}, "core_transaction_add_liquidity_asset_error")
|
|
372
|
+
: undefined;
|
|
373
|
+
|
|
374
|
+
return { assetTx, baseAssetTx };
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
function withdraw({ memo, assetValue, percent, from, to }: WithdrawParams) {
|
|
378
|
+
const targetAsset =
|
|
379
|
+
to === "baseAsset" && from !== "baseAsset"
|
|
380
|
+
? AssetValue.from({ chain: pluginChain })
|
|
381
|
+
: (from === "sym" && to === "sym") || from === "baseAsset" || from === "asset"
|
|
382
|
+
? undefined
|
|
383
|
+
: assetValue;
|
|
384
|
+
|
|
385
|
+
const value = getMinAmountByChain(from === "asset" ? assetValue.chain : pluginChain);
|
|
386
|
+
const memoString =
|
|
387
|
+
memo ||
|
|
388
|
+
getMemoForWithdraw({
|
|
389
|
+
basisPoints: Math.min(10000, Math.round(percent * 100)),
|
|
390
|
+
chain: assetValue.chain,
|
|
391
|
+
symbol: assetValue.symbol,
|
|
392
|
+
targetAsset: targetAsset?.toString(),
|
|
393
|
+
ticker: assetValue.ticker,
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
return depositToPool({ assetValue: value, memo: memoString });
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async function claimTcy({ chain, thorAddress }: { chain: Chain; thorAddress: string }) {
|
|
400
|
+
const inboundData = await getInboundDataByChain(chain);
|
|
401
|
+
const dust_threshold = inboundData.dust_threshold;
|
|
402
|
+
const { baseDecimal: chainDecimal } = getChainConfig(chain);
|
|
403
|
+
const { baseDecimal: tcDecimal } = getChainConfig(Chain.THORChain);
|
|
404
|
+
|
|
405
|
+
return deposit({
|
|
406
|
+
assetValue: AssetValue.from({
|
|
407
|
+
chain,
|
|
408
|
+
fromBaseDecimal: Math.min(chainDecimal, tcDecimal),
|
|
409
|
+
value: chain !== Chain.THORChain ? dust_threshold : 0,
|
|
410
|
+
}),
|
|
411
|
+
memo: getMemoForTcyClaim(MemoType.CLAIM_TCY, { address: thorAddress }),
|
|
412
|
+
recipient: inboundData.address,
|
|
413
|
+
router: inboundData.router,
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
function stakeTcyAction(
|
|
418
|
+
params: { type: "unstake"; unstakeBps: number } | { type: "stake"; assetValue: AssetValue },
|
|
419
|
+
) {
|
|
420
|
+
if (params.type === "stake") {
|
|
421
|
+
if (params.assetValue.toString() !== "THOR.TCY") {
|
|
422
|
+
throw new USwapError("thorchain_asset_is_not_tcy");
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
return deposit({
|
|
426
|
+
assetValue: params.assetValue,
|
|
427
|
+
memo: getMemoForTcyStake(MemoType.STAKE_TCY, {}),
|
|
428
|
+
recipient: "",
|
|
429
|
+
});
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
return deposit({
|
|
433
|
+
assetValue: AssetValue.from({ chain: Chain.THORChain }),
|
|
434
|
+
memo: getMemoForTcyStake(MemoType.UNSTAKE_TCY, { unstakeBps: params.unstakeBps }),
|
|
435
|
+
recipient: "",
|
|
436
|
+
});
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
async function swap({ feeOptionKey, route }: SwapParams<typeof pluginType, QuoteResponseRoute>) {
|
|
440
|
+
const { memo, expiration, targetAddress } = route;
|
|
441
|
+
|
|
442
|
+
const assetValue = await AssetValue.from({
|
|
443
|
+
asset: route.sellAsset,
|
|
444
|
+
asyncTokenLookup: true,
|
|
445
|
+
value: route.sellAmount,
|
|
446
|
+
});
|
|
447
|
+
|
|
448
|
+
if (!assetValue) {
|
|
449
|
+
throw new USwapError("core_swap_asset_not_recognized");
|
|
450
|
+
}
|
|
451
|
+
|
|
452
|
+
const isRecipientValidated = validateAddressType({
|
|
453
|
+
address: route.destinationAddress,
|
|
454
|
+
chain: AssetValue.from({ asset: route.buyAsset }).chain,
|
|
455
|
+
});
|
|
456
|
+
|
|
457
|
+
if (!isRecipientValidated) {
|
|
458
|
+
throw new USwapError("core_transaction_invalid_recipient_address");
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
const { address: recipient } = await getInboundDataByChain(assetValue.chain);
|
|
462
|
+
|
|
463
|
+
return deposit({
|
|
464
|
+
assetValue,
|
|
465
|
+
expiration: Number(expiration),
|
|
466
|
+
feeOptionKey,
|
|
467
|
+
memo,
|
|
468
|
+
recipient,
|
|
469
|
+
router: targetAddress,
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
return {
|
|
474
|
+
addLiquidity,
|
|
475
|
+
addLiquidityPart,
|
|
476
|
+
approveAssetValue,
|
|
477
|
+
claimTcy,
|
|
478
|
+
createLiquidity,
|
|
479
|
+
deposit,
|
|
480
|
+
depositToPool,
|
|
481
|
+
getInboundDataByChain,
|
|
482
|
+
isAssetValueApproved,
|
|
483
|
+
nodeAction,
|
|
484
|
+
registerName,
|
|
485
|
+
registerPreferredAsset,
|
|
486
|
+
stakeTcyAction,
|
|
487
|
+
swap,
|
|
488
|
+
withdraw,
|
|
489
|
+
};
|
|
490
|
+
};
|
|
491
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Chain } from "@tcswap/helpers";
|
|
2
|
+
import type { CoreTxParams } from "./types";
|
|
3
|
+
|
|
4
|
+
export function validateAddressType({ chain, address }: { chain?: Chain; address?: string }) {
|
|
5
|
+
if (!address) return false;
|
|
6
|
+
|
|
7
|
+
return chain === Chain.Bitcoin ? !address.startsWith("bc1p") : true;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function prepareTxParams({
|
|
11
|
+
assetValue,
|
|
12
|
+
from,
|
|
13
|
+
memo = "",
|
|
14
|
+
...restTxParams
|
|
15
|
+
}: CoreTxParams & { from: string; router?: string }) {
|
|
16
|
+
return { ...restTxParams, assetValue, from, memo };
|
|
17
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import type { AssetValue, FeeOption, MemoType } from "@tcswap/helpers";
|
|
2
|
+
|
|
3
|
+
export type AddLiquidityPartParams = {
|
|
4
|
+
assetValue: AssetValue;
|
|
5
|
+
address?: string;
|
|
6
|
+
poolAddress: string;
|
|
7
|
+
symmetric: boolean;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export type AddLiquidityParams = {
|
|
11
|
+
assetAddr?: string;
|
|
12
|
+
assetValue: AssetValue;
|
|
13
|
+
baseAssetAddr?: string;
|
|
14
|
+
baseAssetValue: AssetValue;
|
|
15
|
+
isPendingSymmAsset?: boolean;
|
|
16
|
+
mode?: "sym" | "baseAsset" | "asset";
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type CreateLiquidityParams = { baseAssetValue: AssetValue; assetValue: AssetValue };
|
|
20
|
+
|
|
21
|
+
export type CoreTxParams = {
|
|
22
|
+
assetValue: AssetValue;
|
|
23
|
+
recipient: string;
|
|
24
|
+
memo?: string;
|
|
25
|
+
feeOptionKey?: FeeOption;
|
|
26
|
+
feeRate?: number;
|
|
27
|
+
data?: string;
|
|
28
|
+
from?: string;
|
|
29
|
+
expiration?: number;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
export type NodeActionParams = { address: string } & (
|
|
33
|
+
| { type: MemoType.BOND | MemoType.UNBOND; assetValue: AssetValue }
|
|
34
|
+
| { type: MemoType.LEAVE; assetValue?: undefined }
|
|
35
|
+
);
|
|
36
|
+
|
|
37
|
+
export type RegisterThornameParams = {
|
|
38
|
+
assetValue: AssetValue;
|
|
39
|
+
name: string;
|
|
40
|
+
chain: string;
|
|
41
|
+
address: string;
|
|
42
|
+
owner?: string;
|
|
43
|
+
preferredAsset?: string;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
export type RegisterPreferredAssetParams = {
|
|
47
|
+
assetValue: AssetValue;
|
|
48
|
+
name: string;
|
|
49
|
+
chain: string;
|
|
50
|
+
address: string;
|
|
51
|
+
owner: string;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
type CommonWithdrawParams = { assetValue: AssetValue; memo?: string; percent: number };
|
|
55
|
+
|
|
56
|
+
export type WithdrawParams = CommonWithdrawParams & {
|
|
57
|
+
from: "sym" | "baseAsset" | "asset";
|
|
58
|
+
to: "sym" | "baseAsset" | "asset";
|
|
59
|
+
};
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { Chain } from "@tcswap/helpers";
|
|
6
|
+
import type { FullWallet } from "@tcswap/toolboxes";
|
|
7
|
+
import type { ChainflipPlugin } from "./chainflip";
|
|
8
|
+
import type { EVMPlugin } from "./evm";
|
|
9
|
+
import type { NearPlugin } from "./near";
|
|
10
|
+
import type { RadixPlugin } from "./radix";
|
|
11
|
+
import type { SolanaPlugin } from "./solana/plugin";
|
|
12
|
+
import type { ThorchainPlugin } from "./thorchain";
|
|
13
|
+
|
|
14
|
+
export type * from "./chainflip/types";
|
|
15
|
+
export type * from "./thorchain/types";
|
|
16
|
+
|
|
17
|
+
export type USwapPlugins = typeof ChainflipPlugin &
|
|
18
|
+
typeof ThorchainPlugin &
|
|
19
|
+
typeof RadixPlugin &
|
|
20
|
+
typeof SolanaPlugin &
|
|
21
|
+
typeof EVMPlugin &
|
|
22
|
+
typeof NearPlugin;
|
|
23
|
+
|
|
24
|
+
export type PluginName = keyof USwapPlugins;
|
|
25
|
+
|
|
26
|
+
export type SwapKitPluginParams = { getWallet: <T extends Chain>(chain: T) => FullWallet[T] };
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Modifications © 2025 Horizontal Systems.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import type { ApproveMode, ApproveReturnType, EVMChain, ProviderName } from "@tcswap/helpers";
|
|
6
|
+
import { type AssetValue, EVMChains, USwapError } from "@tcswap/helpers";
|
|
7
|
+
import type { SwapKitPluginParams } from "./types";
|
|
8
|
+
|
|
9
|
+
export function createPlugin<
|
|
10
|
+
const Name extends string,
|
|
11
|
+
T extends (params: SwapKitPluginParams) => Record<string, unknown>,
|
|
12
|
+
K extends { supportedUSwapProviders?: readonly ProviderName[] },
|
|
13
|
+
>({ name, properties, methods }: { name: Name; properties?: K; methods: T }) {
|
|
14
|
+
function plugin(pluginParams: SwapKitPluginParams) {
|
|
15
|
+
return { ...methods(pluginParams), ...properties } as K & ReturnType<T>;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
return { [name]: plugin } as { [key in Name]: typeof plugin };
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function approve<T extends ApproveMode>({ approveMode, getWallet }: { approveMode: T } & SwapKitPluginParams) {
|
|
22
|
+
return function approve({ assetValue, spenderAddress }: { spenderAddress: string; assetValue: AssetValue }) {
|
|
23
|
+
const evmChain = assetValue.chain as EVMChain;
|
|
24
|
+
const isEVMChain = EVMChains.includes(evmChain);
|
|
25
|
+
const isNativeEVM = isEVMChain && assetValue.isGasAsset;
|
|
26
|
+
|
|
27
|
+
if (isNativeEVM || !isEVMChain || assetValue.isSynthetic) {
|
|
28
|
+
const isApproved = approveMode === "checkOnly" || "approved";
|
|
29
|
+
return Promise.resolve(isApproved) as ApproveReturnType<T>;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const wallet = getWallet(evmChain);
|
|
33
|
+
const walletAction = approveMode === "checkOnly" ? wallet.isApproved : wallet.approve;
|
|
34
|
+
|
|
35
|
+
if (!(assetValue.address && wallet.address)) {
|
|
36
|
+
throw new USwapError("core_approve_asset_address_or_from_not_found");
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return walletAction({
|
|
40
|
+
amount: assetValue.getBaseValue("bigint"),
|
|
41
|
+
assetAddress: assetValue.address,
|
|
42
|
+
from: wallet.address,
|
|
43
|
+
spenderAddress,
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
}
|