Skip to content

Commit

Permalink
MayachainAMM approveRouterToSpend and isRouterApprovedToSpend
Browse files Browse the repository at this point in the history
  • Loading branch information
0xp3gasus committed Jan 10, 2024
1 parent 940e74c commit b107410
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
60 changes: 47 additions & 13 deletions packages/xchain-mayachain-amm/src/mayachain-amm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Client as BtcClient, defaultBTCParams as defaultBtcParams } from '@xcha
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 { abi } from '@xchainjs/xchain-evm'
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'
Expand All @@ -11,7 +11,7 @@ import { Asset, CryptoAmount, baseAmount, getContractAddressFromAsset } from '@x
import { Wallet } from '@xchainjs/xchain-wallet'
import { ethers } from 'ethers'

import { TxSubmitted } from './types'
import { ApproveParams, IsApprovedParams, TxSubmitted } from './types'

/**
* MAYAChainAMM class for interacting with THORChain.
Expand Down Expand Up @@ -134,17 +134,12 @@ export class MayachainAMM {
}

if (this.isERC20Asset(fromAsset) && fromAddress) {
const inboundDetails = await this.mayachainQuery.getChainInboundDetails(fromAsset.chain)
if (!inboundDetails.router) throw Error(`Unknown router address for ${fromAsset.chain}`)

const isApprovedResult = await this.wallet.isApproved(
fromAsset,
amount.baseAmount,
fromAddress,
inboundDetails.router,
)

if (!isApprovedResult) errors.push('Maya router has not been approved to spend this amount')
const approveErrors = await this.isRouterApprovedToSpend({
asset: fromAsset,
address: fromAddress,
amount: amount.baseAmount,
})
errors.push(...approveErrors)
}

return errors
Expand Down Expand Up @@ -183,6 +178,45 @@ export class MayachainAMM {
: this.doNonProtocolAssetSwap(amount, quoteSwap.toAddress, quoteSwap.memo)
}

/**
* Approve Mayachain router in the chain of the asset to spend the amount in name of the address
* @param {ApproveParams} approveParams contains the asset and amount the router will be allowed. If amount is not defined,
* an infinity approve will be done
*/
public async approveRouterToSpend({ asset, amount }: ApproveParams): Promise<TxSubmitted> {
const inboundDetails = await this.mayachainQuery.getChainInboundDetails(asset.chain)
if (!inboundDetails.router) throw Error(`Unknown router address for ${asset.chain}`)
const tx = await this.wallet.approve(
asset,
amount || baseAmount(MAX_APPROVAL.toString(), await this.mayachainQuery.getAssetDecimals(asset)),
inboundDetails.router,
)

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 in name of the address
* @param {IsApprovedParams} isApprovedParams contains the asset and the amount the router is supposed to spend
* int name of address
* @returns {string[]} the reasons the router of the asset is not allowed to spend the amount. If it is empty, the asset router is allowed to spend the amount
*/
public async isRouterApprovedToSpend({ asset, amount, address }: IsApprovedParams): Promise<string[]> {
const errors: string[] = []

const inboundDetails = await this.mayachainQuery.getChainInboundDetails(asset.chain)
if (!inboundDetails.router) throw Error(`Unknown router address for ${asset.chain}`)

const isApprovedResult = await this.wallet.isApproved(asset, amount, 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
Expand Down
13 changes: 13 additions & 0 deletions packages/xchain-mayachain-amm/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
import { Address, Asset, BaseAmount } from '@xchainjs/xchain-util'

export type TxSubmitted = {
hash: string
url: string
}

export type ApproveParams = {
asset: Asset
amount: BaseAmount | undefined
}

export type IsApprovedParams = {
asset: Asset
amount: BaseAmount
address: Address
}

0 comments on commit b107410

Please sign in to comment.