Skip to content

Commit

Permalink
Add accountToUtxos rpc (#289)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyi2811 authored Jun 3, 2021
1 parent 2f3ab75 commit 4fb3b90
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 78 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import { BalanceTransferPayload } from '../../../src/category/account'
import { RpcApiError } from '../../../src'

describe('Account with DBTC', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await setup()
})

afterAll(async () => {
await container.stop()
})

let from: string

async function setup (): Promise<void> {
from = await container.call('getnewaddress')
await createToken(from, 'DBTC')
}

async function createToken (address: string, symbol: string): Promise<void> {
const metadata = {
symbol,
name: symbol,
isDAT: true,
mintable: true,
tradeable: true,
collateralAddress: address
}
await container.waitForWalletBalanceGTE(101)
await container.call('createtoken', [metadata])
await container.generate(1)

await container.call('utxostoaccount', [{ [address]: '100@0' }])
await container.generate(1)
}

it('should accountToUtxos', async () => {
const balanceBefore = await container.call('getbalance')

const payload: BalanceTransferPayload = {}
// NOTE(jingyi2811): Only support sending DFI from account to Utxos.
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DFI'

const data = await client.account.accountToUtxos(from, payload)
await container.generate(1)

expect(typeof data).toStrictEqual('string')
expect(data.length).toStrictEqual(64)

const balanceAfter = await container.call('getbalance')
expect(balanceAfter).toBeGreaterThan(balanceBefore)
})

it('should not accountToUtxos for token', async () => {
const payload: BalanceTransferPayload = {}
payload[await container.getNewAddress()] = '5@DBTC'

const promise = client.account.accountToUtxos(from, payload)

await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('RpcApiError: \'Test AccountToUtxosTx execution failed:\nonly available for DFI transactions\', code: -32600, method: accounttoutxos')
})

it('should accountToUtxos with utxos', async () => {
const balanceBefore = await container.call('getbalance')

const { txid, vout } = await container.fundAddress(from, 10)

const payload: BalanceTransferPayload = {}
// NOTE(jingyi2811): Only support sending DFI from account to Utxos.
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DFI'

const data = await client.account.accountToUtxos(from, payload, { utxos: [{ txid, vout }] })
await container.generate(1)

expect(typeof data).toStrictEqual('string')
expect(data.length).toStrictEqual(64)

const balanceAfter = await container.call('getbalance')
expect(balanceAfter).toBeGreaterThan(balanceBefore)
})
})
93 changes: 55 additions & 38 deletions packages/jellyfish-api-core/src/category/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export enum TxType {
AUTO_AUTH_PREP = 'A'
}

type AccountRegexType = `${string}@${string}`
type AccountRegexType = `${number}@${string}`

/**
* Account RPCs for DeFi Blockchain
Expand Down Expand Up @@ -224,28 +224,6 @@ export class Account {
return await this.client.call('gettokenbalances', [pagination, indexedAmounts, symbolLookup], 'bignumber')
}

/**
* Returns information about account history
*
* @param {OwnerType | string} [owner=OwnerType.MINE] single account ID (CScript or address) or reserved words 'mine' to list history for all owned accounts or 'all' to list whole DB
* @param {AccountHistoryOptions} [options]
* @param {number} [options.maxBlockHeight] Optional height to iterate from (down to genesis block), (default = chaintip).
* @param {number} [options.depth] Maximum depth, from the genesis block is the default
* @param {boolean} [options.no_rewards] Filter out rewards
* @param {string} [options.token] Filter by token
* @param {string} [options.txtype] Filter by transaction type, supported letter from 'CRTMNnpuslrUbBG
* @param {number} [options.limit=100] Maximum number of records to return, 100 by default
* @return {Promise<AccountHistory[]>}
*/
async listAccountHistory (
owner: OwnerType | string = OwnerType.MINE,
options: AccountHistoryOptions = {
limit: 100
}
): Promise<AccountHistory[]> {
return await this.client.call('listaccounthistory', [owner, options], 'number')
}

/**
* Create an UTXOs to Account transaction submitted to a connected node.
* Optionally, specific UTXOs to spend to create that transaction.
Expand All @@ -268,16 +246,55 @@ export class Account {
* @param {string} from
* @param {BalanceTransferPayload} payload
* @param {string} payload[address]
* @param {AccountToAccountOptions} [options]
* @param {BalanceTransferAccountOptions} [options]
* @param {UTXO[]} [options.utxos = []]
* @param {string} [options.utxos.txid]
* @param {number} [options.utxos.vout]
* @return {Promise<string>}
*/
async accountToAccount (from: string, payload: BalanceTransferPayload, options: AccountToAccountOptions = { utxos: [] }): Promise<string> {
async accountToAccount (from: string, payload: BalanceTransferPayload, options: BalanceTransferAccountOptions = { utxos: [] }): Promise<string> {
return await this.client.call('accounttoaccount', [from, payload, options.utxos], 'number')
}

/**
* Create an Account to UXTOS transaction submitted to a connected node.
* Optionally, specific UTXOs to spend to create that transaction.
*
* @param {string} from
* @param {BalanceTransferPayload} payload
* @param {string} payload[address]
* @param {BalanceTransferAccountOptions} [options]
* @param {UTXO[]} [options.utxos = []]
* @param {string} [options.utxos.txid]
* @param {number} [options.utxos.vout]
* @return {Promise<string>}
*/
async accountToUtxos (from: string, payload: BalanceTransferPayload, options: BalanceTransferAccountOptions = { utxos: [] }): Promise<string> {
return await this.client.call('accounttoutxos', [from, payload, options.utxos], 'number')
}

/**
* Returns information about account history
*
* @param {OwnerType | string} [owner=OwnerType.MINE] single account ID (CScript or address) or reserved words 'mine' to list history for all owned accounts or 'all' to list whole DB
* @param {AccountHistoryOptions} [options]
* @param {number} [options.maxBlockHeight] Optional height to iterate from (down to genesis block), (default = chaintip).
* @param {number} [options.depth] Maximum depth, from the genesis block is the default
* @param {boolean} [options.no_rewards] Filter out rewards
* @param {string} [options.token] Filter by token
* @param {string} [options.txtype] Filter by transaction type, supported letter from 'CRTMNnpuslrUbBG
* @param {number} [options.limit=100] Maximum number of records to return, 100 by default
* @return {Promise<AccountHistory[]>}
*/
async listAccountHistory (
owner: OwnerType | string = OwnerType.MINE,
options: AccountHistoryOptions = {
limit: 100
}
): Promise<AccountHistory[]> {
return await this.client.call('listaccounthistory', [owner, options], 'number')
}

/**
* Returns count of account history
*
Expand Down Expand Up @@ -332,6 +349,19 @@ export interface GetTokenBalancesOptions {
symbolLookup?: boolean
}

export interface BalanceTransferPayload {
[key: string]: AccountRegexType
}

export interface BalanceTransferAccountOptions {
utxos?: UTXO[]
}

export interface UTXO {
txid: string
vout: number
}

export interface AccountHistory {
owner: string
blockHeight: number
Expand All @@ -352,19 +382,6 @@ export interface AccountHistoryOptions {
limit?: number
}

export interface BalanceTransferPayload {
[key: string]: AccountRegexType
}

export interface AccountToAccountOptions {
utxos?: UTXO[]
}

export interface UTXO {
txid: string
vout: number
}

export interface AccountHistoryCountOptions {
token?: string
txtype?: TxType | string
Expand Down
106 changes: 66 additions & 40 deletions website/docs/jellyfish/api/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,85 +106,71 @@ interface GetTokenBalancesOptions {
}
```

## listAccountHistory
## utxosToAccount

Returns information about account history
Create an UTXOs to Account transaction submitted to a connected node.
Optionally, specific UTXOs to spend to create that transaction.

```ts title="client.account.listAccountHistory()"
```ts title="client.account.utxosToAccount()"
interface account {
listAccountHistory (
owner: OwnerType | string = OwnerType.MINE,
options: AccountHistoryOptions = {
limit: 100
}
): Promise<AccountHistory[]>
utxosToAccount (payload: BalanceTransferPayload, utxos: UTXO[] = []): Promise<string>
}

enum OwnerType {
MINE = "mine",
ALL = "all"
}
type AccountRegexType = `${number}@${string}`

interface AccountHistory {
owner: string
blockHeight: number
blockHash: string
blockTime: number
type: string
txn: number
txid: string
amounts: string[]
interface BalanceTransferPayload {
[key: string]: AccountRegexType
}

interface AccountHistoryOptions {
maxBlockHeight?: number
depth?: number
no_rewards?: boolean
token?: string
txtype?: string
limit?: number
interface UTXO {
txid: string
vout: number
}
```

## utxosToAccount
## accountToAccount

Create an UTXOs to Account transaction submitted to a connected node.
Create an Account to Account transaction submitted to a connected node.
Optionally, specific UTXOs to spend to create that transaction.

```ts title="client.account.utxosToAccount()"
```ts title="client.account.accountToAccount()"
interface account {
utxosToAccount (payload: BalanceTransferPayload, utxos: UTXO[] = []): Promise<string>
accountToAccount (from: string, payload: BalanceTransferPayload, options: BalanceTransferAccountOptions = { utxos: [] }): Promise<string>
}

type AccountRegexType = `${string}@${string}`
type AccountRegexType = `${number}@${string}`

interface BalanceTransferPayload {
[key: string]: AccountRegexType
}

interface BalanceTransferAccountOptions {
utxos?: UTXO[]
}

interface UTXO {
txid: string
vout: number
}
```

## accountToAccount
## accountToUtxos

Create an Account to Account transaction submitted to a connected node.
Create an Account to UTXOS transaction submitted to a connected node.
Optionally, specific UTXOs to spend to create that transaction.

```ts title="client.account.accountToAccount()"
```ts title="client.account.accountToUtxos()"
interface account {
accountToAccount (from: string, payload: BalanceTransferPayload, options: AccountToAccountOptions = { utxos: [] }): Promise<string>
accountToUtxos (from: string, payload: BalanceTransferPayload, options: BalanceTransferAccountOptions = { utxos: [] }): Promise<string>
}

type AccountRegexType = `${string}@${string}`
type AccountRegexType = `${number}@${string}`

interface BalanceTransferPayload {
[key: string]: AccountRegexType
}

interface AccountToAccountOptions {
interface BalanceTransferAccountOptions {
utxos?: UTXO[]
}

Expand All @@ -194,6 +180,46 @@ interface UTXO {
}
```

## listAccountHistory

Returns information about account history

```ts title="client.account.listAccountHistory()"
interface account {
listAccountHistory (
owner: OwnerType | string = OwnerType.MINE,
options: AccountHistoryOptions = {
limit: 100
}
): Promise<AccountHistory[]>
}

enum OwnerType {
MINE = "mine",
ALL = "all"
}

interface AccountHistory {
owner: string
blockHeight: number
blockHash: string
blockTime: number
type: string
txn: number
txid: string
amounts: string[]
}

interface AccountHistoryOptions {
maxBlockHeight?: number
depth?: number
no_rewards?: boolean
token?: string
txtype?: string
limit?: number
}
```

## historyCount

Returns count of account history
Expand Down

0 comments on commit 4fb3b90

Please sign in to comment.