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 utxosToAccount rpc #215

Merged
merged 20 commits into from
May 19, 2021
Merged
Show file tree
Hide file tree
Changes from 19 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
43 changes: 43 additions & 0 deletions packages/jellyfish-api-core/__tests__/category/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../container_adapter_client'
import waitForExpect from 'wait-for-expect'
import BigNumber from 'bignumber.js'
import { UtxosToAccountPayload } from '../../src/category/account'

describe('masternode', () => {
const container = new MasterNodeRegTestContainer()
Expand Down Expand Up @@ -479,4 +480,46 @@ describe('masternode', () => {
})
})
})

describe('utxosToAccount', () => {
let address1: string
let address2: string

beforeAll(async () => {
address1 = await container.call('getnewaddress')
address2 = await container.call('getnewaddress')
})

it('should utxosToAccount', async () => {
const payload: UtxosToAccountPayload = {}
// NOTE(jingyi2811): Only support sending utxos to DFI account.
payload[address1] = '5@DFI'
payload[address2] = '5@DFI'
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved

const data = await client.account.utxosToAccount(payload)

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

it('should utxosToAccount with utxos', async () => {
const payload: UtxosToAccountPayload = {}
// NOTE(jingyi2811): Only support sending utxos to DFI account.
payload[address1] = '5@DFI'
payload[address2] = '5@DFI'
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved

const utxos = await container.call('listunspent')
const inputs = utxos.map((utxo: { txid: string, vout: number }) => {
return {
txid: utxo.txid,
vout: utxo.vout
}
})

const data = await client.account.utxosToAccount(payload, inputs)

expect(typeof data).toBe('string')
expect(data.length).toBe(64)
})
})
})
24 changes: 24 additions & 0 deletions packages/jellyfish-api-core/src/category/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,21 @@ export class Account {
): Promise<AccountHistory[]> {
return await this.client.call('listaccounthistory', [owner, options], 'number')
}

/**
* Creates (and submits to local node and network) a transfer transaction from the wallet UTXOs to specfied account.
* The second optional argument (may be empty array) is an array of specific UTXOs to spend.
*
* @param {UtxosToAccountPayload} payload
* @param {string} payload[address]
* @param {UtxosToAccountUTXO[]} [utxos=[]]
* @param {string} [utxos.txid]
* @param {number} [utxos.vout]
* @return {Promise<string>}
*/
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
async utxosToAccount (payload: UtxosToAccountPayload, utxos: UtxosToAccountUTXO[] = []): Promise<string> {
return await this.client.call('utxostoaccount', [payload, utxos], 'number')
}
}

export interface AccountPagination {
Expand Down Expand Up @@ -277,3 +292,12 @@ export interface AccountHistoryOptions {
txtype?: string
limit?: number
}

export interface UtxosToAccountPayload {
[key: string]: string
}

export interface UtxosToAccountUTXO {
txid: string
vout: number
}
20 changes: 20 additions & 0 deletions website/docs/jellyfish/api/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -142,3 +142,23 @@ interface AccountHistoryOptions {
limit?: number
}
```

## utxosToAccount

Creates (and submits to local node and network) a transfer transaction from the wallet UTXOs to specfied account.
The second optional argument (may be empty array) is an array of specific UTXOs to spend.

```ts title="client.account.utxosToAccount()"
interface account {
utxosToAccount (payload: UtxosToAccountPayload, utxos: UtxosToAccountUTXO[] = []): Promise<string>
}

interface UtxosToAccountPayload {
[key: string]: string;
}

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