From 06291092ad8e9b1dc967e1b6a521369140410db9 Mon Sep 17 00:00:00 2001 From: Fuxing Loh <4266087+fuxingloh@users.noreply.github.com> Date: Wed, 2 Jun 2021 15:16:15 +0800 Subject: [PATCH] Update `@defichain/testcontainers` with chainparams QOL changes (#329) --- package.json | 2 +- .../category/blockchain/getBlock.test.ts | 2 +- .../category/blockchain/getBlockHash.test.ts | 12 +-- .../category/blockchain/getTxOut.test.ts | 48 +++++++++-- .../txn/txn_builder_appoint_oracle.test.ts | 84 ++++++++++++++----- ..._builder_liq_pool_remove_liquidity.test.ts | 2 +- .../txn/txn_builder_remove_oracle.test.ts | 4 +- .../txn/txn_builder_set_oracle_data.test.ts | 8 +- .../txn/txn_builder_update_oracle.test.ts | 6 +- .../src/chains/defid_container.ts | 2 +- packages/testing/__tests__/account.test.ts | 8 +- 11 files changed, 128 insertions(+), 50 deletions(-) diff --git a/package.json b/package.json index a447fa5136..1fedc13c9a 100644 --- a/package.json +++ b/package.json @@ -27,7 +27,7 @@ "publish:next": "lerna exec -- npm publish --tag next --access public", "publish:latest": "lerna exec -- npm publish --tag latest --access public", "standard": "ts-standard --fix", - "test": "jest", + "test": "jest --maxWorkers=100%", "test:ci": "jest --ci --coverage --forceExit --maxWorkers=4", "all": "npm run build && npm run standard && npm run test" }, diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain/getBlock.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain/getBlock.test.ts index 4bfb976f0a..041bcb1b93 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain/getBlock.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain/getBlock.test.ts @@ -79,7 +79,7 @@ describe('Block', () => { expect(block.tx[0].vin[0].sequence).toBeGreaterThanOrEqual(4294967295) expect(block.tx[0].vout[0].n).toStrictEqual(0) - expect(block.tx[0].vout[0].value.toString(10)).toStrictEqual('38') + expect(block.tx[0].vout[0].value.toString(10)).toStrictEqual('76') expect(block.tx[0].vout[0].value instanceof BigNumber).toBeTruthy() expect(block.tx[0].vout[0].scriptPubKey.asm).toStrictEqual('OP_DUP OP_HASH160 b36814fd26190b321aa985809293a41273cfe15e OP_EQUALVERIFY OP_CHECKSIG') diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain/getBlockHash.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain/getBlockHash.test.ts index 7fb357d2f6..61ee15fbbb 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain/getBlockHash.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain/getBlockHash.test.ts @@ -1,6 +1,5 @@ import { MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' -import waitForExpect from 'wait-for-expect' describe('BlockHash', () => { const container = new MasterNodeRegTestContainer() @@ -9,6 +8,7 @@ describe('BlockHash', () => { beforeAll(async () => { await container.start() await container.waitForReady() + await container.waitForBlock(1) }) afterAll(async () => { @@ -16,13 +16,13 @@ describe('BlockHash', () => { }) it('should getBlockHash', async () => { - await waitForExpect(async () => { - const info = await client.blockchain.getBlockchainInfo() - expect(info.blocks).toBeGreaterThan(1) - }) - const blockHash: string = await client.blockchain.getBlockHash(1) expect(typeof blockHash).toStrictEqual('string') expect(blockHash.length).toStrictEqual(64) }) + + it('should get regtest genesis block hash', async () => { + const genesisHash = await client.blockchain.getBlockHash(0) + expect(genesisHash).toStrictEqual('d744db74fb70ed42767ae028a129365fb4d7de54ba1b6575fb047490554f8a7b') + }) }) diff --git a/packages/jellyfish-api-core/__tests__/category/blockchain/getTxOut.test.ts b/packages/jellyfish-api-core/__tests__/category/blockchain/getTxOut.test.ts index 73af39bb52..86b3ba0c56 100644 --- a/packages/jellyfish-api-core/__tests__/category/blockchain/getTxOut.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/blockchain/getTxOut.test.ts @@ -1,4 +1,4 @@ -import { MasterNodeRegTestContainer } from '@defichain/testcontainers' +import { GenesisKeys, MasterNodeRegTestContainer } from '@defichain/testcontainers' import { ContainerAdapterClient } from '../../container_adapter_client' import { BigNumber } from '../../../src' @@ -9,25 +9,63 @@ describe('TxOut', () => { beforeAll(async () => { await container.start() await container.waitForReady() + await container.waitForBlock(1) }) afterAll(async () => { await container.stop() }) - it('should getTxOut', async () => { - const txId = '00eed320c213f506038fa29f77d4d2535232fa97b7789ff6fb516c63201c5e44' + it('should getTxOut of regtest 100M DFI - 0', async () => { + const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7' const txOut = await client.blockchain.getTxOut(txId, 0) + expect(txOut).toHaveProperty('bestblock') expect(txOut.confirmations).toBeGreaterThanOrEqual(1) expect(txOut.value instanceof BigNumber).toStrictEqual(true) - expect(txOut.value.toString()).toStrictEqual('38') + expect(txOut.value.toString()).toStrictEqual('100000000') expect(txOut.scriptPubKey).toHaveProperty('asm') expect(txOut.scriptPubKey).toHaveProperty('hex') expect(txOut.scriptPubKey.reqSigs).toBeGreaterThanOrEqual(1) expect(txOut.scriptPubKey.type).toStrictEqual('pubkeyhash') expect(txOut.scriptPubKey.addresses.length).toBeGreaterThanOrEqual(1) - expect(txOut.scriptPubKey.addresses[0]).toStrictEqual('mwsZw8nF7pKxWH8eoKL9tPxTpaFkz7QeLU') + expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[0].owner.address) + expect(txOut.coinbase).toStrictEqual(true) + }) + + it('should getTxOut of regtest 100M DFI - 1', async () => { + const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7' + const txOut = await client.blockchain.getTxOut(txId, 1) + + expect(txOut.value.toString()).toStrictEqual('100000000') + expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[0].operator.address) + expect(txOut.coinbase).toStrictEqual(true) + }) + + it('should getTxOut of regtest 100M DFI - 2', async () => { + const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7' + const txOut = await client.blockchain.getTxOut(txId, 2) + + expect(txOut.value.toString()).toStrictEqual('100000000') + expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[1].owner.address) + expect(txOut.coinbase).toStrictEqual(true) + }) + + it('should getTxOut of regtest 100M DFI - 3', async () => { + const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7' + const txOut = await client.blockchain.getTxOut(txId, 3) + + expect(txOut.value.toString()).toStrictEqual('100000000') + expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[1].operator.address) + expect(txOut.coinbase).toStrictEqual(true) + }) + + it('should getTxOut of regtest 100M DFI - 4', async () => { + const txId = '9fb9c46b1d12dae8a4a35558f7ef4b047df3b444b1ead61d334e4f187f5f58b7' + const txOut = await client.blockchain.getTxOut(txId, 4) + + expect(txOut.value.toString()).toStrictEqual('100') + expect(txOut.scriptPubKey.addresses[0]).toStrictEqual(GenesisKeys[5].owner.address) expect(txOut.coinbase).toStrictEqual(true) }) }) diff --git a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_appoint_oracle.test.ts b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_appoint_oracle.test.ts index 00a277ae08..13679d381d 100644 --- a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_appoint_oracle.test.ts +++ b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_appoint_oracle.test.ts @@ -4,37 +4,37 @@ import { P2WPKHTransactionBuilder } from '../../src' import { calculateTxid, fundEllipticPair, sendTransaction } from '../test.utils' import { WIF } from '@defichain/jellyfish-crypto' -const container = new MasterNodeRegTestContainer() -let providers: MockProviders -let builder: P2WPKHTransactionBuilder +describe('appoint oracle', () => { + const container = new MasterNodeRegTestContainer() + let providers: MockProviders + let builder: P2WPKHTransactionBuilder -beforeAll(async () => { - await container.start() - await container.waitForReady() - await container.waitForWalletCoinbaseMaturity() + beforeAll(async () => { + await container.start() + await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() - providers = await getProviders(container) - providers.setEllipticPair(WIF.asEllipticPair(GenesisKeys[GenesisKeys.length - 1].owner.privKey)) - builder = new P2WPKHTransactionBuilder(providers.fee, providers.prevout, providers.elliptic) + providers = await getProviders(container) + providers.setEllipticPair(WIF.asEllipticPair(GenesisKeys[GenesisKeys.length - 1].owner.privKey)) + builder = new P2WPKHTransactionBuilder(providers.fee, providers.prevout, providers.elliptic) - // Prep 1000 DFI Token for testing - await container.waitForWalletBalanceGTE(1001) -}) + // Prep 1000 DFI Token for testing + await container.waitForWalletBalanceGTE(1001) + }) -afterAll(async () => { - await container.stop() -}) + afterAll(async () => { + await container.stop() + }) -describe('appoint oracle', () => { beforeEach(async () => { - await container.waitForWalletBalanceGTE(1) - }) + await container.waitForWalletBalanceGTE(11) - it('should appoint oracle(s)', async () => { // Fund 10 DFI UTXO await fundEllipticPair(container, providers.ellipticPair, 10) await providers.setupMocks() // required to move utxos + }) + it('should appoint oracle(s)', async () => { // Appoint Oracle const script = await providers.elliptic.script() const txn = await builder.oracles.appointOracle({ @@ -72,11 +72,53 @@ describe('appoint oracle', () => { expect(getOracleDataResult.priceFeeds[0].currency).toStrictEqual('USD') }) - it('should appoint oracle with multiple currencies', async () => { + it('should reject invalid appoint oracle arg - weightage over 100', async () => { + // Appoint Oracle + const script = await providers.elliptic.script() + await expect(builder.oracles.appointOracle({ + script: script, + weightage: 200, + priceFeeds: [ + { + token: 'TEST', + currency: 'USD' + } + ] + }, script)).rejects.toThrow('Conversion input `appointOracle.weightage` must be above `0` and below `101`') + }) +}) + +describe('appoint oracle', () => { + const container = new MasterNodeRegTestContainer() + let providers: MockProviders + let builder: P2WPKHTransactionBuilder + + beforeAll(async () => { + await container.start() + await container.waitForReady() + await container.waitForWalletCoinbaseMaturity() + + providers = await getProviders(container) + providers.setEllipticPair(WIF.asEllipticPair(GenesisKeys[GenesisKeys.length - 1].owner.privKey)) + builder = new P2WPKHTransactionBuilder(providers.fee, providers.prevout, providers.elliptic) + + // Prep 1000 DFI Token for testing + await container.waitForWalletBalanceGTE(1001) + }) + + afterAll(async () => { + await container.stop() + }) + + beforeEach(async () => { + await container.waitForWalletBalanceGTE(11) + // Fund 10 DFI UTXO await fundEllipticPair(container, providers.ellipticPair, 10) await providers.setupMocks() // required to move utxos + }) + it('should appoint oracle with multiple currencies', async () => { // Appoint Oracle const script = await providers.elliptic.script() const txn = await builder.oracles.appointOracle({ diff --git a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_liq_pool_remove_liquidity.test.ts b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_liq_pool_remove_liquidity.test.ts index a63b8fb39d..613b7f0b31 100644 --- a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_liq_pool_remove_liquidity.test.ts +++ b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_liq_pool_remove_liquidity.test.ts @@ -48,7 +48,6 @@ afterAll(async () => { beforeEach(async () => { await providers.randomizeEllipticPair() - await providers.setupMocks() // required to move utxos builder = new P2WPKHTransactionBuilder(providers.fee, providers.prevout, providers.elliptic) await container.waitForWalletBalanceGTE(1) @@ -64,6 +63,7 @@ beforeEach(async () => { // Fund 1 DFI UTXOS for fee await fundEllipticPair(container, providers.ellipticPair, 1) + await providers.setupMocks() // required to move utxos // Ensure starting balances await container.generate(1) diff --git a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_remove_oracle.test.ts b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_remove_oracle.test.ts index fa49239a84..093a2a258a 100644 --- a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_remove_oracle.test.ts +++ b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_remove_oracle.test.ts @@ -28,13 +28,13 @@ afterAll(async () => { describe('remove oracle', () => { beforeEach(async () => { await container.waitForWalletBalanceGTE(1) - }) - it('should appoint and then remove oracle', async () => { // Fund 10 DFI UTXO await fundEllipticPair(container, providers.ellipticPair, 10) await providers.setupMocks() // required to move utxos + }) + it('should appoint and then remove oracle', async () => { // Appoint Oracle const script = await providers.elliptic.script() const appointTxn = await builder.oracles.appointOracle({ diff --git a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_set_oracle_data.test.ts b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_set_oracle_data.test.ts index f9d73c138e..745bf321cb 100644 --- a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_set_oracle_data.test.ts +++ b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_set_oracle_data.test.ts @@ -28,14 +28,14 @@ afterAll(async () => { describe('set oracle data', () => { beforeEach(async () => { - await container.waitForWalletBalanceGTE(1) - }) + await container.waitForWalletBalanceGTE(11) - it('should appoint and then set oracle data', async () => { // Fund 10 DFI UTXO await fundEllipticPair(container, providers.ellipticPair, 10) await providers.setupMocks() // required to move utxos + }) + it('should appoint and then set oracle data', async () => { // Appoint Oracle const script = await providers.elliptic.script() const appointTxn = await builder.oracles.appointOracle({ @@ -93,5 +93,3 @@ describe('set oracle data', () => { expect(getOracleDataResult.tokenPrices[0].timestamp).toStrictEqual(1621567932) }) }) - -// TODO(monstrobishi): test account state once RPC calls are in place diff --git a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_update_oracle.test.ts b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_update_oracle.test.ts index 05733ab7f8..3d4cd674c7 100644 --- a/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_update_oracle.test.ts +++ b/packages/jellyfish-transaction-builder/__tests__/txn/txn_builder_update_oracle.test.ts @@ -29,14 +29,14 @@ afterAll(async () => { describe('update oracle', () => { beforeEach(async () => { - await container.waitForWalletBalanceGTE(1) - }) + await container.waitForWalletBalanceGTE(11) - it('should appoint and then update oracle', async () => { // Fund 10 DFI UTXO await fundEllipticPair(container, providers.ellipticPair, 10) await providers.setupMocks() // required to move utxos + }) + it('should appoint and then update oracle', async () => { // Appoint Oracle const script = await providers.elliptic.script() const appointTxn = await builder.oracles.appointOracle({ diff --git a/packages/testcontainers/src/chains/defid_container.ts b/packages/testcontainers/src/chains/defid_container.ts index 2b618f0f26..0a4a68cea0 100644 --- a/packages/testcontainers/src/chains/defid_container.ts +++ b/packages/testcontainers/src/chains/defid_container.ts @@ -22,7 +22,7 @@ export interface StartOptions { export abstract class DeFiDContainer extends DockerContainer { /* eslint-disable @typescript-eslint/no-non-null-assertion, no-void */ public static readonly PREFIX = 'defichain-testcontainers-' - public static readonly image = 'defi/defichain:1.7.3' + public static readonly image = 'defi/defichain:HEAD-643194e' // fast-tracked https://github.com/DeFiCh/ain/pull/464 >1.7.3 public static readonly DefaultStartOptions = { user: 'testcontainers-user', diff --git a/packages/testing/__tests__/account.test.ts b/packages/testing/__tests__/account.test.ts index a46789a89b..ba82c9c228 100644 --- a/packages/testing/__tests__/account.test.ts +++ b/packages/testing/__tests__/account.test.ts @@ -12,7 +12,7 @@ beforeAll(async () => { await container.start() await container.waitForReady() await container.waitForWalletCoinbaseMaturity() - await container.waitForWalletBalanceGTE(300) + await container.waitForWalletBalanceGTE(1000000) }) afterAll(async () => { @@ -22,12 +22,12 @@ afterAll(async () => { describe('utxosToAccount', () => { it('should utxosToAccount', async () => { const balanceBefore = await container.call('getbalance') - expect(balanceBefore).toBeGreaterThanOrEqual(300) + expect(balanceBefore).toBeGreaterThanOrEqual(1000000) - await utxosToAccount(container, 100) + await utxosToAccount(container, 1000000) const balanceAfter = await container.call('getbalance') - expect(balanceAfter).toBeLessThan(300) + expect(balanceAfter).toBeLessThan(balanceBefore - 1000000 + 1000) // extra 1000 due to block gen }) })