diff --git a/packages/api/CHANGELOG.md b/packages/api/CHANGELOG.md index 64d68c9c..bacb9bc7 100644 --- a/packages/api/CHANGELOG.md +++ b/packages/api/CHANGELOG.md @@ -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 diff --git a/packages/api/src/account-lcd.ts b/packages/api/src/account-lcd.ts index 06f4c04d..c13da21f 100644 --- a/packages/api/src/account-lcd.ts +++ b/packages/api/src/account-lcd.ts @@ -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' @@ -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) @@ -28,6 +34,17 @@ export default class Account extends LCDClient { return deriveMnemonic(mnemonic, path).privateKey } + transferMsg(from: string, to: string, amount: ICoin[]): IMsg { + return { + type: 'cosmos-sdk/MsgSend', + value: { + from_address: from, + to_address: to, + amount + } + } + } + async import(mnemonic: string, path?: string, prefix: string = bech32Prefix): Promise { const child = await deriveMnemonic(mnemonic, path) const address = encode(prefix, toWords(child.identifier)) diff --git a/packages/api/src/process-lcd.ts b/packages/api/src/process-lcd.ts index 573eb23e..45db7425 100644 --- a/packages/api/src/process-lcd.ts +++ b/packages/api/src/process-lcd.ts @@ -156,6 +156,7 @@ export type INode = { export type IProcess = { hash?: string; + address?: string; name: string; nodes: INode[]; edges: ProcessType.mesg.types.Process.IEdge[]; diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index 669ffde5..35ed3cb1 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -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 diff --git a/packages/cli/src/commands/account/transfer.ts b/packages/cli/src/commands/account/transfer.ts new file mode 100644 index 00000000..ab038f2b --- /dev/null +++ b/packages/cli/src/commands/account/transfer.ts @@ -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') + } +} diff --git a/packages/cli/src/commands/process/create.ts b/packages/cli/src/commands/process/create.ts index 1b9279e2..420c6761 100644 --- a/packages/cli/src/commands/process/create.ts +++ b/packages/cli/src/commands/process/create.ts @@ -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' @@ -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) + return process + } } diff --git a/packages/cli/src/commands/process/list.ts b/packages/cli/src/commands/process/list.ts index 9020992b..7622414e 100644 --- a/packages/cli/src/commands/process/list.ts +++ b/packages/cli/src/commands/process/list.ts @@ -14,9 +14,15 @@ export default class ProcessList extends Command { async run(): Promise { 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 }