-
Notifications
You must be signed in to change notification settings - Fork 100
/
Copy pathmayachain-amm.ts
335 lines (309 loc) · 12.1 KB
/
mayachain-amm.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/**
* Import necessary modules and libraries
*/
import { Client as BtcClient, defaultBTCParams as defaultBtcParams } from '@xchainjs/xchain-bitcoin'
import { Network } from '@xchainjs/xchain-client'
import { Client as DashClient, defaultDashParams } from '@xchainjs/xchain-dash'
import { AssetETH, Client as EthClient, defaultEthParams } from '@xchainjs/xchain-ethereum'
import { MAX_APPROVAL, abi } from '@xchainjs/xchain-evm'
import { Client as KujiraClient, defaultKujiParams } from '@xchainjs/xchain-kujira'
import { Client as MayaClient, MAYAChain } from '@xchainjs/xchain-mayachain'
import { MayachainQuery, QuoteSwap, QuoteSwapParams } from '@xchainjs/xchain-mayachain-query'
import { Client as ThorClient } from '@xchainjs/xchain-thorchain'
import { Asset, CryptoAmount, baseAmount, eqAsset, getContractAddressFromAsset } from '@xchainjs/xchain-util'
import { Wallet } from '@xchainjs/xchain-wallet'
import { ethers } from 'ethers'
import { ApproveParams, IsApprovedParams, TxSubmitted } from './types'
/**
* Mayachain Automated Market Maker (AMM) class.
* MAYAChainAMM class for interacting with THORChain.
* Recommended main class to use for swapping with MAYAChain
* Has access to Midgard and MayaNode data
*/
export class MayachainAMM {
private mayachainQuery: MayachainQuery
private wallet: Wallet
/**
* Constructor to create a MayachainAMM instance.
*
* @param {MayachainQuery} mayachainQuery An instance of the MayachainQuery class.
* @param {Wallet} wallet A wallet instance containing clients for various blockchains.
* @returns {MayachainAMM} Returns the MayachainAMM instance.
*/
constructor(
mayachainQuery = new MayachainQuery(),
wallet = new Wallet({
BTC: new BtcClient({ ...defaultBtcParams, network: Network.Mainnet }),
ETH: new EthClient({ ...defaultEthParams, network: Network.Mainnet }),
DASH: new DashClient({ ...defaultDashParams, network: Network.Mainnet }),
KUJI: new KujiraClient({ ...defaultKujiParams, network: Network.Mainnet }),
THOR: new ThorClient({ network: Network.Mainnet }),
MAYA: new MayaClient({ network: Network.Mainnet }),
}),
) {
this.mayachainQuery = mayachainQuery
this.wallet = wallet
}
/**
* Estimate swap by validating the swap parameters.
*
* @param {QuoteSwapParams} quoteSwapParams Swap parameters.
* @returns {QuoteSwap} Quote swap result. If swap cannot be done, it returns an empty QuoteSwap with reasons.
*/
public async estimateSwap({
fromAsset,
fromAddress,
amount,
destinationAsset,
destinationAddress,
affiliateAddress,
affiliateBps,
toleranceBps,
}: QuoteSwapParams): Promise<QuoteSwap> {
const errors = await this.validateSwap({
fromAsset,
fromAddress,
amount,
destinationAsset,
destinationAddress,
affiliateAddress,
affiliateBps,
})
if (errors.length > 0) {
return {
toAddress: ``,
memo: ``,
expectedAmount: new CryptoAmount(baseAmount(0), destinationAsset),
dustThreshold: this.mayachainQuery.getChainDustValue(fromAsset.chain),
fees: {
asset: destinationAsset,
affiliateFee: new CryptoAmount(baseAmount(0), destinationAsset),
outboundFee: new CryptoAmount(baseAmount(0), destinationAsset),
},
outboundDelayBlocks: 0,
outboundDelaySeconds: 0,
inboundConfirmationSeconds: 0,
inboundConfirmationBlocks: 0,
canSwap: false,
errors,
slipBasisPoints: 0,
totalSwapSeconds: 0,
warning: '',
}
}
return this.mayachainQuery.quoteSwap({
fromAsset,
fromAddress,
amount,
destinationAsset,
destinationAddress,
affiliateAddress,
affiliateBps,
toleranceBps,
})
}
/**
* Validate swap parameters before performing a swap operation.
*
* @param {QuoteSwapParams} quoteSwapParams Swap parameters.
* @returns {string[]} Reasons the swap cannot be executed. Empty array if the swap is valid.
*/
public async validateSwap({
fromAsset,
fromAddress,
destinationAsset,
destinationAddress,
amount,
affiliateAddress,
affiliateBps,
}: QuoteSwapParams): Promise<string[]> {
const errors: string[] = []
// Validate destination address if provided
if (
destinationAddress &&
!this.wallet.validateAddress(destinationAsset.synth ? MAYAChain : destinationAsset.chain, destinationAddress)
) {
errors.push(`destinationAddress ${destinationAddress} is not a valid address`)
}
// Validate affiliate address if provided
if (affiliateAddress) {
const isMayaAddress = this.wallet.validateAddress(MAYAChain, affiliateAddress)
const isMayaName = await this.isMAYAName(affiliateAddress)
if (!(isMayaAddress || isMayaName))
errors.push(`affiliateAddress ${affiliateAddress} is not a valid MAYA address`)
}
// Validate affiliate basis points if provided
if (affiliateBps && (affiliateBps < 0 || affiliateBps > 10000)) {
errors.push(`affiliateBps ${affiliateBps} out of range [0 - 10000]`)
}
// Validate approval if asset is an ERC20 token and fromAddress is provided
if (this.isERC20Asset(fromAsset) && fromAddress) {
const approveErrors = await this.isRouterApprovedToSpend({
asset: fromAsset,
address: fromAddress,
amount,
})
errors.push(...approveErrors)
}
return errors
}
/**
* Perform a swap operation between assets.
* @param {QuoteSwapParams} quoteSwapParams Swap parameters
* @returns {TxSubmitted} Transaction hash and URL of the swap
*/
public async doSwap({
fromAsset,
fromAddress,
amount,
destinationAsset,
destinationAddress,
affiliateAddress,
affiliateBps,
toleranceBps,
}: QuoteSwapParams): Promise<TxSubmitted> {
const quoteSwap = await this.estimateSwap({
fromAsset,
fromAddress,
amount,
destinationAsset,
destinationAddress,
affiliateAddress,
affiliateBps,
toleranceBps,
})
// Check if the swap can be performed
if (!quoteSwap.canSwap) throw Error(`Can not swap. ${quoteSwap.errors.join(' ')}`)
// Perform the swap based on the asset chain
return fromAsset.chain === MAYAChain || fromAsset.synth
? this.doProtocolAssetSwap(amount, quoteSwap.memo)
: this.doNonProtocolAssetSwap(amount, quoteSwap.toAddress, quoteSwap.memo)
}
/**
* Approve the Mayachain router to spend a certain amount in the asset chain.
* @param {ApproveParams} approveParams Parameters for approving the router to spend
* @returns {Promise<TxSubmitted>} Transaction hash and URL
*/
public async approveRouterToSpend({ asset, amount }: ApproveParams): Promise<TxSubmitted> {
// Get inbound details for the asset chain
const inboundDetails = await this.mayachainQuery.getChainInboundDetails(asset.chain)
if (!inboundDetails.router) throw Error(`Unknown router address for ${asset.chain}`)
// Perform approval
const tx = await this.wallet.approve(
asset,
amount?.baseAmount || baseAmount(MAX_APPROVAL.toString(), await this.mayachainQuery.getAssetDecimals(asset)),
inboundDetails.router,
)
// Return transaction hash and URL
return {
hash: tx.hash,
url: await this.wallet.getExplorerTxUrl(asset.chain, tx.hash),
}
}
/**
* Validate if the asset router is allowed to spend the asset amount on behalf of the address.
* @param {IsApprovedParams} isApprovedParams Parameters for checking approval.
* @returns {string[]} Reasons the asset router is not allowed to spend the amount. Empty array if the router is approved.
*/
public async isRouterApprovedToSpend({ asset, amount, address }: IsApprovedParams): Promise<string[]> {
const errors: string[] = []
// Get inbound details for the asset chain
const inboundDetails = await this.mayachainQuery.getChainInboundDetails(asset.chain)
if (!inboundDetails.router) throw Error(`Unknown router address for ${asset.chain}`)
// Check if the router is approved to spend the amount
const isApprovedResult = await this.wallet.isApproved(asset, amount.baseAmount, address, inboundDetails.router)
if (!isApprovedResult) errors.push('Maya router has not been approved to spend this amount')
return errors
}
/**
* Check if a name is a valid MAYAName
* @param {string} name MAYAName to check
* @returns {boolean} True if the name is registered as a MAYAName, otherwise false
*/
private async isMAYAName(name: string): Promise<boolean> {
return !!(await this.mayachainQuery.getMAYANameDetails(name))
}
/**
* Perform a swap from a native protocol asset to any other asset
* @param {CryptoAmount} amount Amount to swap
* @param {string} memo Memo to add to the transaction
* @returns {TxSubmitted} Transaction hash and URL of the swap
*/
private async doProtocolAssetSwap(amount: CryptoAmount, memo: string): Promise<TxSubmitted> {
// Deposit the amount and return transaction hash and URL
const hash = await this.wallet.deposit({ asset: amount.asset, amount: amount.baseAmount, memo })
return {
hash,
url: await this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
}
}
/**
* Perform a swap between assets
* @param {CryptoAmount} amount Amount to swap
* @param {string} memo Memo to add to the transaction to successfully make the swap
* @param {string} recipient inbound address to make swap transaction to
* @returns {TxSubmitted} Transaction hash and URL of the swap
*/
private async doNonProtocolAssetSwap(amount: CryptoAmount, recipient: string, memo: string): Promise<TxSubmitted> {
// For non-ERC20 assets, perform a transfer and return transaction hash and URL
if (!this.isERC20Asset(amount.asset)) {
const hash = await this.wallet.transfer({
asset: amount.asset,
amount: amount.baseAmount,
recipient,
memo,
})
return {
hash,
url: await this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
}
}
// For ERC20 assets, perform a deposit with expiry and return transaction hash and URL
const inboundDetails = await this.mayachainQuery.getChainInboundDetails(amount.asset.chain)
if (!inboundDetails.router) throw Error(`Unknown router for ${amount.asset.chain} chain`)
const contractAddress = getContractAddressFromAsset(amount.asset)
const checkSummedContractAddress = ethers.utils.getAddress(contractAddress)
const expiration = Math.floor(new Date(new Date().getTime() + 15 * 60000).getTime() / 1000)
const depositParams = [
recipient,
checkSummedContractAddress,
amount.baseAmount.amount().toFixed(),
memo,
expiration,
]
const routerContract = new ethers.Contract(inboundDetails.router, abi.router)
const wallet = this.wallet.getChainWallet(amount.asset.chain)
const gasPrices = await this.wallet.getGasFeeRates(amount.asset.chain)
const unsignedTx = await routerContract.populateTransaction.depositWithExpiry(...depositParams, {
from: this.wallet.getAddress(amount.asset.chain),
value: 0,
gasPrice: gasPrices.fast.amount().toFixed(),
gasLimit: '160000',
})
const { hash } = await wallet.sendTransaction(unsignedTx)
return {
hash,
url: await this.wallet.getExplorerTxUrl(amount.asset.chain, hash),
}
}
/**
* Check if the asset is an ERC20 token
* @param {Asset} asset Asset to check
* @returns True if the asset is an ERC20 token, otherwise false
*/
private isERC20Asset(asset: Asset): boolean {
// Check if the asset's chain is an EVM chain and if the symbol matches AssetETH.symbol
return this.isEVMChain(asset.chain)
? [AssetETH].findIndex((nativeEVMAsset) => eqAsset(nativeEVMAsset, asset)) === -1
: false
}
/**
* Check if the chain is an EVM (Ethereum Virtual Machine) chain
* @param {Chain} chain Chain to check
* @returns True if the chain is an EVM chain, otherwise false
*/
private isEVMChain(chain: string): boolean {
// Check if the chain matches AssetETH.chain
return [AssetETH.chain].includes(chain)
}
}