-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #21 from XP-NETWORK/test
Test
- Loading branch information
Showing
71 changed files
with
1,209 additions
and
741 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ✅"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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: ✅"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.