Skip to content

Commit

Permalink
feat: support message types (#3016)
Browse files Browse the repository at this point in the history
* rename JsonAbi MessageType property

* add smo contract

* add test case for smo contract

* remove unnused types from contract

* add new method to exhaustive examples contract

* add test case to interfaces test

* add changeset

* ajust test case

* re-add test case removed by accident

* refact test case

* update changeset

* update SMO contract interface

* implement decodeMessage at Interface class

* decoding message data at assembleResult

* modify test case

* modify changeset

Co-authored-by: Nedim Salkić <[email protected]>

* remove uneeded test case

* remove code to decode message

* improve test case

---------

Co-authored-by: Nedim Salkić <[email protected]>
  • Loading branch information
Torres-ssf and nedsalk authored Sep 19, 2024
1 parent 47b5cd3 commit 48db506
Show file tree
Hide file tree
Showing 8 changed files with 101 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/lovely-rivers-impress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@fuel-ts/abi-typegen": patch
"@fuel-ts/abi-coder": patch
---

feat: support message types
2 changes: 1 addition & 1 deletion packages/abi-coder/src/types/JsonAbiNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface LoggedType {
}

export interface MessageType {
readonly message_id: string;
readonly messageId: string;
readonly concreteTypeId: string;
}
export interface Configurable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ contract;
use std::b512::B512;
use std::bytes::Bytes;
use std::string::String;
use std::message::send_typed_message;

enum EnumWithGeneric<A> {
VariantOne: A,
Expand Down Expand Up @@ -181,6 +182,8 @@ abi MyContract {
arg3: (str[5], bool),
arg4: MyOtherStruct,
);
#[payable]
fn send_typed_message_bool(recipient: b256, msg_data: bool, coins: u64);
}

impl MyContract for Contract {
Expand Down Expand Up @@ -339,4 +342,8 @@ impl MyContract for Contract {
fn simple_vector(arg: Vec<u8>) -> Vec<u8> {
arg
}
#[payable]
fn send_typed_message_bool(recipient: b256, msg_data: bool, coins: u64) {
send_typed_message(recipient, msg_data, coins);
}
}
2 changes: 1 addition & 1 deletion packages/abi-typegen/src/types/interfaces/JsonAbiNew.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ export interface LoggedType {
}

export interface MessageType {
readonly message_id: string;
readonly messageId: string;
readonly concreteTypeId: string;
}
export interface Configurable {
Expand Down
51 changes: 50 additions & 1 deletion packages/fuel-gauge/src/contract.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ import {
Predicate,
PolicyType,
buildFunctionResult,
ReceiptType,
} from 'fuels';
import type { ScriptTransactionRequest, TransferParams } from 'fuels';
import type { ReceiptMessageOut, ScriptTransactionRequest, TransferParams } from 'fuels';
import { expectToThrowFuelError, ASSET_A, ASSET_B, launchTestNode } from 'fuels/test-utils';
import type { DeployContractConfig } from 'fuels/test-utils';

import {
CallTestContract,
CallTestContractFactory,
SmoContractFactory,
StorageTestContract,
StorageTestContractFactory,
} from '../test/typegen/contracts';
Expand Down Expand Up @@ -1111,4 +1113,51 @@ describe('Contract', () => {
expect(scriptGasLimit?.toNumber()).toBe(gasLimit);
expect(bn(maxFeePolicy?.data).toNumber()).toBe(maxFee);
});

it('can call SMO contract', async () => {
using launched = await launchTestNode({
contractsConfigs: [
{
factory: SmoContractFactory,
},
],
});

const {
provider,
wallets: [recipient],
contracts: [contract],
} = launched;

const data = [1, 2, 3, 4, 5, 6, 7, 8];
const baseAssetId = provider.getBaseAssetId();

const { waitForResult } = await contract
.multiCall([
contract.functions
.send_typed_message_u8(recipient.address.toB256(), 10, 1)
.callParams({ forward: [1, baseAssetId] }),
contract.functions
.send_typed_message_bool(recipient.address.toB256(), true, 1)
.callParams({ forward: [1, baseAssetId] }),
contract.functions
.send_typed_message_bytes(recipient.address.toB256(), data, 1)
.callParams({ forward: [1, baseAssetId] }),
])
.call();

const {
transactionResult: { receipts },
} = await waitForResult();

const messageOutReceipts = receipts.filter(
({ type }) => ReceiptType.MessageOut === type
) as ReceiptMessageOut[];

expect(messageOutReceipts.length).toBe(3);

messageOutReceipts.forEach((receipt) => {
expect(receipt.recipient).toBe(recipient.address.toB256());
});
});
});
1 change: 1 addition & 0 deletions packages/fuel-gauge/test/fixtures/forc-projects/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ members = [
"generic-types-contract",
"large-contract",
"multi-token-contract",
"smo-contract",
"options",
"payable-annotation",
"predicate-address",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
entry = "main.sw"
license = "Apache-2.0"
name = "smo-contract"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
contract;

use std::message::send_typed_message;
use std::bytes::Bytes;

abi SMOContract {
#[payable]
fn send_typed_message_bool(recipient: b256, msg_data: bool, coins: u64);
#[payable]
fn send_typed_message_u8(recipient: b256, msg_data: u8, coins: u64);
#[payable]
fn send_typed_message_bytes(recipient: b256, msg_data: Bytes, coins: u64);
}

impl SMOContract for Contract {
#[payable]
fn send_typed_message_bool(recipient: b256, msg_data: bool, coins: u64) {
send_typed_message(recipient, msg_data, coins);
}
#[payable]
fn send_typed_message_u8(recipient: b256, msg_data: u8, coins: u64) {
send_typed_message(recipient, msg_data, coins);
}
#[payable]
fn send_typed_message_bytes(recipient: b256, msg_data: Bytes, coins: u64) {
send_typed_message(recipient, msg_data, coins);
}
}

0 comments on commit 48db506

Please sign in to comment.