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 all 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
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 @@ -479,4 +480,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
}
```