Skip to content

Commit

Permalink
Merge a7b81c3 into 69e7f9c
Browse files Browse the repository at this point in the history
  • Loading branch information
jingyi2811 authored Jun 1, 2021
2 parents 69e7f9c + a7b81c3 commit 8f2be4b
Show file tree
Hide file tree
Showing 8 changed files with 754 additions and 639 deletions.
606 changes: 0 additions & 606 deletions packages/jellyfish-api-core/__tests__/category/account.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import waitForExpect from 'wait-for-expect'
import { AccountHistoryCountOptions, TxType } from '../../../src/category/account'

describe('Account', () => {
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()
})

async function setup (): Promise<void> {
const from = await container.call('getnewaddress')
await createToken(from, 'DBTC', 200)

const to = await accountToAccount('DBTC', 5, from)
await accountToAccount('DBTC', 18, from, to)

await createToken(from, 'DETH', 200)
await accountToAccount('DETH', 46, from)
}

async function createToken (address: string, symbol: string, amount: number): Promise<void> {
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('minttokens', [`${amount.toString()}@${symbol}`])
await container.generate(1)
}

async function accountToAccount (symbol: string, amount: number, from: string, _to = ''): Promise<string> {
const to = _to !== '' ? _to : await container.call('getnewaddress')

await container.call('accounttoaccount', [from, { [to]: `${amount.toString()}@${symbol}` }])

return to
}

it('should get accountHistoryCount', async () => {
await waitForExpect(async () => {
const count = await client.account.historyCount()

expect(typeof count).toBe('number')
expect(count).toBeGreaterThanOrEqual(0)
})
})

it('should get accountHistoryCount with owner as all', async () => {
await waitForExpect(async () => {
const count = await client.account.historyCount('all')

expect(typeof count).toBe('number')
expect(count).toBeGreaterThanOrEqual(0)
})
})

it('should get accountHistoryCount with no_rewards option', async () => {
await waitForExpect(async () => {
const options: AccountHistoryCountOptions = {
no_rewards: true
}
const count = await client.account.historyCount('mine', options)

expect(typeof count).toBe('number')
expect(count).toBeGreaterThanOrEqual(0)
})
})

it('should get accountHistoryCount with token option', async () => {
await waitForExpect(async () => {
const options1: AccountHistoryCountOptions = {
token: 'DBTC'
}
const options2: AccountHistoryCountOptions = {
token: 'DETH'
}
const countWithDBTC = await client.account.historyCount('mine', options1)
const countWithDETH = await client.account.historyCount('mine', options2)

expect(typeof countWithDBTC).toBe('number')
expect(typeof countWithDETH).toBe('number')
expect(countWithDBTC).toStrictEqual(5)
expect(countWithDETH).toStrictEqual(3)
})
})

it('should get accountHistory with txtype option', async () => {
await waitForExpect(async () => {
const options: AccountHistoryCountOptions = {
txtype: TxType.MINT_TOKEN
}
const count = await client.account.historyCount('mine', options)

expect(typeof count).toBe('number')
expect(count).toBeGreaterThanOrEqual(0)
})
})

it('should get different count for different txtypes', async () => {
await waitForExpect(async () => {
const options1: AccountHistoryCountOptions = {
txtype: TxType.MINT_TOKEN
}
const options2: AccountHistoryCountOptions = {
txtype: TxType.POOL_SWAP

}
const count1 = await client.account.historyCount('mine', options1)
const count2 = await client.account.historyCount('mine', options2)

expect(count1 === count2).toStrictEqual(false)
})
})
})
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ContainerAdapterClient } from '../../container_adapter_client'
import { RpcApiError } from '../../../src'
import { BalanceTransferPayload, UTXO } from '../../../src/category/account'

describe('masternode', () => {
describe('Account', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

Expand Down Expand Up @@ -60,46 +60,44 @@ describe('masternode', () => {
return to
}

describe('accountToAccount', () => {
it('should accountToAccount', async () => {
const payload: BalanceTransferPayload = {}
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DBTC'
payload[await container.getNewAddress()] = '5@DETH'
it('should accountToAccount', async () => {
const payload: BalanceTransferPayload = {}
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DBTC'
payload[await container.getNewAddress()] = '5@DETH'

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

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

it('should not accountToAccount for DFI coin if does not own the recipient address', async () => {
const promise = client.account.accountToAccount(from, { '2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz': '5@DFI' })
it('should not accountToAccount for DFI coin if does not own the recipient address', async () => {
const promise = client.account.accountToAccount(from, { '2Mywjs9zEU4NtLknXQJZgozaxMvPn2Bb3qz': '5@DFI' })

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

it('should accountToAccount with utxos', async () => {
const { txid } = await container.fundAddress(from, 10)
it('should accountToAccount with utxos', async () => {
const { txid } = await container.fundAddress(from, 10)

const payload: BalanceTransferPayload = {}
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DBTC'
payload[await container.getNewAddress()] = '5@DETH'
const payload: BalanceTransferPayload = {}
payload[await container.getNewAddress()] = '5@DFI'
payload[await container.getNewAddress()] = '5@DBTC'
payload[await container.getNewAddress()] = '5@DETH'

const utxos = await container.call('listunspent')
const inputs: UTXO[] = utxos.filter((utxo: UTXO) => utxo.txid === txid).map((utxo: UTXO) => {
return {
txid: utxo.txid,
vout: utxo.vout
}
})
const utxos = await container.call('listunspent')
const inputs: UTXO[] = utxos.filter((utxo: UTXO) => utxo.txid === txid).map((utxo: UTXO) => {
return {
txid: utxo.txid,
vout: utxo.vout
}
})

const data = await client.account.accountToAccount(from, payload, { utxos: inputs })
const data = await client.account.accountToAccount(from, payload, { utxos: inputs })

expect(typeof data).toStrictEqual('string')
expect(data.length).toStrictEqual(64)
})
expect(typeof data).toStrictEqual('string')
expect(data.length).toStrictEqual(64)
})
})
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { MasterNodeRegTestContainer } from '@defichain/testcontainers'
import { ContainerAdapterClient } from '../../container_adapter_client'
import waitForExpect from 'wait-for-expect'

describe('Account', () => {
const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
await container.waitForWalletBalanceGTE(100)
await createToken(await container.getNewAddress(), 'DBTC', 200)
})

afterAll(async () => {
await container.stop()
})

async function createToken (address: string, symbol: string, amount: number): Promise<void> {
const metadata = {
symbol,
name: symbol,
isDAT: true,
mintable: true,
tradeable: true,
collateralAddress: address
}
await container.call('createtoken', [metadata])
await container.generate(1)

await container.call('minttokens', [`${amount.toString()}@${symbol}`])
await container.generate(1)
}

it('should getAccount', async () => {
const accounts = await client.account.listAccounts()

const account = await client.account.getAccount(accounts[0].owner.addresses[0])
expect(account.length).toBeGreaterThan(0)
for (let i = 0; i < account.length; i += 1) {
expect(typeof account[i]).toStrictEqual('string')
}
})

it('should getAccount with pagination start and including_start', async () => {
let accounts: any[] = []
let beforeAccountCount = 0

await waitForExpect(async () => {
accounts = await client.account.listAccounts()
expect(accounts.length).toBeGreaterThan(0)

const account = await client.account.getAccount(accounts[0].owner.addresses[0])
beforeAccountCount = account.length
})

const pagination = {
start: beforeAccountCount,
including_start: true
}

const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination)
expect(account.length).toStrictEqual(1)

for (let i = 0; i < account.length; i += 1) {
expect(typeof account[i]).toStrictEqual('string')
}
})

it('should getAccount with pagination.limit', async () => {
const accounts = await client.account.listAccounts()

const pagination = {
limit: 1
}
const account = await client.account.getAccount(accounts[0].owner.addresses[0], pagination)
expect(account.length).toStrictEqual(1)
})

it('should getAccount with indexedAmount true', async () => {
const accounts = await client.account.listAccounts()

const account = await client.account.getAccount(accounts[0].owner.addresses[0], {}, { indexedAmounts: true })
expect(typeof account).toStrictEqual('object')
for (const k in account) {
expect(typeof account[k]).toStrictEqual('number')
}
})
})
Loading

0 comments on commit 8f2be4b

Please sign in to comment.