From 3223ce1685943d59972d78c875f4b05d7d13bb93 Mon Sep 17 00:00:00 2001 From: Nayyir Jutha Date: Wed, 18 Dec 2024 15:11:17 -0500 Subject: [PATCH] updated to use class instantiation for balance, created new substrate base class --- src/balance/command.ts | 5 +++-- src/balance/interaction.ts | 3 ++- src/balance/main.ts | 16 ++++++++++------ src/common/entropy-substrate-base.ts | 13 +++++++++++++ src/faucet/main.ts | 3 ++- tests/account.test.ts | 2 +- tests/balance.test.ts | 25 ++++++++++++------------- tests/faucet.test.ts | 5 +++-- tests/transfer.test.ts | 14 +++++++------- 9 files changed, 53 insertions(+), 33 deletions(-) create mode 100644 src/common/entropy-substrate-base.ts diff --git a/src/balance/command.ts b/src/balance/command.ts index 559e7fe..4278a5f 100644 --- a/src/balance/command.ts +++ b/src/balance/command.ts @@ -24,13 +24,14 @@ export function entropyBalanceCommand () { .addOption(endpointOption()) .action(async (account, opts) => { const substrate = await getLoadedSubstrate(opts.endpoint) + const BalanceService = new EntropyBalance(substrate, opts.endpoint) const { decimals, symbol } = await getTokenDetails(substrate) const toBits = (lilBits: number) => round(lilBitsToBits(lilBits, decimals)) const { accounts } = await config.get(opts.config) if (opts.all) { // Balances for all admin accounts const addresses: string[] = accounts.map((acct: EntropyConfigAccount) => acct.address) - const balances = await EntropyBalance.getBalances(substrate, addresses) + const balances = await BalanceService.getBalances(addresses) .then((infos: BalanceInfo[]) => { return infos.map(info => { return { @@ -55,7 +56,7 @@ export function entropyBalanceCommand () { } } // Balance for singular account - const balance = await EntropyBalance.getAnyBalance(substrate, address) + const balance = await BalanceService.getAnyBalance(address) .then(toBits) cliWrite({ account, balance, symbol }) } diff --git a/src/balance/interaction.ts b/src/balance/interaction.ts index 3173b85..a19c9db 100644 --- a/src/balance/interaction.ts +++ b/src/balance/interaction.ts @@ -7,10 +7,11 @@ import { EntropyBalance } from "./main" export async function entropyBalance (opts: EntropyTuiOptions, storedConfig) { try { const substrate = await getLoadedSubstrate(opts.endpoint) + const BalanceService = new EntropyBalance(substrate, opts.endpoint) // grabbing decimals from chain spec as that is the source of truth for the value const { decimals, symbol } = await getTokenDetails(substrate) const address = findAccountByAddressOrName(storedConfig.accounts, storedConfig.selectedAccount)?.address - const lilBalance = await EntropyBalance.getAnyBalance(substrate, address) + const lilBalance = await BalanceService.getAnyBalance(address) const balance = round(lilBitsToBits(lilBalance, decimals)) print(`Entropy Account [${storedConfig.selectedAccount}] (${address}) has a balance of: ${balance} ${symbol}`) // closing substrate diff --git a/src/balance/main.ts b/src/balance/main.ts index 678eb78..1f5d3bf 100644 --- a/src/balance/main.ts +++ b/src/balance/main.ts @@ -1,20 +1,24 @@ import * as BalanceUtils from "./utils" import { BalanceInfo } from "./types" +import { EntropySubstrateBase } from "src/common/entropy-substrate-base" -export class EntropyBalance { - constructor () {} +const FLOW_CONTEXT = 'ENTROPY-BALANCE' +export class EntropyBalance extends EntropySubstrateBase { + constructor (substrate, endpoint) { + super({ substrate, endpoint, flowContext: FLOW_CONTEXT }) + } - static async getAnyBalance (substrate, address: string) { - const accountInfo = (await substrate.query.system.account(address)) as any + async getAnyBalance (address: string) { + const accountInfo = (await this.substrate.query.system.account(address)) as any const balance = parseInt(BalanceUtils.hexToBigInt(accountInfo.data.free).toString()) return balance } - static async getBalances (substrate, addresses: string[]): Promise { + async getBalances (addresses: string[]): Promise { return Promise.all( addresses.map(async address => { - return EntropyBalance.getAnyBalance(substrate, address) + return this.getAnyBalance(address) .then((balance: number) => { return { address, balance } }) diff --git a/src/common/entropy-substrate-base.ts b/src/common/entropy-substrate-base.ts new file mode 100644 index 0000000..9d5b674 --- /dev/null +++ b/src/common/entropy-substrate-base.ts @@ -0,0 +1,13 @@ +import { EntropyLogger } from "./logger"; + +export abstract class EntropySubstrateBase { + protected logger: EntropyLogger + protected substrate: any + protected endpoint: string + + constructor ({ substrate, endpoint, flowContext }: { substrate: any, endpoint: string, flowContext: string }) { + this.logger = new EntropyLogger(flowContext, endpoint) + this.substrate = substrate + this.endpoint = endpoint + } +} diff --git a/src/faucet/main.ts b/src/faucet/main.ts index 103aaf0..dbd3d6b 100644 --- a/src/faucet/main.ts +++ b/src/faucet/main.ts @@ -72,9 +72,10 @@ export class EntropyFaucet extends EntropyBase { }: SendMoneyParams ): Promise { const programService = new EntropyProgram(this.entropy, this.endpoint) + const BalanceService = new EntropyBalance(this.entropy.substrate, this.endpoint) // check balance of faucet address - const balance = await EntropyBalance.getAnyBalance(this.entropy.substrate, faucetAddress) + const balance = await BalanceService.getAnyBalance(faucetAddress) if (balance <= 0) throw new Error('FundsError: Faucet Account does not have funds') // check verifying key has ONLY the exact program installed diff --git a/tests/account.test.ts b/tests/account.test.ts index 645801c..eb41c2a 100644 --- a/tests/account.test.ts +++ b/tests/account.test.ts @@ -35,7 +35,7 @@ test('Account - list', async t => { 'test-net': 'wss://testnet.entropy.xyz', }, selectedAccount: account.name, - 'migration-version': '0' + 'migration-version': 0 } const accountsArray = EntropyAccount.list(config) diff --git a/tests/balance.test.ts b/tests/balance.test.ts index e14ef13..affaa0f 100644 --- a/tests/balance.test.ts +++ b/tests/balance.test.ts @@ -1,34 +1,33 @@ import test from 'tape' -import { createSubstrate } from '@entropyxyz/sdk/utils' import { EntropyBalance } from '../src/balance/main' import { EntropyAccount } from '../src/account/main' -import { closeSubstrate } from '../src/common/substrate-utils' +import { closeSubstrate, getLoadedSubstrate } from '../src/common/substrate-utils' -import { charlieStashAddress as richAddress, promiseRunner, DEFAULT_ENDPOINT, eveAddress } from './testing-utils' +import { charlieStashAddress as richAddress, promiseRunner, DEFAULT_ENDPOINT } from './testing-utils' test('getAnyBalance + getBalances', async (t) => { const run = promiseRunner(t) // create new account, not saved in config const { address: newAddress } = await EntropyAccount.create({ name: 'TestAnyBalance1' }) - const substrate = createSubstrate(DEFAULT_ENDPOINT) - await run('substrate ready', substrate.isReadyOrError) + const substrate = await run('load substrate', getLoadedSubstrate(DEFAULT_ENDPOINT)) + const BalanceService = new EntropyBalance(substrate, DEFAULT_ENDPOINT) const newAddressBalance = await run( 'getAnyBalance (newAccount)', - EntropyBalance.getAnyBalance(substrate, newAddress) + BalanceService.getAnyBalance(newAddress) ) t.equal(newAddressBalance, 0, 'newSeed balance = 0') const richAddressBalance = await run( 'getBalance (richAddress)', - EntropyBalance.getAnyBalance(substrate, richAddress) + BalanceService.getAnyBalance(richAddress) ) t.true(richAddressBalance > BigInt(10e10), 'richAddress balance >>> 0') /* getBalances */ const balances = await run( 'getBalances', - EntropyBalance.getBalances(substrate, [newAddress, richAddress]) + BalanceService.getBalances([newAddress, richAddress]) ) t.deepEqual( balances, @@ -42,7 +41,7 @@ test('getAnyBalance + getBalances', async (t) => { const badAddresses = ['5Cz6BfUaxxXCA3jninzxdan4JdmC1NVpgkiRPYhXbhr', '5Cz6BfUaxxXCA3jninzxdan4JdmC1NVpgkiRPYhXbhrfnD'] const balancesWithNoGoodAddress = await run( 'getBalances::one good address', - EntropyBalance.getBalances(substrate, badAddresses) + BalanceService.getBalances(badAddresses) ) badAddresses.forEach((addr) => { @@ -59,11 +58,11 @@ test('getAnyBalance + getBalances', async (t) => { test('getAnyBalance: bad account', async (t) => { const run = promiseRunner(t) - const substrate = createSubstrate(DEFAULT_ENDPOINT) - await run('substrate ready', substrate.isReadyOrError) - await EntropyBalance.getAnyBalance(substrate, 'not-a-real-account') + const substrate = await run('load substrate', getLoadedSubstrate(DEFAULT_ENDPOINT)) + const BalanceService = new EntropyBalance(substrate, DEFAULT_ENDPOINT) + await BalanceService.getAnyBalance('not-a-real-account') .then(() => t.fail('Getting balance should fail')) .catch(() => t.pass('Getting balance should fail')) - await run('close substrate', substrate.disconnect().catch(err => console.error('Error closing connection', err.message))) + await run('close substrate', closeSubstrate(substrate)) t.end() }) diff --git a/tests/faucet.test.ts b/tests/faucet.test.ts index 122b466..b6bb9b7 100644 --- a/tests/faucet.test.ts +++ b/tests/faucet.test.ts @@ -77,8 +77,9 @@ test('Faucet Tests: Successfully send funds and register', async t => { const naynayAddress = naynay.keyring.accounts.registration.address const faucet = new EntropyFaucet(naynay, endpoint) + const BalanceService = new EntropyBalance(naynay.substrate, endpoint) - let naynayBalance = await EntropyBalance.getAnyBalance(naynay.substrate, naynayAddress) + let naynayBalance = await BalanceService.getAnyBalance(naynayAddress) t.equal(naynayBalance, 0, 'Naynay is broke af') // 2 BITS @@ -95,7 +96,7 @@ test('Faucet Tests: Successfully send funds and register', async t => { ) t.ok(transferStatus.isFinalized, 'Transfer is good') - naynayBalance = await EntropyBalance.getAnyBalance(naynay.substrate, naynayAddress) + naynayBalance = await BalanceService.getAnyBalance(naynayAddress) t.equal(naynayBalance, amount, 'Naynay is drippin in faucet tokens') // Test if user can register after receiving funds diff --git a/tests/transfer.test.ts b/tests/transfer.test.ts index c4e25f9..822f561 100644 --- a/tests/transfer.test.ts +++ b/tests/transfer.test.ts @@ -8,28 +8,28 @@ import { charlieStashAddress, charlieStashSeed, DEFAULT_TOKEN_DECIMALS } from '. test('Transfer', async (t) => { /* Setup */ - const run = promiseRunner(t) - - const { entropy: charlie, endpoint }= await setupTest(t, { seed: charlieStashSeed }) + const { run, entropy: charlie, endpoint }= await setupTest(t, { seed: charlieStashSeed }) const { entropy: naynay } = await setupTest(t) + const transferService = new EntropyTransfer(charlie, endpoint) + const BalanceService = new EntropyBalance(charlie.substrate, endpoint) + const naynayAddress = naynay.keyring.accounts.registration.address // Check initial balances let naynayBalance = await run( 'getBalance (naynay)', - EntropyBalance.getAnyBalance(naynay.substrate, naynayAddress) + BalanceService.getAnyBalance(naynayAddress) ) t.equal(naynayBalance, 0, 'naynay is broke') let charlieBalance = await run( 'getBalance (charlieStash)', - EntropyBalance.getAnyBalance(charlie.substrate, charlieStashAddress) + BalanceService.getAnyBalance(charlieStashAddress) ) t.true(charlieBalance > 9e16, 'charlie got bank') // Do transer - const transferService = new EntropyTransfer(charlie, endpoint) const inputAmount = "1.5" await run( 'transfer', @@ -39,7 +39,7 @@ test('Transfer', async (t) => { // Re-Check balance naynayBalance = await run( 'getBalance (naynay)', - EntropyBalance.getAnyBalance(naynay.substrate, naynayAddress) + BalanceService.getAnyBalance(naynayAddress) ) const expected = Number(inputAmount) * lilBitsPerBits(DEFAULT_TOKEN_DECIMALS) t.equal(naynayBalance, expected, 'naynay is rolling in it!')