Skip to content

Commit

Permalink
merge branch main into jouzo/spv_listhtlcoutputs
Browse files Browse the repository at this point in the history
  • Loading branch information
Jouzo committed Aug 16, 2021
2 parents f962604 + 553e8f7 commit b0b2993
Show file tree
Hide file tree
Showing 41 changed files with 1,087 additions and 66 deletions.
3 changes: 2 additions & 1 deletion .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

/.github/ @fuxingloh
/.husky/ @fuxingloh
/.idea/ @fuxingloh @jingyi2811 @thedoublejay

/packages/jellyfish/ @fuxingloh @canonbrother
/packages/jellyfish-address/ @fuxingloh @ivan-zynesis
Expand All @@ -20,6 +19,8 @@
/packages/jellyfish-json/ @fuxingloh @canonbrother
/packages/jellyfish-network/ @fuxingloh @ivan-zynesis

/packages/jellyfish-testing/ @fuxingloh @canonbrother @jingyi2811 @surangap @Jouzo

/packages/jellyfish-transaction/ @fuxingloh @ivan-zynesis @canonbrother @monstrobishi @surangap @Jouzo @jingyi2811
/packages/jellyfish-transaction-builder/ @fuxingloh @ivan-zynesis @canonbrother @monstrobishi @surangap @Jouzo @jingyi2811
/packages/jellyfish-transaction-signature/ @fuxingloh @ivan-zynesis @surangap
Expand Down
1 change: 1 addition & 0 deletions .github/governance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ issue:
- jellyfish-crypto
- jellyfish-json
- jellyfish-network
- jellyfish-testing
- jellyfish-transaction
- jellyfish-transaction-builder
- jellyfish-transaction-signature
Expand Down
5 changes: 5 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ labels:
matcher:
files: "packages/jellyfish-network/**"

- label: area/jellyfish-testing
sync: true
matcher:
files: "packages/jellyfish-testing/**"

- label: area/jellyfish-transaction
sync: true
matcher:
Expand Down
2 changes: 2 additions & 0 deletions .github/labels.yml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
name: area/jellyfish-json
- color: fbca04
name: area/jellyfish-network
- color: fbca04
name: area/jellyfish-testing
- color: fbca04
name: area/jellyfish-transaction
- color: fbca04
Expand Down
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,16 @@ Package | Description
`@defichain/jellyfish-crypto` | Cryptography operations for jellyfish, includes a simple 'secp256k1' EllipticPair.
`@defichain/jellyfish-json` | Allows parsing of JSON with 'lossless', 'bignumber' and 'number' numeric precision.
`@defichain/jellyfish-network` | Contains DeFi blockchain various network configuration for mainnet, testnet and regtest.
`@defichain/jellyfish-testing` | Provides many abstractions for various commonly used setup pattern for DeFi blockchain.
`@defichain/jellyfish-transaction` | Dead simple modern stateless raw transaction composer for the DeFi Blockchain.
`@defichain/jellyfish-transaction-builder` | Provides a high-high level abstraction for constructing transaction ready to be broadcast for DeFi Blockchain.
`@defichain/jellyfish-wallet-classic` | WalletClassic implements a simple, single elliptic pair wallet.
`@defichain/jellyfish-transaction-signature` | Stateless utility library to perform transaction signing.
`@defichain/jellyfish-wallet` | Jellyfish wallet is a managed wallet, where account can get discovered from an HD seed.
`@defichain/jellyfish-wallet-classic` | WalletClassic implements a simple, single elliptic pair wallet.
`@defichain/jellyfish-wallet-encrypted` | Library to encrypt MnemonicHdNode as EncryptedMnemonicHdNode. Able to perform as MnemonicHdNode with passphrase known.
`@defichain/jellyfish-wallet-mnemonic` | MnemonicHdNode implements the WalletHdNode from jellyfish-wallet; a CoinType-agnostic HD Wallet for noncustodial DeFi.
`@defichain/testcontainers` | Provides a lightweight, throw away instances for DeFiD node provisioned automatically in a Docker container.
`@defichain/testing` | Provides rich test fixture setup functions for effective and effortless testing.
~~@defichain/testing~~ | Provides rich test fixture setup functions for effective and effortless testing.

## Developing & Contributing

Expand Down
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ module.exports = {
'@defichain/jellyfish-wallet-mnemonic': '<rootDir>/packages/jellyfish-wallet-mnemonic/src',
'@defichain/jellyfish-wallet': '<rootDir>/packages/jellyfish-wallet/src',
'@defichain/testcontainers': '<rootDir>/packages/testcontainers/src',
'@defichain/testing': '<rootDir>/packages/testing/src'
'@defichain/testing': '<rootDir>/packages/testing/src',
'@defichain/jellyfish-testing': '<rootDir>/packages/jellyfish-testing/src'
},
verbose: true,
clearMocks: true,
Expand Down
40 changes: 33 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
},
"devDependencies": {
"@size-limit/preset-app": "^5.0.2",
"@types/jest": "^26.0.24",
"@types/jest": "^27.0.0",
"husky": "^7.0.1",
"jest": "^27.0.6",
"lerna": "^4.0.0",
Expand Down
6 changes: 6 additions & 0 deletions packages/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

56 changes: 56 additions & 0 deletions packages/.idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,16 @@ import {
utxosToAccount
} from '@defichain/testing'
import { RpcApiError } from '../../../src'
import { PoolSwapMetadata, PoolPairInfo } from '@defichain/jellyfish-api-core/category/poolpair'
import { PoolSwapMetadata } from '@defichain/jellyfish-api-core/category/poolpair'
import { Testing } from '@defichain/jellyfish-testing'

describe('poolSwap', () => {
const container = new MasterNodeRegTestContainer()
const testing = Testing.create(container)
const client = new ContainerAdapterClient(container)

beforeAll(async () => {
await container.start()
await container.waitForReady()
await container.waitForWalletCoinbaseMaturity()
})

Expand All @@ -27,24 +28,15 @@ describe('poolSwap', () => {
})

it('should poolSwap', async () => {
const addressReceiver = await getNewAddress(container)
const tokenAddress = await getNewAddress(container)
const dfiAddress = await getNewAddress(container)
const poolLiquidityAddress = await getNewAddress(container)

await createToken(container, 'CAT', { collateralAddress: tokenAddress })
await utxosToAccount(container, 1200, { address: dfiAddress })
await mintTokens(container, 'CAT', { address: dfiAddress })
await createPoolPair(container, 'CAT', 'DFI')

await addPoolLiquidity(container, {
tokenA: 'CAT',
amountA: 1000,
tokenB: 'DFI',
amountB: 500,
shareAddress: poolLiquidityAddress
const poolPairBefore = await testing.fixture.createPoolPair({
a: { amount: 1000, symbol: 'CAT' },
b: { amount: 500, symbol: 'DFI' }
})

const [addressReceiver, dfiAddress] = await testing.generateAddress(2)
await testing.token.dfi({ amount: 700, address: dfiAddress })
await testing.generate(1)

const metadata: PoolSwapMetadata = {
from: dfiAddress,
tokenFrom: 'DFI',
Expand All @@ -53,24 +45,22 @@ describe('poolSwap', () => {
tokenTo: 'CAT'
}

const poolpairResultBefore = Object.values(await container.call('getpoolpair', ['CAT-DFI']))[0] as PoolPairInfo

const hex = await client.poolpair.poolSwap(metadata)
expect(typeof hex).toStrictEqual('string')
expect(hex.length).toStrictEqual(64)

await container.generate(1)

const poolpairResultAfter = Object.values(await container.call('getpoolpair', ['CAT-DFI']))[0] as PoolPairInfo
const reserveBAfter = new BigNumber(poolpairResultBefore.reserveB).plus(555) // 1055
const reserveAAfter = new BigNumber(poolpairResultBefore.totalLiquidity).pow(2).div(reserveBAfter) // 473.93364928032265610654
const poolPairAfter = await testing.poolpair.get('CAT-DFI')
const reserveBAfter = new BigNumber(poolPairBefore.reserveB).plus(555) // 1055
const reserveAAfter = new BigNumber(poolPairBefore.totalLiquidity).pow(2).div(reserveBAfter) // 473.93364928032265610654

expect(new BigNumber(poolpairResultAfter.reserveB)).toStrictEqual(reserveBAfter)
expect(poolpairResultAfter.reserveA.toFixed(8)).toStrictEqual(reserveAAfter.toFixed(8))
expect(new BigNumber(poolPairAfter.reserveB)).toStrictEqual(reserveBAfter)
expect(poolPairAfter.reserveA.toFixed(8)).toStrictEqual(reserveAAfter.toFixed(8))

const accountReceiver = (await client.account.getAccount(addressReceiver))[0]
const accountReceiverBalance = new BigNumber(accountReceiver.split('@')[0]) // 526.06635072
const amountReceived = new BigNumber(poolpairResultBefore.reserveA).minus(reserveAAfter) // 526.06635071967734389346
const amountReceived = new BigNumber(poolPairBefore.reserveA).minus(reserveAAfter) // 526.06635071967734389346

expect(accountReceiverBalance.toFixed(8)).toStrictEqual(amountReceived.toFixed(8))
})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import { /** GenesisKeys, */ MasterNodeRegTestContainer /** ContainerGroup */ } from '@defichain/testcontainers'
import { RpcApiError } from '@defichain/jellyfish-api-core'
import { ContainerAdapterClient } from '../../container_adapter_client'

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

beforeAll(async () => {
await container.start()
await container.call('spv_fundaddress', [await container.call('spv_getnewaddress')]) // Funds 1 BTC
})

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

it('should getHtlcSeed', async () => {
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')])
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')])
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10'])

await container.call('spv_sendtoaddress', [htlc.address, 0.1]) // Funds HTLC address
await container.call('spv_claimhtlc', [htlc.address, await container.call('spv_getnewaddress'), htlc.seed]) // claim HTLC

const secret = await client.spv.getHtlcSeed(htlc.address)
expect(secret).toStrictEqual(htlc.seed)
})

it('should getHtlcSeed with empty secret before spv_claimhtlc', async () => {
const pubKeyA = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')])
const pubKeyB = await container.call('spv_getaddresspubkey', [await container.call('spv_getnewaddress')])
const htlc = await container.call('spv_createhtlc', [pubKeyA, pubKeyB, '10'])

const secret = await client.spv.getHtlcSeed(htlc.address)
expect(secret).toStrictEqual('')
})

it('should not getHtlcSeed with invalid public key as receiverPubKey', async () => {
const promise = client.spv.getHtlcSeed('XXXX')
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow("RpcApiError: 'Error: Invalid address', code: -5, method: spv_gethtlcseed")
})
})

describe('Spv with ContainerGroup', () => {
// const group = new ContainerGroup([
// new MasterNodeRegTestContainer(GenesisKeys[0]),
// new MasterNodeRegTestContainer(GenesisKeys[1])
// ])
// const client0 = new ContainerAdapterClient(group.get(0))
// const client1 = new ContainerAdapterClient(group.get(1))

// beforeAll(async () => {
// // await group.start()
// // await group.get(0).call('spv_fundaddress', [await group.get(0).call('spv_getnewaddress')]) // Funds 1 BTC
// })

// afterAll(async () => {
// // await group.stop()
// })

it.skip('should getHtlcSeed in multi node setup', async () => {
// TODO
})
})
Loading

0 comments on commit b0b2993

Please sign in to comment.