From 1ab186f8352f9666703b14bcba804ba70293e1a8 Mon Sep 17 00:00:00 2001 From: JayJay1024 Date: Fri, 10 Nov 2023 14:59:44 +0800 Subject: [PATCH] LnBridge-default withdraw margin api --- packages/apps/src/abi/lnaccess-controller.ts | 182 ++++++++++++++++++ packages/apps/src/bridges/lnbridge-default.ts | 42 +++- 2 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 packages/apps/src/abi/lnaccess-controller.ts diff --git a/packages/apps/src/abi/lnaccess-controller.ts b/packages/apps/src/abi/lnaccess-controller.ts new file mode 100644 index 000000000..542430417 --- /dev/null +++ b/packages/apps/src/abi/lnaccess-controller.ts @@ -0,0 +1,182 @@ +const abi = [ + { + inputs: [ + { internalType: "address", name: "_dao", type: "address" }, + { internalType: "address", name: "_endpoint", type: "address" }, + ], + stateMutability: "nonpayable", + type: "constructor", + }, + { + anonymous: false, + inputs: [ + { indexed: false, internalType: "uint16", name: "lzRemoteChainId", type: "uint16" }, + { indexed: false, internalType: "bytes", name: "srcAddress", type: "bytes" }, + { indexed: false, internalType: "bool", name: "successed", type: "bool" }, + ], + name: "CallResult", + type: "event", + }, + { + anonymous: false, + inputs: [ + { indexed: false, internalType: "uint16", name: "lzRemoteChainId", type: "uint16" }, + { indexed: false, internalType: "bytes", name: "srcAddress", type: "bytes" }, + { indexed: false, internalType: "address", name: "remoteAppAddress", type: "address" }, + ], + name: "CallerUnMatched", + type: "event", + }, + { + inputs: [ + { internalType: "address", name: "appAddress", type: "address" }, + { internalType: "bool", name: "enable", type: "bool" }, + ], + name: "authoriseAppCaller", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [{ internalType: "address", name: "", type: "address" }], + name: "callerWhiteList", + outputs: [{ internalType: "bool", name: "", type: "bool" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [], + name: "dao", + outputs: [{ internalType: "address", name: "", type: "address" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [], + name: "endpoint", + outputs: [{ internalType: "contract ILayerZeroEndpoint", name: "", type: "address" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "_remoteChainId", type: "uint256" }, + { internalType: "bytes", name: "_message", type: "bytes" }, + ], + name: "fee", + outputs: [ + { internalType: "uint256", name: "nativeFee", type: "uint256" }, + { internalType: "uint256", name: "zroFee", type: "uint256" }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { internalType: "uint16", name: "_srcChainId", type: "uint16" }, + { internalType: "bytes", name: "_srcAddress", type: "bytes" }, + { internalType: "uint64", name: "", type: "uint64" }, + { internalType: "bytes", name: "_payload", type: "bytes" }, + ], + name: "lzReceive", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [], + name: "operator", + outputs: [{ internalType: "address", name: "", type: "address" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "_remoteChainId", type: "uint256" }, + { internalType: "address", name: "_remoteBridge", type: "address" }, + ], + name: "registerRemoteReceiver", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "_remoteChainId", type: "uint256" }, + { internalType: "address", name: "_remoteBridge", type: "address" }, + ], + name: "registerRemoteSender", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [{ internalType: "bytes32", name: "", type: "bytes32" }], + name: "remoteAppReceivers", + outputs: [{ internalType: "address", name: "", type: "address" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [{ internalType: "bytes32", name: "", type: "bytes32" }], + name: "remoteAppSenders", + outputs: [{ internalType: "address", name: "", type: "address" }], + stateMutability: "view", + type: "function", + }, + { + inputs: [{ internalType: "uint256", name: "", type: "uint256" }], + name: "remoteMessagers", + outputs: [ + { internalType: "uint16", name: "lzRemoteChainId", type: "uint16" }, + { internalType: "address", name: "messager", type: "address" }, + ], + stateMutability: "view", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "_remoteChainId", type: "uint256" }, + { internalType: "bytes", name: "_message", type: "bytes" }, + { internalType: "bytes", name: "_params", type: "bytes" }, + ], + name: "sendMessage", + outputs: [], + stateMutability: "payable", + type: "function", + }, + { + inputs: [{ internalType: "address", name: "_operator", type: "address" }], + name: "setOperator", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [ + { internalType: "uint256", name: "_appRemoteChainId", type: "uint256" }, + { internalType: "uint16", name: "_lzRemoteChainId", type: "uint16" }, + { internalType: "address", name: "_remoteMessager", type: "address" }, + ], + name: "setRemoteMessager", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [{ internalType: "address", name: "_dao", type: "address" }], + name: "transferOwnership", + outputs: [], + stateMutability: "nonpayable", + type: "function", + }, + { + inputs: [{ internalType: "uint16", name: "", type: "uint16" }], + name: "trustedRemotes", + outputs: [{ internalType: "bytes32", name: "", type: "bytes32" }], + stateMutability: "view", + type: "function", + }, +] as const; + +export default abi; diff --git a/packages/apps/src/bridges/lnbridge-default.ts b/packages/apps/src/bridges/lnbridge-default.ts index 12bcca403..3ab399e08 100644 --- a/packages/apps/src/bridges/lnbridge-default.ts +++ b/packages/apps/src/bridges/lnbridge-default.ts @@ -1,4 +1,4 @@ -import { Address, TransactionReceipt } from "viem"; +import { Address, TransactionReceipt, bytesToHex } from "viem"; import { LnBridgeBase } from "./lnbridge-base"; import { ChainConfig, ChainID } from "@/types/chain"; import { Token } from "@/types/token"; @@ -165,4 +165,44 @@ export class LnBridgeDefault extends LnBridgeBase { return this.publicClient.waitForTransactionReceipt({ hash }); } } + + async withdrawMargin(recipient: Address, amount: bigint) { + await this.validateNetwork("source"); + + if ( + this.contract && + this.sourceToken && + this.targetToken && + this.targetChain && + this.publicClient && + this.walletClient + ) { + const bridgeAbi = (await import(`../abi/lnbridgev20-default`)).default; + const accessAbi = (await import(`../abi/lnaccess-controller`)).default; + const remoteChainId = BigInt(this.targetChain.id); + + const [sendService, _receiveService] = await this.publicClient.readContract({ + address: this.contract.sourceAddress, + abi: bridgeAbi, + functionName: "messagers", + args: [remoteChainId], + }); + const [nativeFee, _zroFee] = await this.publicClient.readContract({ + address: sendService, + abi: accessAbi, + functionName: "fee", + args: [remoteChainId, bytesToHex(Uint8Array.from([123]), { size: 500 })], + }); + + const hash = await this.walletClient.writeContract({ + address: this.contract.sourceAddress, + abi: bridgeAbi, + functionName: "requestWithdrawMargin", + args: [remoteChainId, this.sourceToken.address, this.targetToken.address, amount, recipient], + gas: this.getTxGasLimit(), + value: nativeFee, + }); + return this.publicClient.waitForTransactionReceipt({ hash }); + } + } }