Skip to content

Commit

Permalink
full test
Browse files Browse the repository at this point in the history
  • Loading branch information
fewensa committed Aug 5, 2024
1 parent dfeab05 commit 4852928
Show file tree
Hide file tree
Showing 5 changed files with 310 additions and 143 deletions.
2 changes: 1 addition & 1 deletion template/ts/files/src/helixconf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ export class HelixChainConf {
}
if (filter.symbol) {
const eq = _equalsIgnoreCase(item.symbol.from, filter.symbol)
|| _equalsIgnoreCase(item.symbol.to, filter.symbol);
&& _equalsIgnoreCase(item.symbol.to, filter.symbol);
if (!eq) return false;
}
return true;
Expand Down
120 changes: 89 additions & 31 deletions template/ts/files/tests/common/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,17 @@ import {
LnBridgeContract,
LnDefaultBridgeContract,
LnOppositeBridgeContract,
Lnv3BridgeContract
Lnv3BridgeContract, MessagerService, TokenRegisteredInfo
} from "./contracts";
import {ChainToken, HelixChainConf, HelixProtocol} from "../../src";
import {Messager} from "./messager";


export class BridgeProtocol {

public _daoOnChain?: string;
public _operatorOnChain?: string;

private readonly bridgeContract: LnBridgeContract;

constructor(
Expand All @@ -34,71 +37,126 @@ export class BridgeProtocol {
return this.bridgeContract.address;
}

messager(remoteChainId: bigint): Promise<MessagerService> {
return this.bridgeContract.messager(remoteChainId);
}

async dao(): Promise<string> {
return await this.bridgeContract.dao();
this._daoOnChain ||= await this.bridgeContract.dao();
return this._daoOnChain;
}

async operator(): Promise<string> {
return await this.bridgeContract.operator();
this._operatorOnChain ||= await this.bridgeContract.operator();
return this._operatorOnChain;
}

async isTokenRegistered(
async tokenRegistered(
tokenFrom: ChainToken,
tokenTo: ChainToken,
targetChain: HelixChainConf
): Promise<boolean> {
): Promise<TokenRegisteredInfo | undefined> {
return await this.bridgeContract.tokenRegistered(
targetChain.id,
tokenFrom.address,
tokenTo.address,
tokenFrom.decimals,
tokenTo.decimals,
);
}

}

export interface BridgeOptions {
sourceChain: HelixChainConf
targetChain: HelixChainConf
sourceBridgeProtocol: BridgeProtocol
targetBridgeProtocol: BridgeProtocol
sourceMessager: Messager
targetMessager: Messager
sourceProtocol: HelixProtocol
targetProtocol: HelixProtocol
}

export class Bridge {
constructor(
private readonly sourceChain: HelixChainConf,
private readonly targetChain: HelixChainConf,
private readonly sourceBridgeEndpoint: BridgeProtocol,
private readonly targetBridgeEndpoint: BridgeProtocol,
private readonly sourceMessager: Messager,
private readonly targetMessager: Messager,
private readonly protocol: HelixProtocol,
private readonly options: BridgeOptions,
) {
}

get sourceChain(): HelixChainConf {
return this.options.sourceChain
}

get targetChain(): HelixChainConf {
return this.options.targetChain
}

get sourceBridgeProtocol(): BridgeProtocol {
return this.options.sourceBridgeProtocol
}

get targetBridgeProtocol(): BridgeProtocol {
return this.options.targetBridgeProtocol
}

get sourceMessager(): Messager {
return this.options.sourceMessager
}

get targetMessager(): Messager {
return this.options.targetMessager
}

get sourceProtocol(): HelixProtocol {
return this.options.sourceProtocol
}

get targetProtocol(): HelixProtocol {
return this.options.targetProtocol
}

async getSourceMessagerFromChain(): Promise<string> {
const messager = await this.sourceBridgeProtocol.messager(this.targetChain.id);
return this.sourceProtocol.name === 'lnv2-default'
? messager.sendService
: messager.receiveService;
}

// async getTargetMessagerFromChain(): Promise<string> {
// const messager = await this.targetBridgeProtocol.messager(this.sourceChain.id);
// return this.targetProtocol.name === 'lnv2-default'
// ? messager.receiveService
// : messager.sendService;
// }

async isSourceConnectedTarget(): Promise<boolean> {
return await this.sourceMessager.isConnected(this.targetChain, this.targetMessager.address());
}

async isTargetConnectedSource(): Promise<boolean> {
return await this.targetMessager.isConnected(this.sourceChain, this.sourceMessager.address());
}
// async isTargetConnectedSource(): Promise<boolean> {
// return await this.targetMessager.isConnected(this.sourceChain, this.sourceMessager.address());
// }

async isSourceAppConnectedTarget(): Promise<boolean> {
return this.sourceMessager[
this.protocol.name === 'lnv2-default'
this.sourceProtocol.name === 'lnv2-default'
? 'remoteAppIsReceiver'
: 'remoteAppIsSender'
](
this.targetChain.id,
this.sourceBridgeEndpoint.address,
this.targetBridgeEndpoint.address,
this.sourceBridgeProtocol.address,
this.targetBridgeProtocol.address,
);
}

async isTargetAppConnectedSource(): Promise<boolean> {
return this.targetMessager[
this.protocol.name === 'lnv2-default'
? 'remoteAppIsSender'
: 'remoteAppIsReceiver'
](
this.sourceChain.id,
this.targetBridgeEndpoint.address,
this.sourceBridgeEndpoint.address,
)
}
// async isTargetAppConnectedSource(): Promise<boolean> {
// return this.targetMessager[
// this.targetProtocol.name === 'lnv2-default'
// ? 'remoteAppIsSender'
// : 'remoteAppIsReceiver'
// ](
// this.sourceChain.id,
// this.targetBridgeProtocol.address,
// this.sourceBridgeProtocol.address,
// )
// }
}
64 changes: 53 additions & 11 deletions template/ts/files/tests/common/contracts.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
ethers,
Contract,
Interface , InterfaceAbi,
Interface, InterfaceAbi,
ContractRunner,
} from "ethers";

Expand All @@ -22,6 +22,7 @@ export type CSigner = ContractRunner | ethers.JsonRpcProvider;
export class EthereumContract {
protected contract: Contract;
public address: string;

constructor(
address: string,
abi: CAbi,
Expand Down Expand Up @@ -70,7 +71,6 @@ export class Erc20 extends EthereumContract {
}



export class LnAccessController extends EthereumContract {
constructor(address: string, abi: CAbi, signer: CSigner) {
super(address, abi, signer);
Expand All @@ -91,6 +91,7 @@ export class LnAccessController extends EthereumContract {

export class MessagerContract extends LnAccessController {
private messagerType: string;

constructor(address: string, abi: CAbi, signer: CSigner, messagerType: string) {
super(address, abi, signer);
this.messagerType = messagerType;
Expand All @@ -101,9 +102,11 @@ export class Eth2ArbSendServiceContract extends MessagerContract {
constructor(address: string, signer: CSigner) {
super(address, abiEth2ArbSendService, signer, "Eth2ArbSendService");
}

async remoteMessager(): Promise<string> {
return (await this.contract['remoteMessager']()).toLowerCase();
}

async appPair(localApp: string): Promise<string> {
return (await this.contract['appPairs'](localApp)).toLowerCase();
}
Expand All @@ -113,6 +116,7 @@ export class Eth2ArbReceiveServiceContract extends MessagerContract {
constructor(address: string, signer: CSigner) {
super(address, abiEth2ArbReceiveService, signer, "Eth2ArbReceiveService");
}

async remoteMessagerAlias(): Promise<string> {
return (await this.contract['remoteMessagerAlias']()).toLowerCase();
}
Expand Down Expand Up @@ -213,40 +217,57 @@ export abstract class LnBridgeContract extends LnAccessController {
}

abstract messager(remoteChainId: bigint): Promise<MessagerService>;
abstract tokenRegistered(remoteChainId: bigint, srcToken: string, dstToken: string, srcDecimals: number, dstDecimals: number): Promise<boolean>;

abstract tokenRegistered(
remoteChainId: bigint,
srcToken: string,
dstToken: string,
): Promise<TokenRegisteredInfo | undefined>;
}

export class LnDefaultBridgeContract extends LnBridgeContract {
constructor(address: string, signer: CSigner) {
super(address, abiLnDefaultBridge, signer);
}

async messager(remoteChainId: bigint): Promise<MessagerService> {
return await this.contract['messagers'](remoteChainId);
}
async tokenRegistered(remoteChainId: bigint, srcToken: string, dstToken: string, srcDecimals: number, dstDecimals: number): Promise<boolean> {

async tokenRegistered(
remoteChainId: bigint,
srcToken: string,
dstToken: string,
): Promise<TokenRegisteredInfo | undefined> {
// TODO
return true;
return undefined;
}
}

export class LnOppositeBridgeContract extends LnBridgeContract {
constructor(address: string, signer: CSigner) {
super(address, abiLnOppositeBridge, signer);
}

async messager(remoteChainId: bigint): Promise<MessagerService> {
return await this.contract['messagers'](remoteChainId);
}
async tokenRegistered(remoteChainId: bigint, srcToken: string, dstToken: string, srcDecimals: number, dstDecimals: number): Promise<boolean> {

async tokenRegistered(
remoteChainId: bigint,
srcToken: string,
dstToken: string,
): Promise<TokenRegisteredInfo | undefined> {
// TODO
return true;
return undefined;
}
}

export interface Lnv3TokenConfigure {
protocolFee: number;
penalty: number;
sourceDecimals: number;
targetDecimals: number;
sourceDecimals: bigint;
targetDecimals: bigint;
}

export interface Lnv3TokenInfo {
Expand All @@ -261,30 +282,51 @@ export class Lnv3BridgeContract extends LnBridgeContract {
constructor(address: string, signer: CSigner) {
super(address, abiLnv3Bridge, signer);
}

private tokenKey(remoteChainId: bigint, sourceToken: string, targetToken: string): string {
return ethers.solidityPackedKeccak256([
"uint256",
"address",
"address",
], [remoteChainId, sourceToken, targetToken]);
}

async messager(remoteChainId: bigint): Promise<MessagerService> {
return await this.contract['messagers'](remoteChainId);
}
async tokenRegistered(remoteChainId: bigint, srcToken: string, dstToken: string, srcDecimals: number, dstDecimals: number): Promise<boolean> {

async tokenRegistered(
remoteChainId: bigint,
srcToken: string,
dstToken: string,
): Promise<TokenRegisteredInfo | undefined> {
const tokenInfo = await this.tokenInfo(remoteChainId, srcToken, dstToken);
const tokenKey = this.tokenKey(remoteChainId, srcToken, dstToken);
return tokenInfo.config.sourceDecimals === srcDecimals && tokenInfo.config.targetDecimals === dstDecimals && tokenKey === await this.indexToTokenKey(tokenInfo.index);
const indexToTokenKey = await this.indexToTokenKey(tokenInfo.index);
return {
sourceDecimals: tokenInfo.config.sourceDecimals,
targetDecimals: tokenInfo.config.targetDecimals,
buildTokenKey: tokenKey.toLowerCase(),
indexToTokenKey: indexToTokenKey.toLowerCase(),
};
}

async tokenInfo(remoteChainId: bigint, sourceToken: string, targetToken: string): Promise<Lnv3TokenInfo> {
const encode = this.tokenKey(remoteChainId, sourceToken, targetToken);
return await this.contract['tokenInfos'](encode);
}

async indexToTokenKey(index: number): Promise<string> {
return await this.contract['tokenIndexer'](index);
}
}

export interface TokenRegisteredInfo {
sourceDecimals: bigint
targetDecimals: bigint
buildTokenKey: string
indexToTokenKey: string
}



Loading

0 comments on commit 4852928

Please sign in to comment.