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