Skip to content

Commit

Permalink
Bump examples (stellar#875)
Browse files Browse the repository at this point in the history
* Update soroban-examples tag

* Undo bump for custom-types because the example was completely rewritten.

* Update upgradeable contract for v21.4.0

* Update token examples for v21.4.0

* Update account contract for v21.4.0

* Update liquidity pool examples for v21.4.0
  • Loading branch information
sisuresh authored Aug 5, 2024
1 parent bee4cbf commit 9a8bc11
Show file tree
Hide file tree
Showing 25 changed files with 161 additions and 118 deletions.
2 changes: 1 addition & 1 deletion docs/build/guides/conventions/deploy-contract.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ This guide walks through the process of deploying a smart contract from installe

### Setup environment

The [deployer example](https://github.com/stellar/soroban-examples/tree/v20.2.0/deployer) demonstrates how to deploy contracts using a contract.
The [deployer example](https://github.com/stellar/soroban-examples/tree/v21.4.0/deployer) demonstrates how to deploy contracts using a contract.

1. Clone the Soroban Examples Repository:

Expand Down
40 changes: 37 additions & 3 deletions docs/build/guides/conventions/upgrading-contracts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ The [upgradeable contract example] demonstrates how to upgrade a Wasm contract.

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)][oigp]

[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v20.2.0
[upgradeable contract example]: https://github.com/stellar/soroban-examples/tree/v20.2.0/upgradeable_contract
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v21.4.0
[upgradeable contract example]: https://github.com/stellar/soroban-examples/tree/v21.4.0/upgradeable_contract
[Rust programming language]: https://www.rust-lang.org/

### Code
Expand All @@ -42,21 +42,32 @@ The example contains both an "old" and "new" contract, where we upgrade from "ol
```rust title="upgradeable_contract/old_contract/src/lib.rs"
#![no_std]

use soroban_sdk::{contractimpl, contracttype, Address, BytesN, Env};
use soroban_sdk::{contractimpl, contracterror, contracttype, Address, BytesN, Env};

#[contracttype]
#[derive(Clone)]
enum DataKey {
Admin,
}

#[contracterror]
#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
pub enum Error {
AlreadyInitialized = 1,
}

#[contract]
pub struct UpgradeableContract;

#[contractimpl]
impl UpgradeableContract {
pub fn init(e: Env, admin: Address) {
if e.storage().instance().has(&DataKey::Admin) {
return Err(Error::AlreadyInitialized);
}
e.storage().instance().set(&DataKey::Admin, &admin);
Ok(())
}

pub fn version() -> u32 {
Expand Down Expand Up @@ -108,6 +119,7 @@ Open the `upgradeable_contract/old_contract/src/test.rs` file to follow along.
```rust title="upgradeable_contract/old_contract/srctest.rs"
#![cfg(test)]

use crate::Error;
use soroban_sdk::{testutils::Address as _, Address, BytesN, Env};

mod old_contract {
Expand Down Expand Up @@ -153,6 +165,28 @@ fn test() {
assert_eq!(1010101, client.new_v2_fn());
}


#[test]
fn test_cannot_re_init() {
let env = Env::default();
env.mock_all_auths();

// Note that we use register_contract_wasm instead of register_contract
// because the old contracts WASM is expected to exist in storage.
let contract_id = env.register_contract_wasm(None, old_contract::WASM);
let client = old_contract::Client::new(&env, &contract_id);
let admin = Address::generate(&env);
client.init(&admin);

// `try_init` is expected to return an error. Since client is generated from Wasm,
// this is a generic SDK error.
let err: soroban_sdk::Error = client.try_init(&admin).err().unwrap().unwrap();
// Convert the SDK error to the contract error.
let contract_err: Error = err.try_into().unwrap();
// Make sure contract error has the expected value.
assert_eq!(contract_err, Error::AlreadyInitialized);
}

```

We first import Wasm files for both contracts:
Expand Down
2 changes: 1 addition & 1 deletion docs/build/guides/conventions/wasm-metadata.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,4 @@ pub trait LiquidityPoolTrait {...
```

[`contractmeta!`]: https://docs.rs/soroban-sdk/20.0.2/soroban_sdk/macro.contractmeta.html
[liquidity pool example]: https://github.com/stellar/soroban-examples/blob/v20.0.0/liquidity_pool/src/lib.rs#L152-L155
[liquidity pool example]: https://github.com/stellar/soroban-examples/blob/v21.4.0/liquidity_pool/src/lib.rs#L152-L155
10 changes: 5 additions & 5 deletions docs/build/smart-contracts/example-contracts/TEMPLATE.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@ Place each link definition at the bottom of the section it first is used in.

:::

[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v20.0.0
[example demonstrates]: https://github.com/stellar/soroban-examples/tree/v20.0.0/hello_world
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v21.4.0
[example demonstrates]: https://github.com/stellar/soroban-examples/tree/v21.4.0/hello_world
[other example]: ../getting-started/setup.mdx

## Run the Example

First go through the [Setup] process to get your development environment configured, then clone the `v20.0.0` tag of `soroban-examples` repository:
First go through the [Setup] process to get your development environment configured, then clone the `v21.4.0` tag of `soroban-examples` repository:

```sh
git clone -b v20.0.0 https://github.com/stellar/soroban-examples
git clone -b v21.4.0 https://github.com/stellar/soroban-examples
```

Or, skip the development environment setup and open this example in [Gitpod][oigp].
Expand Down Expand Up @@ -67,7 +67,7 @@ impl HelloContract {
mod test;
```

Ref: https://github.com/stellar/soroban-examples/tree/v20.0.0/hello_world
Ref: https://github.com/stellar/soroban-examples/tree/v21.4.0/hello_world

## How it Works

Expand Down
10 changes: 5 additions & 5 deletions docs/build/smart-contracts/example-contracts/alloc.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,19 @@ The [allocator example] demonstrates how to utilize the allocator feature when w

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)][oigp]

[allocator example]: https://github.com/stellar/soroban-examples/tree/v20.0.0/alloc
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v20.0.0
[allocator example]: https://github.com/stellar/soroban-examples/tree/v21.4.0/alloc
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v21.4.0

The `soroban-sdk` crate provides a lightweight bump-pointer allocator which can be used to emulate heap memory allocation in a Wasm smart contract.

## Run the Example

First go through the [Setup] process to get your development environment configured, then clone the `v20.0.0` tag of `soroban-examples` repository:
First go through the [Setup] process to get your development environment configured, then clone the `v21.4.0` tag of `soroban-examples` repository:

[setup]: ../getting-started/setup.mdx

```
git clone -b v20.0.0 https://github.com/stellar/soroban-examples
git clone -b v21.4.0 https://github.com/stellar/soroban-examples
```

Or, skip the development environment setup and open this example in [Gitpod][oigp].
Expand Down Expand Up @@ -94,7 +94,7 @@ impl AllocContract {
}
```

Ref: https://github.com/stellar/soroban-examples/tree/v20.0.0/alloc
Ref: https://github.com/stellar/soroban-examples/tree/v21.4.0/alloc

## How it Works

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ Follow the comments in the code for more information.

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)][oigp]

[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v20.0.0
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v21.4.0
[atomic swap]: atomic-swap.mdx
[atomic swap batching example]: https://github.com/stellar/soroban-examples/tree/v20.0.0/atomic_multiswap
[atomic swap batching example]: https://github.com/stellar/soroban-examples/tree/v21.4.0/atomic_multiswap
12 changes: 6 additions & 6 deletions docs/build/smart-contracts/example-contracts/atomic-swap.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ This is example demonstrates advanced usage of Soroban auth framework and assume

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)][oigp]

[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v20.0.0
[atomic swap example]: https://github.com/stellar/soroban-examples/tree/v20.0.0/atomic_swap
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v21.4.0
[atomic swap example]: https://github.com/stellar/soroban-examples/tree/v21.4.0/atomic_swap

## Run the Example

First go through the [Setup] process to get your development environment configured, then clone the `v20.0.0` tag of `soroban-examples` repository:
First go through the [Setup] process to get your development environment configured, then clone the `v21.4.0` tag of `soroban-examples` repository:

[setup]: ../getting-started/setup.mdx

```
git clone -b v20.0.0 https://github.com/stellar/soroban-examples
git clone -b v21.4.0 https://github.com/stellar/soroban-examples
```

Or, skip the development environment setup and open this example in [Gitpod][oigp].
Expand Down Expand Up @@ -123,7 +123,7 @@ fn move_token(
}
```

Ref: https://github.com/stellar/soroban-examples/tree/v20.0.0/atomic_swap
Ref: https://github.com/stellar/soroban-examples/tree/v21.4.0/atomic_swap

## How it Works

Expand Down Expand Up @@ -186,7 +186,7 @@ The swap itself is implemented via two token moves: from `a` to `b` and from `b`

Open the [`atomic_swap/src/test.rs`] file to follow along.

[`atomic_swap/src/test.rs`]: https://github.com/stellar/soroban-examples/tree/v20.0.0/atomic_swap/src/test.rs
[`atomic_swap/src/test.rs`]: https://github.com/stellar/soroban-examples/tree/v21.4.0/atomic_swap/src/test.rs

Refer to another examples for the general information on the test setup.

Expand Down
12 changes: 6 additions & 6 deletions docs/build/smart-contracts/example-contracts/auth.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ This example is an extension of the [storing data example].

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)][oigp]

[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v20.0.0
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v21.4.0
[storing data example]: ../getting-started/storing-data.mdx
[auth example]: https://github.com/stellar/soroban-examples/tree/v20.0.0/auth
[auth example]: https://github.com/stellar/soroban-examples/tree/v21.4.0/auth

## Run the Example

First go through the [Setup] process to get your development environment configured, then clone the `v20.0.0` tag of `soroban-examples` repository:
First go through the [Setup] process to get your development environment configured, then clone the `v21.4.0` tag of `soroban-examples` repository:

[setup]: ../getting-started/setup.mdx

```
git clone -b v20.0.0 https://github.com/stellar/soroban-examples
git clone -b v21.4.0 https://github.com/stellar/soroban-examples
```

Or, skip the development environment setup and open this example in [Gitpod][oigp].
Expand Down Expand Up @@ -110,7 +110,7 @@ impl IncrementContract {
}
```

Ref: https://github.com/stellar/soroban-examples/tree/v20.0.0/auth
Ref: https://github.com/stellar/soroban-examples/tree/v21.4.0/auth

## How it Works

Expand Down Expand Up @@ -166,7 +166,7 @@ user.require_auth_for_args((value,).into_val(&env));

Open the [`auth/src/test.rs`] file to follow along.

[`auth/src/test.rs`]: https://github.com/stellar/soroban-examples/tree/v20.0.0/auth/src/test.rs
[`auth/src/test.rs`]: https://github.com/stellar/soroban-examples/tree/v21.4.0/auth/src/test.rs

```rust title="auth/src/test.rs"
fn test() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,17 @@ In this example there are two contracts that are compiled separately, deployed s

:::

[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v20.0.0
[cross contract call example]: https://github.com/stellar/soroban-examples/tree/v20.0.0/cross_contract
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v21.4.0
[cross contract call example]: https://github.com/stellar/soroban-examples/tree/v21.4.0/cross_contract

## Run the Example

First go through the [Setup] process to get your development environment configured, then clone the `v20.0.0` tag of `soroban-examples` repository:
First go through the [Setup] process to get your development environment configured, then clone the `v21.4.0` tag of `soroban-examples` repository:

[setup]: ../getting-started/setup.mdx

```
git clone -b v20.0.0 https://github.com/stellar/soroban-examples
git clone -b v21.4.0 https://github.com/stellar/soroban-examples
```

Or, skip the development environment setup and open this example in [Gitpod][oigp].
Expand Down Expand Up @@ -93,7 +93,7 @@ impl ContractB {
}
```

Ref: https://github.com/stellar/soroban-examples/tree/v20.0.0/cross_contract
Ref: https://github.com/stellar/soroban-examples/tree/v21.4.0/cross_contract

## How it Works

Expand Down
16 changes: 8 additions & 8 deletions docs/build/smart-contracts/example-contracts/custom-account.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,17 @@ While custom accounts are supported by the Stellar protocol and Soroban SDK, the

[![Open in Gitpod](https://gitpod.io/button/open-in-gitpod.svg)][oigp]

[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v20.0.0
[custom account example]: https://github.com/stellar/soroban-examples/tree/v20.0.0/account
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v21.4.0
[custom account example]: https://github.com/stellar/soroban-examples/tree/v21.4.0/account

## Run the Example

First go through the [Setup] process to get your development environment configured, then clone the `v20.0.0` tag of `soroban-examples` repository:
First go through the [Setup] process to get your development environment configured, then clone the `v21.4.0` tag of `soroban-examples` repository:

[setup]: ../getting-started/setup.mdx

```
git clone -b v20.0.0 https://github.com/stellar/soroban-examples
git clone -b v21.4.0 https://github.com/stellar/soroban-examples
```

Or, skip the development environment setup and open this example in [Gitpod][oigp].
Expand Down Expand Up @@ -128,7 +128,7 @@ This function allows users to set and modify the per-token spend limit described
pub fn __check_auth(
env: Env,
signature_payload: BytesN<32>,
signatures: Vec<Signature>,
signatures: Vec<AccSignature>,
auth_context: Vec<Context>,
) -> Result<(), AccError> {
// Perform authentication.
Expand Down Expand Up @@ -174,7 +174,7 @@ Here it is implemented in two steps. First, authentication is performed using th
fn authenticate(
env: &Env,
signature_payload: &BytesN<32>,
signatures: &Vec<Signature>,
signatures: &Vec<AccSignature>,
) -> Result<(), AccError> {
for i in 0..signatures.len() {
let signature = signatures.get_unchecked(i);
Expand Down Expand Up @@ -278,15 +278,15 @@ Then we check for the standard token function names and verify that for these fu

Open the [`account/src/test.rs`] file to follow along.

[`account/src/test.rs`]: https://github.com/stellar/soroban-examples/tree/v20.0.0/account/src/test.rs
[`account/src/test.rs`]: https://github.com/stellar/soroban-examples/tree/v21.4.0/account/src/test.rs

Refer to another examples for the general information on the test setup.

Here we only look at some points specific to the account contracts.

```rust
fn sign(e: &Env, signer: &Keypair, payload: &BytesN<32>) -> RawVal {
Signature {
AccSignature {
public_key: signer_public_key(e, signer),
signature: signer
.sign(payload.to_array().as_slice())
Expand Down
10 changes: 5 additions & 5 deletions docs/build/smart-contracts/example-contracts/deployer.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ In this example there are two contracts that are compiled separately, and the te

:::

[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v20.0.0
[deployer example]: https://github.com/stellar/soroban-examples/tree/v20.0.0/deployer
[oigp]: https://gitpod.io/#https://github.com/stellar/soroban-examples/tree/v21.4.0
[deployer example]: https://github.com/stellar/soroban-examples/tree/v21.4.0/deployer

## Run the Example

First go through the [Setup] process to get your development environment configured, then clone the `v20.0.0` tag of `soroban-examples` repository:
First go through the [Setup] process to get your development environment configured, then clone the `v21.4.0` tag of `soroban-examples` repository:

[setup]: ../getting-started/setup.mdx

```
git clone -b v20.0.0 https://github.com/stellar/soroban-examples
git clone -b v21.4.0 https://github.com/stellar/soroban-examples
```

Or, skip the development environment setup and open this example in [Gitpod][oigp].
Expand Down Expand Up @@ -108,7 +108,7 @@ impl Deployer {
}
```

Ref: https://github.com/stellar/soroban-examples/tree/v20.0.0/deployer
Ref: https://github.com/stellar/soroban-examples/tree/v21.4.0/deployer

## How it Works

Expand Down
Loading

0 comments on commit 9a8bc11

Please sign in to comment.