Skip to content

Commit

Permalink
feat!: implement non-blocking contract call (#2692)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf committed Jul 10, 2024
1 parent e9b70ee commit 661b153
Show file tree
Hide file tree
Showing 60 changed files with 1,011 additions and 563 deletions.
6 changes: 6 additions & 0 deletions .changeset/spicy-trains-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/contract": minor
"@fuel-ts/program": minor
---

feat!: implement non-blocking contract call
7 changes: 5 additions & 2 deletions apps/create-fuels-counter-guide/src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@ export default function Home() {
);
}

const { value } = await contract.functions.increment_counter(bn(1)).call();
const { waitForResult } = await contract.functions.increment_counter(bn(1)).call();
const { value } = await waitForResult();
setCounter(value.toNumber());

await refreshWalletBalance?.();
Expand All @@ -68,7 +69,9 @@ export default function Home() {
);
}

const { value } = await contract.functions.decrement_counter(bn(1)).call();
const { waitForResult } = await contract.functions.decrement_counter(bn(1)).call();
const { value } = await waitForResult();

setCounter(value.toNumber());

await refreshWalletBalance?.();
Expand Down
19 changes: 14 additions & 5 deletions apps/demo-bun-fuels/src/bun.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,20 @@ describe('ExampleContract', () => {
const { contract } = await waitForResult();

// Call
const { value } = await contract.functions.return_input(1337).call();
const call1 = await contract.functions.return_input(1337).call();

// Wait for result
const { value } = await call1.waitForResult();

// Assert
expect(value.toHex()).toEqual(toHex(1337));

// You can also make a call using the factory
const contractInstance = SampleAbi__factory.connect(contract.id, wallet);
const { value: v2 } = await contractInstance.functions.return_input(1337).call();
const call2 = await contractInstance.functions.return_input(1337).call();

// Wait for result
const { value: v2 } = await call2.waitForResult();
expect(v2.toHex()).toBe(toHex(1337));
});

Expand All @@ -48,11 +54,14 @@ describe('ExampleContract', () => {
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);

// Deploy
const { waitForResult } = await SampleAbi__factory.deployContract(bytecode, wallet);
const { contract } = await waitForResult();
const deploy = await SampleAbi__factory.deployContract(bytecode, wallet);
const { contract } = await deploy.waitForResult();

// Call
const { value } = await contract.functions.return_input(1337).call();
const { waitForResult } = await contract.functions.return_input(1337).call();

// Wait for result
const { value } = await waitForResult();

// Assert
expect(value.toHex()).toEqual(toHex(1337));
Expand Down
17 changes: 10 additions & 7 deletions apps/demo-fuels/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,20 @@ describe('ExampleContract', () => {

// Deploy
const factory = new ContractFactory(bytecode, SampleAbi__factory.abi, wallet);
const { waitForResult } = await factory.deployContract();
const { contract } = await waitForResult();
const deploy = await factory.deployContract();
const { contract } = await deploy.waitForResult();

// Call
const { value } = await contract.functions.return_input(1337).call();
const { waitForResult } = await contract.functions.return_input(1337).call();
const { value } = await waitForResult();

// Assert
expect(value.toHex()).toEqual(toHex(1337));

// You can also make a call using the factory
const contractInstance = SampleAbi__factory.connect(contract.id, wallet);
const { value: v2 } = await contractInstance.functions.return_input(1337).call();
const call2 = await contractInstance.functions.return_input(1337).call();
const { value: v2 } = await call2.waitForResult();
expect(v2.toHex()).toBe(toHex(1337));
});

Expand All @@ -48,11 +50,12 @@ describe('ExampleContract', () => {
const wallet = await generateTestWallet(provider, [[500_000, baseAssetId]]);

// Deploy
const { waitForResult } = await SampleAbi__factory.deployContract(bytecode, wallet);
const { contract } = await waitForResult();
const deploy = await SampleAbi__factory.deployContract(bytecode, wallet);
const { contract } = await deploy.waitForResult();

// Call
const { value } = await contract.functions.return_input(1337).call();
const { waitForResult } = await contract.functions.return_input(1337).call();
const { value } = await waitForResult();

// Assert
expect(value.toHex()).toEqual(toHex(1337));
Expand Down
20 changes: 12 additions & 8 deletions apps/demo-typegen/src/demo.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,13 @@ describe('ExampleContract', () => {

// Deploy
const factory = new ContractFactory(bytecode, DemoContractAbi__factory.abi, wallet);
const { waitForResult } = await factory.deployContract();
const { contract } = await waitForResult();
const deploy = await factory.deployContract();
const { contract } = await deploy.waitForResult();
const contractId = contract.id;

// Call
const { value } = await contract.functions.return_input(1337).call();
const { waitForResult } = await contract.functions.return_input(1337).call();
const { value } = await waitForResult();

// Assert
expect(value.toHex()).toEqual(toHex(1337));
Expand All @@ -56,7 +57,8 @@ describe('ExampleContract', () => {
// #context import { DemoContractAbi__factory } from './types';

const contractInstance = DemoContractAbi__factory.connect(contractId, wallet);
const { value: v2 } = await contractInstance.functions.return_input(1337).call();
const call2 = await contractInstance.functions.return_input(1337).call();
const { value: v2 } = await call2.waitForResult();
// #endregion typegen-demo-contract-factory-connect
expect(v2.toHex()).toBe(toHex(1337));
});
Expand All @@ -70,13 +72,14 @@ describe('ExampleContract', () => {
// #context import bytecode from './types/DemoContractAbi.hex';

// Deploy
const { waitForResult } = await DemoContractAbi__factory.deployContract(bytecode, wallet);
const { contract } = await waitForResult();
const deploy = await DemoContractAbi__factory.deployContract(bytecode, wallet);
const { contract } = await deploy.waitForResult();

// #endregion typegen-demo-contract-factory-deploy

// Call
const { value } = await contract.functions.return_input(1337).call();
const { waitForResult } = await contract.functions.return_input(1337).call();
const { value } = await waitForResult();

// Assert
expect(value.toHex()).toEqual(toHex(1337));
Expand Down Expand Up @@ -120,7 +123,8 @@ test('Example script', async () => {
// #context import { ScriptAbi__factory } from './types';

const script = ScriptAbi__factory.createInstance(wallet);
const { value } = await script.functions.main().call();
const { waitForResult } = await script.functions.main().call();
const { value } = await waitForResult();
// #endregion typegen-demo-script
expect(value).toStrictEqual(10);
});
Expand Down
11 changes: 9 additions & 2 deletions apps/docs-snippets/src/guide/contracts/add-transfer.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ describe(__filename, () => {
// #region add-transfer-1
const recipient = Wallet.generate({ provider });

await contract.functions
const { waitForResult } = await contract.functions
.echo_u64(100)
.addTransfer({
destination: recipient.address,
amount: 100,
assetId: baseAssetId,
})
.call();

await waitForResult();
// #endregion add-transfer-1

const recipientBalance = await recipient.getBalance(baseAssetId);
Expand All @@ -57,7 +59,12 @@ describe(__filename, () => {
{ destination: recipient2.address, amount: 300, assetId: ASSET_B },
];

await contract.functions.echo_u64(100).addBatchTransfer(transferParams).call();
const { waitForResult } = await contract.functions
.echo_u64(100)
.addBatchTransfer(transferParams)
.call();

await waitForResult();
// #endregion add-transfer-2

const recipient1BalanceBaseAsset = await recipient1.getBalance(baseAssetId);
Expand Down
17 changes: 11 additions & 6 deletions apps/docs-snippets/src/guide/contracts/call-parameters.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,33 @@ describe(__filename, () => {
// #region call-params-1
const amountToForward = 10;

const { value } = await contract.functions
const { waitForResult } = await contract.functions
.return_context_amount()
.callParams({
forward: [amountToForward, baseAssetId],
})
.call();

const { value } = await waitForResult();

expect(new BN(value).toNumber()).toBe(amountToForward);
// #endregion call-params-1
});

it('should throw error due not enough gas', async () => {
// #region call-params-2

await expect(
contract.functions
await expect(async () => {
const call = await contract.functions
.return_context_amount()
.callParams({
forward: [10, baseAssetId],
gasLimit: 1,
})
.call()
).rejects.toThrow('The transaction reverted with reason: "OutOfGas"');
.call();

await call.waitForResult();
}).rejects.toThrow('The transaction reverted with reason: "OutOfGas"');
// #endregion call-params-2
});

Expand All @@ -53,7 +57,7 @@ describe(__filename, () => {
const contractCallGasLimit = 4000;
const transactionGasLimit = 100_000;

const result = await contract.functions
const { waitForResult } = await contract.functions
.return_context_amount()
.callParams({
forward: [amountToForward, baseAssetId],
Expand All @@ -64,6 +68,7 @@ describe(__filename, () => {
})
.call();

const result = await waitForResult();
const { value } = result;
const expectedValue = 10;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ describe(__filename, () => {
bits: baseAssetId,
};

await contract.functions
const { waitForResult } = await contract.functions
.transfer(amountToTransfer, asset, recipient.address.toB256())
.callParams({
forward: [amountToForward, baseAssetId],
})
.call();

await waitForResult();

const contractBalance = await contract.getBalance(baseAssetId);

const expectedBalance = amountToForward - amountToTransfer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ describe(__filename, () => {
// #endregion contract-setup-4

// #region contract-setup-5
const { value } = await contract.functions.echo_u8(15).call();
const call = await contract.functions.echo_u8(15).call();

const { value } = await call.waitForResult();
// #endregion contract-setup-5

expect(transactionId).toBeDefined();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,22 @@ describe(__filename, () => {
it('should successfully make call to another contract', async () => {
// #region inter-contract-calls-3
const amountToDeposit = 70;
const { value: initialBalance } = await simpleToken.functions
.get_balance(wallet.address.toB256())
.call();
const call1 = await simpleToken.functions.get_balance(wallet.address.toB256()).call();

const { value: initialBalance } = await call1.waitForResult();

expect(new BN(initialBalance).toNumber()).toBe(0);

await tokenDepositor.functions
const call2 = await tokenDepositor.functions
.deposit_to_simple_token(simpleToken.id.toB256(), amountToDeposit)
.addContracts([simpleToken])
.call();

const { value: finalBalance } = await simpleToken.functions
.get_balance(wallet.address.toB256())
.call();
await call2.waitForResult();

const call3 = await simpleToken.functions.get_balance(wallet.address.toB256()).call();

const { value: finalBalance } = await call3.waitForResult();

expect(new BN(finalBalance).toNumber()).toBe(amountToDeposit);
// #endregion inter-contract-calls-3
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ describe(__filename, () => {
});

it('should successfully use "get" to read from the blockchain', async () => {
await counterContract.functions.increment_count(1).call();
const { waitForResult } = await counterContract.functions.increment_count(1).call();
await waitForResult();

const { id: contractId, interface: abi } = counterContract;

Expand Down Expand Up @@ -54,14 +55,27 @@ describe(__filename, () => {
expect(value.toNumber()).toBeGreaterThanOrEqual(10);
});

it('should successfully execute a contract call without a wallet', async () => {
it('should successfully execute a contract call without a wallet [call]', async () => {
const contract = counterContract;

// #region interacting-with-contracts-4
await contract.functions.increment_count(10).call();
const { transactionId, waitForResult } = await contract.functions.increment_count(10).call();

const { value } = await waitForResult();
// #endregion interacting-with-contracts-4

const { value } = await contract.functions.get_count().get();
expect(transactionId).toBeDefined();
expect(value.toNumber()).toBeGreaterThanOrEqual(10);
});

it('should successfully execute a contract call without a wallet [call]', async () => {
const contract = counterContract;

// #region interacting-with-contracts-5
const { waitForResult } = await contract.functions.increment_count(10).call();
const { value } = await waitForResult();
// #endregion interacting-with-contracts-5

expect(value.toNumber()).toBeGreaterThanOrEqual(10);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ test('isReadOnly returns true for read-only functions', async () => {
if (isReadOnly) {
await contract.functions.get_count().get();
} else {
await contract.functions.get_count().call();
const { waitForResult } = await contract.functions.get_count().call();
await waitForResult();
}
// #endregion is-function-readonly-1

Expand Down
6 changes: 5 additions & 1 deletion apps/docs-snippets/src/guide/contracts/logs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ describe(__filename, () => {
const value3 = 'Fuel';
const value4 = [1, 2, 3];

const { logs } = await contract.functions.log_values(value1, value2, value3, value4).call();
const { waitForResult } = await contract.functions
.log_values(value1, value2, value3, value4)
.call();

const { logs } = await waitForResult();

expect(new BN(logs[0]).toNumber()).toBe(value1);
expect(logs[1]).toBe(value2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ describe(__filename, () => {
const subID = '0xc7fd1d987ada439fc085cfa3c49416cf2b504ac50151e3c2335d60595cb90745';
const mintAmount = bn(1000);

const txResult = await contract.functions.mint_coins(subID, mintAmount).call();
const { waitForResult } = await contract.functions.mint_coins(subID, mintAmount).call();
const txResult = await waitForResult();

const mintedAssetId = getMintedAssetId(subID, contract.id.toB256());
// #endregion minted-token-asset-id-2
Expand Down
Loading

0 comments on commit 661b153

Please sign in to comment.