Skip to content

Commit

Permalink
update: v0.4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
ALPAC-4 committed Aug 16, 2024
1 parent 8fa8e21 commit 0a6aa67
Show file tree
Hide file tree
Showing 151 changed files with 30,082 additions and 11,026 deletions.
267 changes: 226 additions & 41 deletions initia_stdlib/sources/account.move

Large diffs are not rendered by default.

93 changes: 59 additions & 34 deletions initia_stdlib/sources/address.move
Original file line number Diff line number Diff line change
@@ -1,55 +1,70 @@
module initia_std::address {
use std::string::{Self, String};
use std::string::String;
use std::bcs;
use initia_std::from_bcs;
use initia_std::query;
use initia_std::simple_json;
use initia_std::json;
use initia_std::option;

struct FromSdkRequest has copy, drop {
sdk_addr: String,
}

struct FromSdkResponse has copy, drop {
vm_addr: address,
}

public fun from_sdk(sdk_addr: String): address {
let obj = simple_json::empty();
simple_json::set_object(&mut obj, option::none<String>());
simple_json::increase_depth(&mut obj);

simple_json::set_string(&mut obj, option::some(string::utf8(b"sdk_addr")), sdk_addr);

let req = json::stringify(simple_json::to_json_object(&obj));
let res = query::query_custom(b"from_sdk_address", *string::bytes(&req));
let res = simple_json::from_json_object(json::parse(string::utf8(res)));

simple_json::increase_depth(&mut res);
let (_, data) = json::unpack_elem(simple_json::borrow(&mut res));

from_string(json::as_string(data))
let res =
json::unmarshal<FromSdkResponse>(
query::query_custom(
b"from_sdk_address",
json::marshal(&FromSdkRequest { sdk_addr: sdk_addr, }),
),
);

res.vm_addr
}

public fun to_sdk(vm_addr: address): String {
let obj = simple_json::empty();
simple_json::set_object(&mut obj, option::none<String>());
simple_json::increase_depth(&mut obj);

simple_json::set_string(&mut obj, option::some(string::utf8(b"vm_addr")), to_string(vm_addr));
struct ToSdkRequest has copy, drop {
vm_addr: address,
}

let req = json::stringify(simple_json::to_json_object(&obj));
let res = query::query_custom(b"to_sdk_address", *string::bytes(&req));
let res = simple_json::from_json_object(json::parse(string::utf8(res)));
struct ToSdkResponse has copy, drop {
sdk_addr: String,
}

simple_json::increase_depth(&mut res);
let (_, data) = json::unpack_elem(simple_json::borrow(&mut res));
public fun to_sdk(vm_addr: address): String {
let res =
json::unmarshal<ToSdkResponse>(
query::query_custom(
b"to_sdk_address",
json::marshal(&ToSdkRequest { vm_addr: vm_addr, }),
),
);

json::as_string(data)
res.sdk_addr
}

#[test_only]
use std::string;

#[test]
fun test_to_string() {
let addr = @0x123abc;
let addr_str = string::utf8(b"0x0000000000000000000000000000000000000000000000000000000000123abc");
let addr_str =
string::utf8(
b"0x0000000000000000000000000000000000000000000000000000000000123abc"
);
assert!(to_string(addr) == addr_str, 0)
}

#[test]
fun test_from_string() {
let addr = @0x908def;
let addr_str = string::utf8(b"0x0000000000000000000000000000000000000000000000000000000000908def");
let addr_str =
string::utf8(
b"0x0000000000000000000000000000000000000000000000000000000000908def"
);
assert!(from_string(addr_str) == addr, 0)
}

Expand All @@ -67,6 +82,16 @@ module initia_std::address {
assert!(addr == from_sdk(addr_sdk), 0)
}

public native fun to_string(addr: address): String;
public native fun from_string(addr_str: String): address;
}
// string <> address
native public fun to_string(addr: address): String;
native public fun from_string(addr_str: String): address;

// bytes <> address
public fun to_bytes(addr: address): vector<u8> {
bcs::to_bytes(&addr)
}

public fun from_bytes(bytes: vector<u8>): address {
from_bcs::to_address(bytes)
}
}
14 changes: 8 additions & 6 deletions initia_stdlib/sources/any.move
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,15 @@ module initia_std::any {
/// Pack a value into the `Any` representation. Because Any can be stored and dropped, this is
/// also required from `T`.
public fun pack<T: drop + store>(x: T): Any {
Any {
type_name: type_info::type_name<T>(),
data: to_bytes(&x)
}
Any { type_name: type_info::type_name<T>(), data: to_bytes(&x) }
}

/// Unpack a value from the `Any` representation. This aborts if the value has not the expected type `T`.
public fun unpack<T>(x: Any): T {
assert!(type_info::type_name<T>() == x.type_name, error::invalid_argument(ETYPE_MISMATCH));
assert!(
type_info::type_name<T>() == x.type_name,
error::invalid_argument(ETYPE_MISMATCH),
);
from_bytes<T>(x.data)
}

Expand All @@ -45,7 +45,9 @@ module initia_std::any {
}

#[test_only]
struct S has store, drop { x: u64 }
struct S has store, drop {
x: u64
}

#[test]
fun test_any() {
Expand Down
4 changes: 2 additions & 2 deletions initia_stdlib/sources/base64.move
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ module initia_std::base64 {
decode(*string::bytes(&str))
}

public native fun encode(bytes: vector<u8>): vector<u8>;
public native fun decode(bytes: vector<u8>): vector<u8>;
native public fun encode(bytes: vector<u8>): vector<u8>;
native public fun decode(bytes: vector<u8>): vector<u8>;
}
Loading

0 comments on commit 0a6aa67

Please sign in to comment.