Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Layerzero withdraw margin #563

Merged
merged 11 commits into from
Nov 14, 2023
18 changes: 16 additions & 2 deletions packages/apps/src/__tests__/cross-config.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getChainsConfig } from "../utils/chain";
import { getChainsConfig, getChainConfig } from "../utils/chain";
import type { ChainConfig } from "../types/chain";

describe.each(getChainsConfig() as ChainConfig[])(
describe.each(getChainsConfig("all") as ChainConfig[])(
"Should configure price for HelixLpBridge to cross native token",
({ network, tokens }) => {
if (tokens.length) {
Expand All @@ -17,3 +17,17 @@ describe.each(getChainsConfig() as ChainConfig[])(
}
},
);

describe.each(getChainsConfig("all") as ChainConfig[])("Should configure target token", ({ network, tokens }) => {
if (tokens.length) {
describe.each(tokens)(`Cross $symbol from ${network}`, (token) => {
if (token.cross.length) {
it.each(token.cross)(`to $target.network ($target.symbol)`, (cross) => {
const targetChain = getChainConfig(cross.target.network) as ChainConfig | undefined;
const targetToken = targetChain?.tokens.find((t) => t.symbol === cross.target.symbol);
expect(targetToken).not.toBeUndefined();
});
}
});
}
});
10 changes: 10 additions & 0 deletions packages/apps/src/__tests__/native-token-config.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { getChainsConfig } from "../utils/chain";
import type { ChainConfig } from "../types/chain";

describe.each(getChainsConfig("all") as ChainConfig[])("Should configure native token", ({ network, tokens }) => {
it(`${network}`, () => {
if (tokens.length) {
expect(tokens.some((t) => t.type === "native")).not.toBeFalsy();
}
});
});
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;
51 changes: 50 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,53 @@ export class LnBridgeDefault extends LnBridgeBase {
return this.publicClient.waitForTransactionReceipt({ hash });
}
}

async getWithdrawFee() {
if (this.contract && this.sourceNativeToken && this.targetChain && this.sourcePublicClient) {
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.sourcePublicClient.readContract({
address: this.contract.sourceAddress,
abi: bridgeAbi,
functionName: "messagers",
args: [remoteChainId],
});
const [nativeFee, _zroFee] = await this.sourcePublicClient.readContract({
address: sendService,
abi: accessAbi,
functionName: "fee",
args: [remoteChainId, bytesToHex(Uint8Array.from([123]), { size: 500 })],
});

return { fee: nativeFee, token: this.sourceNativeToken };
}
}

async withdrawMargin(recipient: Address, amount: bigint, fee: bigint) {
await this.validateNetwork("source");

if (
this.contract &&
this.sourceToken &&
this.targetToken &&
this.targetChain &&
this.publicClient &&
this.walletClient
) {
const abi = (await import(`../abi/lnbridgev20-default`)).default;
const remoteChainId = BigInt(this.targetChain.id);

const hash = await this.walletClient.writeContract({
address: this.contract.sourceAddress,
abi,
functionName: "requestWithdrawMargin",
args: [remoteChainId, this.sourceToken.address, this.targetToken.address, amount, recipient],
gas: this.getTxGasLimit(),
value: fee,
});
return this.publicClient.waitForTransactionReceipt({ hash });
}
}
}
Loading
Loading