Skip to content

Commit

Permalink
Implement u256 support torii grpc (starkware-libs#1345)
Browse files Browse the repository at this point in the history
  • Loading branch information
broody committed Dec 30, 2023
1 parent bc5dd6f commit f5d6ea0
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
13 changes: 5 additions & 8 deletions crates/torii/grpc/src/types/schema.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crypto_bigint::{Encoding, U256};
use dojo_types::primitive::Primitive;
use dojo_types::schema::{Enum, EnumOption, Member, Struct, Ty};
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -198,14 +199,8 @@ impl TryFrom<proto::types::Primitive> for Primitive {
.map_err(ClientError::SliceError)?,
))
}
_ => return Err(ClientError::UnsupportedType),
}
}
proto::types::value::ValueType::StringValue(_string) => {
match proto::types::PrimitiveType::from_i32(primitive_type) {
Some(proto::types::PrimitiveType::U256) => {
// TODO: Handle u256
Primitive::U256(None)
Primitive::U256(Some(U256::from_be_slice(bytes)))
}
_ => return Err(ClientError::UnsupportedType),
}
Expand Down Expand Up @@ -234,7 +229,9 @@ impl TryFrom<Primitive> for proto::types::Primitive {
Primitive::U128(u128) => {
u128.map(|val| ValueType::ByteValue(val.to_be_bytes().to_vec()))
}
Primitive::U256(u256) => u256.map(|val| ValueType::StringValue(val.to_string())),
Primitive::U256(u256) => {
u256.map(|val| ValueType::ByteValue(val.to_be_bytes().to_vec()))
}
Primitive::Felt252(felt) => {
felt.map(|val| ValueType::ByteValue(val.to_bytes_be().to_vec()))
}
Expand Down
9 changes: 7 additions & 2 deletions crates/torii/types-test/src/contracts.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,18 @@ mod records {
}

let type_felt: felt252 = record_idx.into();
let random_u8: u8 = random(pedersen::pedersen(seed(), record_idx.into()), 0, 100)
let random_u8 = random(pedersen::pedersen(seed(), record_idx.into()), 0, 100)
.try_into()
.unwrap();
let random_u128 = random(
pedersen::pedersen(seed(), record_idx.into()),
0,
0xffffffffffffffffffffffffffffffff_u128
);
let composite_u256 = u256 {
low: random_u128,
high: random_u128
};

let record_id = world.uuid();
let subrecord_id = world.uuid();
Expand Down Expand Up @@ -98,7 +102,8 @@ mod records {
type_string: 2,
},
random_u8,
random_u128
random_u128,
composite_u256
},
RecordSibling {
record_id, random_u8
Expand Down
1 change: 1 addition & 0 deletions crates/torii/types-test/src/models.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct Record {
type_nested_two: NestedMost,
random_u8: u8,
random_u128: u128,
composite_u256: u256,
}

#[derive(Model, Copy, Drop, Serde)]
Expand Down

0 comments on commit f5d6ea0

Please sign in to comment.