Skip to content

Commit

Permalink
test: update typegen demo-test
Browse files Browse the repository at this point in the history
  • Loading branch information
maschad committed Jul 12, 2024
1 parent 7c8a51e commit f401c95
Showing 1 changed file with 72 additions and 23 deletions.
95 changes: 72 additions & 23 deletions apps/demo-typegen/src/demo.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// #region Testing-in-ts-ts
import { ContractFactory, Provider, toHex, Wallet, FUEL_NETWORK_URL, Address } from 'fuels';

Check warning on line 2 in apps/demo-typegen/src/demo.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'Provider' is defined but never used

Check warning on line 2 in apps/demo-typegen/src/demo.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'FUEL_NETWORK_URL' is defined but never used
import { generateTestWallet, safeExec } from 'fuels/test-utils';
import { generateTestWallet, launchTestNode, safeExec } from 'fuels/test-utils';

Check warning on line 3 in apps/demo-typegen/src/demo.test.ts

View workflow job for this annotation

GitHub Actions / Lint

'generateTestWallet' is defined but never used

import storageSlots from '../contract/out/release/demo-contract-storage_slots.json';

Expand All @@ -10,19 +10,21 @@ import type { PredicateAbiInputs } from './predicate-types';
import { PredicateAbi__factory } from './predicate-types';
import { ScriptAbi__factory } from './script-types';

let baseAssetId: string;

/**
* @group node
* @group browser
*/
describe('ExampleContract', () => {
beforeAll(async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
baseAssetId = provider.getBaseAssetId();
});
it('with imported storage slots', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
using launched = await launchTestNode({
walletsConfig: {
amountPerCoin: 500_000,
},
});

const {
wallets: [wallet],
} = launched;

// #region typegen-demo-contract-storage-slots
// #context import storageSlots from './contract/out/debug/demo-contract-storage_slots.json';
Expand All @@ -36,8 +38,15 @@ describe('ExampleContract', () => {
expect(contract.id).toBeTruthy();
});
it('should return the input', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
using launched = await launchTestNode({
walletsConfig: {
amountPerCoin: 500_000,
},
});

const {
wallets: [wallet],
} = launched;

// Deploy
const factory = new ContractFactory(bytecode, DemoContractAbi__factory.abi, wallet);
Expand All @@ -64,8 +73,15 @@ describe('ExampleContract', () => {
});

it('deployContract method', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
using launched = await launchTestNode({
walletsConfig: {
amountPerCoin: 500_000,
},
});

const {
wallets: [wallet],
} = launched;

// #region typegen-demo-contract-factory-deploy
// #context import { DemoContractAbi__factory } from './types';
Expand All @@ -88,8 +104,17 @@ describe('ExampleContract', () => {
// #endregion Testing-in-ts-ts

it('should throw when simulating via contract factory with wallet with no resources', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const fundedWallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
using launched = await launchTestNode({
walletsConfig: {
amountPerCoin: 500_000,
},
});

const {
provider,
wallets: [fundedWallet],
} = launched;

const unfundedWallet = Wallet.generate({ provider });

const factory = new ContractFactory(bytecode, DemoContractAbi__factory.abi, fundedWallet);
Expand All @@ -103,8 +128,16 @@ it('should throw when simulating via contract factory with wallet with no resour
});

it('should not throw when dry running via contract factory with wallet with no resources', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const fundedWallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
using launched = await launchTestNode({
walletsConfig: {
amountPerCoin: 500_000,
},
});

const {
provider,
wallets: [fundedWallet],
} = launched;
const unfundedWallet = Wallet.generate({ provider });

const factory = new ContractFactory(bytecode, DemoContractAbi__factory.abi, fundedWallet);
Expand All @@ -116,8 +149,15 @@ it('should not throw when dry running via contract factory with wallet with no r
});

test('Example script', async () => {
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
using launched = await launchTestNode({
walletsConfig: {
amountPerCoin: 500_000,
},
});

const {
wallets: [wallet],
} = launched;

// #region typegen-demo-script
// #context import { ScriptAbi__factory } from './types';
Expand All @@ -135,18 +175,27 @@ test('Example predicate', async () => {
// #context import { PredicateAbi__factory } from './types';

// In this exchange, we are first transferring some coins to the predicate
const provider = await Provider.create(FUEL_NETWORK_URL);
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);
using launched = await launchTestNode({
walletsConfig: {
amountPerCoin: 10_000_000_000,
},
});

const {
provider,
wallets: [wallet],
} = launched;

const receiver = Wallet.fromAddress(Address.fromRandom(), provider);

const predicateData: PredicateAbiInputs = [];
const predicate = PredicateAbi__factory.createInstance(provider, predicateData);

const tx = await wallet.transfer(predicate.address, 150_000, baseAssetId);
const tx = await wallet.transfer(predicate.address, 1_000_000, provider.getBaseAssetId());
const { isStatusSuccess } = await tx.wait();

// Then we are transferring some coins from the predicate to a random address (receiver)
const tx2 = await predicate.transfer(receiver.address, 50_000, baseAssetId);
const tx2 = await predicate.transfer(receiver.address, 50_000, provider.getBaseAssetId());
await tx2.wait();

expect((await receiver.getBalance()).toNumber()).toEqual(50_000);
Expand Down

0 comments on commit f401c95

Please sign in to comment.