-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Mayachain action * Changeset version file
- Loading branch information
Showing
8 changed files
with
200 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@xchainjs/xchain-mayachain-amm': patch | ||
--- | ||
|
||
Bug fix with Arbitrum chain |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@xchainjs/xchain-mayachain-amm': patch | ||
--- | ||
|
||
Refactor of Mayachain action |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import { abi } from '@xchainjs/xchain-evm' | ||
import { MAYAChain } from '@xchainjs/xchain-mayachain' | ||
import { MayachainQuery } from '@xchainjs/xchain-mayachain-query' | ||
import { Address, CryptoAmount, baseAmount, getContractAddressFromAsset } from '@xchainjs/xchain-util' | ||
import { Wallet } from '@xchainjs/xchain-wallet' | ||
import { ethers } from 'ethers' | ||
|
||
import { TxSubmitted } from './types' | ||
import { isProtocolBFTChain, isProtocolERC20Asset, isProtocolEVMChain } from './utils' | ||
|
||
export type NonProtocolActionParams = { | ||
wallet: Wallet | ||
assetAmount: CryptoAmount | ||
recipient: Address | ||
memo: string | ||
} | ||
|
||
export type ProtocolActionParams = { | ||
wallet: Wallet | ||
assetAmount: CryptoAmount | ||
memo: string | ||
} | ||
|
||
export type ActionParams = ProtocolActionParams | NonProtocolActionParams | ||
|
||
export class MayachainAction { | ||
public static async makeAction(actionParams: ActionParams): Promise<TxSubmitted> { | ||
return this.isNonProtocolParams(actionParams) | ||
? this.makeNonProtocolAction(actionParams) | ||
: this.makeProtocolAction(actionParams) | ||
} | ||
|
||
private static async makeProtocolAction({ wallet, assetAmount, memo }: ProtocolActionParams): Promise<TxSubmitted> { | ||
const hash = await wallet.deposit({ | ||
chain: MAYAChain, | ||
asset: assetAmount.asset, | ||
amount: assetAmount.baseAmount, | ||
memo, | ||
}) | ||
|
||
return { | ||
hash, | ||
url: await wallet.getExplorerTxUrl(MAYAChain, hash), | ||
} | ||
} | ||
|
||
private static async makeNonProtocolAction({ | ||
wallet, | ||
assetAmount, | ||
recipient, | ||
memo, | ||
}: NonProtocolActionParams): Promise<TxSubmitted> { | ||
// Non EVM actions | ||
|
||
if (!isProtocolEVMChain(assetAmount.asset.chain)) { | ||
if (isProtocolBFTChain(assetAmount.asset.chain)) { | ||
const hash = await wallet.transfer({ | ||
asset: assetAmount.asset, | ||
amount: assetAmount.baseAmount, | ||
recipient, | ||
memo, | ||
}) | ||
return { | ||
hash, | ||
url: await wallet.getExplorerTxUrl(assetAmount.asset.chain, hash), | ||
} | ||
} | ||
const feeRates = await wallet.getFeeRates(assetAmount.asset.chain) | ||
const hash = await wallet.transfer({ | ||
asset: assetAmount.asset, | ||
amount: assetAmount.baseAmount, | ||
recipient, | ||
memo, | ||
feeRate: feeRates.fast, | ||
}) | ||
return { | ||
hash, | ||
url: await wallet.getExplorerTxUrl(assetAmount.asset.chain, hash), | ||
} | ||
} | ||
|
||
// EVM actions | ||
const mayachainQuery: MayachainQuery = new MayachainQuery() | ||
|
||
const inboundDetails = await mayachainQuery.getChainInboundDetails(assetAmount.asset.chain) | ||
if (!inboundDetails.router) throw Error(`Unknown router for ${assetAmount.asset.chain} chain`) | ||
|
||
const isERC20 = isProtocolERC20Asset(assetAmount.asset) | ||
|
||
const checkSummedContractAddress = isERC20 | ||
? ethers.utils.getAddress(getContractAddressFromAsset(assetAmount.asset)) | ||
: ethers.constants.AddressZero | ||
|
||
const expiration = Math.floor(new Date(new Date().getTime() + 15 * 60000).getTime() / 1000) | ||
const depositParams = [ | ||
recipient, | ||
checkSummedContractAddress, | ||
assetAmount.baseAmount.amount().toFixed(), | ||
memo, | ||
expiration, | ||
] | ||
|
||
const routerContract = new ethers.Contract(inboundDetails.router, abi.router) | ||
const gasPrices = await wallet.getFeeRates(assetAmount.asset.chain) | ||
|
||
const unsignedTx = await routerContract.populateTransaction.depositWithExpiry(...depositParams) | ||
|
||
const nativeAsset = wallet.getAssetInfo(assetAmount.asset.chain) | ||
|
||
const hash = await wallet.transfer({ | ||
asset: nativeAsset.asset, | ||
amount: isERC20 ? baseAmount(0, nativeAsset.decimal) : assetAmount.baseAmount, | ||
memo: unsignedTx.data, | ||
recipient: inboundDetails.router, | ||
gasPrice: gasPrices.fast, | ||
isMemoEncoded: true, | ||
gasLimit: ethers.BigNumber.from(160000), | ||
}) | ||
|
||
return { | ||
hash, | ||
url: await wallet.getExplorerTxUrl(assetAmount.asset.chain, hash), | ||
} | ||
} | ||
|
||
private static isNonProtocolParams(params: ActionParams): params is NonProtocolActionParams { | ||
if ( | ||
(params.assetAmount.asset.chain === MAYAChain || params.assetAmount.asset.synth) && | ||
'address' in params && | ||
!!params.address | ||
) { | ||
throw Error('Inconsistent params. Native actions do not support recipient') | ||
} | ||
return params.assetAmount.asset.chain !== MAYAChain && !params.assetAmount.asset.synth | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.