Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NayNay] Only substrate #329

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ Version header format: `[version] Name - year-month-day (entropy-core compatibil
- Shared
- updated return data displayed to user on account creation (create or import) [#311](https://github.com/entropyxyz/cli/pull/311)
- Balance now displays the number of BITS to the nearest 4 decimal places [#306](https://github.com/entropyxyz/cli/pull/306)
- removed use of entropy instance from transfer flow [#329](https://github.com/entropyxyz/cli/pull/329)

- CLI
- updated balance command to take in any address, and be able to return the balance for the inputted address [#315](https://github.com/entropyxyz/cli/pull/315)
Expand Down
5 changes: 2 additions & 3 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Entropy } from '@entropyxyz/sdk'
import { Buffer } from 'node:buffer'
import { homedir } from 'node:os'
import { join } from 'node:path'
Expand Down Expand Up @@ -111,11 +110,11 @@ export function absolutePath (somePath: string) {
}
}

export function formatDispatchError (entropy: Entropy, dispatchError) {
export function formatDispatchError (substrate: any, dispatchError) {
let msg: string
if (dispatchError.isModule) {
// for module errors, we have the section indexed, lookup
const decoded = entropy.substrate.registry.findMetaError(
const decoded = substrate.registry.findMetaError(
dispatchError.asModule
)
const { docs, name, section } = decoded
Expand Down
2 changes: 1 addition & 1 deletion src/faucet/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class EntropyFaucet extends EntropyBase {
// status would still be set, but in the case of error we can shortcut
// to just check it (so an error would indicate InBlock or Finalized)
if (dispatchError) {
const error = formatDispatchError(this.entropy, dispatchError)
const error = formatDispatchError(this.entropy.substrate, dispatchError)
return reject(error)
}
if (status.isFinalized) resolve(status)
Expand Down
4 changes: 2 additions & 2 deletions src/transfer/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ export function entropyTransferCommand () {
.action(async (destination, amount, opts) => {
// TODO: destination as <name|address> ?
const entropy = await loadEntropyCli(opts)
const transferService = new EntropyTransfer(entropy, opts.endpoint)
const transferService = new EntropyTransfer(opts.endpoint)
const { symbol } = await getTokenDetails(entropy.substrate)

await transferService.transfer(destination, amount)
await transferService.transfer(entropy.keyring.accounts.registration.pair, destination, amount)

cliWrite({
source: entropy.keyring.accounts.registration.address,
Expand Down
4 changes: 2 additions & 2 deletions src/transfer/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ export async function entropyTransfer (entropy, opts: EntropyTuiOptions) {
if (transferSpinner.isSpinning) transferSpinner.stop()
try {
const { symbol } = await getTokenDetails(entropy.substrate)
const transferService = new EntropyTransfer(entropy, opts.endpoint)
const transferService = new EntropyTransfer(opts.endpoint)
const { amount, recipientAddress } = await inquirer.prompt(transferInputQuestions)
if (!transferSpinner.isSpinning) transferSpinner.start()
await transferService.transfer(recipientAddress, amount)
await transferService.transfer(entropy.keyring.accounts.registration.pair, recipientAddress, amount)
if (transferSpinner.isSpinning) transferSpinner.stop()
print('')
print(`Transaction successful: Sent ${amount} ${symbol} to ${recipientAddress}`)
Expand Down
35 changes: 23 additions & 12 deletions src/transfer/main.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,55 @@
import Entropy from "@entropyxyz/sdk";
// @ts-ignore
import { Pair } from '@entropyxyz/sdk/keys'

import { EntropyBase } from "../common/entropy-base";
import { closeSubstrate, getLoadedSubstrate } from "../common/substrate-utils";
import { EntropyLogger } from "../common/logger";
import { bitsToLilBits, formatDispatchError, getTokenDetails } from "../common/utils";

import { TransferOptions } from "./types";

const FLOW_CONTEXT = 'ENTROPY_TRANSFER'

export class EntropyTransfer extends EntropyBase {
constructor (entropy: Entropy, endpoint: string) {
super({ entropy, endpoint, flowContext: FLOW_CONTEXT })
export class EntropyTransfer {
private readonly substrate: any
private readonly logger: EntropyLogger
private readonly endpoint: string
constructor (endpoint: string) {
this.logger = new EntropyLogger(FLOW_CONTEXT, endpoint)
this.endpoint = endpoint
}

// NOTE: a more accessible function which handles
// - setting `from`
// - converting `amount` (string => BigInt)
// - progress callbacks (optional)

async transfer (toAddress: string, amountInBits: string) {
const { decimals } = await getTokenDetails(this.entropy.substrate)
async transfer (from: Pair, toAddress: string, amountInBits: string) {
const substrate = await getLoadedSubstrate(this.endpoint)
const { decimals } = await getTokenDetails(substrate)
const lilBits = bitsToLilBits(Number(amountInBits), decimals)

return this.rawTransfer({
from: this.entropy.keyring.accounts.registration.pair,
const transferStatus = await this.rawTransfer(substrate, {
from,
to: toAddress,
lilBits
})

await closeSubstrate(substrate)
return transferStatus
}

private async rawTransfer (payload: TransferOptions): Promise<any> {
private async rawTransfer (substrate: any, payload: TransferOptions): Promise<any> {
const { from, to, lilBits } = payload

return new Promise((resolve, reject) => {
// WARN: await signAndSend is dangerous as it does not resolve
// after transaction is complete :melt:
this.entropy.substrate.tx.balances
substrate.tx.balances
.transferAllowDeath(to, lilBits)
// @ts-ignore
.signAndSend(from, ({ status, dispatchError }) => {
if (dispatchError) {
const error = formatDispatchError(this.entropy, dispatchError)
const error = formatDispatchError(this.substrate, dispatchError)
this.logger.error('There was an issue sending this transfer', error)
return reject(error)
}
Expand Down
6 changes: 3 additions & 3 deletions tests/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -84,9 +84,9 @@ const endpoint = 'ws://127.0.0.1:9944'

async function fundAccount (t, entropy: Entropy) {
const { entropy: charlie } = await setupTest(t, { seed: charlieStashSeed })
const transfer = new EntropyTransfer(charlie, endpoint)
const transfer = new EntropyTransfer(endpoint)

await transfer.transfer(entropy.keyring.accounts.registration.address, "1000")
await transfer.transfer(charlie.keyring.accounts.registration.pair, entropy.keyring.accounts.registration.address, "1000")
}


Expand Down
4 changes: 2 additions & 2 deletions tests/faucet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ async function setupAndFundFaucet (t) {
const { run, entropy, endpoint } = await setupTest(t, { seed: eveSeed })

const account = new EntropyAccount(entropy, endpoint)
const transfer = new EntropyTransfer(entropy, endpoint)
const transfer = new EntropyTransfer(endpoint)
const faucet = new EntropyFaucet(entropy, endpoint)

// Deploy faucet program
Expand Down Expand Up @@ -65,7 +65,7 @@ async function setupAndFundFaucet (t) {
const verifyingKeys = await faucet.getAllFaucetVerifyingKeys(eveAddress)
// @ts-expect-error
const { chosenVerifyingKey, faucetAddress } = faucet.getRandomFaucet([], verifyingKeys)
await run('Transfer funds to faucet address', transfer.transfer(faucetAddress, "1000"))
await run('Transfer funds to faucet address', transfer.transfer(entropy.keyring.accounts.registration.pair, faucetAddress, "1000"))

return { faucetProgramPointer, chosenVerifyingKey, faucetAddress }
}
Expand Down
4 changes: 2 additions & 2 deletions tests/transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ test('Transfer', async (t) => {
t.true(charlieBalance > 9e16, 'charlie got bank')

// Do transer
const transferService = new EntropyTransfer(charlie, endpoint)
const transferService = new EntropyTransfer(endpoint)
const inputAmount = "1.5"
await run(
'transfer',
transferService.transfer(naynayAddress, inputAmount)
transferService.transfer(charlie.keyring.accounts.registration.pair, naynayAddress, inputAmount)
)

// Re-Check balance
Expand Down
Loading