diff --git a/packages/jellyfish-api-core/__tests__/category/account/accountToUtxos.test.ts b/packages/jellyfish-api-core/__tests__/category/account/accountToUtxos.test.ts new file mode 100644 index 0000000000..b413d1a752 --- /dev/null +++ b/packages/jellyfish-api-core/__tests__/category/account/accountToUtxos.test.ts @@ -0,0 +1,92 @@ +import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { ContainerAdapterClient } from '../../container_adapter_client' +import { BalanceTransferPayload } from '../../../src/category/account' +import { RpcApiError } from '../../../src' + +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 { + from = await container.call('getnewaddress') + await createToken(from, 'DBTC') + } + + async function createToken (address: string, symbol: string): Promise { + 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 () => { + const balanceBefore = await container.call('getbalance') + + const payload: BalanceTransferPayload = {} + // NOTE(jingyi2811): Only support sending DFI from account to Utxos. + payload[await container.getNewAddress()] = '5@DFI' + payload[await container.getNewAddress()] = '5@DFI' + + const data = await client.account.accountToUtxos(from, payload) + await container.generate(1) + + expect(typeof data).toStrictEqual('string') + expect(data.length).toStrictEqual(64) + + const balanceAfter = await container.call('getbalance') + expect(balanceAfter).toBeGreaterThan(balanceBefore) + }) + + it('should not accountToUtxos for token', async () => { + const payload: BalanceTransferPayload = {} + payload[await container.getNewAddress()] = '5@DBTC' + + const promise = client.account.accountToUtxos(from, payload) + + await expect(promise).rejects.toThrow(RpcApiError) + await expect(promise).rejects.toThrow('RpcApiError: \'Test AccountToUtxosTx execution failed:\nonly available for DFI transactions\', code: -32600, method: accounttoutxos') + }) + + it('should accountToUtxos with utxos', async () => { + const balanceBefore = await container.call('getbalance') + + const { txid, vout } = await container.fundAddress(from, 10) + + const payload: BalanceTransferPayload = {} + // NOTE(jingyi2811): Only support sending DFI from account to Utxos. + payload[await container.getNewAddress()] = '5@DFI' + payload[await container.getNewAddress()] = '5@DFI' + + const data = await client.account.accountToUtxos(from, payload, { utxos: [{ txid, vout }] }) + await container.generate(1) + + expect(typeof data).toStrictEqual('string') + expect(data.length).toStrictEqual(64) + + const balanceAfter = await container.call('getbalance') + expect(balanceAfter).toBeGreaterThan(balanceBefore) + }) +}) diff --git a/packages/jellyfish-api-core/src/category/account.ts b/packages/jellyfish-api-core/src/category/account.ts index 2899177242..25c5e60d4e 100644 --- a/packages/jellyfish-api-core/src/category/account.ts +++ b/packages/jellyfish-api-core/src/category/account.ts @@ -31,7 +31,7 @@ export enum TxType { AUTO_AUTH_PREP = 'A' } -type AccountRegexType = `${string}@${string}` +type AccountRegexType = `${number}@${string}` /** * Account RPCs for DeFi Blockchain @@ -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} - */ - async listAccountHistory ( - owner: OwnerType | string = OwnerType.MINE, - options: AccountHistoryOptions = { - limit: 100 - } - ): Promise { - 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. @@ -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} */ - async accountToAccount (from: string, payload: BalanceTransferPayload, options: AccountToAccountOptions = { utxos: [] }): Promise { + async accountToAccount (from: string, payload: BalanceTransferPayload, options: BalanceTransferAccountOptions = { utxos: [] }): Promise { 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} + */ + async accountToUtxos (from: string, payload: BalanceTransferPayload, options: BalanceTransferAccountOptions = { utxos: [] }): Promise { + 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} + */ + async listAccountHistory ( + owner: OwnerType | string = OwnerType.MINE, + options: AccountHistoryOptions = { + limit: 100 + } + ): Promise { + return await this.client.call('listaccounthistory', [owner, options], 'number') + } + /** * Returns count of account history * @@ -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 @@ -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 diff --git a/website/docs/jellyfish/api/account.md b/website/docs/jellyfish/api/account.md index f3a3230043..c10abd85ae 100644 --- a/website/docs/jellyfish/api/account.md +++ b/website/docs/jellyfish/api/account.md @@ -106,85 +106,71 @@ interface GetTokenBalancesOptions { } ``` -## listAccountHistory +## utxosToAccount -Returns information about account history +Create an UTXOs to Account transaction submitted to a connected node. +Optionally, specific UTXOs to spend to create that transaction. -```ts title="client.account.listAccountHistory()" +```ts title="client.account.utxosToAccount()" interface account { - listAccountHistory ( - owner: OwnerType | string = OwnerType.MINE, - options: AccountHistoryOptions = { - limit: 100 - } - ): Promise + utxosToAccount (payload: BalanceTransferPayload, utxos: UTXO[] = []): Promise } -enum OwnerType { - MINE = "mine", - ALL = "all" -} +type AccountRegexType = `${number}@${string}` -interface AccountHistory { - owner: string - blockHeight: number - blockHash: string - blockTime: number - type: string - txn: number - txid: string - amounts: string[] +interface BalanceTransferPayload { + [key: string]: AccountRegexType } -interface AccountHistoryOptions { - maxBlockHeight?: number - depth?: number - no_rewards?: boolean - token?: string - txtype?: string - limit?: number +interface UTXO { + txid: string + vout: number } ``` -## utxosToAccount +## accountToAccount -Create an UTXOs to Account transaction submitted to a connected node. +Create an Account to Account transaction submitted to a connected node. Optionally, specific UTXOs to spend to create that transaction. -```ts title="client.account.utxosToAccount()" +```ts title="client.account.accountToAccount()" interface account { - utxosToAccount (payload: BalanceTransferPayload, utxos: UTXO[] = []): Promise + accountToAccount (from: string, payload: BalanceTransferPayload, options: BalanceTransferAccountOptions = { utxos: [] }): Promise } -type AccountRegexType = `${string}@${string}` +type AccountRegexType = `${number}@${string}` interface BalanceTransferPayload { [key: string]: AccountRegexType } +interface BalanceTransferAccountOptions { + utxos?: UTXO[] +} + interface UTXO { txid: string vout: number } ``` -## accountToAccount +## accountToUtxos -Create an Account to Account transaction submitted to a connected node. +Create an Account to UTXOS transaction submitted to a connected node. Optionally, specific UTXOs to spend to create that transaction. -```ts title="client.account.accountToAccount()" +```ts title="client.account.accountToUtxos()" interface account { - accountToAccount (from: string, payload: BalanceTransferPayload, options: AccountToAccountOptions = { utxos: [] }): Promise + accountToUtxos (from: string, payload: BalanceTransferPayload, options: BalanceTransferAccountOptions = { utxos: [] }): Promise } -type AccountRegexType = `${string}@${string}` +type AccountRegexType = `${number}@${string}` interface BalanceTransferPayload { [key: string]: AccountRegexType } -interface AccountToAccountOptions { +interface BalanceTransferAccountOptions { utxos?: UTXO[] } @@ -194,6 +180,46 @@ interface UTXO { } ``` +## listAccountHistory + +Returns information about account history + +```ts title="client.account.listAccountHistory()" +interface account { + listAccountHistory ( + owner: OwnerType | string = OwnerType.MINE, + options: AccountHistoryOptions = { + limit: 100 + } + ): Promise +} + +enum OwnerType { + MINE = "mine", + ALL = "all" +} + +interface AccountHistory { + owner: string + blockHeight: number + blockHash: string + blockTime: number + type: string + txn: number + txid: string + amounts: string[] +} + +interface AccountHistoryOptions { + maxBlockHeight?: number + depth?: number + no_rewards?: boolean + token?: string + txtype?: string + limit?: number +} +``` + ## historyCount Returns count of account history