Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Update the hello_world contract #92

Merged
merged 1 commit into from
Aug 30, 2022
Merged
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
31 changes: 14 additions & 17 deletions docs/examples/hello-world.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,14 @@ test test::test ... ok

```rust title="hello_world/src/lib.rs"
#![no_std]
use soroban_sdk::{contractimpl, vec, Env, Symbol, Vec};
use soroban_sdk::{contractimpl, symbol, vec, Env, Symbol, Vec};

pub struct HelloContract;

#[contractimpl]
impl HelloContract {
pub fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
const GREETING: Symbol = Symbol::from_str("Hello");
vec![&env, GREETING, to]
vec![&env, symbol!("Hello"), to]
}
}
```
Expand Down Expand Up @@ -75,7 +74,7 @@ pub struct HelloContract;
Contract functions look much like regular Rust functions. They mave have any
number of arguments, but arguments must support being transmitted to and from
the Soroban environment that the contract runs in. The Soroban SDK provides some
types like `Vec`, `Map`, `BigInt`, `Symbol`, `Binary`, `FixedBinary`, etc that
types like `Vec`, `Map`, `BigInt`, `Symbol`, `Bytes`, `BytesN`, etc that
can be used. Primitive values like `u64`, `i64`, `u32`, `i32`, and `bool` can
also be used. Floats are not supported.

Expand Down Expand Up @@ -103,14 +102,12 @@ Open the `hello_world/src/test.rs` file to follow along.
#[test]
fn test() {
let env = Env::default();
let contract_id = FixedBinary::from_array(&env, [0; 32]);
let contract_id = BytesN::from_array(&env, &[0; 32]);
env.register_contract(&contract_id, HelloContract);
let client = HelloContractClient::new(&env, &contract_id);

let words = hello::invoke(&env, &contract_id, &Symbol::from_str("SourBun"));
assert_eq!(
words,
vec![&env, Symbol::from_str("Hello"), Symbol::from_str("SourBun"),]
);
let words = client.hello(&symbol!("SourBun"));
assert_eq!(words, vec![&env, symbol!("Hello"), symbol!("Dev"),]);
}
```

Expand All @@ -130,19 +127,19 @@ env.register_contract(&contract_id, HelloContract);
```

All public functions within an `impl` block that is annotated with the
`#[contractimpl]` attribute have an `invoke` function generated, that can be
used to invoke the contract function within the environment.
`#[contractimpl]` attribute have a corresponding function generated in a
generated client type. The client type will be named the same as the contract
type with `Client` appended. For example, in our contract the contract type is
`HelloContract`, and the client is named `HelloContractClient`.

```rust
let words = hello::invoke(&env, &contract_id, &Symbol::from_str("SourBun"));
let client = ContractClient::new(&env, &contract_id);
let words = client.hello(&symbol!("Dev"));
```

The values returned by functions can be asserted on:
```rust
assert_eq!(
words,
vec![&env, Symbol::from_str("Hello"), Symbol::from_str("SourBun"),]
);
assert_eq!(words, vec![&env, symbol!("Hello"), symbol!("Dev"),]);
```

## Build the Contract
Expand Down