Skip to content

Commit

Permalink
refactor(e2e): burn tokens minted in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mirceahasegan committed Mar 12, 2023
1 parent 6cac383 commit dbaa22d
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 6 deletions.
46 changes: 44 additions & 2 deletions packages/e2e/test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,15 @@ import {
throwError,
timeout
} from 'rxjs';
import { InMemoryKeyAgent } from '@cardano-sdk/key-management';
import { InitializeTxProps, ObservableWallet, SignedTx, SingleAddressWallet, buildTx } from '@cardano-sdk/wallet';
import {
FinalizeTxProps,
InitializeTxProps,
ObservableWallet,
SignedTx,
SingleAddressWallet,
buildTx
} from '@cardano-sdk/wallet';
import { InMemoryKeyAgent, TransactionSigner } from '@cardano-sdk/key-management';
import { TestWallet, faucetProviderFactory, getEnv, networkInfoProviderFactory, walletVariables } from '../src';
import { assertTxIsValid } from '../../wallet/test/util';
import { logger } from '@cardano-sdk/util-dev';
Expand Down Expand Up @@ -261,3 +268,38 @@ export const createStandaloneKeyAgent = async (
},
{ bip32Ed25519, inputResolver: { resolveInput: async () => null }, logger }
);

/**
* Create burn transaction to cleanup minted assets in tests.
* Each test or test suite should call this function to remove any minted assets.
* Pass `tokens` with positive value. This method will negate them.
*/
export const burnTokens = async ({
wallet,
tokens,
scripts,
policySigners: extraSigners
}: {
wallet: SingleAddressWallet;
tokens: Cardano.TokenMap;
scripts: Cardano.Script[];
policySigners: TransactionSigner[];
}) => {
const negativeTokens = new Map([...tokens].map(([assetId, value]) => [assetId, -value]));
const txProps: InitializeTxProps = {
mint: negativeTokens,
scripts,
witness: { extraSigners }
};

const unsignedTx = await wallet.initializeTx(txProps);

const finalizeProps: FinalizeTxProps = {
scripts,
tx: unsignedTx,
witness: { extraSigners }
};

const signedTx = await wallet.finalizeTx(finalizeProps);
await submitAndConfirm(wallet, signedTx);
};
4 changes: 3 additions & 1 deletion packages/e2e/test/wallet/SingleAddressWallet/mint.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Cardano, nativeScriptPolicyId } from '@cardano-sdk/core';
import { FinalizeTxProps, InitializeTxProps, SingleAddressWallet } from '@cardano-sdk/wallet';
import { KeyRole, util } from '@cardano-sdk/key-management';
import { burnTokens, createStandaloneKeyAgent, submitAndConfirm, walletReady } from '../../util';
import { createLogger } from '@cardano-sdk/util-dev';
import { createStandaloneKeyAgent, submitAndConfirm, walletReady } from '../../util';
import { filter, firstValueFrom, map, take } from 'rxjs';
import { getEnv, getWallet, walletVariables } from '../../../src';
import { isNotNil } from '@cardano-sdk/util';
Expand Down Expand Up @@ -104,5 +104,7 @@ describe('SingleAddressWallet/mint', () => {
expect(value!.assets!.has(assetId)).toBeTruthy();
expect(value!.assets!.get(assetId)).toBe(1n);
expect(txFoundInHistory.inputSource).toBe(Cardano.InputSource.inputs);

await burnTokens({ policySigners: [alicePolicySigner], scripts: [policyScript], tokens, wallet });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
import { Cardano, nativeScriptPolicyId } from '@cardano-sdk/core';
import { FinalizeTxProps, InitializeTxProps, SingleAddressWallet } from '@cardano-sdk/wallet';
import { KeyRole, util } from '@cardano-sdk/key-management';
import { burnTokens, createStandaloneKeyAgent, submitAndConfirm, walletReady } from '../../util';
import { createLogger } from '@cardano-sdk/util-dev';
import { createStandaloneKeyAgent, submitAndConfirm, walletReady } from '../../util';
import { filter, firstValueFrom } from 'rxjs';
import { getEnv, getWallet, walletVariables } from '../../../src';

Expand Down Expand Up @@ -107,5 +107,12 @@ describe('SingleAddressWallet/multisignature', () => {
expect(value).toBeDefined();
expect(value!.assets!.has(assetId)).toBeTruthy();
expect(value!.assets!.get(assetId)).toBe(10n);

await burnTokens({
policySigners: [alicePolicySigner, bobPolicySigner],
scripts: [policyScript],
tokens,
wallet
});
});
});
19 changes: 17 additions & 2 deletions packages/e2e/test/wallet/SingleAddressWallet/nft.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
import { Cardano, metadatum, nativeScriptPolicyId } from '@cardano-sdk/core';
import { FinalizeTxProps, InitializeTxProps, SingleAddressWallet } from '@cardano-sdk/wallet';
import { KeyRole, TransactionSigner, util } from '@cardano-sdk/key-management';
import { burnTokens, createStandaloneKeyAgent, submitAndConfirm, walletReady } from '../../util';
import { combineLatest, filter, firstValueFrom, map } from 'rxjs';
import { createLogger } from '@cardano-sdk/util-dev';
import { createStandaloneKeyAgent, submitAndConfirm, walletReady } from '../../util';
import { getEnv, getWallet, walletVariables } from '../../../src';

const env = getEnv(walletVariables);
Expand All @@ -23,6 +23,7 @@ describe('SingleAddressWallet.assets/nft', () => {
let fingerprints: Cardano.AssetFingerprint[];
const assetNames = ['4e46542d66696c6573', '4e46542d303031', '4e46542d303032'];
let walletAddress: Cardano.PaymentAddress;
let allMintedTokens: Cardano.TokenMap = new Map();

beforeAll(async () => {
wallet = (await getWallet({ env, logger, name: 'Minting Wallet', polling: { interval: 50 } })).wallet;
Expand Down Expand Up @@ -152,6 +153,13 @@ describe('SingleAddressWallet.assets/nft', () => {
const signedTx = await wallet.finalizeTx(finalizeProps);
await submitAndConfirm(wallet, signedTx);

// assetIds[TOKEN_BURN_INDEX] is already burned as part of a test
allMintedTokens = new Map([
...allMintedTokens,
[assetIds[TOKEN_METADATA_1_INDEX], 1n],
[assetIds[TOKEN_METADATA_2_INDEX], 1n]
]);

// Wait until wallet is aware of the minted tokens.
await firstValueFrom(
combineLatest([wallet.assets$, wallet.balance.utxo.total$]).pipe(
Expand All @@ -170,7 +178,13 @@ describe('SingleAddressWallet.assets/nft', () => {
);
});

afterAll(() => {
afterAll(async () => {
await burnTokens({
policySigners: [policySigner],
scripts: [policyScript],
tokens: allMintedTokens,
wallet
});
wallet.shutdown();
});

Expand Down Expand Up @@ -343,6 +357,7 @@ describe('SingleAddressWallet.assets/nft', () => {
const signedTx = await wallet.finalizeTx(finalizeProps);

await submitAndConfirm(wallet, signedTx);
allMintedTokens = new Map([...allMintedTokens, ...tokens]);

// try remove the asset.nftMetadata filter
const nfts = await firstValueFrom(
Expand Down

0 comments on commit dbaa22d

Please sign in to comment.