Skip to content

Commit

Permalink
add td rpc singlekeycheck test
Browse files Browse the repository at this point in the history
  • Loading branch information
canonbrother committed Nov 27, 2023
1 parent 4f45556 commit 76c9a82
Showing 1 changed file with 226 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ import { RpcApiError } from '@defichain/jellyfish-api-core'
import BigNumber from 'bignumber.js'

describe('TransferDomain', () => {
let dvmAddr: string, evmAddr: string, p2shAddr: string
let dvmAddr: string
let evmAddr: string
let legacyAddr: string
let legacyErc55Addr: string
let p2shAddr: string

const container = new MasterNodeRegTestContainer()
const client = new ContainerAdapterClient(container)

Expand Down Expand Up @@ -33,6 +38,10 @@ describe('TransferDomain', () => {
dvmAddr = await container.getNewAddress('address1', 'legacy')
evmAddr = await container.getNewAddress('erc55', 'erc55')

// same key address
legacyAddr = await container.getNewAddress('legacy', 'legacy')
legacyErc55Addr = (await container.call('addressmap', [legacyAddr, 1])).format.erc55

p2shAddr = await container.getNewAddress('address', 'p2sh-segwit')

await container.call('utxostoaccount', [{ [dvmAddr]: '100000@0' }])
Expand Down Expand Up @@ -724,6 +733,25 @@ describe('TransferDomain', () => {
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('TransferDomain currently only supports a single transfer per transaction')
})

it('(dvm -> evm) should fail as different key', async () => {
const promise = client.account.transferDomain([
{
src: {
address: dvmAddr,
amount: '3@DFI',
domain: TransferDomainType.DVM
},
dst: {
address: evmAddr,
amount: '3@DFI',
domain: TransferDomainType.EVM
}
}
])
await expect(promise).rejects.toThrow(RpcApiError)
await expect(promise).rejects.toThrow('Dst address does not match source key')
})
})

it('(dvm -> evm) should transfer domain - DFI', async () => {
Expand Down Expand Up @@ -928,6 +956,203 @@ describe('TransferDomain', () => {
expect(new BigNumber(dvmBalance1))
.toStrictEqual(new BigNumber(dvmBalance0).plus(3))
})

it('(dvm -> evm) should transfer domain - DFI - singleKeyCheck', async () => {
await client.account.accountToAccount(dvmAddr, { [legacyAddr]: '2@DFI' })
await container.generate(1)

const legacyAcc = await getAccountValues(client, legacyAddr)
const tokenId = 'DFI'
const legacyBalance0 = legacyAcc[tokenId]
const prevBalance = await getEVMBalances(client)

await client.account.transferDomain([
{
src: {
address: legacyAddr,
amount: '1@DFI',
domain: TransferDomainType.DVM
},
dst: {
address: legacyErc55Addr,
amount: '1@DFI',
domain: TransferDomainType.EVM
}
}
])
await container.generate(1)

const legacyAcc1 = await getAccountValues(client, legacyAddr)
const legacyBalance1 = legacyAcc1[tokenId]

// check: dvm balance is transferred
expect(new BigNumber(legacyBalance0))
.toStrictEqual(new BigNumber(legacyBalance1).plus(1))

// check: evm balance = dvm balance - transferred
const currentBalance = await getEVMBalances(client)
expect(new BigNumber(prevBalance))
.toStrictEqual(new BigNumber(currentBalance).minus(1))
})

it('(evm -> dvm) should transfer domain - DFI - singleKeyCheck', async () => {
const legacyAcc = await getAccountValues(client, legacyAddr)
const tokenId = 'DFI'
const legacyBalance0 = legacyAcc[tokenId]
const prevBalance = await getEVMBalances(client)

await client.account.transferDomain([
{
src: {
address: legacyErc55Addr,
amount: '1@DFI',
domain: TransferDomainType.EVM
},
dst: {
address: legacyAddr,
amount: '1@DFI',
domain: TransferDomainType.DVM
}
}
])
await container.generate(1)

const legacyAcc1 = await getAccountValues(client, legacyAddr)
const legacyBalance1 = legacyAcc1[tokenId]
expect(new BigNumber(legacyBalance0))
.toStrictEqual(new BigNumber(legacyBalance1).minus(1))

// check EVM balance
const currentBalance = await getEVMBalances(client)
expect(new BigNumber(prevBalance))
.toStrictEqual(new BigNumber(currentBalance).plus(1))
})

it('(dvm -> evm) should transfer domain - dToken - singleKeyCheck', async () => {
await client.account.accountToAccount(dvmAddr, { [legacyAddr]: '2@BTC' })
await container.generate(1)

const legacyAcc = await getAccountValues(client, legacyAddr)
const btcTokenId = 'BTC'
const legacyBalance = legacyAcc[btcTokenId]

await client.account.transferDomain([
{
src: {
address: legacyAddr,
amount: '1@BTC',
domain: TransferDomainType.DVM
},
dst: {
address: legacyErc55Addr,
amount: '1@BTC',
domain: TransferDomainType.EVM
}
}
])
await container.generate(1)

const legacyAcc1 = await getAccountValues(client, legacyAddr)
const legacyBalance1 = legacyAcc1[btcTokenId]

// check: BTC balance is transferred
expect(new BigNumber(legacyBalance1))
.toStrictEqual(new BigNumber(legacyBalance).minus(1))
})

it('(evm -> dvm) should transfer domain - dToken - singleKeyCheck', async () => {
const legacyAcc = await getAccountValues(client, legacyAddr)
const tokenId = 'BTC'
const legacyBalance = legacyAcc[tokenId]

await client.account.transferDomain([
{
src: {
address: legacyErc55Addr,
amount: '1@BTC',
domain: TransferDomainType.EVM
},
dst: {
address: legacyAddr,
amount: '1@BTC',
domain: TransferDomainType.DVM
}
}
])
await container.generate(1)

const legacyAcc1 = await getAccountValues(client, legacyAddr)
const legacyBalance1 = legacyAcc1[tokenId]
expect(new BigNumber(legacyBalance))
.toStrictEqual(new BigNumber(legacyBalance1).minus(1))

// check: BTC balance is transferred
expect(new BigNumber(legacyBalance1))
.toStrictEqual(new BigNumber(legacyBalance).plus(1))
})

it('(dvm -> evm) should transfer domain - loan token - singleKeyCheck', async () => {
await client.account.accountToAccount(dvmAddr, { [legacyAddr]: '2@AAPL' })
await container.generate(1)

const legacyAcc = await getAccountValues(client, legacyAddr)
const tokenId = 'AAPL'
const legacyBalance = legacyAcc[tokenId]

await client.account.transferDomain([
{
src: {
address: legacyAddr,
amount: '1@AAPL',
domain: TransferDomainType.DVM
},
dst: {
address: legacyErc55Addr,
amount: '1@AAPL',
domain: TransferDomainType.EVM
}
}
])
await container.generate(1)

const legacyAcc1 = await getAccountValues(client, legacyAddr)
const legacyBalance1 = legacyAcc1[tokenId]

// check: AAPL balance is transferred
expect(new BigNumber(legacyBalance1))
.toStrictEqual(new BigNumber(legacyBalance).minus(1))
})

it('(evm -> dvm) should transfer domain - loan token - singleKeyCheck', async () => {
const legacyAcc = await getAccountValues(client, legacyAddr)
const tokenId = 'AAPL'
const legacyBalance = legacyAcc[tokenId]

await client.account.transferDomain([
{
src: {
address: legacyErc55Addr,
amount: '1@AAPL',
domain: TransferDomainType.EVM
},
dst: {
address: legacyAddr,
amount: '1@AAPL',
domain: TransferDomainType.DVM
}
}
])
await container.generate(1)

const legacyAcc1 = await getAccountValues(client, legacyAddr)
const legacyBalance1 = legacyAcc1[tokenId]
expect(new BigNumber(legacyBalance))
.toStrictEqual(new BigNumber(legacyBalance1).minus(1))

// check: AAPL balance is transferred
expect(new BigNumber(legacyBalance1))
.toStrictEqual(new BigNumber(legacyBalance).plus(1))
})
})

async function getEVMBalances (client: ContainerAdapterClient): Promise<BigNumber> {
Expand Down

0 comments on commit 76c9a82

Please sign in to comment.