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 accountToUtxos rpc #289

Merged
merged 12 commits into from
Jun 3, 2021
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import { BalanceTransferPayload, UTXO } from '../../../src/category/account'

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 () => {
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
const payload: BalanceTransferPayload = {}
// NOTE(jingyi2811): Only support sending from account to DFI Utxos.
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DFI'

const data = await client.account.accountToUtxos(from, payload)
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved

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

it('should accountToUtxos with utxos', async () => {
const { txid } = await container.fundAddress(from, 10)

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

const utxos = await container.call('listunspent')
const inputs: UTXO[] = utxos.filter((utxo: UTXO) => utxo.txid === txid).map((utxo: UTXO) => {
return {
txid: utxo.txid,
vout: utxo.vout
}
})
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved

const data = await client.account.accountToUtxos(from, payload, { utxos: inputs })
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved

expect(typeof data).toStrictEqual('string')
expect(data.length).toStrictEqual(64)
})
})
91 changes: 54 additions & 37 deletions packages/jellyfish-api-core/src/category/account.ts
Original file line number Diff line number Diff line change
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> {
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
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
30 changes: 28 additions & 2 deletions website/docs/jellyfish/api/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ Optionally, specific UTXOs to spend to create that transaction.

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

type AccountRegexType = `${string}@${string}`
Expand All @@ -184,7 +184,33 @@ interface BalanceTransferPayload {
[key: string]: AccountRegexType
}

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

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

## accountToUtxos

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

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

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

interface BalanceTransferPayload {
[key: string]: AccountRegexType
}

interface BalanceTransferAccountOptions {
utxos?: UTXO[]
}

Expand Down