Skip to content

Commit

Permalink
merged
Browse files Browse the repository at this point in the history
  • Loading branch information
D4mph1r committed Dec 16, 2024
2 parents fc3894f + e568608 commit 2a335cd
Show file tree
Hide file tree
Showing 12 changed files with 197 additions and 1,681 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"@ton/ton": "^13.9.0",
"@vechain/connex-driver": "^2.0.8",
"@vechain/connex-framework": "^2.0.8",
"@vechain/web3-providers-connex": "^1.1.2",
"@xp/cosmos-client": "git+https://github.com/XP-NETWORK/cosmos-client#bleeding-edge",
"async-mutex": "^0.5.0",
"axios": "^1.6.7",
Expand All @@ -66,7 +67,7 @@
"chalk": "4",
"dotenv": "^16.4.5",
"elysia": "^1.0.25",
"ethers": "^6.11.1",
"ethers": "^6.10.0",
"express": "^4.19.2",
"fp-ts": "^2.16.6",
"http-status-codes": "^2.3.0",
Expand All @@ -83,7 +84,6 @@
"tonweb": "^0.0.62",
"tslog": "^4.9.3",
"web3-eth-accounts": "^4.1.1",
"web3-providers-connex": "^0.3.1",
"xp-decentralized-sdk": "git+https://github.com/XP-NETWORK/xp-decentralized-client-library#bleeding-edge",
"zod": "^3.22.4"
}
Expand Down
28 changes: 14 additions & 14 deletions src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,25 +140,25 @@ export const bridgeTestChains = [
nativeCoinSymbol: "APT",
network: Network.TESTNET,
},
{
chainType: "casper",
chain: "CASPER",
network: "casper-test",
contractAddress:
"047ddacdb2d4c44b3d0d6c02341ff96e951409d30d4627d7b580db46743a6111",
decimals: 9,
intialFund: "1000000000",
lastBlock: 4123388,
nativeCoinSymbol: "CSPR",
rpcURL: "https://rpc.testnet.casperlabs.io",
},
// {
// chainType: "casper",
// chain: "CASPER",
// network: "casper-test",
// contractAddress:
// "047ddacdb2d4c44b3d0d6c02341ff96e951409d30d4627d7b580db46743a6111",
// decimals: 9,
// intialFund: "1000000000",
// lastBlock: 4123388,
// nativeCoinSymbol: "CSPR",
// rpcURL: "https://rpc.testnet.casperlabs.io",
// },
{
chain: "VECHAIN",
rpcURL: "https://sync-testnet.veblocks.net",
nativeCoinSymbol: "ETH",
nativeCoinSymbol: "VET",
intialFund: "50000000000000000",
contractAddress: "0x7111eb5f8d9dA472e9608f2ab3De275C040D60B2",
chainType: "evm",
chainType: "vechain",
lastBlock: 20343220,
decimals: 18,
blockChunks: 1000,
Expand Down
14 changes: 14 additions & 0 deletions src/deps/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
configTonHandler,
} from "../handler/chains";
import { configCasperHandler } from "../handler/chains/casper";
import { configVechainHandler } from "../handler/chains/evm/config-vechain";
import type { LogInstance, THandler } from "../handler/types";
import MikroOrmConfig from "../mikro-orm.config";
import type { IBridgeConfig, IGeneratedWallets } from "../types";
Expand Down Expand Up @@ -180,6 +181,18 @@ export async function configDeps(
)
: undefined;

const vechainConf = config.bridgeChains.find(
(e) => e.chainType === "vechain",
);

const vechain = vechainConf
? await configVechainHandler(
vechainConf,
secrets.evmWallet,
...otherArguments("VECHAIN"),
)
: undefined;

const casperConf = config.bridgeChains.find((e) => e.chainType === "casper");

const casper = casperConf
Expand Down Expand Up @@ -214,6 +227,7 @@ export async function configDeps(
// Configure Ethereum Virtual Machine (EVM) chains iteratively as they share the same configuration pattern
...evmChains,
...cosmwasmChains,
vechain,
hedera,
casper,
tzHelper,
Expand Down
9 changes: 7 additions & 2 deletions src/handler/chains/casper/utils/getBalance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,17 @@ export default async function getBalance(
signer: Keys.Ed25519,
) {
try {
console.log(
"casper",
signer.publicKey.toHex(),
signer.publicKey.toAccountHashStr(),
);
const balance = await useMutexAndRelease(
fetchProvider,
async (provider) => {
return await provider.nodeClient.queryBalance(
PurseIdentifier.MainPurseUnderAccountHash,
signer.publicKey.toAccountHashStr(),
PurseIdentifier.MainPurseUnderPublicKey,
signer.publicKey.toHex(),
);
},
);
Expand Down
9 changes: 3 additions & 6 deletions src/handler/chains/evm/config-hedera.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import type { EntityManager } from "@mikro-orm/sqlite";
import { Mutex } from "async-mutex";
import type { AxiosInstance } from "axios";
import { type BrowserProvider, JsonRpcProvider, Wallet } from "ethers";
import { JsonRpcProvider, Wallet } from "ethers";
import { privateKeyToAccount } from "web3-eth-accounts";
import type { TSupportedChainTypes, TSupportedChains } from "../../../config";
import type { BridgeStorage, ERC20Staking } from "../../../contractsTypes/evm";
import { Block } from "../../../persistence/entities/block";
import type { IEvmWallet, IHederaChainConfig } from "../../../types";
import type { LogInstance } from "../../types";
import { evmHandler } from "./handler";
import type { MutexReleaser } from "./types";

export async function configHederaHandler(
conf: IHederaChainConfig,
Expand All @@ -27,11 +26,9 @@ export async function configHederaHandler(
});
const mutex = new Mutex();
const provider = new JsonRpcProvider(conf.rpcURL);
const fetchProvider = async (): Promise<
[JsonRpcProvider | BrowserProvider, MutexReleaser]
> => {
const fetchProvider = async () => {
const release = await mutex.acquire();
return [provider, release];
return [provider, release] as const;
};
return evmHandler({
blockChunks: conf.blockChunks,
Expand Down
68 changes: 68 additions & 0 deletions src/handler/chains/evm/config-vechain.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import type { EntityManager } from "@mikro-orm/sqlite";
import { Driver, SimpleNet, SimpleWallet } from "@vechain/connex-driver";
import { Framework } from "@vechain/connex-framework";
import * as thor from "@vechain/web3-providers-connex";
import { Mutex } from "async-mutex";
import type { AxiosInstance } from "axios";
import { BrowserProvider, Wallet } from "ethers";
import { privateKeyToAccount } from "web3-eth-accounts";
import type { TSupportedChainTypes, TSupportedChains } from "../../../config";
import type { BridgeStorage, ERC20Staking } from "../../../contractsTypes/evm";
import { Block } from "../../../persistence/entities/block";
import type { IEvmWallet, IVeChainConfig } from "../../../types";
import type { LogInstance } from "../../types";
import { evmHandler } from "./handler";

export async function configVechainHandler(
conf: IVeChainConfig,
wallet: IEvmWallet,
storage: BridgeStorage,
em: EntityManager,
serverLinkHandler: AxiosInstance | undefined,
evmLogger: LogInstance,
staking: ERC20Staking,
_validatorAddress: string,
) {
const lb = await em.findOne(Block, {
chain: conf.chain,
contractAddress: conf.contractAddress,
});
const mutex = new Mutex();
const net = new SimpleNet(conf.rpcURL);
const simpleWalletObj = new SimpleWallet();
simpleWalletObj.import(wallet.privateKey);
const driver = await Driver.connect(net, simpleWalletObj);
const connexObj = new Framework(driver);
const provider = thor.ethers.modifyProvider(
new BrowserProvider(
new thor.Provider({
connex: connexObj,
net,
wallet: simpleWalletObj,
}),
),
);
const fetchProvider = async () => {
const release = await mutex.acquire();
return [provider, release] as const;
};
return evmHandler({
blockChunks: conf.blockChunks,
bridge: conf.contractAddress,
chainIdent: conf.chain as TSupportedChains,
currency: conf.nativeCoinSymbol,
decimals: conf.decimals,
em: em.fork(),
initialFunds: BigInt(conf.intialFund),
lastBlock_: lb?.lastBlock ?? conf.lastBlock,
fetchProvider: fetchProvider as never,
signer: new Wallet(wallet.privateKey),
storage,
txSigner: privateKeyToAccount(wallet.privateKey),
royaltyProxy: undefined,
chainType: conf.chainType as TSupportedChainTypes,
serverLinkHandler,
logger: evmLogger,
staking,
});
}
26 changes: 4 additions & 22 deletions src/handler/chains/evm/config.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
import type { EntityManager } from "@mikro-orm/sqlite";
import { Driver, SimpleNet } from "@vechain/connex-driver";
import { Framework } from "@vechain/connex-framework";
import { Mutex } from "async-mutex";
import type { AxiosInstance } from "axios";
import { BrowserProvider, JsonRpcProvider, Wallet } from "ethers";
import { JsonRpcProvider, Wallet } from "ethers";
import { privateKeyToAccount } from "web3-eth-accounts";
import * as thor from "web3-providers-connex";
import type { TSupportedChainTypes, TSupportedChains } from "../../../config";
import type { BridgeStorage, ERC20Staking } from "../../../contractsTypes/evm";
import { Block } from "../../../persistence/entities/block";
import type { IEvmChainConfig, IEvmWallet } from "../../../types";
import type { LogInstance } from "../../types";
import { evmHandler } from "./handler";
import type { MutexReleaser } from "./types";

export async function configEvmHandler(
conf: IEvmChainConfig,
Expand All @@ -29,24 +25,10 @@ export async function configEvmHandler(
contractAddress: conf.contractAddress,
});
const mutex = new Mutex();
let provider: JsonRpcProvider | BrowserProvider;
if (conf.chain === "VECHAIN") {
const net = new SimpleNet(conf.rpcURL);
const driver = await Driver.connect(net);
const connexObj = new Framework(driver);
provider = new BrowserProvider(
new thor.ConnexProvider({
connex: connexObj,
}),
);
} else {
provider = new JsonRpcProvider(conf.rpcURL);
}
const fetchProvider = async (): Promise<
[JsonRpcProvider | BrowserProvider, MutexReleaser]
> => {
const provider = new JsonRpcProvider(conf.rpcURL);
const fetchProvider = async () => {
const release = await mutex.acquire();
return [provider, release];
return [provider, release] as const;
};
return evmHandler({
blockChunks: conf.blockChunks,
Expand Down
9 changes: 8 additions & 1 deletion src/handler/chains/evm/handler.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { ContractRunner } from "ethers";
import { raise } from "xp-decentralized-sdk";
import { type Bridge, Bridge__factory } from "../../../contractsTypes/evm";
import pollForLockEvents from "../../poller";
Expand Down Expand Up @@ -35,7 +36,13 @@ export function evmHandler({
}: EVMHandlerParams): THandler {
const bc = async (): Promise<[Bridge, MutexReleaser]> => {
const [provider, release] = await fetchProvider();
const contract = Bridge__factory.connect(bridge, signer.connect(provider));
let runner: ContractRunner;
if (chainIdent === "VECHAIN") {
runner = await provider.getSigner(signer.address);
} else {
runner = signer.connect(provider);
}
const contract = Bridge__factory.connect(bridge, runner);
return [contract, release];
};
return {
Expand Down
4 changes: 2 additions & 2 deletions src/handler/chains/evm/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { EntityManager } from "@mikro-orm/sqlite";
import type { AxiosInstance } from "axios";
import type { BrowserProvider, JsonRpcProvider, Wallet } from "ethers";
import type { JsonRpcProvider, Wallet } from "ethers";
import type { Web3Account } from "web3-eth-accounts";
import type {
TSupportedChainTypes,
Expand Down Expand Up @@ -35,5 +35,5 @@ export type EVMHandlerParams = {
export type MutexReleaser = () => void;

export type EVMProviderFetch = () => Promise<
[JsonRpcProvider | BrowserProvider, MutexReleaser]
readonly [JsonRpcProvider, MutexReleaser]
>;
3 changes: 2 additions & 1 deletion src/handler/chains/secrets/utils/listenForLockEvents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export default async function listenForLockEvents(
token_amount: tokenAmount, // amount of nfts to be transfered ( 1 in 721 case )
nft_type: nftType, // Sigular or multiple ( 721 / 1155)
source_chain: sourceChain, // Source chain of NFT
metadata_uri: metadataUri, // Source chain of NFT
} = parsedLog;
let convertedTokenId = tokenId;
if (sourceChain === "SECRET") {
Expand All @@ -92,7 +93,7 @@ export default async function listenForLockEvents(
sourceChain,
log.transactionHash,
CHAIN_IDENT,
"",
metadataUri,
),
);
}
Expand Down
10 changes: 9 additions & 1 deletion src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ type IHederaChainConfig = {
blockChunks: number;
} & IChainConfig;

type IVeChainConfig = {
chainType: "vechain";
rpcURL: string;
blockChunks: number;
} & IChainConfig;

type ISecretChainConfig = {
chainType: "scrt";
rpcURL: string;
Expand Down Expand Up @@ -197,7 +203,8 @@ type TChain =
| IICPChainConfig
| INearChainConfig
| IAptosChainConfig
| ICasperChainConfig;
| ICasperChainConfig
| IVeChainConfig;

type IBridgeConfig = {
bridgeChains: TChain[];
Expand Down Expand Up @@ -237,4 +244,5 @@ export type {
TChain,
ICasperChainConfig,
ICasperWallet,
IVeChainConfig,
};
Loading

0 comments on commit 2a335cd

Please sign in to comment.