|
@@ -10,6 +10,10 @@ export class MerklyClient {
|
|
|
static logger = newLogger('MerklyClient')
|
|
|
|
|
|
constructor(privateKey: string, chainId: number) {
|
|
|
+ const from = chainInfoMap[chainId].merklyInfo
|
|
|
+ if (!from) {
|
|
|
+ throw new Error('Unsupported chain')
|
|
|
+ }
|
|
|
this.chainId = chainId
|
|
|
this.provider = new ethers.JsonRpcProvider(chainInfoMap[chainId].rpcUrl)
|
|
|
this.wallet = new ethers.Wallet(privateKey, this.provider)
|
|
@@ -31,15 +35,15 @@ export class MerklyClient {
|
|
|
}
|
|
|
const balance = await this.provider.getBalance(this.wallet.address)
|
|
|
const quote = await this.router.quoteBridge(toInfo.merklyChainId, balance)
|
|
|
- console.log('quote', ethers.formatEther(quote))
|
|
|
- console.log('balance', ethers.formatEther(balance))
|
|
|
if (balance > quote) {
|
|
|
const feeData = await this.provider.getFeeData()
|
|
|
const estimatedGas = await this.router.bridgeETH.estimateGas(toInfo.merklyChainId, balance - quote, {
|
|
|
value: balance,
|
|
|
})
|
|
|
const gasCost = (feeData.gasPrice * estimatedGas * 108n) / 100n
|
|
|
- console.log('gasCost', ethers.formatEther(gasCost))
|
|
|
+ if (balance - quote - gasCost < 0) {
|
|
|
+ throw new Error('Insufficient balance')
|
|
|
+ }
|
|
|
const tx = await this.router.bridgeETH(toInfo.merklyChainId, balance - quote - gasCost, {
|
|
|
value: balance - gasCost,
|
|
|
gasLimit: (estimatedGas * 105n) / 100n,
|