|
| 1 | +import BCHJS from '@psf/bch-js'; |
| 2 | +import { Contract, SignatureTemplate, FullStackNetworkProvider } from '../../../src/index.js'; |
| 3 | +import { |
| 4 | + alicePriv, |
| 5 | + bobPkh, |
| 6 | + bobPriv, |
| 7 | + bobPub, |
| 8 | +} from '../../fixture/vars.js'; |
| 9 | +import { getTxOutputs } from '../../test-util.js'; |
| 10 | +import { FailedRequireError } from '../../../src/Errors.js'; |
| 11 | +import artifact from '../../fixture/p2pkh.json' with { type: 'json' }; |
| 12 | + |
| 13 | +if (!process.env.TESTS_USE_MOCKNET) { |
| 14 | + describe('test FullStackNetworkProvider', () => { |
| 15 | + const provider = new FullStackNetworkProvider('mainnet', new BCHJS({ restURL: 'https://api.fullstack.cash/v5/' })); |
| 16 | + |
| 17 | + describe('get utxos using FullStackNetworkProvider', () => { |
| 18 | + it('should get the utxos for a p2sh20 contract', async () => { |
| 19 | + // Note: We instantiate the contract with bobPkh to avoid mempool conflicts with other tests |
| 20 | + const p2pkhInstance = new Contract(artifact, [bobPkh], { provider, addressType: 'p2sh20' }); |
| 21 | + console.log(p2pkhInstance.address); |
| 22 | + |
| 23 | + const utxos = await p2pkhInstance.getUtxos(); |
| 24 | + expect(Array.isArray(utxos)).toBe(true); |
| 25 | + }); |
| 26 | + // Note: does not currently support p2sh32 |
| 27 | + it.skip('should get the utxos for a p2sh32 contract', async () => { |
| 28 | + // Note: We instantiate the contract with bobPkh to avoid mempool conflicts with other tests |
| 29 | + const p2pkhInstance = new Contract(artifact, [bobPkh], { provider, addressType: 'p2sh32' }); |
| 30 | + console.log(p2pkhInstance.address); |
| 31 | + |
| 32 | + const utxos = await p2pkhInstance.getUtxos(); |
| 33 | + expect(Array.isArray(utxos)).toBe(true); |
| 34 | + }); |
| 35 | + }); |
| 36 | + |
| 37 | + describe('send using FullStackNetworkProvider', () => { |
| 38 | + // Note: We instantiate the contract with bobPkh to avoid mempool conflicts with other tests |
| 39 | + const p2pkhInstance = new Contract(artifact, [bobPkh], { provider, addressType: 'p2sh20' }); |
| 40 | + console.log(p2pkhInstance.address); |
| 41 | + |
| 42 | + it('should fail when using incorrect function arguments', async () => { |
| 43 | + // given |
| 44 | + const to = p2pkhInstance.address; |
| 45 | + const amount = 10000n; |
| 46 | + |
| 47 | + // when |
| 48 | + const txPromise = p2pkhInstance.functions |
| 49 | + .spend(bobPub, new SignatureTemplate(alicePriv)) |
| 50 | + .to(to, amount) |
| 51 | + .send(); |
| 52 | + |
| 53 | + // then |
| 54 | + await expect(txPromise).rejects.toThrow(FailedRequireError); |
| 55 | + await expect(txPromise).rejects.toThrow('P2PKH.cash:5 Require statement failed at input 0 in contract P2PKH.cash at line 5.'); |
| 56 | + }); |
| 57 | + |
| 58 | + it('should succeed when using correct function arguments', async () => { |
| 59 | + // given |
| 60 | + const to = p2pkhInstance.address; |
| 61 | + const amount = 10000n; |
| 62 | + |
| 63 | + // when |
| 64 | + const tx = await p2pkhInstance.functions |
| 65 | + .spend(bobPub, new SignatureTemplate(bobPriv)) |
| 66 | + .to(to, amount) |
| 67 | + .send(); |
| 68 | + |
| 69 | + // then |
| 70 | + const txOutputs = getTxOutputs(tx, 'mainnet'); |
| 71 | + expect(txOutputs).toEqual(expect.arrayContaining([{ to, amount }])); |
| 72 | + expect(tx.txid).toBeDefined(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + }); |
| 76 | +} |
0 commit comments