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

Commit

Permalink
Update the tutorial for writing a contract (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
leighmcculloch authored Aug 30, 2022
1 parent de75fd7 commit 85363a8
Showing 1 changed file with 7 additions and 11 deletions.
18 changes: 7 additions & 11 deletions docs/tutorials/write-a-contract.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ well suited to being deployed into small programs like those deployed to
blockchains.

```rust
use soroban_sdk::{contractimpl, vec, Env, Symbol, Vec};
use soroban_sdk::{contractimpl, symbol, vec, Env, Symbol, Vec};
```

The contract will need to import the types and macros that it needs from the
Expand All @@ -26,13 +26,13 @@ how to setup a project.
Many of the types available in typical Rust programs, such as `std::vec::Vec`,
are not available, as there is no allocator and no heap memory in Soroban
contracts. The `soroban-sdk` provides a variety of types like `Vec`, `Map`,
`BigInt`, `Binary`, `FixedBinary`, that all utilize the Soroban environment's
memory and native capabilities.
`BigInt`, `Bytes`, `BytesN`, `Symbol`, that all utilize the Soroban
environment's memory and native capabilities.

```rust
pub struct Contract;

#[contractimpl(export_if = "export")]
#[contractimpl]
impl Contract {
pub fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
todo!()
Expand All @@ -46,22 +46,18 @@ externally are should be marked with `pub` visibility. The first argument can be
an `Env` argument to get a copy of the Soroban environment, which is necessary
for most things.

Implementations annotated can be configured to export the contract functions
only if a feature is enabled, with `export_if = "[feature-name]"`.

Putting those pieces together a simple contract will look like this.

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

pub struct Contract;

#[contractimpl(export_if = "export")]
#[contractimpl]
impl Contract {
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

0 comments on commit 85363a8

Please sign in to comment.