const { MoroBestRate } = require('@moromoro/moro-sdk')
const { ethers } = require('ethers')
class MultiChainSwapManager {
constructor(privateKey) {
this.privateKey = privateKey
this.clients = {}
this.providers = {}
}
addChain(network, rpcUrl) {
this.providers[network] = new ethers.providers.JsonRpcProvider(rpcUrl)
this.clients[network] = new MoroBestRate(
this.providers[network],
network
)
}
getSigner(network) {
return new ethers.Wallet(this.privateKey, this.providers[network])
}
async swap(network, srcToken, destToken, amountIn, slippage = 1) {
if (!this.clients[network]) {
throw new Error(`Network ${network} not configured`)
}
const client = this.clients[network]
const signer = this.getSigner(network)
// Get appropriate gas price
const gasPrice = await this.providers[network].getGasPrice()
// Get quote
const isL2 = network === 'arbitrum' || network === 'optimism'
const quoteOpts = { enableSplit: true }
if (isL2) {
quoteOpts.gasPriceL2 = gasPrice.div(10) // Estimate L2 gas
}
const quote = await client.getQuote(
srcToken,
destToken,
amountIn,
gasPrice,
quoteOpts
)
// Approve if needed
if (srcToken !== '0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee' &&
srcToken !== '0x5555555555555555555555555555555555555555') {
const approveTx = await client.approve(signer, srcToken, amountIn)
if (approveTx) await approveTx.wait()
}
// Calculate min output
const minOut = ethers.BigNumber.from(quote.amountOut)
.mul(100 - slippage)
.div(100)
// Execute swap
const tx = await client.swap(
signer,
srcToken,
destToken,
amountIn,
minOut,
quote
)
return tx.wait()
}
}
// Usage
async function useMultiChainManager() {
const manager = new MultiChainSwapManager(process.env.PRIVATE_KEY)
// Configure chains
manager.addChain('hyperevm', 'https://rpc.hyperliquid.xyz/evm')
manager.addChain('bsc', 'https://bsc-dataseed.binance.org')
manager.addChain('polygon', 'https://polygon-rpc.com')
// Swap on HyperEVM
await manager.swap(
'hyperevm',
'0x5555555555555555555555555555555555555555', // HYPE
'0xb8ce59fc3717ada4c02eadf9682a9e934f625ebb', // USDT
ethers.utils.parseUnits('10', 18)
)
console.log('✓ Swapped on HyperEVM')
// Swap on BSC
await manager.swap(
'bsc',
'0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee', // BNB
'0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56', // BUSD
ethers.utils.parseUnits('1', 18)
)
console.log('✓ Swapped on BSC')
}
useMultiChainManager()