Skip to content

Commit

Permalink
Partial support for Fuel Bytes type (#225)
Browse files Browse the repository at this point in the history
* Add support for Fuel Bytes

* Add partial support for Bytes type

* Remove console.log
  • Loading branch information
DZakh authored Sep 24, 2024
1 parent 8246af4 commit dabd985
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 21 deletions.
7 changes: 3 additions & 4 deletions codegenerator/cli/src/fuel/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,7 @@ impl FuelAbi {

let get_unknown_res_type_ident = |type_field: &str| {
println!("Unhandled type_field \"{}\" in abi", type_field);
RescriptTypeIdent::TypeApplication {
name: "unknown".to_string(),
type_params: vec![],
}
RescriptTypeIdent::Unknown
};

let get_unknown_res_type_expr =
Expand Down Expand Up @@ -216,6 +213,8 @@ impl FuelAbi {
.context("Failed getting param for struct Vec")?,
)))
.to_ok_expr(),
// It's decoded as Uint8Array, but we don't have a schema for it yet
"struct std::bytes::Bytes" => Unknown.to_ok_expr(),
//TODO: handle nested option since this would need to be flattened to
//single level rescript option.
"enum Option" => Option(Box::new(GenericParam(
Expand Down
7 changes: 6 additions & 1 deletion codegenerator/cli/src/rescript_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ pub enum RescriptTypeIdent {
Address,
String,
Bool,
Unknown,
Timestamp,
//Enums defined in the user's schema
SchemaEnum(CapitalizedOptions),
Expand Down Expand Up @@ -379,6 +380,7 @@ impl RescriptTypeIdent {
Self::Int => "int".to_string(),
Self::Float => "GqlDbCustomTypes.Float.t".to_string(),
Self::BigInt => "bigint".to_string(),
Self::Unknown => "unknown".to_string(),
Self::BigDecimal => "BigDecimal.t".to_string(),
Self::Address => "Address.t".to_string(),
Self::String => "string".to_string(),
Expand Down Expand Up @@ -425,6 +427,7 @@ impl RescriptTypeIdent {
match self {
Self::Unit => "S.literal(%raw(`null`))->S.variant(_ => ())".to_string(),
Self::Int => "S.int".to_string(),
Self::Unknown => "S.unknown".to_string(),
Self::Float => "GqlDbCustomTypes.Float.schema".to_string(),
Self::BigInt => "BigInt.schema".to_string(),
Self::BigDecimal => "BigDecimal.schema".to_string(),
Expand Down Expand Up @@ -484,6 +487,7 @@ impl RescriptTypeIdent {
| Self::BigDecimal
| Self::Address
| Self::String
| Self::Unknown
| Self::ID
| Self::Bool
| Self::Timestamp
Expand All @@ -502,6 +506,7 @@ impl RescriptTypeIdent {
match self {
Self::Unit => "()".to_string(),
Self::Int => "0".to_string(),
Self::Unknown => "%raw(`undefined`)".to_string(),
Self::Float => "0.0".to_string(),
Self::BigInt => "0n".to_string(),
Self::BigDecimal => "BigDecimal.zero".to_string(),
Expand Down Expand Up @@ -565,7 +570,7 @@ impl RescriptTypeIdent {

pub fn get_default_value_non_rescript(&self) -> String {
match self {
Self::Unit => "undefined".to_string(),
Self::Unit | Self::Unknown => "undefined".to_string(),
Self::Int | Self::Float => "0".to_string(),
Self::BigInt => "0n".to_string(),
Self::BigDecimal => "// default value not required since BigDecimal doesn't exist on \
Expand Down
49 changes: 43 additions & 6 deletions scenarios/fuel_test/abis/all-events-abi.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,20 @@
"concreteTypeId": "c0710b6731b1dd59799cf6bef33eee3b3b04a2e40e80a0724090215bbf2ca974",
"metadataTypeId": 12
},
{
"type": "struct std::bytes::Bytes",
"concreteTypeId": "cdd87b7d12fe505416570c294c884bca819364863efe3bf539245fa18515fbbb",
"metadataTypeId": 13
},
{
"type": "struct std::contract_id::ContractId",
"concreteTypeId": "29c10735d33b5159f0c71ee1dbd17b36a3e69e41f00fab0d42e1bd9f428d8a54",
"metadataTypeId": 13
"metadataTypeId": 15
},
{
"type": "struct std::vec::Vec<u64>",
"concreteTypeId": "d5bfe1d4e1ace20166c9b50cadd47e862020561bde24f5189cfc2723f5ed76f4",
"metadataTypeId": 15,
"metadataTypeId": 17,
"typeArguments": [
"1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0"
]
Expand Down Expand Up @@ -259,8 +264,36 @@
]
},
{
"type": "struct std::contract_id::ContractId",
"type": "struct std::bytes::Bytes",
"metadataTypeId": 13,
"components": [
{
"name": "buf",
"typeId": 14
},
{
"name": "len",
"typeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0"
}
]
},
{
"type": "struct std::bytes::RawBytes",
"metadataTypeId": 14,
"components": [
{
"name": "ptr",
"typeId": 7
},
{
"name": "cap",
"typeId": "1506e6f44c1d6291cdf46395a8e573276a4fa79e8ace3fc891e092ef32d1b0a0"
}
]
},
{
"type": "struct std::contract_id::ContractId",
"metadataTypeId": 15,
"components": [
{
"name": "bits",
Expand All @@ -270,7 +303,7 @@
},
{
"type": "struct std::vec::RawVec",
"metadataTypeId": 14,
"metadataTypeId": 16,
"components": [
{
"name": "ptr",
Expand All @@ -285,11 +318,11 @@
},
{
"type": "struct std::vec::Vec",
"metadataTypeId": 15,
"metadataTypeId": 17,
"components": [
{
"name": "buf",
"typeId": 14,
"typeId": 16,
"typeArguments": [
{
"name": "",
Expand Down Expand Up @@ -453,6 +486,10 @@
{
"logId": "15402277555065905665",
"concreteTypeId": "d5bfe1d4e1ace20166c9b50cadd47e862020561bde24f5189cfc2723f5ed76f4"
},
{
"logId": "14832741149864513620",
"concreteTypeId": "cdd87b7d12fe505416570c294c884bca819364863efe3bf539245fa18515fbbb"
}
],
"messagesTypes": [],
Expand Down
6 changes: 4 additions & 2 deletions scenarios/fuel_test/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ networks:
- name: ClearGreeting
- name: AllEvents
address:
- 0xbcad9115ac67d80538705c58f830c66c7ebdda8ee74a1bb2611f2f4e2eabf719
- 0x54a4b027fca0f718d3cb0cbdddb1beec670100b70c6856824befac0440dc0b84
abi_file_path: abis/all-events-abi.json
handler: src/AllEventsHandlers.ts
events:
Expand Down Expand Up @@ -53,5 +53,7 @@ networks:
logId: "10732353433239600734"
- name: Option2
logId: "8688528864679113840"
- name: Vec
- name: VecLog
logId: "15402277555065905665"
- name: BytesLog
logId: "14832741149864513620"
5 changes: 5 additions & 0 deletions scenarios/fuel_test/contracts/all-events/src/main.sw
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
contract;

use std::asset::{burn, mint, transfer};
use std::bytes::Bytes;

abi Token {
fn transfer_to_address(target: Address, asset_id: AssetId, coins: u64);
Expand Down Expand Up @@ -148,5 +149,9 @@ impl AllEvents for Contract {
vec.push(69);
vec.push(23);
log(vec);

let mut bytes = Bytes::new();
bytes.push(40u8);
log(bytes)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ abigen!(Contract(
),);

const ALL_EVENTS_CONTRACT: &str =
"0xbcad9115ac67d80538705c58f830c66c7ebdda8ee74a1bb2611f2f4e2eabf719";
"0x54a4b027fca0f718d3cb0cbdddb1beec670100b70c6856824befac0440dc0b84";

#[tokio::main]
async fn main() -> Result<()> {
Expand Down
24 changes: 17 additions & 7 deletions scenarios/fuel_test/src/AllEventsHandlers.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
/*
* Please refer to https://docs.envio.dev for a thorough guide on all Envio indexer features
*/
import { AllEvents, eventLog } from "generated";
import { AllEvents } from "generated";
import { expectType, TypeEqual } from "ts-expect";
import * as S from "rescript-schema";

type RemoveReadonly<T> = {
-readonly [key in keyof T]: RemoveReadonly<T[key]>;
};
type RemoveReadonly<T> = T extends {}
? {
-readonly [key in keyof T]: RemoveReadonly<T[key]>;
}
: T;

type AssertSchemaType<Target, Value extends S.Schema<unknown>> = TypeEqual<
type AssertSchemaType<Target, Schema> = TypeEqual<
RemoveReadonly<Target>,
S.Output<Value>
S.Output<Schema>
>;

const SExtra = {
Expand Down Expand Up @@ -196,7 +198,15 @@ AllEvents.Option2.handler(async ({ event }) => {
});

const vecLogSchema = S.array(SExtra.bigint);
AllEvents.Vec.handler(async ({ event }) => {
AllEvents.VecLog.handler(async ({ event }) => {
vecLogSchema.assert(event.params)!;
expectType<AssertSchemaType<typeof event.params, typeof vecLogSchema>>(true);
});

const bytesLogSchema = S.unknown;
AllEvents.BytesLog.handler(async ({ event }) => {
bytesLogSchema.assert(event.params)!;
expectType<AssertSchemaType<typeof event.params, typeof bytesLogSchema>>(
true
);
});

0 comments on commit dabd985

Please sign in to comment.