Skip to content

Commit

Permalink
support sdk 0.2.1 & add liquidity pool example
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-rogobete committed May 26, 2023
1 parent 92b6ce8 commit 2184414
Show file tree
Hide file tree
Showing 72 changed files with 1,264 additions and 388 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# [Stellar Soroban Examples for AssemblyScript](https://github.com/Soneso/as-soroban-examples)

![v0.1.2](https://img.shields.io/badge/v0.1.2-yellow.svg)
![v0.2.1](https://img.shields.io/badge/v0.2.1-yellow.svg)

AssemblyScript contract examples for [Soroban](https://soroban.stellar.org).

Expand All @@ -16,7 +16,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://github.com/stellar/soroban-cli).

```sh
cargo install --locked --version 0.7.0 soroban-cli
cargo install --locked --version 0.8.0 soroban-cli
```

### 3. Run an example contract
Expand Down Expand Up @@ -66,6 +66,7 @@ soroban contract invoke --wasm build/release.wasm --id 1 --fn hello -- --to frie
| [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.|
| [multi swap example](https://github.com/Soneso/as-soroban-examples/tree/main/multi_swap)| This example demonstrates how authorized calls can be batched together. It swaps a pair of tokens between the two groups of users that authorized the swap operation from the atomic swap example.|
| [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.|

### 5. Create your own contract

Expand Down
2 changes: 1 addition & 1 deletion add/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The [add example](https://github.com/Soneso/as-soroban-examples/tree/main/add) d
To run a contract in the sandbox, you must first install the official ```soroban cli``` as described here: [stellar soroban cli](https://github.com/stellar/soroban-cli).

```sh
cargo install --locked --version 0.7.0 soroban-cli
cargo install --locked --version 0.8.0 soroban-cli
```

Then, to run the example, navigate it's directory and install the sdk. Then build the contract:
Expand Down
4 changes: 2 additions & 2 deletions add/contract.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "add",
"version": "0.1.8",
"version": "0.1.9",
"description": "soroban contract example adding 2 integers",
"host_functions_version": 32,
"host_functions_version": 37,
"functions": [
{
"name" : "add",
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.1.8",
"version": "0.1.9",
"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.1.9"
"as-soroban-sdk": "^0.2.1"
}
}
30 changes: 16 additions & 14 deletions atomic-swap/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ function move_token(token: BytesObject, from: AddressObject, to:AddressObject,
incrArgs.pushBack(from);
incrArgs.pushBack(contract_address);
incrArgs.pushBack(approve_amount);
let func = Sym.fromSymbolString("incr_allow").getHostObject(); // "incr_allow" has more than 9 chars.
let func = Sym.fromSymbolString("increase_allowance").getHostObject();
contract.callContract(token, func, incrArgs.getHostObject());

let xferArgs = new Vec();
xferArgs.pushBack(contract_address);
xferArgs.pushBack(from);
xferArgs.pushBack(to);
xferArgs.pushBack(xfer_amount);
contract.callContract(token, fromSmallSymbolStr("xfer_from"), xferArgs.getHostObject());
let transferFromArgs = new Vec();
transferFromArgs.pushBack(contract_address);
transferFromArgs.pushBack(from);
transferFromArgs.pushBack(to);
transferFromArgs.pushBack(xfer_amount);
contract.callContract(token, Sym.fromSymbolString("transfer_from").getHostObject(), transferFromArgs.getHostObject());

}

Expand Down Expand Up @@ -165,15 +165,15 @@ function move_token(token: BytesObject, from: AddressObject, to:AddressObject,
incrArgs.pushBack(from);
incrArgs.pushBack(contract_address);
incrArgs.pushBack(approve_amount);
let func = Sym.fromSymbolString("incr_allow").getHostObject(); // "incr_allow" has more than 9 chars.
let func = Sym.fromSymbolString("increase_allowance").getHostObject();
contract.callContract(token, func, incrArgs.getHostObject());

let xferArgs = new Vec();
xferArgs.pushBack(contract_address);
xferArgs.pushBack(from);
xferArgs.pushBack(to);
xferArgs.pushBack(xfer_amount);
contract.callContract(token, fromSmallSymbolStr("xfer_from"), xferArgs.getHostObject());
let transferFromArgs = new Vec();
transferFromArgs.pushBack(contract_address);
transferFromArgs.pushBack(from);
transferFromArgs.pushBack(to);
transferFromArgs.pushBack(xfer_amount);
contract.callContract(token, Sym.fromSymbolString("transfer_from").getHostObject(), transferFromArgs.getHostObject());

}
```
Expand All @@ -200,3 +200,5 @@ node testContract.cjs
```

It will build and deploy the contract to futurenet and then execute the python script [swap_test.py](https://github.com/Soneso/as-soroban-examples/tree/main/atomic-swap/swap_test.py) which prepares the data and then invokes the contract.

**Currently the script is not working because the phython sdk does not yet support the new xdr for soroban preview 9. As soon as available we will update the script.**
14 changes: 7 additions & 7 deletions atomic-swap/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@ function move_token(token: BytesObject, from: AddressObject, to:AddressObject,
incrArgs.pushBack(from);
incrArgs.pushBack(contract_address);
incrArgs.pushBack(approve_amount);
let func = Sym.fromSymbolString("incr_allow").getHostObject(); // "incr_allow" has more than 9 chars.
let func = Sym.fromSymbolString("increase_allowance").getHostObject();
contract.callContract(token, func, incrArgs.getHostObject());

let xferArgs = new Vec();
xferArgs.pushBack(contract_address);
xferArgs.pushBack(from);
xferArgs.pushBack(to);
xferArgs.pushBack(xfer_amount);
contract.callContract(token, fromSmallSymbolStr("xfer_from"), xferArgs.getHostObject());
let transferFromArgs = new Vec();
transferFromArgs.pushBack(contract_address);
transferFromArgs.pushBack(from);
transferFromArgs.pushBack(to);
transferFromArgs.pushBack(xfer_amount);
contract.callContract(token, Sym.fromSymbolString("transfer_from").getHostObject(), transferFromArgs.getHostObject());

}
2 changes: 0 additions & 2 deletions atomic-swap/build/.gitignore

This file was deleted.

4 changes: 2 additions & 2 deletions atomic-swap/contract.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "atomic_swap",
"version": "0.1.8",
"version": "0.1.9",
"description": "atomic swap contract example",
"host_functions_version": 32,
"host_functions_version": 37,
"functions": [
{
"name" : "swap",
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.0.1",
"version": "0.1.9",
"description": "atomic swap contract example",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -28,6 +28,6 @@
}
},
"dependencies": {
"as-soroban-sdk": "^0.1.9"
"as-soroban-sdk": "^0.2.1"
}
}
4 changes: 2 additions & 2 deletions atomic-swap/swap_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
sub_invocations=[
AuthorizedInvocation(
contract_id=token_a_contract_id,
function_name="incr_allow",
function_name="increase_allowance",
args=[
Address(alice_kp.public_key), # owner
Address.from_raw_contract(atomic_swap_contract_id),
Expand All @@ -87,7 +87,7 @@
sub_invocations=[
AuthorizedInvocation(
contract_id=token_b_contract_id,
function_name="incr_allow",
function_name="increase_allowance",
args=[
Address(bob_kp.public_key), # owner
Address.from_raw_contract(atomic_swap_contract_id),
Expand Down
2 changes: 1 addition & 1 deletion atomic-swap/testContract.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const util = require('util');
const exec = util.promisify(require('child_process').exec);
var assert = require('assert');

const rpcUrl = 'https://horizon-futurenet.stellar.cash:443/soroban/rpc';
const rpcUrl = 'https://rpc-futurenet.stellar.org:443';
const networkPassphrase = "'Test SDF Future Network ; October 2022'";
const submitterSeed = "SANB7KW6E65BEP6WKTELQ7FMDZTN7HRDMXERYQVLYYO32RK2FOHWBK57"; // GCWXCVSG7R45HWGOXUJPSEQ5TOOMTMF4OKTDKNCT5AAVKDZTFLO3JR2T

Expand Down
2 changes: 1 addition & 1 deletion auth/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The [auth example](https://github.com/Soneso/as-soroban-examples/tree/main/auth)
To run a contract in the sandbox, you must first install the official `soroban-cli` as described here: [stellar soroban cli](https://github.com/stellar/soroban-cli).

```sh
cargo install --locked --version 0.7.0 soroban-cli
cargo install --locked --version 0.8.0 soroban-cli
```

Then, to run the example, navigate it's directory and install the sdk. Then build the contract:
Expand Down
4 changes: 2 additions & 2 deletions auth/contract.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "auth",
"version": "0.1.8",
"version": "0.1.9",
"description": "demonstrates how to implement authentication and authorization using the Soroban Host-managed auth framework",
"host_functions_version": 32,
"host_functions_version": 37,
"functions": [
{
"name" : "auth",
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.1.0",
"version": "0.1.9",
"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.1.9"
"as-soroban-sdk": "^0.2.1"
}
}
41 changes: 28 additions & 13 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``` as described here: [stellar soroban cli](https://github.com/stellar/soroban-cli).
To run a contract in the sandbox, you must first install the official `soroban cli` as described here: [stellar soroban cli](https://github.com/stellar/soroban-cli).

```sh
cargo install --locked --version 0.7.0 soroban-cli
cargo install --locked --version 0.8.0 soroban-cli
```

Then, to run the example, navigate it's directory and install the sdk. Then build the contract:
Expand All @@ -30,21 +30,36 @@ soroban contract invoke --wasm build/release.wasm --id 9 -- events
You should see the output:
```sh
1

#0:
event: {"ext":"v0","contractId":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9],
"type":"contract","body":{"v0":{"topics":[{"symbol":[67,79,85,78,84,69,82]},
{"symbol":[105,110,99,114,101,109,101,110,116]}],"data":{"u32":1}}}}
```

If you run the contract again, you should see the output:
The `soroban cli` logs events locally in the file .soroban/events.json. Look into that file to see the published event:

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

#0:
event: {"ext":"v0","contractId":[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,9],
"type":"contract","body":{"v0":{"topics":[{"symbol":[67,79,85,78,84,69,82]},
{"symbol":[105,110,99,114,101,109,101,110,116]}],"data":{"u32":2}}}}
You should see the output:
```json
{
"events": [
{
"type": "contract",
"ledger": "1",
"ledgerClosedAt": "1970-01-01T00:00:05Z",
"id": "0000000004294971393-0000000002",
"pagingToken": "0000000004294971393-0000000002",
"contractId": "0000000000000000000000000000000000000000000000000000000000000009",
"topic": [
"AAAADwAAAAdDT1VOVEVSAA==",
"AAAADwAAAAlpbmNyZW1lbnQAAAA="
],
"value": {
"xdr": "AAAAAwAAAAE="
}
}
],
"latestLedger": 1
}
```

## Code
Expand Down
4 changes: 2 additions & 2 deletions contract_events/contract.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "events",
"version": "0.1.8",
"version": "0.1.9",
"description": "soroban contract example publishing events",
"host_functions_version": 32,
"host_functions_version": 37,
"functions": [
{
"name" : "events",
Expand Down
4 changes: 2 additions & 2 deletions contract_events/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contract_events",
"version": "0.1.0",
"version": "0.1.9",
"description": "soroban contract example that publishes events",
"main": "index.js",
"scripts": {
Expand Down Expand Up @@ -28,6 +28,6 @@
}
},
"dependencies": {
"as-soroban-sdk": "^0.1.9"
"as-soroban-sdk": "^0.2.1"
}
}
2 changes: 1 addition & 1 deletion cross_contract/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The [cross contract call example](https://github.com/Soneso/as-soroban-examples/
To run a contract in the sandbox, you must first install the official `soroban-cli` as described here: [stellar soroban cli](https://github.com/stellar/soroban-cli).

```sh
cargo install --locked --version 0.7.0 soroban-cli
cargo install --locked --version 0.8.0 soroban-cli
```

The example contains two contracts. To run them first navigate in the directory of the first contract and build the contract (`contract_a`):
Expand Down
4 changes: 2 additions & 2 deletions cross_contract/contract_a/contract.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "contract_a",
"version": "0.1.8",
"version": "0.1.9",
"description": "soroban contract to be called by another contract",
"host_functions_version": 32,
"host_functions_version": 37,
"functions": [
{
"name" : "add",
Expand Down
4 changes: 2 additions & 2 deletions cross_contract/contract_a/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contract_a",
"version": "0.1.8",
"version": "0.1.9",
"description": "a soroban contract to be called by another contract",
"main": "index.js",
"scripts": {
Expand All @@ -23,6 +23,6 @@
}
},
"dependencies": {
"as-soroban-sdk": "^0.1.9"
"as-soroban-sdk": "^0.2.1"
}
}
2 changes: 1 addition & 1 deletion cross_contract/contract_b/assembly/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as contract from "as-soroban-sdk/lib/contract";

export function callc(): I32Val {

let contractId = "4014747356d8a39399d6d43609504f0f18c6a127fcaa6fdddcdfa3986bd65058";
let contractId = "e6dd1c7617ec7b64ed91f444b6eaa46df751c7782a9d7697954decdf271a41ce";
let func = "add";
let args = new Vec();
args.pushBack(fromI32(3));
Expand Down
4 changes: 2 additions & 2 deletions cross_contract/contract_b/contract.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name": "crontract b",
"version": "0.1.8",
"version": "0.1.9",
"description": "the contract that calls the other contract",
"host_functions_version": 32,
"host_functions_version": 37,
"functions": [
{
"name" : "callc",
Expand Down
4 changes: 2 additions & 2 deletions cross_contract/contract_b/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "contract_b",
"version": "0.1.0",
"version": "0.1.9",
"description": "a contract that calls another soroban contract",
"main": "index.js",
"scripts": {
Expand All @@ -23,6 +23,6 @@
}
},
"dependencies": {
"as-soroban-sdk": "^0.1.9"
"as-soroban-sdk": "^0.2.1"
}
}
2 changes: 1 addition & 1 deletion deployer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ The [deployer example](https://github.com/Soneso/as-soroban-examples/tree/main/d
To run a contract in the sandbox, you must first install the official `soroban cli` as described here: [stellar soroban cli](https://github.com/stellar/soroban-cli).

```sh
cargo install --locked --version 0.7.0 soroban-cli
cargo install --locked --version 0.8.0 soroban-cli
```

### Install the `add` contract
Expand Down
Loading

0 comments on commit 2184414

Please sign in to comment.