Skip to content

Commit

Permalink
Factory: Helpers: Migrate from chain implementation to sepolia emmet …
Browse files Browse the repository at this point in the history
…data
  • Loading branch information
rsbkmr committed Jun 25, 2024
1 parent f010cd8 commit 59d99dd
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 83 deletions.
8 changes: 0 additions & 8 deletions src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -201,11 +201,3 @@ export interface GetEstimatedTime {
export interface GetBridgeAddress {
bridge: () => Promise<string>;
}

export interface GetTokenPrice {
getTokenPrice: (symbol: string) => Promise<bigint>;
}

export interface GetTokenPriceDecimals {
getPriceDecimals: (symbol: string) => Promise<bigint>;
}
12 changes: 1 addition & 11 deletions src/chains/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ import type {
GetProtocolFeeInUSD,
GetProvider,
GetTokenBalance,
GetTokenPrice,
GetTokenPriceDecimals,
GetTxFee,
NativeCoinName,
ProtocolFee,
Expand Down Expand Up @@ -51,9 +49,7 @@ export type TonHelper = GetBalance &
TokenInfo &
GetEstimatedTime &
GetBridgeAddress &
GetProtocolFeeInUSD &
GetTokenPrice &
GetTokenPriceDecimals;
GetProtocolFeeInUSD;

export interface TonParams {
client: TonClient;
Expand Down Expand Up @@ -261,12 +257,6 @@ export function tonHandler({

return {
estimateTime: () => Promise.resolve(undefined),
async getPriceDecimals(_) {
return 0n;
},
async getTokenPrice(_) {
return 0n;
},
async emmetHashFromtx(hash) {
const txs = await client.getTransactions(bridge, {
hash,
Expand Down
14 changes: 1 addition & 13 deletions src/chains/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ import type {
GetProtocolFeeInUSD,
GetProvider,
GetTokenBalance,
GetTokenPrice,
GetTokenPriceDecimals,
GetTxFee,
NativeCoinName,
PreTransfer,
Expand Down Expand Up @@ -53,9 +51,7 @@ export type Web3Helper = GetBalance &
GetEmmetHashFromTx &
GetEstimatedTime &
GetBridgeAddress &
GetProtocolFeeInUSD &
GetTokenPrice &
GetTokenPriceDecimals;
GetProtocolFeeInUSD;

export interface Web3Params {
provider: Provider;
Expand Down Expand Up @@ -211,13 +207,5 @@ export async function web3Helper({
tx: tx,
};
},
async getTokenPrice(symbol) {
const tokenPrice = data.getTokenPrice(symbol);
return tokenPrice;
},
async getPriceDecimals(symbol) {
const tokenDecimals = data.getPriceDecimals(symbol);
return tokenDecimals;
},
};
}
48 changes: 23 additions & 25 deletions src/factory/config.ts
Original file line number Diff line number Diff line change
@@ -1,63 +1,61 @@
import { ethers, JsonRpcProvider } from "ethers";
import { TestNetRpcUri } from "./rpcs";
import { Address } from "@ton/core";
import { TonClient } from "@ton/ton";
import { sha256_sync } from "@ton/crypto";
import type { ChainParams } from "./types";
import { ethers, JsonRpcProvider } from 'ethers';
import { TestNetRpcUri } from './rpcs';
import { Address } from '@ton/core';
import { TonClient } from '@ton/ton';
import { sha256_sync } from '@ton/crypto';
import type { ChainParams } from './types';

export namespace ChainFactoryConfigs {
export function TestNet() {
return {
tonParams: {
bridge: Address.parse(
"EQBVLgSAoR9rGm8WHx9SUbDlvxQEtQZnO-bmyF0y_cvt0Gir",
'EQBVLgSAoR9rGm8WHx9SUbDlvxQEtQZnO-bmyF0y_cvt0Gir'
),
client: new TonClient({
endpoint:
"https://testnet.toncenter.com/api/v2/jsonRPC?api_key=9e899d38874458e92addb70d6f336ccbe51e21e378af5797486ba9a9d1a3c5c3",
'https://testnet.toncenter.com/api/v2/jsonRPC?api_key=9e899d38874458e92addb70d6f336ccbe51e21e378af5797486ba9a9d1a3c5c3',
apiKey:
"9e899d38874458e92addb70d6f336ccbe51e21e378af5797486ba9a9d1a3c5c3",
'9e899d38874458e92addb70d6f336ccbe51e21e378af5797486ba9a9d1a3c5c3',
}),
nativeTokenId: BigInt(`0x${sha256_sync("TON").toString("hex")}`),
nativeTokenId: BigInt(`0x${sha256_sync('TON').toString('hex')}`),
oracle: Address.parse(
"EQAx41_27fvdX4C30RxQmzbiLeHyO090XzGHBadgGfdp5Uqd",
'EQAx41_27fvdX4C30RxQmzbiLeHyO090XzGHBadgGfdp5Uqd'
),
chainId: 65535n, // TON Testnet
burner: Address.parse(
"EQBtE7sxSqbDZwuWhxxQRzSZZ3UAm8j4mhR25iWS2xfEmZ6D",
'EQBtE7sxSqbDZwuWhxxQRzSZZ3UAm8j4mhR25iWS2xfEmZ6D'
),
chainName: "tonTestnet",
chainName: 'tonTestnet',
},
bscParams: {
chainName: "bscTestnet",
chainName: 'bscTestnet',
addressBook: ethers.getAddress(
"0x3564336Ad556295A368EEa2b2CA1a7D3f43B4029",
'0x3564336Ad556295A368EEa2b2CA1a7D3f43B4029'
),
nativeCoin: "BNB",
nativeCoin: 'BNB',
provider: new JsonRpcProvider(TestNetRpcUri.BSC),
},
polygonParams: {
addressBook: ethers.getAddress(
"0x8d948925A0CB920c965C3296Eb4aef31EfE32ce9",
'0x8d948925A0CB920c965C3296Eb4aef31EfE32ce9'
),
provider: new JsonRpcProvider(TestNetRpcUri.POLYGON),
// oracle: ethers.getAddress("0x95DB799744A5b36D6E7BE9AD3b451dBC5b8De673"),
chainName: "polygon",
nativeCoin: "MATIC",
chainName: 'polygon',
nativeCoin: 'MATIC',
},
ethParams: {
addressBook: ethers.getAddress(
"0x8b87FE2b3f3D9816432b34D5A6a30B1330594082",
'0x8b87FE2b3f3D9816432b34D5A6a30B1330594082'
),
chainName: "sepolia",
nativeCoin: "ETH",
chainName: 'sepolia',
nativeCoin: 'ETH',
provider: new JsonRpcProvider(TestNetRpcUri.ETH),
},
multisigParams: {
provider: new JsonRpcProvider(TestNetRpcUri.ETH),
address: ethers.getAddress(
"0x57d49F8cA354002692284d38edD4c5772eC72e81",
),
ab: ethers.getAddress('0x8b87FE2b3f3D9816432b34D5A6a30B1330594082'),
},
} satisfies Partial<ChainParams>;
}
Expand Down
56 changes: 38 additions & 18 deletions src/factory/factory.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
import { EmmetMultisig__factory } from "@emmet-contracts/web3";
import { tonHandler } from "../chains/ton";
import { web3Helper } from "../chains/web3";
import { Chain, type ChainFactory } from "./types";
import {
EmmetAddressBook__factory,
EmmetData__factory,
EmmetMultisig__factory,
} from '@emmet-contracts/web3';
import { tonHandler } from '../chains/ton';
import { web3Helper } from '../chains/web3';
import { Chain, type ChainFactory } from './types';

import type {
ChainInfo,
ChainNonce,
ChainParams,
HelperMap,
ParamMap,
} from "./types";
import { ChainIDToDomain, type SupportedChainID } from "../explorer-utils";
} from './types';
import { ChainIDToDomain, type SupportedChainID } from '../explorer-utils';

function mapNonceToParams(chainParams: Partial<ChainParams>): ParamMap {
const cToP: ParamMap = new Map();
Expand All @@ -26,40 +30,50 @@ export const CHAIN_INFO: ChainInfo = new Map();
CHAIN_INFO.set(Chain.POLYGON, {
constructor: web3Helper,
decimals: 18,
name: "Polygon",
name: 'Polygon',
nonce: Chain.POLYGON,
});

CHAIN_INFO.set(Chain.BSC, {
constructor: web3Helper,
decimals: 18,
name: "BSC",
name: 'BSC',
nonce: Chain.BSC,
});

CHAIN_INFO.set(Chain.ETHEREUM, {
constructor: web3Helper,
decimals: 18,
name: "Ethereum",
name: 'Ethereum',
nonce: Chain.ETHEREUM,
});

CHAIN_INFO.set(Chain.TON, {
decimals: 18,
name: "Ton",
name: 'Ton',
nonce: Chain.TON,
constructor: async (...args) => tonHandler(...args),
});

export function ChainFactoryBuilder(
chainParams: Partial<ChainParams>,
): ChainFactory {
export async function ChainFactoryBuilder(
chainParams: Partial<ChainParams>
): Promise<ChainFactory> {
const helpers: HelperMap<ChainNonce> = new Map();

const cToP = mapNonceToParams(chainParams);
const ab = EmmetAddressBook__factory.connect(
chainParams.multisigParams!.ab,
chainParams.multisigParams?.provider
);
const msig = await ab.get('EmmetMultisig');
const mData = await ab.get('EmmetData');
const multisig = EmmetMultisig__factory.connect(
chainParams.multisigParams!.address,
chainParams.multisigParams?.provider,
msig,
chainParams.multisigParams?.provider
);
const emmetData = EmmetData__factory.connect(
mData,
chainParams.multisigParams?.provider
);

const inner = async <T extends ChainNonce>(chain: T) => {
Expand Down Expand Up @@ -150,7 +164,7 @@ export function ChainFactoryBuilder(
fromSymbol,
tokenSymbol,
destAddress,
gasArgs,
gasArgs
) => {
const dc = await inner(chainId as ChainNonce);
const targetChainId = await dc.id();
Expand All @@ -160,7 +174,7 @@ export function ChainFactoryBuilder(
const isValid = await dc.validateAddress(destAddress);
if (!isValid) {
throw new Error(
`Invalid destination user address for chain id: ${chainId}`,
`Invalid destination user address for chain id: ${chainId}`
);
}
const fee = await chain.txFee(targetChainId, fromSymbol, tokenSymbol);
Expand All @@ -172,8 +186,14 @@ export function ChainFactoryBuilder(
tokenSymbol,
destAddress,
fee,
gasArgs,
gasArgs
);
},
getTokenPrice(symbol) {
return emmetData.getTokenPrice(symbol);
},
getPriceDecimals(symbol) {
return emmetData.getPriceDecimals(symbol);
},
};
}
18 changes: 10 additions & 8 deletions src/factory/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { Web3Helper, Web3Params } from "../../chains/web3";
import type { TonHelper, TonParams } from "../../chains/ton";
import type { GetTxFee, PreTransfer, SendInstallment } from "../../chains";
import type { JsonRpcProvider } from "ethers";
import type { Web3Helper, Web3Params } from '../../chains/web3';
import type { TonHelper, TonParams } from '../../chains/ton';
import type { GetTxFee, PreTransfer, SendInstallment } from '../../chains';
import type { JsonRpcProvider } from 'ethers';

export type EvmMeta = [Web3Helper, Web3Params];
export type TonMeta = [TonHelper, TonParams];
Expand Down Expand Up @@ -50,7 +50,7 @@ export interface ChainParams {
tonParams: TonParams;
multisigParams: {
provider: JsonRpcProvider;
address: string;
ab: string;
};
}

Expand Down Expand Up @@ -81,23 +81,25 @@ export interface ChainFactory {
fromSymbol: string,
tokenSymbol: string,
destAddress: string,
gasArgs?: GasArgs,
gasArgs?: GasArgs
) => Promise<{ hash: string; tx: RetTx }>;
preTransfer: <Signer, GasArgs>(
chain: PreTransfer<Signer, GasArgs>,
signer: Signer,
tid: string,
spender: string,
amount: bigint,
gasArgs: GasArgs,
gasArgs: GasArgs
) => Promise<string>;
getTransactions: (
batch: bigint | number,
offset: bigint | number,
offset: bigint | number
) => Promise<Transaction[]>;
getTransaction: (hash: string) => Promise<DetailedTx>;
getExplorerStats: () => Promise<ExplorerMeta>;
getTxCount: () => Promise<bigint>;
getTokenPrice: (symbol: string) => Promise<bigint>;
getPriceDecimals: (symbol: string) => Promise<bigint>;
}

export interface Transaction {
Expand Down

0 comments on commit 59d99dd

Please sign in to comment.