Skip to content

Commit

Permalink
update FullStackNetworkProvider & improve tests (#232)
Browse files Browse the repository at this point in the history
Co-authored-by: Rosco Kalis <[email protected]>
  • Loading branch information
mr-zwets and rkalis authored Nov 4, 2024
1 parent 6d7fccc commit bb90f4b
Show file tree
Hide file tree
Showing 4 changed files with 242 additions and 1,737 deletions.
2 changes: 1 addition & 1 deletion packages/cashscript/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
},
"devDependencies": {
"@jest/globals": "^29.4.1",
"@psf/bch-js": "^4.15.0",
"@psf/bch-js": "^6.8.0",
"@types/pako": "^2.0.3",
"eslint": "^8.54.0",
"jest": "^29.4.1",
Expand Down
57 changes: 0 additions & 57 deletions packages/cashscript/test/e2e/FullStack.test.ts

This file was deleted.

76 changes: 76 additions & 0 deletions packages/cashscript/test/e2e/network/FullStack.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import BCHJS from '@psf/bch-js';
import { Contract, SignatureTemplate, FullStackNetworkProvider } from '../../../src/index.js';
import {
alicePriv,
bobPkh,
bobPriv,
bobPub,
} from '../../fixture/vars.js';
import { getTxOutputs } from '../../test-util.js';
import { FailedRequireError } from '../../../src/Errors.js';
import artifact from '../../fixture/p2pkh.json' with { type: 'json' };

if (!process.env.TESTS_USE_MOCKNET) {
describe('test FullStackNetworkProvider', () => {
const provider = new FullStackNetworkProvider('mainnet', new BCHJS({ restURL: 'https://api.fullstack.cash/v5/' }));

describe('get utxos using FullStackNetworkProvider', () => {
it('should get the utxos for a p2sh20 contract', async () => {
// Note: We instantiate the contract with bobPkh to avoid mempool conflicts with other tests
const p2pkhInstance = new Contract(artifact, [bobPkh], { provider, addressType: 'p2sh20' });
console.log(p2pkhInstance.address);

const utxos = await p2pkhInstance.getUtxos();
expect(Array.isArray(utxos)).toBe(true);
});
// Note: does not currently support p2sh32
it.skip('should get the utxos for a p2sh32 contract', async () => {
// Note: We instantiate the contract with bobPkh to avoid mempool conflicts with other tests
const p2pkhInstance = new Contract(artifact, [bobPkh], { provider, addressType: 'p2sh32' });
console.log(p2pkhInstance.address);

const utxos = await p2pkhInstance.getUtxos();
expect(Array.isArray(utxos)).toBe(true);
});
});

describe('send using FullStackNetworkProvider', () => {
// Note: We instantiate the contract with bobPkh to avoid mempool conflicts with other tests
const p2pkhInstance = new Contract(artifact, [bobPkh], { provider, addressType: 'p2sh20' });
console.log(p2pkhInstance.address);

it('should fail when using incorrect function arguments', async () => {
// given
const to = p2pkhInstance.address;
const amount = 10000n;

// when
const txPromise = p2pkhInstance.functions
.spend(bobPub, new SignatureTemplate(alicePriv))
.to(to, amount)
.send();

// then
await expect(txPromise).rejects.toThrow(FailedRequireError);
await expect(txPromise).rejects.toThrow('P2PKH.cash:5 Require statement failed at input 0 in contract P2PKH.cash at line 5.');
});

it('should succeed when using correct function arguments', async () => {
// given
const to = p2pkhInstance.address;
const amount = 10000n;

// when
const tx = await p2pkhInstance.functions
.spend(bobPub, new SignatureTemplate(bobPriv))
.to(to, amount)
.send();

// then
const txOutputs = getTxOutputs(tx, 'mainnet');
expect(txOutputs).toEqual(expect.arrayContaining([{ to, amount }]));
expect(tx.txid).toBeDefined();
});
});
});
}
Loading

0 comments on commit bb90f4b

Please sign in to comment.