Skip to content

Commit

Permalink
Merge pull request #21 from XP-NETWORK/test
Browse files Browse the repository at this point in the history
Test
  • Loading branch information
D4mph1r authored Dec 1, 2024
2 parents a475fcd + 28ee7f7 commit 3fefa78
Show file tree
Hide file tree
Showing 71 changed files with 1,209 additions and 741 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ name: Docker

on:
push:
branches: [ 'main' ]
branches: [ 'main', 'test' ]

env:
# Use docker.io for Docker Hub if empty
Expand Down
7 changes: 5 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"format": "npx @biomejs/biome format src/ --write",
"lint": "npx @biomejs/biome lint src/ --write",
"postinstall": "lefthook install",
"dev": "tsc && node dist/index.js"
"dev": "tsc && node dist/index.js",
"start": "ts-node src/index.ts"
},
"devDependencies": {
"@biomejs/biome": "^1.5.3",
Expand All @@ -37,6 +38,7 @@
"@dfinity/ledger-icp": "^2.4.0",
"@dfinity/principal": "^2.0.0",
"@dfinity/utils": "^2.4.0",
"@make-software/ces-js-parser": "^1.3.3",
"@mikro-orm/core": "^6.1.4",
"@mikro-orm/migrations": "^6.1.4",
"@mikro-orm/sqlite": "^6.1.4",
Expand All @@ -56,7 +58,8 @@
"@xp/cosmos-client": "git+https://github.com/XP-NETWORK/cosmos-client#bleeding-edge",
"async-mutex": "^0.5.0",
"axios": "^1.6.7",
"casper-js-sdk": "^3.0.0-rc05",
"casper-cep78-js-client": "^1.5.1",
"casper-js-sdk": "=2.15.2",
"chalk": "4",
"dotenv": "^16.4.5",
"elysia": "^1.0.25",
Expand Down
49 changes: 49 additions & 0 deletions src/balances/chains-balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import type { Interface } from "node:readline/promises";
import { formatEther } from "ethers";
import type { LogInstance, THandler } from "../handler/types";

export async function requireEnoughBalanceInChains(
chains: THandler[],
stdio: Interface,
log: LogInstance,
) {
const notValidatorChains = (
await Promise.all(
chains.map(async (chain) => {
try {
if (await chain.selfIsValidator()) {
log.info(`${chain.chainIdent} Validator ✅.`);
return [];
}
log.warn(`${chain.chainIdent} Validator ❌.`);
return [chain];
} catch (e) {
return [chain];
}
// throw new Error("Unreachable");
}),
)
).flat();
let funded = false;
for (const chain of notValidatorChains) {
while (!funded) {
const balance = await chain.getBalance();
const remainingRaw = chain.initialFunds - BigInt(balance);
if (balance < chain.initialFunds) {
log.error(
`Balance: ${formatEther(balance)}. Fund wallet ${chain.address} on ${
chain.chainIdent
} with ${Number(remainingRaw) / Number(chain.decimals)} ${
chain.currency
}.`,
);
// Sleep for 10 Seconds
await stdio.question("Press Enter to continue...");
continue;
}
funded = true;
log.info(`${chain.chainIdent} Has Enough Funds: ✅`);
}
}
return notValidatorChains;
}
36 changes: 36 additions & 0 deletions src/balances/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createInterface } from "node:readline/promises";
import { raise } from "../handler/chains";
import type { LogInstance, THandler } from "../handler/types";
import type {
IEvmChainConfig,
IGeneratedWallets,
IStakingConfig,
} from "../types";
import { requireEnoughStakingBalanceAndChainBalance } from "./staking-balance";
import { requireEnoughStorageChainBalance } from "./storage-balance";

export async function requireEnoughBalanceForStakingAndStorage(
chains: THandler[],
storageConfig: IEvmChainConfig,
stakingConfig: IStakingConfig,
secrets: IGeneratedWallets,
log: LogInstance,
) {
const stdio = createInterface({
input: process.stdin,
output: process.stdout,
});
const bscHandler =
chains.find((chain) => chain.chainIdent === "BSC") ||
raise("BSC Chain not found");

await requireEnoughStorageChainBalance(storageConfig, stdio, secrets, log);

await requireEnoughStakingBalanceAndChainBalance(
stakingConfig,
stdio,
bscHandler,
secrets,
log,
);
}
79 changes: 79 additions & 0 deletions src/balances/staking-balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import type { Interface } from "node:readline/promises";
import { ethers } from "ethers";
import {
ERC20Staking__factory,
ERC20Token__factory,
} from "../contractsTypes/evm";
import type { LogInstance, THandler } from "../handler/types";
import type { IGeneratedWallets, IStakingConfig } from "../types";
import { requireEnoughBalanceInChains } from "./chains-balance";

export async function requireEnoughStakingBalanceAndChainBalance(
stakingConfig: IStakingConfig,
stdio: Interface,
bscHandler: THandler,
secrets: IGeneratedWallets,
log: LogInstance,
): Promise<void> {
let requireFunds =
BigInt(bscHandler.initialFunds) + BigInt(stakingConfig.intialFund);
let stakingChainFunded = false;
const provider = new ethers.JsonRpcProvider(stakingConfig.rpcURL);
const stakingContract = ERC20Staking__factory.connect(
stakingConfig.contractAddress,
provider,
);
const amtToStake = await stakingContract.stakingAmount();
const isStaked = await stakingContract.stakingBalances(
secrets.evmWallet.address,
);
let erc20Balance = await ERC20Token__factory.connect(
stakingConfig.coinAddress,
provider,
).balanceOf(secrets.evmWallet.address);

if (await bscHandler.selfIsValidator()) {
log.info(
`Chain ${bscHandler.chainIdent} is already validator. Skipping Checking for funding.`,
);
requireFunds -= BigInt(bscHandler.initialFunds);
} else {
await requireEnoughBalanceInChains([bscHandler], stdio, log);
}
while (!(amtToStake <= erc20Balance) && !isStaked) {
erc20Balance = await ERC20Token__factory.connect(
stakingConfig.coinAddress,
provider,
).balanceOf(secrets.evmWallet.address);

log.error(
`Current balance: ${ethers.formatEther(
erc20Balance,
)}; Fund staking chain, your wallet ${secrets.evmWallet.address} on ${
stakingConfig.chain
} with ${ethers.formatEther(amtToStake - erc20Balance)} ${
stakingConfig.coinSymbol
}.`,
);
await stdio.question("Press Enter to continue...");
}
while (!stakingChainFunded) {
const balance = await bscHandler.getBalance();
if (balance < requireFunds) {
log.error(
`Current balance: ${ethers.formatEther(
balance,
)}; Fund staking chain your wallet ${secrets.evmWallet.address} on ${
stakingConfig.chain
} with ${ethers.formatEther(requireFunds - balance)} ${
stakingConfig.nativeCoinSymbol
}.`,
);
// Sleep for 10 Seconds
await stdio.question("Press Enter to continue...");
continue;
}
stakingChainFunded = true;
}
log.info("Staking Has Enough Funds: ✅");
}
39 changes: 39 additions & 0 deletions src/balances/storage-balance.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import type { Interface } from "node:readline/promises";
import { JsonRpcProvider, VoidSigner, ethers } from "ethers";
import { getBalance } from "../handler/chains/evm/utils";
import type { LogInstance } from "../handler/types";
import type { IEvmChainConfig, IGeneratedWallets } from "../types";

export async function requireEnoughStorageChainBalance(
storageConfig: IEvmChainConfig,
stdio: Interface,
secrets: IGeneratedWallets,
log: LogInstance,
) {
// Check for Storage Funds
let storageFunded = false;
while (!storageFunded) {
const balance = await getBalance(
new VoidSigner(secrets.evmWallet.address),
async () => {
return [new JsonRpcProvider(storageConfig.rpcURL), () => {}];
},
);
if (balance < BigInt(storageConfig.intialFund)) {
log.error(
`Balance: ${ethers.formatEther(balance)}; Fund your wallet ${
secrets.evmWallet.address
} on ${storageConfig.chain} with ${ethers.formatEther(
BigInt(storageConfig.intialFund) - balance,
)} ${storageConfig.nativeCoinSymbol}.`,
);
// Sleep for 10 Seconds
await stdio.question(
"Press Enter to continue after funding the wallet...",
);
continue;
}
storageFunded = true;
}
log.info("Storage Has Enough Funds: ✅");
}
41 changes: 27 additions & 14 deletions src/config.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// import { Network } from "@aptos-labs/ts-sdk";
// import { Network } from "@aptos-labs/ts-sdk";
import { Network } from "@aptos-labs/ts-sdk";
import type {
IBridgeConfig,
Expand Down Expand Up @@ -103,20 +104,20 @@ export const bridgeTestChains = [
nativeCoinSymbol: "ICP",
rpcURL: "https://tools.xp.network/",
},
{
chain: "NEAR",
chainType: "near",
contractAddress: "xp-bridge-test.testnet",
decimals: 24,
intialFund: "100000000000000000000000",
lastBlock: 0,
nativeCoinSymbol: "NEAR",
nearBlocksUrl: "https://api-testnet.nearblocks.io/v1/",
networkId: "testnet",
rpcURL: "https://archival-rpc.testnet.near.org",
theGraphApiUrl:
"https://api.studio.thegraph.com/query/89122/near-xp/version/latest",
},
// {
// chain: "NEAR",
// chainType: "near",
// contractAddress: "xp-bridge-test.testnet",
// decimals: 24,
// intialFund: "100000000000000000000000",
// lastBlock: 0,
// nativeCoinSymbol: "NEAR",
// nearBlocksUrl: "https://api-testnet.nearblocks.io/v1/",
// networkId: "testnet",
// rpcURL: "https://archival-rpc.testnet.near.org",
// theGraphApiUrl:
// "https://api.studio.thegraph.com/query/89122/near-xp/version/latest",
// },
{
chain: "BLAST",
rpcURL: "https://blast-sepolia.blockpi.network/v1/rpc/public",
Expand All @@ -139,6 +140,18 @@ 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",
},
] as const satisfies TChain[];

export const storageTestnetConfig: IEvmChainConfig = {
Expand Down
Loading

0 comments on commit 3fefa78

Please sign in to comment.