Skip to content

Commit

Permalink
tweeks
Browse files Browse the repository at this point in the history
  • Loading branch information
mixmix committed Aug 28, 2024
1 parent b7ff0a9 commit 4b29dba
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 47 deletions.
2 changes: 1 addition & 1 deletion src/common/utils-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Entropy from '@entropyxyz/sdk'
import { initializeEntropy } from './initializeEntropy'

export function cliWrite (result) {
const prettyResult = stringify(result)
const prettyResult = stringify(result, 0)
process.stdout.write(prettyResult)
}

Expand Down
29 changes: 10 additions & 19 deletions src/sign/command.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
import { Command, Option } from 'commander'
import { Command, /* Option */ } from 'commander'
import { Entropy } from '@entropyxyz/sdk'
import { cliWrite, currentAccountAddressOption, endpointOption, passwordOption } from '../common/utils-cli'
import { EntropySign } from './main'

export async function entropySignCommand (entropy: Entropy, rootCommand: Command) {
const signCommand = rootCommand.command('sign')
.description('Commands for working with signing with the Entropy Network')

entropySign(entropy, signCommand)
}

function entropySign (entropy: Entropy, signCommand: Command) {
signCommand.command('sign')
.description('Sign a message using the Entropy network. Output is a signature (string)')
rootCommand.command('sign')
.description('Sign a message using the Entropy network. Output is a JSON { verifyingKey, signature }')
.argument('msg', 'Message you would like to sign (string)')
.addOption(passwordOption('Password for the source account (if required)'))
.addOption(endpointOption())
.addOption(currentAccountAddressOption())
.addOption(
new Option(
'-r, --raw',
'Signs the provided message using the Raw Signing method. Output is a signature (string)'
)
)
// .addOption(
// new Option(
// '-r, --raw',
// 'Signs the provided message using the Raw Signing method. Output is a signature (string)'
// )
// )
.action(async (msg, opts) => {
const SigningService = new EntropySign(entropy, opts.endpoint)
// TO-DO: Add ability for raw signing here, maybe? new raw option can be used for the conditional
Expand All @@ -32,9 +25,7 @@ function entropySign (entropy: Entropy, signCommand: Command) {
* }
*/
const { verifyingKey, signature } = await SigningService.signMessageWithAdapters({ msg })
cliWrite(`Message Signed: ${msg}`)
cliWrite(`Verifying Key: ${verifyingKey}`)
cliWrite(`Signature: ${signature}`)
cliWrite({ verifyingKey, signature })
process.exit(0)
})
}
4 changes: 2 additions & 2 deletions src/transfer/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ export async function entropyTransferCommand (entropy: Entropy, rootCommand: Com
.addOption(currentAccountAddressOption())
.action(async (destination, amount, opts) => {
const TransferService = new EntropyTransfer(entropy, opts.endpoint)
await TransferService.sendTransfer(destination, amount)
await TransferService.transfer(destination, amount)
// cliWrite(??) // TODO: write the output
process.exit(0)
})
}
}
6 changes: 3 additions & 3 deletions src/transfer/interaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import { transferInputQuestions } from "./utils"
import { setupProgress } from "src/common/progress"

export async function entropyTransfer (entropy, endpoint) {
const { start: startProgress, stop: stopProgress } = setupProgress('Transferring Funds')
const progressTracker = setupProgress('Transferring Funds')
const TransferService = new EntropyTransfer(entropy, endpoint)
const { amount, recipientAddress } = await inquirer.prompt(transferInputQuestions)
await TransferService.sendTransfer(recipientAddress, amount, startProgress, stopProgress)
await TransferService.transfer(recipientAddress, amount, progressTracker)
print('')
print(`Transaction successful: Sent ${amount} to ${recipientAddress}`)
print('')
print('Press enter to return to main menu')
}
}
52 changes: 32 additions & 20 deletions src/transfer/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,34 @@ export class EntropyTransfer extends EntropyBase {
super(entropy, endpoint, FLOW_CONTEXT)
}

async transfer (payload: TransferOptions): Promise<any> {
// NOTE: a more accessible function which handles
// - setting `from`
// - converting `amount` (string => BigInt)
// - progress callbacks (optional)

async transfer (toAddress: string, amount: string, progress?: { start: ()=>void, stop: ()=>void }) {
const formattedAmount = BigInt(parseInt(amount) * 1e10)

if (progress) progress.start()
try {
const transferStatus = await this.rawTransfer({
from: this.entropy.keyring.accounts.registration.pair,
to: toAddress,
amount: formattedAmount
})
if (transferStatus.isFinalized) {
if (progress) return progress.stop()
return
}
} catch (error) {
if (progress) return progress.stop()
throw error
}
}

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

return new Promise((resolve, reject) => {
// WARN: await signAndSend is dangerous as it does not resolve
// after transaction is complete :melt:
Expand All @@ -33,27 +58,14 @@ export class EntropyTransfer extends EntropyBase {
// Other, CannotLookup, BadOrigin, no extra info
msg = dispatchError.toString()
}
return reject(Error(msg))
const error = Error(msg)
this.logger.error('There was an issue sending this transfer', error)
return reject(error)
}

if (status.isFinalized) resolve(status)
})
})
}

public async sendTransfer (toAddress: string, amount: string, startProgress?: ()=>void, stopProgress?: ()=>void) {
const formattedAmount = BigInt(parseInt(amount) * 1e10)
if (startProgress) startProgress()
try {
const transferStatus = await this.transfer({ from: this.entropy.keyring.accounts.registration.pair, to: toAddress, amount: formattedAmount })
if (transferStatus.isFinalized) {
if (stopProgress) return stopProgress()
return
}
} catch (error) {
this.logger.error('There was an issue sending this transfer', error)
if (stopProgress) return stopProgress()
throw error
}
}
}
}
4 changes: 2 additions & 2 deletions src/transfer/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ import { Pair } from '@entropyxyz/sdk/keys'
export interface TransferOptions {
from: Pair
to: string
amount: bigint
}
amount: bigint
}

0 comments on commit 4b29dba

Please sign in to comment.