diff --git a/packages/jellyfish-api-core/__tests__/category/account/transferDomain.test.ts b/packages/jellyfish-api-core/__tests__/category/account/transferDomain.test.ts index 629f38389b..693414cb31 100644 --- a/packages/jellyfish-api-core/__tests__/category/account/transferDomain.test.ts +++ b/packages/jellyfish-api-core/__tests__/category/account/transferDomain.test.ts @@ -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) @@ -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' }]) @@ -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 () => { @@ -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 {