Skip to content

Commit

Permalink
Merge branch 'naynay/any-balance' into naynay/substrate-overload
Browse files Browse the repository at this point in the history
  • Loading branch information
rh0delta committed Dec 18, 2024
2 parents 6fa210a + 3223ce1 commit b2c0875
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/balance/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 })
}
Expand Down
3 changes: 2 additions & 1 deletion src/balance/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 10 additions & 6 deletions src/balance/main.ts
Original file line number Diff line number Diff line change
@@ -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<BalanceInfo[]> {
async getBalances (addresses: string[]): Promise<BalanceInfo[]> {
return Promise.all(
addresses.map(async address => {
return EntropyBalance.getAnyBalance(substrate, address)
return this.getAnyBalance(address)
.then((balance: number) => {
return { address, balance }
})
Expand Down
13 changes: 13 additions & 0 deletions src/common/entropy-substrate-base.ts
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 2 additions & 1 deletion src/faucet/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ export class EntropyFaucet extends EntropyBase {
}: SendMoneyParams
): Promise<any> {
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
Expand Down
25 changes: 12 additions & 13 deletions tests/balance.test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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) => {
Expand All @@ -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()
})
5 changes: 3 additions & 2 deletions tests/faucet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
9 changes: 5 additions & 4 deletions tests/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,24 +20,25 @@ test('Transfer', async (t) => {
// setuptest still needed to run here to start up wasm and get the config ready
const { run, endpoint }= await setupTest(t, { seed: charlieStashSeed })
const substrate = await run('load substrate', getLoadedSubstrate(endpoint))
const transferService = new EntropyTransfer(endpoint)
const BalanceService = new EntropyBalance(substrate, endpoint)

const naynayAddress = naynay.address
const charlieKeyring = new Keyring({ seed: charlieStashSeed, path: '', debug: true })
// Check initial balances
let naynayBalance = await run(
'getBalance (naynay)',
EntropyBalance.getAnyBalance(substrate, naynayAddress)
BalanceService.getAnyBalance(naynayAddress)
)
t.equal(naynayBalance, 0, 'naynay is broke')

let charlieBalance = await run(
'getBalance (charlieStash)',
EntropyBalance.getAnyBalance(substrate, charlieStashAddress)
BalanceService.getAnyBalance(charlieStashAddress)
)
t.true(charlieBalance > 9e16, 'charlie got bank')

// Do transer
const transferService = new EntropyTransfer(endpoint)
const inputAmount = "1.5"
await run(
'transfer',
Expand All @@ -47,7 +48,7 @@ test('Transfer', async (t) => {
// Re-Check balance
naynayBalance = await run(
'getBalance (naynay)',
EntropyBalance.getAnyBalance(substrate, naynayAddress)
BalanceService.getAnyBalance(naynayAddress)
)
const expected = Number(inputAmount) * lilBitsPerBits(DEFAULT_TOKEN_DECIMALS)
t.equal(naynayBalance, expected, 'naynay is rolling in it!')
Expand Down

0 comments on commit b2c0875

Please sign in to comment.