Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions examples/demo/app/tests/gtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,8 @@ async fn dog_barks() {

#[tokio::test]
async fn dog_walks() {
use demo_client::dog::{Dog as _, events::DogEvents};
use demo_client::dog::Dog as _;
use demo_client::walker_service::{WalkerService as _, events::WalkerServiceEvents};
// Arrange
let (env, code_id, _gas_limit) = create_env();

Expand All @@ -245,22 +246,23 @@ async fn dog_walks() {
.await
.unwrap();

let mut dog_client = demo_program.dog();
let dog_listener = dog_client.listener();
let mut dog_events = dog_listener.listen().await.unwrap();
let dog_client = demo_program.dog();
let mut walker_client = dog_client.walker_service();
let listener = walker_client.listener();
let mut events = listener.listen().await.unwrap();

// Act
dog_client.walk(10, 20).await.unwrap();
walker_client.walk(10, 20).await.unwrap();

// Assert
let position = dog_client.position().await.unwrap();
let event = dog_events.next().await.unwrap();
let position = walker_client.position().await.unwrap();
let event = events.next().await.unwrap();

assert_eq!(position, (11, 19));
assert_eq!(
(
demo_program.id(),
DogEvents::Walked {
WalkerServiceEvents::Walked {
from: (1, -1),
to: (11, 19)
}
Expand All @@ -272,6 +274,7 @@ async fn dog_walks() {
#[tokio::test]
async fn dog_weights() {
use demo_client::dog::Dog as _;
use demo_client::mammal_service::MammalService as _;
// Arrange
let (env, code_id, _gas_limit) = create_env();

Expand All @@ -285,8 +288,9 @@ async fn dog_weights() {
.unwrap();

let dog_client = demo_program.dog();
let mammal_client = dog_client.mammal_service();

let avg_weight = dog_client.avg_weight().await.unwrap();
let avg_weight = mammal_client.avg_weight().await.unwrap();

assert_eq!(avg_weight, 42);
}
Expand Down
2 changes: 2 additions & 0 deletions examples/demo/client/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ fn main() {

ClientGenerator::from_idl_path(&idl_file_path)
.with_mocks("with_mocks")
.with_external_type("NonZeroU8", "core::num::NonZeroU8")
.with_external_type("NonZeroU32", "core::num::NonZeroU32")
.generate_to(client_rs_file_path)
.unwrap();
}
166 changes: 96 additions & 70 deletions examples/demo/client/demo_client.idl
Original file line number Diff line number Diff line change
@@ -1,57 +1,19 @@
program Demo {
constructors {
/// Program constructor (called once at the very beginning of the program lifetime)
Default();
/// Another program constructor (called once at the very beginning of the program lifetime)
New(counter: Option<u32>, dog_position: Option<(i32, i32)>);
}
services {
PingPong,
Counter,
Dog,
References,
ThisThat,
ValueFee,
Chaos,
}
types {
struct ReferenceCount(u32);

struct DoThatParam {
p1: NonZeroU32,
p2: ActorId,
p3: ManyVariants,
}

enum ManyVariants {
One,
Two(u32),
Three(Option<U256>),
Four { a: u32, b: Option<u16> },
Five(string, H256),
Six(u32),
}

enum ManyVariantsReply {
One,
Two,
Three,
Four,
Five,
Six,
}

struct TupleStruct(bool);
}
}
!@sails: 0.9.2

service PingPong {
functions {
Ping(input: string) -> Result<string, string>;
Ping(input: String) -> String throws String;
}
}

service Counter {
events {
/// Emitted when a new value is added to the counter
Added(u32),
/// Emitted when a value is subtracted from the counter
Subtracted(u32),
}
functions {
/// Add a value to the counter
Add(value: u32) -> u32;
Expand All @@ -61,75 +23,139 @@ service Counter {
@query
Value() -> u32;
}
}

service WalkerService {
events {
/// Emitted when a new value is added to the counter
Added(u32),
/// Emitted when a value is subtracted from the counter
Subtracted(u32),
Walked {
from: (i32, i32),
to: (i32, i32),
},
}
functions {
Walk(dx: i32, dy: i32);
@query
Position() -> (i32, i32);
}
}

service Dog {
service MammalService {
functions {
MakeSound() -> string;
Walk(dx: i32, dy: i32) -> ();
MakeSound() -> String;
@query
AvgWeight() -> u32;
@query
Position() -> (i32, i32);
}
}

service Dog {
extends {
WalkerService,
MammalService,
}
events {
Barked,
Walked { from: (i32, i32), to: (i32, i32) },
}
functions {
MakeSound() -> String;
}
}

service References {
functions {
Add(v: u32) -> u32;
AddByte(byte: u8) -> [u8];
GuessNum(number: u8) -> Result<string, string>;
GuessNum(number: u8) -> String throws String;
Incr() -> ReferenceCount;
SetNum(number: u8) -> Result<(), string>;
SetNum(number: u8) throws String;
@query
Baked() -> string;
Baked() -> String;
@query
LastByte() -> Option<u8>;
@query
Message() -> Option<string>;
Message() -> Option<String>;
}
types {
struct ReferenceCount(u32);
}
}

service ThisThat {
functions {
DoThat(param: DoThatParam) -> Result<(ActorId, NonZeroU32, ManyVariantsReply), (string,)>;
DoThis(p1: u32, p2: string, p3: (Option<H160>, NonZeroU8), p4: TupleStruct) -> (string, u32);
Noop() -> ();
DoThat(param: DoThatParam) -> (ActorId, NonZeroU32, ManyVariantsReply) throws (String);
DoThis(p1: u32, p2: String, p3: (Option<H160>, NonZeroU8), p4: TupleStruct) -> (String, u32);
Noop();
@query
That() -> Result<string, string>;
That() -> String throws String;
@query
This() -> u32;
}
types {
struct DoThatParam {
p1: NonZeroU32,
p2: ActorId,
p3: ManyVariants,
}
enum ManyVariants {
One,
Two(u32),
Three(Option<U256>),
Four {
a: u32,
b: Option<u16>,
},
Five(String, H256),
Six((u32)),
}
enum ManyVariantsReply {
One,
Two,
Three,
Four,
Five,
Six,
}
struct NonZeroU32(u32);
struct NonZeroU8(u8);
struct TupleStruct(bool);
}
}

service ValueFee {
events {
Withheld(u128),
}
functions {
/// Return flag if fee taken and remain value,
/// using special type `CommandReply<T>`
DoSomethingAndTakeFee() -> bool;
}
events {
Withheld(u128),
}
}

service Chaos {
functions {
@query
PanicAfterWait() -> ();
PanicAfterWait();
@query
ReplyHookCounter() -> u32;
@query
TimeoutWait() -> ();
TimeoutWait();
}
}

program ProgramToDo {
constructors {
/// Program constructor (called once at the very beginning of the program lifetime)
Default();
/// Another program constructor (called once at the very beginning of the program lifetime)
New(counter: Option<u32>, dog_position: Option<(i32, i32)>);
}
services {
PingPong,
Counter,
Dog,
References,
ThisThat,
ValueFee,
Chaos,
}
}
}
Loading