Skip to content

Commit

Permalink
Merge pull request #1 from Soneso/soroban_20.0.0_prep
Browse files Browse the repository at this point in the history
Soroban 20.0.2 prep
  • Loading branch information
christian-rogobete committed Dec 18, 2023
2 parents 9c8a044 + 1802e22 commit 09d246d
Show file tree
Hide file tree
Showing 78 changed files with 1,388 additions and 1,397 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Uses the [AssemblyScript soroban SDK](https://github.com/Soneso/as-soroban-sdk)
To run a contract, you must first install the official `soroban-cli` as described here: [stellar soroban cli](https://soroban.stellar.org/docs/getting-started/setup).

```sh
cargo install --locked --version 20.0.0-rc2 soroban-cli
cargo install --locked --version 20.0.2 soroban-cli
```

### 3. Run an example contract
Expand Down Expand Up @@ -64,6 +64,7 @@ Instead of a tutorial, we have created a series of contract examples with many e
| [testing example](https://github.com/Soneso/as-soroban-examples/tree/main/testing)| Shows a simple way to test your contract.|
| [token example](https://github.com/Soneso/as-soroban-examples/tree/main/token)| Demonstrates how to write a token contract that implements the Stellar [token interface](https://soroban.stellar.org/docs/reference/interfaces/token-interface).|
| [atomic swap example](https://github.com/Soneso/as-soroban-examples/tree/main/atomic-swap)| Swaps two tokens between two authorized parties atomically while following the limits they set. This example demonstrates advanced usage of Soroban auth framework and assumes the reader is familiar with the auth example and with Soroban token usage.|
| [atomic swap batched example](https://github.com/Soneso/as-soroban-examples/tree/main/multi_swap)| Swaps a pair of tokens between the two groups of users that authorized the swap operation from the [atomic swap example](https://github.com/Soneso/as-soroban-examples/tree/main/atomic-swap).|
| [timelock example](https://github.com/Soneso/as-soroban-examples/tree/main/timelock)| Demonstrates how to write a timelock and implements a greatly simplified claimable balance similar to the claimable balance feature available on Stellar.|
| [single offer sale example](https://github.com/Soneso/as-soroban-examples/tree/main/single_offer)| The single offer sale example demonstrates how to write a contract that allows a seller to set up an offer to sell token A for token B to multiple buyers.|
| [liquidity pool example](https://github.com/Soneso/as-soroban-examples/tree/main/liquidity_pool)| Demonstrates how to write a constant product liquidity pool contract.|
Expand Down
24 changes: 19 additions & 5 deletions add/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ The [add example](https://github.com/Soneso/as-soroban-examples/tree/main/add) d

## Run the example

To run a contract in the sandbox, you must first install the official [soroban-cli](https://soroban.stellar.org/docs/getting-started/setup):
To run a contract, you must first install the official [soroban-cli](https://soroban.stellar.org/docs/getting-started/setup):

```sh
cargo install --locked --version 20.0.0-rc2 soroban-cli
cargo install --locked --version 20.0.2 soroban-cli
```

Then, to run the example, navigate it's directory and install the SDK. Then build the contract:
Expand All @@ -23,8 +23,24 @@ You can find the generated ```.wasm``` (WebAssembly) file in the ```build``` fol

Run the example contract:

To run the contract, we first need to deploy it:

```sh
soroban -q contract invoke --wasm build/release.wasm --id 1 -- add --a 1 --b 5
soroban contract deploy \
--wasm build/release.wasm \
--source SAIPPNG3AGHSK2CLHIYQMVBPHISOOPT64MMW2PQGER47SDCN6C6XFWQM \
--rpc-url https://soroban-testnet.stellar.org \
--network-passphrase "Test SDF Network ; September 2015"
```
This returns the ID of the contract, starting with a C. Next let's invoke:

```sh
soroban -q contract invoke \
--source SAIPPNG3AGHSK2CLHIYQMVBPHISOOPT64MMW2PQGER47SDCN6C6XFWQM \
--rpc-url https://soroban-testnet.stellar.org \
--network-passphrase "Test SDF Network ; September 2015" \
--id <your contract id here> \
-- add --a 1 --b 5
```

You should see the output:
Expand Down Expand Up @@ -60,8 +76,6 @@ export function add(a: I32Val, b: I32Val): I32Val {
}
```

Ref: https://github.com/Soneso/as-soroban-examples/tree/main/add

## How it works

When calling a contract function the host will only pass so called host values. The SDK can encode and decode host values. A host value is a 64-bit integer carrying a bit-packed disjoint union of several cases, each identified by a different tag value (e.g. `i32`, `u32`, `symbol`, `timestamp`, `bool` etc. or ```object handles``` such as references to vectors, maps, bytes, strings that live in the host).
Expand Down
2 changes: 1 addition & 1 deletion add/contract.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
"key" : "version",
"value" : "0.3.0"
"value" : "0.4.0"
},
{
"key" : "description",
Expand Down
4 changes: 2 additions & 2 deletions add/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "add",
"version": "0.3.0",
"version": "0.4.0",
"description": "as soroban contract example adding two integers",
"scripts": {
"asbuild:debug": "asc assembly/index.ts --target debug",
Expand All @@ -25,6 +25,6 @@
}
},
"dependencies": {
"as-soroban-sdk": "^0.3.0"
"as-soroban-sdk": "^0.4.0"
}
}
2 changes: 1 addition & 1 deletion atomic-swap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ To build the example, navigate it's directory install the sdk. Then build the co
```sh
cd atomic-swap
npm install as-soroban-sdk
asc assembly/index.ts --target release
npm run asbuild:release
```

You can find the generated `.wasm` (WebAssembly) file in the `build` folder. You can also find the `.wat` file there (text format of the `.wasm`).
Expand Down
2 changes: 1 addition & 1 deletion atomic-swap/contract.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
{
"key" : "version",
"value" : "0.3.0"
"value" : "0.4.0"
},
{
"key" : "description",
Expand Down
4 changes: 2 additions & 2 deletions atomic-swap/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "atomic_swap",
"version": "0.3.0",
"version": "0.4.0",
"description": "atomic swap contract example",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -28,6 +28,6 @@
}
},
"dependencies": {
"as-soroban-sdk": "^0.3.0"
"as-soroban-sdk": "^0.4.0"
}
}
15 changes: 5 additions & 10 deletions atomic-swap/swap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,17 @@
network_passphrase = Network.TESTNET_NETWORK_PASSPHRASE

submitter_kp = Keypair.from_secret(
"SDO7H2BPXKB6JHCT7ZFVM7MJWHS6FC7KIX5RO37J65UAVMOEK37TS4RR"
"SA2EAJDXWUPGMSZLWN3XV2RUFN2BY4RFVG2Z2EK32GF2Z4T2MTGA75IL"
)
alice_kp = Keypair.from_secret(
"SCA3CMTPTOOWMRYT3I626W53QFI5OWHEZJELRMA3HMZZBJLKLFPHJGLY"
"SASU324JBGQ6TEQQWVYQPHKUY7K23US6IMTJL7FHUXL2XTRMF7RTO66T"
)
bob_kp = Keypair.from_secret(
"SAJ5IY663ZNGXSX6NVP7SEZMEQLUSISNMBYXKLIDQ2TRGPUSGNFCGTEQ"
"SCJQ455QZC6RMDCRK62EGXIRT3L36ZMJNLI24ESBH4UTE736J4FMKODQ"
)
atomic_swap_contract_id = (sys.argv[1])
token_a_contract_id = "CC6HEDM5IZSPOZ7R57LMUBNUGZGTRI2QTJEDHII6KZKZFDF7U3GG2WB2"
token_b_contract_id = "CBGT22P4NQZJ6FLINL6RZLAG6CHTH745V2AE5GF5KXQ7E2TSZGVJEBHK"
token_a_contract_id = "CDEXJAMSALQVE7SJEX5M36S5JMPBPBV3SCAOJFYX6UYZVF5LB4DZPCOT"
token_b_contract_id = "CCWDMAJTKNRRYAKJZRA56TGR7Y24WLHDOX7DHY4B443LGHHZPQPN3757"


source = soroban_server.load_account(submitter_kp.public_key)
Expand Down Expand Up @@ -89,11 +89,6 @@
print(f"Got exception: {e.simulate_transaction_response}")
raise e

tx.transaction.soroban_data.resources.instructions = stellar_xdr.Uint32(
tx.transaction.soroban_data.resources.instructions.uint32 +
round(tx.transaction.soroban_data.resources.instructions.uint32 / 4)
)
tx.transaction.fee += 1005000
tx.sign(submitter_kp)
# print(f"Signed XDR:\n{tx.to_xdr()}")

Expand Down
61 changes: 44 additions & 17 deletions auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ The [auth example](https://github.com/Soneso/as-soroban-examples/tree/main/auth)

## Run the example

To run a contract in the sandbox, you must first install the official [soroban-cli](https://soroban.stellar.org/docs/getting-started/setup):
To run a contract, you must first install the official [soroban-cli](https://soroban.stellar.org/docs/getting-started/setup):

```sh
cargo install --locked --version 20.0.0-rc2 soroban-cli
cargo install --locked --version 20.0.2 soroban-cli
```

Then, to run the example, navigate it's directory and install the sdk. Then build the contract:
Expand All @@ -19,6 +19,21 @@ npm install as-soroban-sdk
npm run asbuild:release
```

Next, we have to deploy the contract:

```sh
soroban contract deploy \
--wasm build/release.wasm \
--source SAIPPNG3AGHSK2CLHIYQMVBPHISOOPT64MMW2PQGER47SDCN6C6XFWQM \
--rpc-url https://soroban-testnet.stellar.org \
--network-passphrase "Test SDF Network ; September 2015"
```

This returns the ID of the contract, starting with a C. For example:
```sh
CCAMMD5JFXXLRY7FO776G7K2LSXSBQQEBOKLD4MO6PIIVTIQIRXAWQNQ
```

Since we are dealing with authorization and signatures, we need to set up some identities to use for testing and get their public keys:

```sh
Expand All @@ -30,19 +45,28 @@ soroban config identity address acc2

Example output with the public key of the identity:
```sh
GDNNLVOEOAQADO5UKZ5PI3WETSAHKGZWLOTKFNOADXAKK76DWHXK47KO
GD5R3D5WMZWWMIEMJXFAQWN4OS5MTMBKCU4XI2OLS56J7OWPBGJP3DLR
GDX2K5D2QFJMW3EGZHQA7CY7OLNGDIXWQ5QL6WDB2T7TKFHAZZUQDZTB
GALXIPNJMHZU7AZSD7RCC265RGUKMA6QDA2X3JBEXDPOGUJNUFZLBC4F
```

Next, let's fund the accounts (replace the account ids):

```sh
curl https://friendbot.stellar.org?addr=GDX2K5D2QFJMW3EGZHQA7CY7OLNGDIXWQ5QL6WDB2T7TKFHAZZUQDZTB

curl https://friendbot.stellar.org?addr=GALXIPNJMHZU7AZSD7RCC265RGUKMA6QDA2X3JBEXDPOGUJNUFZLBC4F
```

Now the contract itself can be invoked. Notice the --source has to match --user argument in order to allow soroban tool to automatically sign the necessary payload for the invocation.

```sh
soroban -q contract invoke \
--source acc1 \
--id 1 \
--wasm build/release.wasm \
--id CCAMMD5JFXXLRY7FO776G7K2LSXSBQQEBOKLD4MO6PIIVTIQIRXAWQNQ \
--rpc-url https://soroban-testnet.stellar.org \
--network-passphrase "Test SDF Network ; September 2015" \
-- auth \
--user GDNNLVOEOAQADO5UKZ5PI3WETSAHKGZWLOTKFNOADXAKK76DWHXK47KO \
--user GDX2K5D2QFJMW3EGZHQA7CY7OLNGDIXWQ5QL6WDB2T7TKFHAZZUQDZTB \
--value 3
```

Expand All @@ -51,30 +75,33 @@ Run a few more increments for both accounts.
```sh
soroban -q contract invoke \
--source acc2 \
--id 1 \
--wasm build/release.wasm \
--id CCAMMD5JFXXLRY7FO776G7K2LSXSBQQEBOKLD4MO6PIIVTIQIRXAWQNQ \
--rpc-url https://soroban-testnet.stellar.org \
--network-passphrase "Test SDF Network ; September 2015" \
-- auth \
--user GBI5PLC6I7VUW47RIHIRHX3FHWXSBRBNO4ZO5UJRJEPDGHIPZHVUH7GC \
--user GALXIPNJMHZU7AZSD7RCC265RGUKMA6QDA2X3JBEXDPOGUJNUFZLBC4F \
--value 2
```

```sh
soroban -q contract invoke \
--source acc1 \
--id 1 \
--wasm build/release.wasm \
-- auth
--user GDNNLVOEOAQADO5UKZ5PI3WETSAHKGZWLOTKFNOADXAKK76DWHXK47KO \
--id CCAMMD5JFXXLRY7FO776G7K2LSXSBQQEBOKLD4MO6PIIVTIQIRXAWQNQ \
--rpc-url https://soroban-testnet.stellar.org \
--network-passphrase "Test SDF Network ; September 2015" \
-- auth \
--user GDX2K5D2QFJMW3EGZHQA7CY7OLNGDIXWQ5QL6WDB2T7TKFHAZZUQDZTB \
--value 14
```

```sh
soroban -q contract invoke \
--source acc2 \
--id 1 \
--wasm build/release.wasm \
--id CCAMMD5JFXXLRY7FO776G7K2LSXSBQQEBOKLD4MO6PIIVTIQIRXAWQNQ \
--rpc-url https://soroban-testnet.stellar.org \
--network-passphrase "Test SDF Network ; September 2015" \
-- auth \
--user GBI5PLC6I7VUW47RIHIRHX3FHWXSBRBNO4ZO5UJRJEPDGHIPZHVUH7GC \
--user GALXIPNJMHZU7AZSD7RCC265RGUKMA6QDA2X3JBEXDPOGUJNUFZLBC4F \
--value 5
```

Expand Down
2 changes: 1 addition & 1 deletion auth/contract.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
},
{
"key" : "version",
"value" : "0.3.0"
"value" : "0.4.0"
},
{
"key" : "description",
Expand Down
4 changes: 2 additions & 2 deletions auth/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "auth",
"version": "0.3.0",
"version": "0.4.0",
"description": "demonstrates how to implement authentication and authorization using the Soroban Host-managed auth framework",
"main": "index.js",
"scripts": {
Expand All @@ -23,6 +23,6 @@
}
},
"dependencies": {
"as-soroban-sdk": "^0.3.0"
"as-soroban-sdk": "^0.4.0"
}
}
62 changes: 23 additions & 39 deletions contract_events/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ The [events example](https://github.com/Soneso/as-soroban-examples/tree/main/con

## Run the example

To run a contract in the sandbox, you must first install the official [soroban-cli](https://soroban.stellar.org/docs/getting-started/setup):
To run a contract, you must first install the official [soroban-cli](https://soroban.stellar.org/docs/getting-started/setup):

```sh
cargo install --locked --version 20.0.0-rc2 soroban-cli
cargo install --locked --version 20.0.2 soroban-cli
```

Then, to run the example, navigate it's directory and install the sdk. Then build the contract:
Expand All @@ -21,21 +21,35 @@ npm run asbuild:release

You can find the generated `.wasm` (WebAssembly) file in the `build` folder. You can also find the `.wat` file there (text format of the `.wasm`).

Run the example contract:
Deploy:

```sh
soroban contract invoke --wasm build/release.wasm --id 9 -- increment
soroban contract deploy \
--wasm build/release.wasm \
--source SAIPPNG3AGHSK2CLHIYQMVBPHISOOPT64MMW2PQGER47SDCN6C6XFWQM \
--rpc-url https://soroban-testnet.stellar.org \
--network-passphrase "Test SDF Network ; September 2015"
```

You should see the output:
This returns the ID of the contract, starting with a C. Next let's invoke:

```sh
// ...
soroban contract invoke \
--source SAIPPNG3AGHSK2CLHIYQMVBPHISOOPT64MMW2PQGER47SDCN6C6XFWQM \
--rpc-url https://soroban-testnet.stellar.org \
--network-passphrase "Test SDF Network ; September 2015" \
--id CAIJF5TVBGXMNWZPPVQEOFY2GK67M6RR3XXBHW4BFGMJDLIEPPIVMGG3 \
-- increment
```

2023-11-08T14:02:22.179585Z INFO soroban_cli::log::host_event: 1: HostEvent {
You should see the output:
```sh
2023-12-13T18:47:17.587107Z INFO soroban_cli::log::diagnostic_event: 0: DiagnosticEvent {
in_successful_contract_call: true,
event: ContractEvent {
ext: V0,
contract_id: Some(
Hash(0000000000000000000000000000000000000000000000000000000000000009),
Hash(17a1166743b43e4cd08b21a710467978162f0e117c690f7b0de1cb8743d645f2),
),
type_: Contract,
body: V0(
Expand All @@ -55,45 +69,15 @@ You should see the output:
],
),
data: U32(
3,
1,
),
},
),
},
failed_call: false,
}

// ...
1
```

The `soroban cli` also logs events locally in the file .soroban/events.json. Look into that file to see the published event:

```sh
more .soroban/events.json
```

You should be able to find this event:

```json

{
"type": "contract",
"ledger": "1",
"ledgerClosedAt": "1970-01-01T00:00:05Z",
"id": "0000000004294971393-0000000002",
"pagingToken": "0000000004294971393-0000000002",
"contractId": "0000000000000000000000000000000000000000000000000000000000000009",
"topic": [
"AAAADwAAAAdDT1VOVEVSAA==",
"AAAADwAAAAlpbmNyZW1lbnQAAAA="
],
"value": {
"xdr": "AAAAAwAAAAE="
}
}
```

## Code

You can find the code in:
Expand Down
Loading

0 comments on commit 09d246d

Please sign in to comment.