Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rkalis committed Oct 29, 2024
1 parent b8fd36e commit fd68987
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 40 deletions.
31 changes: 7 additions & 24 deletions packages/cashscript/test/e2e/Announcement.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import {
Contract, ElectrumNetworkProvider, MockNetworkProvider, Network,
} from '../../src/index.js';
import { getTxOutputs } from '../test-util.js';
import { getLargestUtxo, getTxOutputs } from '../test-util.js';
import { FailedRequireError } from '../../src/Errors.js';
import {
createOpReturnOutput, randomUtxo, utxoComparator,
} from '../../src/utils.js';
import { createOpReturnOutput, randomUtxo } from '../../src/utils.js';
import { aliceAddress } from '../fixture/vars.js';
import artifact from '../fixture/announcement.json' with { type: 'json' };

Expand All @@ -28,10 +26,7 @@ describe('Announcement', () => {
const to = announcement.address;
const amount = 1000n;

const largestUtxo = (await announcement.getUtxos())
.sort(utxoComparator)
.reverse()
.slice(0, 1);
const largestUtxo = getLargestUtxo(await announcement.getUtxos());

// when
const txPromise = announcement.functions
Expand All @@ -50,10 +45,7 @@ describe('Announcement', () => {
it('should fail when trying to announce incorrect announcement', async () => {
// given
const str = 'A contract may injure a human being and, through inaction, allow a human being to come to harm.';
const largestUtxo = (await announcement.getUtxos())
.sort(utxoComparator)
.reverse()
.slice(0, 1);
const largestUtxo = getLargestUtxo(await announcement.getUtxos());

// when
const txPromise = announcement.functions
Expand All @@ -73,10 +65,7 @@ describe('Announcement', () => {
it('should fail when sending incorrect amount of change', async () => {
// given
const str = 'A contract may not injure a human being or, through inaction, allow a human being to come to harm.';
const largestUtxo = (await announcement.getUtxos())
.sort(utxoComparator)
.reverse()
.slice(0, 1);
const largestUtxo = getLargestUtxo(await announcement.getUtxos());

// when
const txPromise = announcement.functions
Expand All @@ -96,10 +85,7 @@ describe('Announcement', () => {
it('should fail when sending the correct change amount to an incorrect address', async () => {
// given
const str = 'A contract may not injure a human being or, through inaction, allow a human being to come to harm.';
const [largestUtxo] = (await announcement.getUtxos())
.sort(utxoComparator)
.reverse()
.slice(0, 1);
const largestUtxo = getLargestUtxo(await announcement.getUtxos());
const changeAmount = largestUtxo?.satoshis - minerFee;

// when
Expand All @@ -121,10 +107,7 @@ describe('Announcement', () => {
it('should succeed when announcing correct announcement', async () => {
// given
const str = 'A contract may not injure a human being or, through inaction, allow a human being to come to harm.';
const largestUtxo = (await announcement.getUtxos())
.sort(utxoComparator)
.reverse()
.slice(0, 1);
const largestUtxo = getLargestUtxo(await announcement.getUtxos());

// when
const tx = await announcement.functions
Expand Down
8 changes: 3 additions & 5 deletions packages/cashscript/test/e2e/P2PKH.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ describe('P2PKH-no-tokens', () => {
// given
const to = p2pkhInstance.address;
const amount = 1000n;
const utxos = await p2pkhInstance.getUtxos();
utxos.sort(utxoComparator).reverse();
const utxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();
const { utxos: gathered } = gatherUtxos(utxos, { amount });
const failureAmount = gathered.reduce((acc, utxo) => acc + utxo.satoshis, 0n) + 1n;

Expand All @@ -109,8 +108,7 @@ describe('P2PKH-no-tokens', () => {
// given
const to = p2pkhInstance.address;
const amount = 1000n;
const utxos = await p2pkhInstance.getUtxos();
utxos.sort(utxoComparator).reverse();
const utxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();
const { utxos: gathered } = gatherUtxos(utxos, { amount });

// when
Expand Down Expand Up @@ -191,7 +189,7 @@ describe('P2PKH-no-tokens', () => {
const to = bobAddress;
const amount = 10000n;

const contractUtxos = await p2pkhInstance.getUtxos();
const contractUtxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();
const bobUtxos = await provider.getUtxos(bobAddress);

// when
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ describe('Transaction Builder', () => {
const amount = 1000n;
const fee = 1000n;

const utxos = await p2pkhInstance.getUtxos();
utxos.sort(utxoComparator).reverse();
const utxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();
const { utxos: gathered, total } = gatherUtxos(utxos, { amount });

const change = total - amount - fee;
Expand Down Expand Up @@ -76,7 +75,7 @@ describe('Transaction Builder', () => {
const amount = 10000n;
const fee = 1000n;

const contractUtxos = await p2pkhInstance.getUtxos();
const contractUtxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();
const bobUtxos = await getAddressUtxos(bobAddress);
const bobTemplate = new SignatureTemplate(bobPriv);

Expand Down Expand Up @@ -125,9 +124,9 @@ describe('Transaction Builder', () => {
it('should build a transaction that can spend from 2 different contracts and P2PKH + OP_RETURN', async () => {
const fee = 1000n;

const carolUtxos = await provider.getUtxos(carolAddress);
const p2pkhUtxos = await p2pkhInstance.getUtxos();
const twtUtxos = await twtInstance.getUtxos();
const carolUtxos = (await provider.getUtxos(carolAddress)).sort(utxoComparator).reverse();
const p2pkhUtxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();
const twtUtxos = (await twtInstance.getUtxos()).sort(utxoComparator).reverse();

const change = carolUtxos[0].satoshis - fee;
const dustAmount = calculateDust({ to: carolAddress, amount: change });
Expand Down Expand Up @@ -157,7 +156,7 @@ describe('Transaction Builder', () => {
it('should fail when fee is higher than maxFee', async () => {
const fee = 2000n;
const maxFee = 1000n;
const p2pkhUtxos = await p2pkhInstance.getUtxos();
const p2pkhUtxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();

const amount = p2pkhUtxos[0].satoshis - fee;
const dustAmount = calculateDust({ to: p2pkhInstance.address, amount });
Expand All @@ -178,7 +177,7 @@ describe('Transaction Builder', () => {
it('should succeed when fee is lower than maxFee', async () => {
const fee = 1000n;
const maxFee = 2000n;
const p2pkhUtxos = await p2pkhInstance.getUtxos();
const p2pkhUtxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();

const amount = p2pkhUtxos[0].satoshis - fee;
const dustAmount = calculateDust({ to: p2pkhInstance.address, amount });
Expand All @@ -199,7 +198,7 @@ describe('Transaction Builder', () => {

it('should fail when locktime is higher than current block height', async () => {
const fee = 1000n;
const p2pkhUtxos = await p2pkhInstance.getUtxos();
const p2pkhUtxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();

const amount = p2pkhUtxos[0].satoshis - fee;
const dustAmount = calculateDust({ to: p2pkhInstance.address, amount });
Expand All @@ -221,7 +220,7 @@ describe('Transaction Builder', () => {

it('should succeed when locktime is lower than current block height', async () => {
const fee = 1000n;
const p2pkhUtxos = await p2pkhInstance.getUtxos();
const p2pkhUtxos = (await p2pkhInstance.getUtxos()).sort(utxoComparator).reverse();

const amount = p2pkhUtxos[0].satoshis - fee;
const dustAmount = calculateDust({ to: p2pkhInstance.address, amount });
Expand Down
7 changes: 6 additions & 1 deletion packages/cashscript/test/test-util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import {
Transaction,
binToHex,
} from '@bitauth/libauth';
import { Output, Network } from '../src/interfaces.js';
import { Output, Network, Utxo } from '../src/interfaces.js';
import { network as defaultNetwork } from './fixture/vars.js';
import { getNetworkPrefix, libauthOutputToCashScriptOutput } from '../src/utils.js';
import { utxoComparator } from '../src/utils.js';

export function getTxOutputs(tx: Transaction, network: Network = defaultNetwork): Output[] {
return tx.outputs.map((o) => {
Expand All @@ -30,3 +31,7 @@ export function getTxOutputs(tx: Transaction, network: Network = defaultNetwork)
};
});
}

export function getLargestUtxo(utxos: Utxo[]): Utxo {
return [...utxos].sort(utxoComparator).reverse()[0];
}

0 comments on commit fd68987

Please sign in to comment.