Skip to content

Commit 85031e6

Browse files
update soroban doc
1 parent 9a15982 commit 85031e6

8 files changed

+86
-81
lines changed

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
## [1.6.0] - 19.Jul.2023.
2+
- add soroban prev. 10 support
3+
14
## [1.5.8] - 11.Jul.2023.
25
- add SEP-24 support
36
-

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ The Soneso open source Stellar SDK for Flutter is build with Dart and provides A
1111
1. Add the dependency to your pubspec.yaml file:
1212
```
1313
dependencies:
14-
stellar_flutter_sdk: ^1.5.8
14+
stellar_flutter_sdk: ^1.6.0
1515
```
1616
2. Install it (command line or IDE):
1717
```

documentation/sdk_api_doc.zip

408 KB
Binary file not shown.

lib/src/stellar_sdk.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import 'requests/liquidity_pools_request_builder.dart';
3131

3232
/// Main class of the flutter stellar sdk.
3333
class StellarSDK {
34-
static const versionNumber = "1.5.8";
34+
static const versionNumber = "1.6.0";
3535

3636
static final StellarSDK PUBLIC = StellarSDK("https://horizon.stellar.org");
3737
static final StellarSDK TESTNET = StellarSDK("https://horizon-testnet.stellar.org");

pubspec.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: stellar_flutter_sdk
22
description: A stellar blockchain sdk that query's horizon, build, signs and submits transactions to the stellar network.
3-
version: 1.5.8
3+
version: 1.6.0
44
homepage: https://github.com/Soneso/stellar_flutter_sdk
55

66
environment:

soroban.md

+77-75
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ To deploy and/or invoke smart contracts with the Flutter SDK use the ```SorobanS
1515

1616
Soroban-RPC can be simply described as a “live network gateway for Soroban”. It provides information that the network currently has in its view (i.e. current state). It also has the ability to send a transaction to the network and query the network for the status of previously sent transactions.
1717

18-
You can install your own instance of a Soroban-RPC Server as described [here](https://soroban.stellar.org/docs/tutorials/deploy-to-futurenet). Alternatively, you can use a public remote instance for testing.
18+
You can install your own instance of a Soroban-RPC Server as described [here](https://soroban.stellar.org/docs/getting-started/deploy-to-futurenet). Alternatively, you can use a public remote instance for testing.
1919

2020
The Soroban-RPC API is described [here](https://soroban.stellar.org/api/).
2121

@@ -61,7 +61,7 @@ AccountResponse submitter = await sdk.accounts.account(submitterId);
6161

6262
#### Deploy your contract
6363

64-
If you want to create a smart contract for testing, you can easily build one with our [AssemblyScript Soroban SDK](https://github.com/Soneso/as-soroban-sdk) or with the [official Stellar Rust SDK](https://soroban.stellar.org/docs/examples/hello-world). Here you can find [examples](https://github.com/Soneso/as-soroban-examples) to be build with the AssemblyScript SDK.
64+
If you want to create a smart contract for testing, you can find the official examples [here](https://github.com/stellar/soroban-examples).
6565

6666
There are two main steps involved in the process of deploying a contract. First you need to **upload** the **contract code** and then to **create** the **contract**.
6767

@@ -73,7 +73,7 @@ UploadContractWasmHostFunction uploadFunction =
7373
UploadContractWasmHostFunction(contractCode);
7474
7575
InvokeHostFunctionOperation operation =
76-
(InvokeHostFuncOpBuilder()).addFunction(uploadFunction).build();
76+
InvokeHostFuncOpBuilder(uploadFunction).build();
7777
7878
// Build the transaction
7979
Transaction transaction =
@@ -128,13 +128,16 @@ if (GetTransactionResponse.STATUS_NOT_FOUND == status) {
128128
}
129129
```
130130

131+
Hint: If you experience an error with the transaction result ```txInternalError``` it is most likely that a ledger entry used in the transaction has expired. This is an issue specific to soroban prev. 10 (see [here](https://discord.com/channels/897514728459468821/1130347673627664515)). You can fix it by restoring the footprint (see this [example](https://github.com/Soneso/stellar_flutter_sdk/blob/9a15982ac862bdcab33713184c800065e573f39b/test/soroban_test.dart#L57) in the soroban test of the SDK).
132+
131133
If the transaction was successful, the status response contains the ```wasmId``` of the installed contract code. We need the ```wasmId``` in our next step to **create** the contract:
132134

133135
```dart
134136
// Build the operation for creating the contract
135-
InvokeHostFunctionOperation operation = (InvokeHostFuncOpBuilder())
136-
.addFunction(CreateContractHostFunction(wasmId))
137-
.build();
137+
CreateContractHostFunction function = CreateContractHostFunction(
138+
Address.forAccountId(accountId), contractWasmId);
139+
InvokeHostFunctionOperation operation =
140+
InvokeHostFuncOpBuilder(function).build();
138141
139142
// Build the transaction for creating the contract
140143
Transaction transaction = new TransactionBuilder(account)
@@ -144,9 +147,10 @@ Transaction transaction = new TransactionBuilder(account)
144147
SimulateTransactionResponse simulateResponse =
145148
await sorobanServer.simulateTransaction(transaction);
146149
147-
// set transaction data, add resource fee and sign transaction
150+
// set transaction data, add resource fee & auth and sign transaction
148151
transaction.sorobanTransactionData = simulateResponse.transactionData;
149152
transaction.addResourceFee(simulateResponse.minResourceFee!);
153+
transaction.setSorobanAuth(simulateResponse.sorobanAuth);
150154
transaction.sign(accountKeyPair, Network.FUTURENET);
151155
152156
// Send the transaction to the network.
@@ -159,7 +163,7 @@ if (sendResponse.error == null) {
159163
}
160164
```
161165

162-
As you can see, we use the ```wasmId``` to create the operation and the transaction for creating the contract. After simulating, we obtain the footprint to be set in the transaction. Next, sign the transaction and send it to the Soroban-RPC Server. The transaction status will be "pending", so we need to wait a bit and poll for the current status:
166+
As you can see, we use the ```wasmId``` to create the operation and the transaction for creating the contract. After simulating, we obtain the transaction data and auth to be set in the transaction. Next, sign the transaction and send it to the Soroban-RPC Server. The transaction status will be "pending", so we need to wait a bit and poll for the current status:
163167

164168
```dart
165169
// Fetch transaction
@@ -193,21 +197,15 @@ GetLedgerEntryResponse contractCodeEntry =
193197

194198
Now, that we successfully deployed our contract, we are going to invoke it using the Flutter SDK.
195199

196-
First let's have a look to a simple (hello word) contract created with the [AssemblyScript Soroban SDK](https://github.com/Soneso/as-soroban-sdk). The code and instructions on how to build it, can be found in this [example](https://github.com/Soneso/as-soroban-examples/tree/main/hello_word).
197-
198-
*Hello Word contract AssemblyScript code:*
200+
First let's have a look to a simple (hello word) contract created with the Rust Soroban SDK. The code and instructions on how to build it, can be found in the official [soroban docs](https://soroban.stellar.org/docs/getting-started/hello-world).
199201

200-
```typescript
201-
import {Symbol, VecObject, fromSmallSymbolStr} from 'as-soroban-sdk/lib/value';
202-
import {Vec} from 'as-soroban-sdk/lib/vec';
202+
*Hello Word contract code:*
203203

204-
export function hello(to: Symbol): VecObject {
205-
206-
let vec = new Vec();
207-
vec.pushFront(fromSmallSymbolStr("Hello"));
208-
vec.pushBack(to);
209-
210-
return vec.getHostObject();
204+
```rust
205+
impl HelloContract {
206+
pub fn hello(env: Env, to: Symbol) -> Vec<Symbol> {
207+
vec![&env, symbol_short!("Hello"), to]
208+
}
211209
}
212210
```
213211

@@ -217,18 +215,18 @@ To invoke the contract with the Flutter SDK, we first need to build the correspo
217215

218216

219217
```dart
220-
// Name of the method to be invoked
221-
String method = "hello";
218+
// Name of the function to be invoked
219+
String functionName = "hello";
222220
223221
// Prepare the argument (Symbol)
224222
XdrSCVal arg = XdrSCVal.forSymbol("friend");
225223
226224
// Prepare the "invoke" operation
227225
InvokeContractHostFunction hostFunction = InvokeContractHostFunction(
228-
contractId!, functionName,arguments: [arg]);
226+
contractId!, functionName, arguments: [arg]);
229227
230228
InvokeHostFunctionOperation operation =
231-
(InvokeHostFuncOpBuilder()).addFunction(hostFunction).build();
229+
InvokeHostFuncOpBuilder(hostFunction).build();
232230
233231
// Build the transaction
234232
Transaction transaction =
@@ -303,74 +301,75 @@ Success!
303301

304302
#### Deploying Stellar Asset Contract (SAC)
305303

306-
The Flutter SDK also provides support for deploying the build-in [Stellar Asset Contract](https://soroban.stellar.org/docs/built-in-contracts/stellar-asset-contract) (SAC). The following operations are available for this purpose:
304+
The Flutter SDK also provides support for deploying the build-in [Stellar Asset Contract](https://soroban.stellar.org/docs/advanced-tutorials/stellar-asset-contract) (SAC). The following operations are available for this purpose:
307305

308306
1. Deploy SAC with source account:
309307

310308
```dart
311-
InvokeHostFunctionOperation operation = (InvokeHostFuncOpBuilder())
312-
.addFunction(DeploySACWithSourceAccountHostFunction())
313-
.build();
309+
DeploySACWithSourceAccountHostFunction function =
310+
DeploySACWithSourceAccountHostFunction(
311+
Address.forAccountId(accountId));
312+
313+
InvokeHostFunctionOperation operation =
314+
InvokeHostFuncOpBuilder(function).build();
315+
316+
//...
317+
// set transaction data, add resource fee & auth and sign transaction
318+
transaction.sorobanTransactionData = simulateResponse.transactionData;
319+
transaction.addResourceFee(simulateResponse.minResourceFee!);
320+
transaction.setSorobanAuth(simulateResponse.sorobanAuth);
321+
transaction.sign(accountKeyPair, Network.FUTURENET);
314322
```
315323

316324
2. Deploy SAC with asset:
317325

318326
```dart
319-
InvokeHostFunctionOperation operation = (InvokeHostFuncOpBuilder())
320-
.addFunction(DeploySACWithAssetHostFunction(assetFsdk))
321-
.build();
327+
InvokeHostFunctionOperation operation =
328+
InvokeHostFuncOpBuilder(DeploySACWithAssetHostFunction(asset))
329+
.build();
322330
```
323331

324332
#### Soroban Authorization
325333

326-
The Flutter SDK provides support for the [Soroban Authorization Framework](https://soroban.stellar.org/docs/learn/authorization).
334+
The Flutter SDK provides support for the [Soroban Authorization Framework](https://soroban.stellar.org/docs/fundamentals-and-concepts/authorization).
335+
The SDK's implementation can be found [here](https://github.com/Soneso/stellar_flutter_sdk/blob/master/lib/src/soroban/soroban_auth.dart).
327336

328-
For this purpose, it offers the `Address`, `AuthorizedInvocation` and `ContractAuth` classes as well as helper functions like `getNonce(...)`.
329-
330-
Here is a code fragment showing how they can be used:
337+
To provide authorization you can add a list of `SorobanAuthorizationEntry` to the transaction before sending it.
331338

332339
```dart
333-
Address invokerAddress = Address.forAccountId(invokerId);
334-
335-
String functionName = "auth";
336-
List<XdrSCVal> args = [invokerAddress.toXdrSCVal(), XdrSCVal.forU32(3)];
337-
338-
AuthorizedInvocation rootInvocation =
339-
AuthorizedInvocation(contractId, functionName, args: args);
340-
341-
int nonce = await sorobanServer.getNonce(invokerId, contractId);
342-
343-
ContractAuth contractAuth =
344-
ContractAuth(rootInvocation, address: invokerAddress, nonce: nonce);
345-
346-
// sign
347-
contractAuth.sign(invokerKeyPair, Network.FUTURENET);
348-
349-
InvokeContractHostFunction hostFunction = InvokeContractHostFunction(
350-
contractId, functionName,
351-
arguments: args, auth: [contractAuth]);
340+
transaction.setSorobanAuth(myAuthList);
341+
```
352342

353-
InvokeHostFunctionOperation operation =
354-
(InvokeHostFuncOpBuilder()).addFunction(hostFunction).build();
343+
The easiest way to do this is to use the auth data generated by the simulation.
355344

356-
// simulate first to obtain the transaction data + resource fee
357-
GetAccountResponse submitter =
358-
await sorobanServer.getAccount(submitterId);
345+
```dart
346+
transaction.setSorobanAuth(simulateResponse.sorobanAuth);
347+
```
348+
But you can also compose the authorization entries by yourself.
359349

360-
Transaction transaction =
361-
TransactionBuilder(submitter).addOperation(invokeOp).build();
350+
If the entries need to be signed you can do it as follows:
351+
```dart
352+
// sign auth
353+
List<SorobanAuthorizationEntry>? auth = simulateResponse.sorobanAuth;
354+
assert(auth != null);
355+
356+
GetLatestLedgerResponse latestLedgerResponse = await sorobanServer.getLatestLedger();
357+
358+
for (SorobanAuthorizationEntry a in auth!) {
359+
// update signature expiration ledger
360+
a.credentials.addressCredentials!.signatureExpirationLedger =
361+
latestLedgerResponse.sequence! + 10;
362+
// sign
363+
a.sign(invokerKeypair, Network.FUTURENET);
364+
}
362365
363-
SimulateTransactionResponse simulateResponse =
364-
await sorobanServer.simulateTransaction(transaction);
366+
transaction.setSorobanAuth(auth);
365367
```
366368

367-
The example above invokes this assembly script [auth contract](https://github.com/Soneso/as-soroban-examples/tree/main/auth#code). In this example the submitter of the transaction is not the same as the "invoker" of the contract function.
368-
369-
One can find another example in the [Soroban Auth Test Cases](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test_auth.dart) of the SDK where the submitter and invoker are the same, as well as an example where contract auth from the simulation response is used.
369+
One can find multiple examples in the [Soroban Auth Test](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test_auth.dart) and [Soroban Atomic Swap Test](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test_atomic_swap.dart) of the SDK.
370370

371-
An advanced auth example can be found in the [flutter atomic swap](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test_atomic_swap.dart) test.
372371

373-
Hint: Resource values and fees have been added in the new soroban preview 9 version. The calculation of the minimum resource values and fee by the simulation (preflight) is not always accurate, because it does not consider signatures. This may result in a failing transaction because of insufficient resources. In this case one can experiment and increase the resources values within the soroban transaction data before signing and submitting the transaction. E.g.:
372+
Hint: Resource values and fees have been added since soroban preview 9 version. The calculation of the minimum resource values and fee by the simulation (preflight) is not always accurate, because it does not consider signatures. This may result in a failing transaction because of insufficient resources. In this case one can experiment and increase the resources values within the soroban transaction data before signing and submitting the transaction. E.g.:
374373

375374
```dart
376375
int instructions = simulateResponse.transactionData!.resources.instructions.uint32;
@@ -392,20 +391,23 @@ The Soroban-RPC server provides the possibility to request contract events.
392391
You can use the Flutter SDK to request events like this:
393392

394393
```dart
395-
EventFilter eventFilter =
396-
EventFilter(type: "contract", contractIds: [contractId]);
394+
TopicFilter topicFilter = TopicFilter(
395+
["*", XdrSCVal.forSymbol('increment').toBase64EncodedXdrString()]);
396+
397+
EventFilter eventFilter = EventFilter(
398+
type: "contract", contractIds: [contractId], topics: [topicFilter]);
397399
398400
GetEventsRequest eventsRequest =
399-
GetEventsRequest(startLedger, endLedger, filters: [eventFilter]);
401+
GetEventsRequest(startLedger, filters: [eventFilter]);
400402
401403
GetEventsResponse eventsResponse =
402-
await sorobanServer.getEvents(eventsRequest);
404+
await sorobanServer.getEvents(eventsRequest);
403405
```
404-
Find the complete code [here](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test.dart#L488).
406+
Find the complete code in the [Soroban Test](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test.dart).
405407

406408
#### Hints and Tips
407409

408-
You can find the working code and more in the [Soroban Test Cases](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test.dart) and [Soroban Auth Test Cases](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test_auth.dart) of the Flutter SDK. The used wasm byte-code files can be found in the [test/wasm](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/wasm/) folder.
410+
You can find the working code and more in the [Soroban Test](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test.dart), [Soroban Auth Test](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test_auth.dart) and [Soroban Atomic Swap Test](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/soroban_test_atomic_swap.dart) of the Flutter SDK. The used wasm byte-code files can be found in the [test/wasm](https://github.com/Soneso/stellar_flutter_sdk/blob/master/test/wasm/) folder.
409411

410412
Because Soroban and the Flutter SDK support for Soroban are in development, errors may occur. For a better understanding of an error you can enable the ```SorobanServer``` logging:
411413

test/account_test.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ void main() {
332332
response = await sdk.submitTransaction(transaction);
333333
assert(response.success);
334334

335-
await Future.delayed(const Duration(seconds: 20), () {});
335+
await Future.delayed(const Duration(seconds: 30), () {});
336336

337337
subscription.cancel();
338338
assert(count == 3);

test/payments_test.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -1072,8 +1072,8 @@ void main() {
10721072
response = await sdk.submitTransaction(transaction);
10731073
assert(response.success);
10741074

1075-
// wait 3 seconds for the payment event.
1076-
await Future.delayed(const Duration(seconds: 3), () {});
1075+
// wait 10 seconds for the payment event.
1076+
await Future.delayed(const Duration(seconds: 10), () {});
10771077
subscription.cancel();
10781078
assert(paymentReceived);
10791079
});

0 commit comments

Comments
 (0)