sui_graphql_rpc/types/move_value.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0
use async_graphql::*;
use move_core_types::{
account_address::AccountAddress,
annotated_value as A, ident_str,
identifier::{IdentStr, Identifier},
language_storage::{StructTag, TypeTag},
};
use serde::{Deserialize, Serialize};
use sui_types::object::bounded_visitor::BoundedVisitor;
use crate::data::package_resolver::PackageResolver;
use crate::{error::Error, types::json::Json, types::move_type::unexpected_signer_error};
use super::{base64::Base64, big_int::BigInt, move_type::MoveType, sui_address::SuiAddress};
const STD: AccountAddress = AccountAddress::ONE;
const SUI: AccountAddress = AccountAddress::TWO;
const MOD_ASCII: &IdentStr = ident_str!("ascii");
const MOD_OBJECT: &IdentStr = ident_str!("object");
const MOD_OPTION: &IdentStr = ident_str!("option");
const MOD_STRING: &IdentStr = ident_str!("string");
const TYP_ID: &IdentStr = ident_str!("ID");
const TYP_OPTION: &IdentStr = ident_str!("Option");
const TYP_STRING: &IdentStr = ident_str!("String");
const TYP_UID: &IdentStr = ident_str!("UID");
#[derive(SimpleObject)]
#[graphql(complex)]
pub(crate) struct MoveValue {
/// The value's Move type.
#[graphql(name = "type")]
type_: MoveType,
/// The BCS representation of this value, Base64 encoded.
bcs: Base64,
}
scalar!(
MoveData,
"MoveData",
"The contents of a Move Value, corresponding to the following recursive type:
type MoveData =
{ Address: SuiAddress }
| { UID: SuiAddress }
| { ID: SuiAddress }
| { Bool: bool }
| { Number: BigInt }
| { String: string }
| { Vector: [MoveData] }
| { Option: MoveData? }
| { Struct: [{ name: string , value: MoveData }] }
| { Variant: {
name: string,
fields: [{ name: string, value: MoveData }],
}"
);
#[derive(Serialize, Deserialize, Debug)]
pub(crate) enum MoveData {
Address(SuiAddress),
#[serde(rename = "UID")]
Uid(SuiAddress),
#[serde(rename = "ID")]
Id(SuiAddress),
Bool(bool),
Number(BigInt),
String(String),
Vector(Vec<MoveData>),
Option(Option<Box<MoveData>>),
Struct(Vec<MoveField>),
Variant(MoveVariant),
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct MoveVariant {
name: String,
fields: Vec<MoveField>,
}
#[derive(Serialize, Deserialize, Debug)]
pub(crate) struct MoveField {
name: String,
value: MoveData,
}
/// An instance of a Move type.
#[ComplexObject]
impl MoveValue {
/// Structured contents of a Move value.
async fn data(&self, ctx: &Context<'_>) -> Result<MoveData> {
let resolver: &PackageResolver = ctx
.data()
.map_err(|_| Error::Internal("Unable to fetch Package Cache.".to_string()))
.extend()?;
let Some(layout) = self.type_.layout_impl(resolver).await.extend()? else {
return Err(Error::Internal(
"Move value must have valid layout".to_string(),
))
.extend();
};
// Factor out into its own non-GraphQL, non-async function for better testability
self.data_impl(layout).extend()
}
/// Representation of a Move value in JSON, where:
///
/// - Addresses, IDs, and UIDs are represented in canonical form, as JSON strings.
/// - Bools are represented by JSON boolean literals.
/// - u8, u16, and u32 are represented as JSON numbers.
/// - u64, u128, and u256 are represented as JSON strings.
/// - Vectors are represented by JSON arrays.
/// - Structs are represented by JSON objects.
/// - Empty optional values are represented by `null`.
///
/// This form is offered as a less verbose convenience in cases where the layout of the type is
/// known by the client.
async fn json(&self, ctx: &Context<'_>) -> Result<Json> {
let resolver: &PackageResolver = ctx
.data()
.map_err(|_| Error::Internal("Unable to fetch Package Cache.".to_string()))
.extend()?;
let Some(layout) = self.type_.layout_impl(resolver).await.extend()? else {
return Err(Error::Internal(
"Move value must have valid layout".to_string(),
))
.extend();
};
// Factor out into its own non-GraphQL, non-async function for better testability
self.json_impl(layout).extend()
}
}
impl MoveValue {
pub fn new(tag: TypeTag, bcs: Base64) -> Self {
let type_ = MoveType::from(tag);
Self { type_, bcs }
}
fn value_impl(&self, layout: A::MoveTypeLayout) -> Result<A::MoveValue, Error> {
// TODO (annotated-visitor): deserializing directly using a custom visitor.
BoundedVisitor::deserialize_value(&self.bcs.0[..], &layout).map_err(|_| {
let type_tag: TypeTag = (&layout).into();
Error::Internal(format!(
"Failed to deserialize Move value for type: {}",
type_tag
))
})
}
fn data_impl(&self, layout: A::MoveTypeLayout) -> Result<MoveData, Error> {
MoveData::try_from(self.value_impl(layout)?)
}
fn json_impl(&self, layout: A::MoveTypeLayout) -> Result<Json, Error> {
Ok(try_to_json_value(self.value_impl(layout)?)?.into())
}
}
impl TryFrom<A::MoveValue> for MoveData {
type Error = Error;
fn try_from(value: A::MoveValue) -> Result<Self, Error> {
use A::MoveValue as V;
Ok(match value {
V::U8(n) => Self::Number(BigInt::from(n)),
V::U16(n) => Self::Number(BigInt::from(n)),
V::U32(n) => Self::Number(BigInt::from(n)),
V::U64(n) => Self::Number(BigInt::from(n)),
V::U128(n) => Self::Number(BigInt::from(n)),
V::U256(n) => Self::Number(BigInt::from(n)),
V::Bool(b) => Self::Bool(b),
V::Address(a) => Self::Address(a.into()),
V::Vector(v) => Self::Vector(
v.into_iter()
.map(MoveData::try_from)
.collect::<Result<Vec<_>, _>>()?,
),
V::Struct(s) => {
let A::MoveStruct { type_, fields } = s;
if is_type(&type_, &STD, MOD_OPTION, TYP_OPTION) {
// 0x1::option::Option
Self::Option(match extract_option(&type_, fields)? {
Some(value) => Some(Box::new(MoveData::try_from(value)?)),
None => None,
})
} else if is_type(&type_, &STD, MOD_ASCII, TYP_STRING)
|| is_type(&type_, &STD, MOD_STRING, TYP_STRING)
{
// 0x1::ascii::String, 0x1::string::String
Self::String(extract_string(&type_, fields)?)
} else if is_type(&type_, &SUI, MOD_OBJECT, TYP_UID) {
// 0x2::object::UID
Self::Uid(extract_uid(&type_, fields)?.into())
} else if is_type(&type_, &SUI, MOD_OBJECT, TYP_ID) {
// 0x2::object::ID
Self::Id(extract_id(&type_, fields)?.into())
} else {
// Arbitrary structs
let fields: Result<Vec<_>, _> =
fields.into_iter().map(MoveField::try_from).collect();
Self::Struct(fields?)
}
}
V::Variant(A::MoveVariant {
type_: _,
variant_name,
tag: _,
fields,
}) => {
let fields = fields
.into_iter()
.map(MoveField::try_from)
.collect::<Result<_, _>>()?;
Self::Variant(MoveVariant {
name: variant_name.to_string(),
fields,
})
}
// Sui does not support `signer` as a type.
V::Signer(_) => return Err(unexpected_signer_error()),
})
}
}
impl TryFrom<(Identifier, A::MoveValue)> for MoveField {
type Error = Error;
fn try_from((ident, value): (Identifier, A::MoveValue)) -> Result<Self, Error> {
Ok(MoveField {
name: ident.to_string(),
value: MoveData::try_from(value)?,
})
}
}
fn try_to_json_value(value: A::MoveValue) -> Result<Value, Error> {
use A::MoveValue as V;
Ok(match value {
V::U8(n) => Value::Number(n.into()),
V::U16(n) => Value::Number(n.into()),
V::U32(n) => Value::Number(n.into()),
V::U64(n) => Value::String(n.to_string()),
V::U128(n) => Value::String(n.to_string()),
V::U256(n) => Value::String(n.to_string()),
V::Bool(b) => Value::Boolean(b),
V::Address(a) => Value::String(a.to_canonical_string(/* with_prefix */ true)),
V::Vector(xs) => Value::List(
xs.into_iter()
.map(try_to_json_value)
.collect::<Result<_, _>>()?,
),
V::Struct(s) => {
let A::MoveStruct { type_, fields } = s;
if is_type(&type_, &STD, MOD_OPTION, TYP_OPTION) {
// 0x1::option::Option
match extract_option(&type_, fields)? {
Some(value) => try_to_json_value(value)?,
None => Value::Null,
}
} else if is_type(&type_, &STD, MOD_ASCII, TYP_STRING)
|| is_type(&type_, &STD, MOD_STRING, TYP_STRING)
{
// 0x1::ascii::String, 0x1::string::String
Value::String(extract_string(&type_, fields)?)
} else if is_type(&type_, &SUI, MOD_OBJECT, TYP_UID) {
// 0x2::object::UID
Value::String(
extract_uid(&type_, fields)?.to_canonical_string(/* with_prefix */ true),
)
} else if is_type(&type_, &SUI, MOD_OBJECT, TYP_ID) {
// 0x2::object::ID
Value::String(
extract_id(&type_, fields)?.to_canonical_string(/* with_prefix */ true),
)
} else {
// Arbitrary structs
Value::Object(
fields
.into_iter()
.map(|(name, value)| {
Ok((Name::new(name.to_string()), try_to_json_value(value)?))
})
.collect::<Result<_, Error>>()?,
)
}
}
V::Variant(A::MoveVariant {
type_: _,
variant_name,
tag: _,
fields,
}) => {
let fields = fields
.into_iter()
.map(|(name, value)| Ok((Name::new(name.to_string()), try_to_json_value(value)?)))
.collect::<Result<_, Error>>()?;
Value::Object(
vec![(Name::new(variant_name.to_string()), Value::Object(fields))]
.into_iter()
.collect(),
)
}
// Sui does not support `signer` as a type.
V::Signer(_) => return Err(unexpected_signer_error()),
})
}
fn is_type(tag: &StructTag, address: &AccountAddress, module: &IdentStr, name: &IdentStr) -> bool {
&tag.address == address
&& tag.module.as_ident_str() == module
&& tag.name.as_ident_str() == name
}
macro_rules! extract_field {
($type:expr, $fields:expr, $name:ident) => {{
let _name = ident_str!(stringify!($name));
let _type = $type;
if let Some(value) = ($fields)
.into_iter()
.find_map(|(name, value)| (&*name == _name).then_some(value))
{
value
} else {
return Err(Error::Internal(format!(
"Couldn't find expected field '{_name}' of {_type}."
)));
}
}};
}
/// Extracts a vector of bytes from `value`, assuming it's a `MoveValue::Vector` where all the
/// values are `MoveValue::U8`s.
fn extract_bytes(value: A::MoveValue) -> Result<Vec<u8>, Error> {
use A::MoveValue as V;
let V::Vector(elements) = value else {
return Err(Error::Internal("Expected a vector.".to_string()));
};
let mut bytes = Vec::with_capacity(elements.len());
for element in elements {
let V::U8(byte) = element else {
return Err(Error::Internal("Expected a byte.".to_string()));
};
bytes.push(byte)
}
Ok(bytes)
}
/// Extracts a Rust String from the contents of a Move Struct assuming that struct matches the
/// contents of Move String:
///
/// ```notrust
/// { bytes: vector<u8> }
/// ```
///
/// Which is conformed to by both `std::ascii::String` and `std::string::String`.
fn extract_string(
type_: &StructTag,
fields: Vec<(Identifier, A::MoveValue)>,
) -> Result<String, Error> {
let bytes = extract_bytes(extract_field!(type_, fields, bytes))?;
String::from_utf8(bytes).map_err(|e| {
const PREFIX: usize = 30;
let bytes = e.as_bytes();
// Provide a sample of the string in question.
let sample = if bytes.len() < PREFIX {
String::from_utf8_lossy(bytes)
} else {
String::from_utf8_lossy(&bytes[..PREFIX - 3]) + "..."
};
Error::Internal(format!("{e} in {sample:?}"))
})
}
/// Extracts an address from the contents of a Move Struct, assuming the struct matches the
/// following shape:
///
/// ```notrust
/// { bytes: address }
/// ```
///
/// Which matches `0x2::object::ID`.
fn extract_id(
type_: &StructTag,
fields: Vec<(Identifier, A::MoveValue)>,
) -> Result<AccountAddress, Error> {
use A::MoveValue as V;
let V::Address(addr) = extract_field!(type_, fields, bytes) else {
return Err(Error::Internal(
"Expected ID.bytes to have type address.".to_string(),
));
};
Ok(addr)
}
/// Extracts an address from the contents of a Move Struct, assuming the struct matches the
/// following shape:
///
/// ```notrust
/// { id: 0x2::object::ID { bytes: address } }
/// ```
///
/// Which matches `0x2::object::UID`.
fn extract_uid(
type_: &StructTag,
fields: Vec<(Identifier, A::MoveValue)>,
) -> Result<AccountAddress, Error> {
use A::MoveValue as V;
let V::Struct(s) = extract_field!(type_, fields, id) else {
return Err(Error::Internal(
"Expected UID.id to be a struct".to_string(),
));
};
let A::MoveStruct { type_, fields } = s;
if !is_type(&type_, &SUI, MOD_OBJECT, TYP_ID) {
return Err(Error::Internal(
"Expected UID.id to have type ID.".to_string(),
));
}
extract_id(&type_, fields)
}
/// Extracts a value from the contents of a Move Struct, assuming the struct matches the following
/// shape:
///
/// ```notrust
/// { vec: vector<T> }
/// ```
///
/// Where `vec` contains at most one element. This matches the shape of `0x1::option::Option<T>`.
fn extract_option(
type_: &StructTag,
fields: Vec<(Identifier, A::MoveValue)>,
) -> Result<Option<A::MoveValue>, Error> {
let A::MoveValue::Vector(mut elements) = extract_field!(type_, fields, vec) else {
return Err(Error::Internal(
"Expected Option.vec to be a vector.".to_string(),
));
};
if elements.len() > 1 {
return Err(Error::Internal(
"Expected Option.vec to contain at most one element.".to_string(),
));
};
Ok(elements.pop())
}
#[cfg(test)]
mod tests {
use std::str::FromStr;
use expect_test::expect;
use move_core_types::{
annotated_value::{self as A, MoveFieldLayout, MoveStructLayout as S, MoveTypeLayout as L},
u256::U256,
};
use super::*;
macro_rules! struct_layout {
($type:literal { $($name:literal : $layout:expr),* $(,)?}) => {
A::MoveTypeLayout::Struct(Box::new(S {
type_: StructTag::from_str($type).expect("Failed to parse struct"),
fields: vec![$(MoveFieldLayout {
name: ident_str!($name).to_owned(),
layout: $layout,
}),*]
}))
}
}
macro_rules! vector_layout {
($inner:expr) => {
A::MoveTypeLayout::Vector(Box::new($inner))
};
}
fn address(a: &str) -> SuiAddress {
SuiAddress::from_str(a).unwrap()
}
fn data<T: Serialize>(layout: A::MoveTypeLayout, data: T) -> Result<MoveData, Error> {
let tag: TypeTag = (&layout).into();
// The format for type from its `Display` impl does not technically match the format that
// the RPC expects from the data layer (where a type's package should be canonicalized), but
// it will suffice.
data_with_tag(format!("{}", tag), layout, data)
}
fn data_with_tag<T: Serialize>(
tag: impl Into<String>,
layout: A::MoveTypeLayout,
data: T,
) -> Result<MoveData, Error> {
let tag = TypeTag::from_str(tag.into().as_str()).unwrap();
let type_ = MoveType::from(tag);
let bcs = Base64(bcs::to_bytes(&data).unwrap());
MoveValue { type_, bcs }.data_impl(layout)
}
fn json<T: Serialize>(layout: A::MoveTypeLayout, data: T) -> Result<Json, Error> {
let tag: TypeTag = (&layout).into();
let type_ = MoveType::from(tag);
let bcs = Base64(bcs::to_bytes(&data).unwrap());
MoveValue { type_, bcs }.json_impl(layout)
}
#[test]
fn bool_data() {
let v = data(L::Bool, true);
let expect = expect!["Ok(Bool(true))"];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn bool_json() {
let v = json(L::Bool, true).unwrap();
let expect = expect!["true"];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn u8_data() {
let v = data(L::U8, 42u8);
let expect = expect![[r#"Ok(Number(BigInt("42")))"#]];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn u8_json() {
let v = json(L::U8, 42u8).unwrap();
let expect = expect!["42"];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn u16_data() {
let v = data(L::U16, 424u16);
let expect = expect![[r#"Ok(Number(BigInt("424")))"#]];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn u16_json() {
let v = json(L::U16, 424u16).unwrap();
let expect = expect!["424"];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn u32_data() {
let v = data(L::U32, 424_242u32);
let expect = expect![[r#"Ok(Number(BigInt("424242")))"#]];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn u32_json() {
let v = json(L::U32, 424_242u32).unwrap();
let expect = expect!["424242"];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn u64_data() {
let v = data(L::U64, 42_424_242_424u64);
let expect = expect![[r#"Ok(Number(BigInt("42424242424")))"#]];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn u64_json() {
let v = json(L::U64, 42_424_242_424u64).unwrap();
let expect = expect![[r#""42424242424""#]];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn u128_data() {
let v = data(L::U128, 424_242_424_242_424_242_424u128);
let expect = expect![[r#"Ok(Number(BigInt("424242424242424242424")))"#]];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn u128_json() {
let v = json(L::U128, 424_242_424_242_424_242_424u128).unwrap();
let expect = expect![[r#""424242424242424242424""#]];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn u256_data() {
let v = data(
L::U256,
U256::from_str("42424242424242424242424242424242424242424").unwrap(),
);
let expect =
expect![[r#"Ok(Number(BigInt("42424242424242424242424242424242424242424")))"#]];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn u256_json() {
let v = json(
L::U256,
U256::from_str("42424242424242424242424242424242424242424").unwrap(),
)
.unwrap();
let expect = expect![[r#""42424242424242424242424242424242424242424""#]];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn ascii_string_data() {
let l = struct_layout!("0x1::ascii::String" {
"bytes": vector_layout!(L::U8)
});
let v = data(l, "The quick brown fox");
let expect = expect![[r#"Ok(String("The quick brown fox"))"#]];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn ascii_string_json() {
let l = struct_layout!("0x1::ascii::String" {
"bytes": vector_layout!(L::U8)
});
let v = json(l, "The quick brown fox").unwrap();
let expect = expect![[r#""The quick brown fox""#]];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn utf8_string_data() {
let l = struct_layout!("0x1::string::String" {
"bytes": vector_layout!(L::U8)
});
let v = data(l, "jumped over the lazy dog.");
let expect = expect![[r#"Ok(String("jumped over the lazy dog."))"#]];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn utf8_string_json() {
let l = struct_layout!("0x1::string::String" {
"bytes": vector_layout!(L::U8)
});
let v = json(l, "jumped over the lazy dog.").unwrap();
let expect = expect![[r#""jumped over the lazy dog.""#]];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn string_encoding_error() {
let l = struct_layout!("0x1::string::String" {
"bytes": vector_layout!(L::U8)
});
let mut bytes = "Lorem ipsum dolor sit amet consectetur".as_bytes().to_vec();
bytes[5] = 0xff;
let v = data(l, bytes);
let expect = expect![[r#"
Err(
Internal(
"invalid utf-8 sequence of 1 bytes from index 5 in \"Lorem�ipsum dolor sit amet ...\"",
),
)"#]];
expect.assert_eq(&format!("{v:#?}"));
}
#[test]
fn address_data() {
let v = data(L::Address, address("0x42"));
let expect = expect!["Ok(Address(SuiAddress([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 66])))"];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn address_json() {
let v = json(L::Address, address("0x42")).unwrap();
let expect =
expect![[r#""0x0000000000000000000000000000000000000000000000000000000000000042""#]];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn uid_data() {
let l = struct_layout!("0x2::object::UID" {
"id": struct_layout!("0x2::object::ID" {
"bytes": L::Address,
})
});
let v = data(l, address("0x42"));
let expect = expect!["Ok(Uid(SuiAddress([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 66])))"];
expect.assert_eq(&format!("{v:?}"));
}
#[test]
fn uid_json() {
let l = struct_layout!("0x2::object::UID" {
"id": struct_layout!("0x2::object::ID" {
"bytes": L::Address,
})
});
let v = json(l, address("0x42")).unwrap();
let expect =
expect![[r#""0x0000000000000000000000000000000000000000000000000000000000000042""#]];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn compound_data() {
let l = struct_layout!("0x42::foo::Bar" {
"baz": struct_layout!("0x1::option::Option" { "vec": vector_layout!(L::U8) }),
"qux": vector_layout!(struct_layout!("0x43::xy::Zzy" {
"quy": L::U16,
"quz": struct_layout!("0x1::option::Option" {
"vec": vector_layout!(struct_layout!("0x1::ascii::String" {
"bytes": vector_layout!(L::U8),
}))
}),
"frob": L::Address,
})),
});
let v = data(
l,
(
vec![] as Vec<Vec<u8>>,
vec![
(44u16, vec!["Hello, world!"], address("0x45")),
(46u16, vec![], address("0x47")),
],
),
);
let expect = expect![[r#"
Ok(
Struct(
[
MoveField {
name: "baz",
value: Option(
None,
),
},
MoveField {
name: "qux",
value: Vector(
[
Struct(
[
MoveField {
name: "quy",
value: Number(
BigInt(
"44",
),
),
},
MoveField {
name: "quz",
value: Option(
Some(
String(
"Hello, world!",
),
),
),
},
MoveField {
name: "frob",
value: Address(
SuiAddress(
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
69,
],
),
),
},
],
),
Struct(
[
MoveField {
name: "quy",
value: Number(
BigInt(
"46",
),
),
},
MoveField {
name: "quz",
value: Option(
None,
),
},
MoveField {
name: "frob",
value: Address(
SuiAddress(
[
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
0,
71,
],
),
),
},
],
),
],
),
},
],
),
)"#]];
expect.assert_eq(&format!("{v:#?}"));
}
#[test]
fn compound_json() {
let l = struct_layout!("0x42::foo::Bar" {
"baz": struct_layout!("0x1::option::Option" { "vec": vector_layout!(L::U8) }),
"qux": vector_layout!(struct_layout!("0x43::xy::Zzy" {
"quy": L::U16,
"quz": struct_layout!("0x1::option::Option" {
"vec": vector_layout!(struct_layout!("0x1::ascii::String" {
"bytes": vector_layout!(L::U8),
}))
}),
"frob": L::Address,
})),
});
let v = json(
l,
(
vec![] as Vec<Vec<u8>>,
vec![
(44u16, vec!["Hello, world!"], address("0x45")),
(46u16, vec![], address("0x47")),
],
),
)
.unwrap();
let expect = expect![[
r#"{baz: null,qux: [{quy: 44,quz: "Hello, world!",frob: "0x0000000000000000000000000000000000000000000000000000000000000045"},{quy: 46,quz: null,frob: "0x0000000000000000000000000000000000000000000000000000000000000047"}]}"#
]];
expect.assert_eq(&format!("{v}"));
}
#[test]
fn signer_value() {
let v = data(L::Signer, address("0x42"));
let expect = expect![[r#"
Err(
Internal(
"Unexpected value of type: signer.",
),
)"#]];
expect.assert_eq(&format!("{v:#?}"));
}
#[test]
fn signer_json() {
let err = json(L::Signer, address("0x42")).unwrap_err();
let expect = expect![[r#"Internal("Unexpected value of type: signer.")"#]];
expect.assert_eq(&format!("{err:?}"));
}
#[test]
fn signer_nested_data() {
let v = data(
vector_layout!(L::Signer),
vec![address("0x42"), address("0x43")],
);
let expect = expect![[r#"
Err(
Internal(
"Unexpected value of type: signer.",
),
)"#]];
expect.assert_eq(&format!("{v:#?}"));
}
#[test]
fn signer_nested_json() {
let err = json(
vector_layout!(L::Signer),
vec![address("0x42"), address("0x43")],
)
.unwrap_err();
let expect = expect![[r#"Internal("Unexpected value of type: signer.")"#]];
expect.assert_eq(&format!("{err:?}"));
}
}