Skip to content

replace all .json to .artifact.ts #283

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 11, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/testing-suite/artifacts/example.artifact.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
export default {
contractName: 'Example',
constructorInputs: [],
abi: [
{
name: 'test',
inputs: [
{
name: 'value',
type: 'int',
},
],
},
],
bytecode: 'OP_1 OP_NUMEQUAL',
source: 'contract Example() {\n function test(int value) {\n console.log(value, \'test\');\n require(value == 1, \'Wrong value passed\');\n }\n}\n',
debug: {
bytecode: '007a519c',
sourceMap: '4:12:4:17;;:21::22;:12:::1',
logs: [
{
ip: 0,
line: 3,
data: [
{
stackIndex: 0,
type: 'int',
ip: 0,
},
'test',
],
},
],
requires: [
{
ip: 4,
line: 4,
message: 'Wrong value passed',
},
],
},
compiler: {
name: 'cashc',
version: '0.11.0-next.3',
},
updatedAt: '2025-04-11T09:08:09.750Z',
} as const;
4 changes: 2 additions & 2 deletions examples/testing-suite/test/example.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import artifact from '../artifacts/example.json' with { type: 'json' };
import artifact from '../artifacts/example.artifact.js';
import { Contract, MockNetworkProvider, TransactionBuilder, randomUtxo } from 'cashscript';
import 'cashscript/jest';

describe('test example contract functions', () => {
it('should check for output logs and error messages', async () => {
const provider = new MockNetworkProvider();
const contract = new Contract(artifact, [], { provider });

// Create a contract Utxo
const contractUtxo = randomUtxo();
provider.addUtxo(contract.address, contractUtxo);
22 changes: 11 additions & 11 deletions packages/cashscript/test/Contract.test.ts
Original file line number Diff line number Diff line change
@@ -11,21 +11,24 @@ import {
import {
alicePkh, alicePriv, alicePub, bobPriv,
} from './fixture/vars.js';
import p2pkhArtifact from './fixture/p2pkh.json' with { type: 'json' };
import twtArtifact from './fixture/transfer_with_timeout.json' with { type: 'json' };
import hodlVaultArtifact from './fixture/hodl_vault.json' with { type: 'json' };
import mecenasArtifact from './fixture/mecenas.json' with { type: 'json' };
import p2pkhArtifact from './fixture/p2pkh.artifact.js';
import twtArtifact from './fixture/transfer_with_timeout.artifact.js';
import hodlVaultArtifact from './fixture/hodl_vault.artifact.js';
import mecenasArtifact from './fixture/mecenas.artifact.js';
import deprecatedMecenasArtifact from './fixture/deprecated/mecenas-v0.6.0.json' with { type: 'json' };
import boundedBytesArtifact from './fixture/bounded_bytes.json' with { type: 'json' };
import boundedBytesArtifact from './fixture/bounded_bytes.artifact.js';

describe('Contract', () => {
describe('new', () => {
it('should fail with incorrect constructor args', () => {
const provider = new ElectrumNetworkProvider(Network.CHIPNET);

// @ts-expect-error invalid constructor type
expect(() => new Contract(p2pkhArtifact, [], { provider })).toThrow();
// @ts-expect-error invalid constructor type
expect(() => new Contract(p2pkhArtifact, [20n], { provider })).toThrow();
expect(
// @ts-expect-error invalid constructor type
() => new Contract(p2pkhArtifact, [placeholder(20), placeholder(20)], { provider }),
).toThrow();
expect(() => new Contract(p2pkhArtifact, [placeholder(19)], { provider })).toThrow();
@@ -62,8 +65,7 @@ describe('Contract', () => {

it('should create new TransferWithTimeout instance', () => {
const provider = new ElectrumNetworkProvider(Network.CHIPNET);
const constructorArgs = [placeholder(65), placeholder(65), 1000000n];
const instance = new Contract(twtArtifact, constructorArgs, { provider });
const instance = new Contract(twtArtifact, [placeholder(65), placeholder(65), 1000000n], { provider });
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just notice now that adding as const will make the array as read-only, however passing it to Contract will error because it expect mutable array. I just inline it, or any other suggestion?


expect(typeof instance.address).toBe('string');
expect(typeof instance.functions.transfer).toBe('function');
@@ -73,8 +75,7 @@ describe('Contract', () => {

it('should create new HodlVault instance', () => {
const provider = new ElectrumNetworkProvider(Network.CHIPNET);
const constructorArgs = [placeholder(65), placeholder(65), 1000000n, 10000n];
const instance = new Contract(hodlVaultArtifact, constructorArgs, { provider });
const instance = new Contract(hodlVaultArtifact, [placeholder(65), placeholder(65), 1000000n, 10000n], { provider });

expect(typeof instance.address).toBe('string');
expect(typeof instance.functions.spend).toBe('function');
@@ -83,8 +84,7 @@ describe('Contract', () => {

it('should create new Mecenas instance', () => {
const provider = new ElectrumNetworkProvider(Network.CHIPNET);
const constructorArgs = [placeholder(20), placeholder(20), 1000000n];
const instance = new Contract(mecenasArtifact, constructorArgs, { provider });
const instance = new Contract(mecenasArtifact, [placeholder(20), placeholder(20), 1000000n], { provider });

expect(typeof instance.address).toBe('string');
expect(typeof instance.functions.receive).toBe('function');
8 changes: 4 additions & 4 deletions packages/cashscript/test/TransactionBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -12,8 +12,8 @@ import {
} from './fixture/vars.js';
import { Network } from '../src/interfaces.js';
import { utxoComparator, calculateDust, randomUtxo, randomToken, isNonTokenUtxo, isFungibleTokenUtxo } from '../src/utils.js';
import p2pkhArtifact from './fixture/p2pkh.json' with { type: 'json' };
import twtArtifact from './fixture/transfer_with_timeout.json' with { type: 'json' };
import p2pkhArtifact from './fixture/p2pkh.artifact.js';
import twtArtifact from './fixture/transfer_with_timeout.artifact.js';
import { TransactionBuilder } from '../src/TransactionBuilder.js';
import { gatherUtxos, getTxOutputs } from './test-util.js';

@@ -22,8 +22,8 @@ describe('Transaction Builder', () => {
? new MockNetworkProvider()
: new ElectrumNetworkProvider(Network.CHIPNET);

let p2pkhInstance: Contract;
let twtInstance: Contract;
let p2pkhInstance: Contract<typeof p2pkhArtifact>;
let twtInstance: Contract<typeof twtArtifact>;

beforeAll(() => {
// Note: We instantiate the contract with carolPkh to avoid mempool conflicts with other (P2PKH) tests
4 changes: 2 additions & 2 deletions packages/cashscript/test/e2e/P2PKH-tokens.test.ts
Original file line number Diff line number Diff line change
@@ -9,10 +9,10 @@ import {
} from '../fixture/vars.js';
import { getTxOutputs } from '../test-util.js';
import { Network, TokenDetails, Utxo } from '../../src/interfaces.js';
import artifact from '../fixture/p2pkh.json' with { type: 'json' };
import artifact from '../fixture/p2pkh.artifact.js';

describe('P2PKH-tokens', () => {
let p2pkhInstance: Contract;
let p2pkhInstance: Contract<typeof artifact>;

beforeAll(() => {
const provider = process.env.TESTS_USE_MOCKNET
2 changes: 1 addition & 1 deletion packages/cashscript/test/e2e/TokenCategoryCheck.test.ts
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ import {
TransactionBuilder,
} from '../../src/index.js';
import { Network } from '../../src/interfaces.js';
import artifact from '../fixture/token_category_comparison.json' with { type: 'json' };
import artifact from '../fixture/token_category_comparison.artifact.js';

describe('TokenCategoryCheck', () => {
const provider = process.env.TESTS_USE_MOCKNET
8 changes: 4 additions & 4 deletions packages/cashscript/test/e2e/misc/timelocks.test.ts
Original file line number Diff line number Diff line change
@@ -9,8 +9,8 @@ import {
} from '../../fixture/vars.js';
import { Network } from '../../../src/interfaces.js';
import { utxoComparator, calculateDust, randomUtxo, isNonTokenUtxo } from '../../../src/utils.js';
import p2pkhArtifact from '../../fixture/p2pkh.json' with { type: 'json' };
import twtArtifact from '../../fixture/transfer_with_timeout.json' with { type: 'json' };
import p2pkhArtifact from '../../fixture/p2pkh.artifact.js';
import twtArtifact from '../../fixture/transfer_with_timeout.artifact.js';
import { TransactionBuilder } from '../../../src/TransactionBuilder.js';
import { describeOrSkip, getTxOutputs } from '../../test-util.js';

@@ -20,8 +20,8 @@ describe('Timelocks', () => {
? new MockNetworkProvider()
: new ElectrumNetworkProvider(Network.CHIPNET);

let p2pkhInstance: Contract;
let twtInstance: Contract;
let p2pkhInstance: Contract<typeof p2pkhArtifact>;
let twtInstance: Contract<typeof twtArtifact>;

beforeAll(() => {
// Note: We instantiate the contract with carolPkh to avoid mempool conflicts with other (P2PKH) tests
2 changes: 1 addition & 1 deletion website/docs/sdk/testing-setup.md
Original file line number Diff line number Diff line change
@@ -107,7 +107,7 @@ contract Example() {
```

```ts title="Example test file"
import artifact from '../artifacts/example.json' with { type: "json" };
import artifact from '../artifacts/example.artifact.js';
import { Contract, MockNetworkProvider, randomUtxo } from 'cashscript';
import 'cashscript/jest';