Skip to content

Commit

Permalink
Improve ln-bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
JayJay1024 committed Nov 14, 2024
1 parent 92de717 commit 7b9b235
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 67 deletions.
2 changes: 1 addition & 1 deletion packages/sdk-bridge/src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { LnBridge } from "./ln-bridge";
export { LnBridge } from "./lnbridge";
5 changes: 3 additions & 2 deletions packages/sdk-bridge/src/lnbridge-v2-default.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Address, Hash } from "viem";
import { Chain } from "viem";
import { ConstructorOptions, LnBridge, RelayInfo } from "./ln-bridge";
import { ConstructorOptions, RelayInfo } from "./lnbridge";
import { HelixProtocolName } from "@helixbridge/helixconf";
import assert from "assert";
import { DEFAULT_CONFIRMATIONS } from "./config";
import { LnBridgeV2 } from "./lnbridge-v2";

export class LnBridgeV2Default extends LnBridge {
export class LnBridgeV2Default extends LnBridgeV2 {
constructor(
fromChain: Chain,
toChain: Chain,
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk-bridge/src/lnbridge-v2-opposite.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Address, Hash } from "viem";
import { Chain } from "viem";
import { ConstructorOptions, LnBridge, RelayInfo } from "./ln-bridge";
import { ConstructorOptions, RelayInfo } from "./lnbridge";
import { HelixProtocolName } from "@helixbridge/helixconf";
import assert from "assert";
import { DEFAULT_CONFIRMATIONS } from "./config";
import { LnBridgeV2 } from "./lnbridge-v2";

export class LnBridgeV2Opposite extends LnBridge {
export class LnBridgeV2Opposite extends LnBridgeV2 {
constructor(
fromChain: Chain,
toChain: Chain,
Expand Down
66 changes: 66 additions & 0 deletions packages/sdk-bridge/src/lnbridge-v2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { Address, Hash, encodeFunctionData, TransactionReceipt } from "viem";
import { Chain } from "viem";
import { ConstructorOptions, LnBridge, RelayInfo } from "./lnbridge";
import { HelixProtocolName } from "@helixbridge/helixconf";

export class LnBridgeV2 extends LnBridge {
constructor(
fromChain: Chain,
toChain: Chain,
fromToken: Address,
toToken: Address,
protocol: HelixProtocolName,
options?: ConstructorOptions,
) {
super(fromChain, toChain, fromToken, toToken, protocol, options);
}

// eslint-disable-next-line no-unused-vars
transfer(amount: bigint, recipient: Address, totalFee: bigint, relayInfo: RelayInfo): Promise<TransactionReceipt> {
throw new Error("Method not implemented.");
}

async getLayerZeroWithdrawFee() {
const [sendService] = await this.sourcePublicClient.readContract({
address: this.sourceBridgeContract,
abi: (await import(`./abi/lnv2-default`)).default,
functionName: "messagers",
args: [BigInt(this.targetChain.id)],
});
const [value] = await this.getLayerZeroFee(sendService, this.targetChain, this.sourcePublicClient);
return { value, token: this.sourceNativeToken };
}

async getMsgportWithdrawFee(args: {
transferId: Hash;
withdrawNonce: string;
relayer: Address;
refundAddress: Address;
amount: bigint;
}) {
const message = encodeFunctionData({
abi: (await import(`./abi/lnv2-default`)).default,
functionName: "withdraw",
args: [
BigInt(this.sourceChain.id),
args.transferId,
BigInt(args.withdrawNonce),
args.relayer,
this.sourceToken.address,
this.targetToken.address,
args.amount,
],
});
const feeAndParams = await this.getMsgportFeeAndParams(
message,
args.refundAddress,
this.sourceChain,
this.targetChain,
this.sourceBridgeContract,
this.targetBridgeContract,
this.sourceToken,
this.targetToken,
);
return feeAndParams ? { value: feeAndParams.fee, token: this.sourceNativeToken } : undefined;
}
}
123 changes: 121 additions & 2 deletions packages/sdk-bridge/src/lnbridge-v3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Address, Chain } from "viem";
import { ConstructorOptions, LnBridge, RelayInfo } from "./ln-bridge";
import { Address, Chain, encodeFunctionData, encodePacked, Hash, keccak256 } from "viem";
import { ConstructorOptions, LnBridge, RelayInfo } from "./lnbridge";
import { HelixProtocolName } from "@helixbridge/helixconf";
import assert from "assert";
import { DEFAULT_CONFIRMATIONS } from "./config";
Expand Down Expand Up @@ -43,4 +43,123 @@ export class LnBridgeV3 extends LnBridge {
const hash = await this.walletClient.writeContract(request);
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: DEFAULT_CONFIRMATIONS });
}

async getPenaltyReserves(relayer: Address) {
const value = await this.sourcePublicClient.readContract({
address: this.sourceBridgeContract,
abi: (await import("./abi/lnbridge-v3")).default,
functionName: "penaltyReserves",
args: [keccak256(encodePacked(["address", "address"], [this.sourceToken.address, relayer]))],
});
return { value, token: this.sourceToken };
}

async register(baseFee: bigint, feeRate: number, transferLimit: bigint) {
assert(this.walletClient, "Wallet client not found");
const signer = (await this.walletClient.getAddresses())[0];
const { request } = await this.sourcePublicClient.simulateContract({
address: this.sourceBridgeContract,
abi: (await import("./abi/lnbridge-v3")).default,
functionName: "registerLnProvider",
args: [
BigInt(this.targetChain.id),
this.sourceToken.address,
this.targetToken.address,
baseFee,
feeRate,
transferLimit,
],
gas: this.getGasLimit(),
account: signer,
});
const hash = await this.walletClient.writeContract(request);
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: DEFAULT_CONFIRMATIONS });
}

async depositPenaltyReserve(amount: bigint) {
assert(this.walletClient, "Wallet client not found");
const signer = (await this.walletClient.getAddresses())[0];
const { request } = await this.sourcePublicClient.simulateContract({
address: this.sourceBridgeContract,
abi: (await import("./abi/lnbridge-v3")).default,
functionName: "depositPenaltyReserve",
args: [this.sourceToken.address, amount],
value: this.sourceToken.isNative ? amount : undefined,
gas: this.getGasLimit(),
account: signer,
});
const hash = await this.walletClient.writeContract(request);
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: DEFAULT_CONFIRMATIONS });
}

async withdrawPenaltyReserve(amount: bigint) {
assert(this.walletClient, "Wallet client not found");
const signer = (await this.walletClient.getAddresses())[0];
const { request } = await this.sourcePublicClient.simulateContract({
address: this.sourceBridgeContract,
abi: (await import("./abi/lnbridge-v3")).default,
functionName: "withdrawPenaltyReserve",
args: [this.sourceToken.address, amount],
gas: this.getGasLimit(),
account: signer,
});
const hash = await this.walletClient.writeContract(request);
return this.sourcePublicClient.waitForTransactionReceipt({ hash, confirmations: DEFAULT_CONFIRMATIONS });
}

async getLiquidityWithdrawFeeAndParams(
relayer: Address,
transferIds: Hash[],
messageChannel: "layerzero" | "msgline" | "",
) {
const token = this.targetNativeToken;
let value: bigint | undefined;
let params: Hash | undefined;

if (messageChannel === "layerzero") {
const [sendService] = await this.targetPublicClient.readContract({
address: this.targetBridgeContract,
abi: (await import("./abi/lnbridge-v3")).default,
functionName: "messagers",
args: [BigInt(this.sourceChain.id)],
});
[value] = await this.getLayerZeroFee(sendService, this.sourceChain, this.targetPublicClient);
} else if (messageChannel === "msgline") {
const message = encodeFunctionData({
abi: (await import("./abi/lnbridge-v3")).default,
functionName: "withdrawLiquidity",
args: [transferIds, BigInt(this.targetChain.id), relayer],
});
const feeAndParams = await this.getMsgportFeeAndParams(
message,
relayer,
this.targetChain,
this.sourceChain,
this.targetBridgeContract,
this.sourceBridgeContract,
this.targetToken,
this.sourceToken,
);
value = feeAndParams?.fee;
params = feeAndParams?.params;
}

return value ? { value, token, params } : undefined;
}

async requestLiquidityWithdraw(relayer: Address, transferIds: Hash[], fee: bigint, params: Hash) {
assert(this.walletClient, "Wallet client not found");
const signer = (await this.walletClient.getAddresses())[0];
const { request } = await this.targetPublicClient.simulateContract({
address: this.targetBridgeContract,
abi: (await import("./abi/lnbridge-v3")).default,
functionName: "requestWithdrawLiquidity",
args: [BigInt(this.sourceChain.id), transferIds, relayer, params],
value: fee,
gas: this.getGasLimit(),
account: signer,
});
const hash = await this.walletClient.writeContract(request);
return this.targetPublicClient.waitForTransactionReceipt({ hash, confirmations: DEFAULT_CONFIRMATIONS });
}
}
Loading

0 comments on commit 7b9b235

Please sign in to comment.