Skip to content

Commit

Permalink
Add utxosToAccount rpc (#215)
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyi2811 authored May 19, 2021
1 parent 7894dd2 commit 355a30b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
35 changes: 35 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 @@ -481,4 +482,38 @@ describe('masternode', () => {
})
})
})

describe('utxosToAccount', () => {
it('should utxosToAccount', async () => {
const payload: UtxosToAccountPayload = {}
// NOTE(jingyi2811): Only support sending utxos to DFI account.
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DFI'

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[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DFI'

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 a connect node; a transfer transaction from the wallet UTXOs to a specified account.
* Optionally, specific UTXOs to spend to create that transaction.
*
* @param {UtxosToAccountPayload} payload
* @param {string} payload[address]
* @param {UtxosToAccountUTXO[]} [utxos=[]]
* @param {string} [utxos.txid]
* @param {number} [utxos.vout]
* @return {Promise<string>}
*/
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 a connect node; a transfer transaction from the wallet UTXOs to a specified account.
Optionally, specific UTXOs to spend to create that transaction.

```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
}
```

0 comments on commit 355a30b

Please sign in to comment.