Skip to content

Commit

Permalink
LnBridge-default withdraw margin api
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Nov 10, 2023
1 parent 82523d8 commit 1ab186f
Show file tree
Hide file tree
Showing 2 changed files with 223 additions and 1 deletion.
182 changes: 182 additions & 0 deletions packages/apps/src/abi/lnaccess-controller.ts
Original file line number Diff line number Diff line change
@@ -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;
42 changes: 41 additions & 1 deletion packages/apps/src/bridges/lnbridge-default.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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 });
}
}
}

0 comments on commit 1ab186f

Please sign in to comment.