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

Add balance to the process #187

Merged
merged 8 commits into from
Mar 17, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion packages/api/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
- [#179](https://github.com/mesg-foundation/js-sdk/pull/179) Migrate resolver to use LCD server
- [#183](https://github.com/mesg-foundation/js-sdk/pull/183) Add account import from a mnemonic
- [#181](https://github.com/mesg-foundation/js-sdk/pull/181) Add transaction support with signature and broadcast
- [#186](htttps://github.com/mesg-foundation/js-sdk/pull/186) Update type for processes and add new message to create/delete process
- [#186](https://github.com/mesg-foundation/js-sdk/pull/186) Update type for processes and add new message to create/delete process
- [#187](https://github.com/mesg-foundation/js-sdk/pull/187) Add transfer message to account

#### Bug fixes

Expand Down
19 changes: 18 additions & 1 deletion packages/api/src/account-lcd.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICoin } from './transaction'
import { ICoin, IMsg } from './transaction'
import LCDClient from './util/lcd'
import { validateMnemonic, mnemonicToSeedSync } from 'bip39'
import { fromSeed, BIP32Interface } from 'bip32'
Expand All @@ -15,6 +15,12 @@ export type IAccount = {
sequence: number
}

export type IMsgTransfer = {
from_address: string;
to_address: string;
amount: ICoin[];
}

const deriveMnemonic = (mnemonic: string, path: string = defaultHDPath): BIP32Interface => {
if (!validateMnemonic(mnemonic)) throw new Error('invalid mnemonic')
const seed = mnemonicToSeedSync(mnemonic)
Expand All @@ -28,6 +34,17 @@ export default class Account extends LCDClient {
return deriveMnemonic(mnemonic, path).privateKey
}

transferMsg(from: string, to: string, amount: ICoin[]): IMsg<IMsgTransfer> {
return {
type: 'cosmos-sdk/MsgSend',
value: {
from_address: from,
to_address: to,
amount
}
}
}

async import(mnemonic: string, path?: string, prefix: string = bech32Prefix): Promise<IAccount> {
const child = await deriveMnemonic(mnemonic, path)
const address = encode(prefix, toWords(child.identifier))
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/process-lcd.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ export type INode = {

export type IProcess = {
hash?: string;
address?: string;
name: string;
nodes: INode[];
edges: ProcessType.mesg.types.Process.IEdge[];
Expand Down
2 changes: 2 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
- [#183](https://github.com/mesg-foundation/js-sdk/pull/183) Add account commands `account:balance`, `account:create`, `account:export`, `account:list`
- [#181](https://github.com/mesg-foundation/js-sdk/pull/181) Add util to get hash from a transaction log
- [#186](https://github.com/mesg-foundation/js-sdk/pull/186) Create/delete process based on LCD endpoint
- [#187](https://github.com/mesg-foundation/js-sdk/pull/187) Add `account:transfer` command
- [#187](https://github.com/mesg-foundation/js-sdk/pull/187) Transfer token when creating a process

#### Bug fixes

Expand Down
39 changes: 39 additions & 0 deletions packages/cli/src/commands/account/transfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Command from '../../root-command'
import { flags } from '@oclif/command'
import { ICoin } from '@mesg/api/lib/transaction'

export default class AccountTransfer extends Command {
static description = 'Transfer token to another address'

static flags = {
...Command.flags,
account: flags.string({
description: 'Account to use to take the tokens from'
}),
}

static args = [{
name: 'TO',
required: true
}, {
name: 'AMOUNT',
required: true
}]

async run() {
const { args, flags } = this.parse(AccountTransfer)
const { account, mnemonic } = await this.getAccount(flags.account)

this.spinner.start(`Transfering ${args.AMOUNT}atto to ${args.TO}`)
const coins: ICoin[] = [{
amount: args.AMOUNT,
denom: 'atto'
}]
const tx = await this.lcd.createTransaction(
[this.lcd.account.transferMsg(account.address, args.TO, coins)],
account
)
await this.lcd.broadcast(tx.signWithMnemonic(mnemonic))
this.spinner.stop('success')
}
}
6 changes: 4 additions & 2 deletions packages/cli/src/commands/process/create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {flags} from '@oclif/command'
import Command from '../../root-command'
import {findHash} from '../../utils/txevent'
import {IProcess} from '@mesg/api/lib/process-lcd'
import {ICoin} from '@mesg/api/lib/transaction'

export default class ProcessCreate extends Command {
static description = 'Create a process'
Expand Down Expand Up @@ -36,6 +37,7 @@ export default class ProcessCreate extends Command {
if (hashes.length != 1) throw new Error('error while getting the hash of the process created')
const hash = hashes[0]
this.spinner.stop(hash)
return this.lcd.process.get(hash)
}
const process = await this.lcd.process.get(hash)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this change doesn't need to be but not blocking

return process
}
}
8 changes: 7 additions & 1 deletion packages/cli/src/commands/process/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,15 @@ export default class ProcessList extends Command {
async run(): Promise<IProcess[]> {
const {flags} = this.parse(ProcessList)
const processes = await this.lcd.process.list()
cli.table(processes, {
const data = await Promise.all(processes.map(async x => ({
...x,
account: await this.lcd.account.get(x.address),
})))
cli.table(data, {
hash: {header: 'HASH'},
name: {header: 'NAME'},
address: {header: 'ADDRESS'},
balance: {header: 'BALANCE', get: (x) => x.account.coins.map(x => `${x.amount}${x.denom}`).join(', ')}
}, {printLine: this.log, ...flags})
return processes
}
Expand Down