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 accountToAccount RPC #284

Merged
merged 23 commits into from
May 28, 2021
Merged
Show file tree
Hide file tree
Changes from 8 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
94 changes: 92 additions & 2 deletions packages/jellyfish-api-core/__tests__/category/account.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ 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'
import { AccountToAccountPayload, UtxosToAccountPayload } from '../../src/category/account'
import { RpcApiError } from '../../src'

describe('masternode', () => {
const container = new MasterNodeRegTestContainer()
Expand Down Expand Up @@ -44,6 +45,10 @@ describe('masternode', () => {
await container.call('createtoken', [metadata])
await container.generate(1)

const payload: { [key: string]: string } = {}
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
payload[address] = '100@0'
await container.call('utxostoaccount', [payload])
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved

await container.call('minttokens', [`${amount.toString()}@${symbol}`])
await container.generate(1)
}
Expand Down Expand Up @@ -287,7 +292,7 @@ describe('masternode', () => {
it('should getTokenBalances with pagination limit', async () => {
await waitForExpect(async () => {
const tokenBalances = await client.account.getTokenBalances()
expect(tokenBalances.length).toBe(2)
expect(tokenBalances.length).toBe(3)
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
})
const pagination = {
limit: 1
Expand Down Expand Up @@ -515,5 +520,90 @@ describe('masternode', () => {
expect(typeof data).toBe('string')
expect(data.length).toBe(64)
})

it('should not utxosToAccount with utxos for DFI coin if does not own the recipient address', async () => {
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
const payload: UtxosToAccountPayload = {}
// NOTE(jingyi2811): Only support sending utxos to DFI account.
payload['2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz'] = '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 promise = client.account.utxosToAccount(payload, inputs)

await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('The address (2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz) is not your own address')
})
})

describe('accountToAccount', () => {
it('should accountToAccount', async () => {
const from = await container.getNewAddress()
await createToken(from, 'DABC', 5)
await createToken(from, 'DDEF', 5)

const payload: AccountToAccountPayload = {}
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DABC'
payload[await container.getNewAddress()] = '5@DDEF'

const data = await client.account.accountToAccount(from, payload)

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

it('should accountToAccount with utxos', async () => {
const from = await container.getNewAddress()
await createToken(from, 'DGHI', 5)
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
await createToken(from, 'DJKL', 5)

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

const payload: AccountToAccountPayload = {}
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DGHI'
payload[await container.getNewAddress()] = '5@DJKL'

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

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

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

it('should not accountToAccount with utxos for DFI coin if does not own the recipient address', async () => {
const from = await container.getNewAddress()

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

const payload: AccountToAccountPayload = {}
payload['2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz'] = '5@DFI'

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

const promise = client.account.accountToAccount(from, payload, inputs)

await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('The address (2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz) is not your own address')
})
})
})
25 changes: 25 additions & 0 deletions packages/jellyfish-api-core/src/category/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,22 @@ export class Account {
async utxosToAccount (payload: UtxosToAccountPayload, utxos: UtxosToAccountUTXO[] = []): Promise<string> {
return await this.client.call('utxostoaccount', [payload, utxos], 'number')
}

/**
* Creates and submits to a local node and transfer transaction from the specified account to the specfied accounts.
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
* Optionally, specific UTXOs to spend to create that transaction.
*
* @param {string} from
* @param {AccountToAccountPayload} payload
* @param {string} payload[address]
* @param {AccountToAccountUTXO[]} [utxos=[]]
* @param {string} [utxos.txid]
* @param {number} [utxos.vout]
* @return {Promise<string>}
*/
async accountToAccount (from: string, payload: AccountToAccountPayload, utxos: AccountToAccountUTXO[] = []): Promise<string> {
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
return await this.client.call('accounttoaccount', [from, payload, utxos], 'number')
}
}

export interface AccountPagination {
Expand Down Expand Up @@ -301,3 +317,12 @@ export interface UtxosToAccountUTXO {
txid: string
vout: number
}

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

export interface AccountToAccountUTXO {
txid: string
vout: number
}
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 21 additions & 1 deletion website/docs/jellyfish/api/account.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ interface AccountHistoryOptions {

## utxosToAccount

Creates and submits to a connect node; a transfer transaction from the wallet UTXOs to a specified account.
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()"
Expand All @@ -162,3 +162,23 @@ interface UtxosToAccountUTXO {
vout: number
}
```

## accountToAccount

Creates and submits to a local node and transfer transaction from the specified account to the specfied accounts.
Optionally, specific UTXOs to spend to create that transaction.

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

interface AccountToAccountPayload {
[key: string]: string;
jingyi2811 marked this conversation as resolved.
Show resolved Hide resolved
}

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