Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
142 changes: 139 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

This tool simplifies the process of interacting with Soroban contracts by generating the necessary code to call contract
methods directly from your preferred programming language. Currently, it supports
Python and Java. [stellar-cli](https://github.com/stellar/stellar-cli) provides support for TypeScript and Rust.
Python, Java, Flutter/Dart, PHP, and Swift/iOS. [stellar-cli](https://github.com/stellar/stellar-cli) provides support for TypeScript and Rust.

## Web Interface
We have a web interface for generating bindings. You can access via [https://stellar-contract-bindings.fly.dev/](https://stellar-contract-bindings.fly.dev/).
Expand All @@ -25,13 +25,34 @@ Please check the help message for the most up-to-date usage information:
stellar-contract-bindings --help
```

### Example
### Examples

#### Python
```shell
stellar-contract-bindings python --contract-id CDOAW6D7NXAPOCO7TFAWZNJHK62E3IYRGNRVX3VOXNKNVOXCLLPJXQCF --rpc-url https://mainnet.sorobanrpc.com --output ./bindings
```

This command will generate Python binding for the specified contract and save it in the `./bindings` directory.
#### Java
```shell
stellar-contract-bindings java --contract-id CDOAW6D7NXAPOCO7TFAWZNJHK62E3IYRGNRVX3VOXNKNVOXCLLPJXQCF --rpc-url https://mainnet.sorobanrpc.com --output ./bindings --package com.example
```

#### Flutter/Dart
```shell
stellar-contract-bindings flutter --contract-id CDOAW6D7NXAPOCO7TFAWZNJHK62E3IYRGNRVX3VOXNKNVOXCLLPJXQCF --rpc-url https://mainnet.sorobanrpc.com --output ./lib --class-name MyContract
```

#### PHP
```shell
stellar-contract-bindings php --contract-id CDOAW6D7NXAPOCO7TFAWZNJHK62E3IYRGNRVX3VOXNKNVOXCLLPJXQCF --rpc-url https://mainnet.sorobanrpc.com --output ./generated --namespace MyApp\\Contracts --class-name MyContractClient
```

#### Swift/iOS
```shell
stellar-contract-bindings swift --contract-id CDOAW6D7NXAPOCO7TFAWZNJHK62E3IYRGNRVX3VOXNKNVOXCLLPJXQCF --rpc-url https://mainnet.sorobanrpc.com --output ./Sources --class-name MyContract
```

These commands will generate language-specific bindings for the specified contract and save them in the respective directories.

### Using the Generated Binding

Expand Down Expand Up @@ -64,6 +85,121 @@ public class Example extends ContractClient {
}
```

#### Flutter/Dart
```dart
import 'package:stellar_flutter_sdk/stellar_flutter_sdk.dart';
import 'lib/my_contract_client.dart'; // Import the generated bindings

void main() async {
final sourceKeyPair = KeyPair.fromAccountId("GD5KKP3LHUDXLDCGKP55NLEOEHMS3Z4BS6IDDZFCYU3BDXUZTBWL7JNF");
// or: final sourceKeyPair = KeyPair.fromSecretSeed("S...");

// Create client instance
final client = await MyContractClient.forContractId(
sourceAccountKeyPair: sourceKeyPair,
contractId: "CDOAW6D7NXAPOCO7TFAWZNJHK62E3IYRGNRVX3VOXNKNVOXCLLPJXQCF",
network: Network.PUBLIC,
rpcUrl: "https://mainnet.sorobanrpc.com",
);

// Call contract method directly
try {
final result = await client.hello(input: "World");
print("Contract response: $result");
} catch (e) {
print("Error calling contract: $e");
}

// Or build an assembled transaction for more control
final assembledTx = await client.buildHelloTx(
input: "World",
methodOptions: MethodOptions(),
);
}
```

#### PHP
```php
<?php

use Soneso\StellarSDK\Crypto\KeyPair;
use Soneso\StellarSDK\Network;
use Soneso\StellarSDK\Soroban\Contract\ClientOptions;
use Soneso\StellarSDK\Soroban\Contract\MethodOptions;
use MyApp\Contracts\MyContractClient; // Import the generated bindings

// Initialize
$sourceKeyPair = KeyPair::fromAccountId("GD5KKP3LHUDXLDCGKP55NLEOEHMS3Z4BS6IDDZFCYU3BDXUZTBWL7JNF");
// or: $sourceKeyPair = KeyPair::fromSeed("S...")

// Create client instance
$options = new ClientOptions(
sourceAccountKeyPair: $sourceKeyPair,
contractId: "CDOAW6D7NXAPOCO7TFAWZNJHK62E3IYRGNRVX3VOXNKNVOXCLLPJXQCF",
network: Network::public(),
rpcUrl: "https://mainnet.sorobanrpc.com"
);

$client = MyContractClient::forClientOptions($options);

// Call contract method directly
try {
$result = $client->hello("World");
echo "Contract response: " . $result . "\n";
} catch (Exception $e) {
echo "Error calling contract: " . $e->getMessage() . "\n";
}

// Or build an assembled transaction for more control
$methodOptions = new MethodOptions();
$assembledTx = $client->buildHelloTx("World", $methodOptions);
```

#### Swift/iOS
```swift
import stellarsdk
import Foundation

// Import the generated bindings
// Assuming the generated file is named MyContract.swift

// Initialize
let sourceKeyPair = try! KeyPair.init(accountId: "GD5KKP3LHUDXLDCGKP55NLEOEHMS3Z4BS6IDDZFCYU3BDXUZTBWL7JNF")
// or: let sourceKeyPair = try! KeyPair.init(secretSeed: "S...")

// Create client instance
let options = ClientOptions(
sourceAccountKeyPair: sourceKeyPair,
contractId: "CDOAW6D7NXAPOCO7TFAWZNJHK62E3IYRGNRVX3VOXNKNVOXCLLPJXQCF",
network: .public,
rpcUrl: "https://mainnet.sorobanrpc.com"
)

Task {
do {
let client = try await MyContract.forClientOptions(options: options)

// Call contract method directly
let result = try await client.hello(
to: "World",
methodOptions: nil,
force: false
)
print("Contract response: \(result)")

// Or build an assembled transaction for more control
let methodOptions = MethodOptions()
let assembledTx = try await client.buildHelloTx(
to: "World",
methodOptions: methodOptions
)

} catch {
print("Error calling contract: \(error)")
}
}
```

## License

This project is licensed under the Apache-2.0 License. See the [LICENSE](LICENSE) file for details.
Expand Down
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ dependencies = [
stellar-contract-bindings = "stellar_contract_bindings.cli:cli"
stellar-contract-bindings-python = "stellar_contract_bindings.cli:cli_python"
stellar-contract-bindings-java = "stellar_contract_bindings.cli:cli_java"
stellar-contract-bindings-flutter = "stellar_contract_bindings.cli:cli_flutter"
stellar-contract-bindings-php = "stellar_contract_bindings.cli:cli_php"
stellar-contract-bindings-swift = "stellar_contract_bindings.cli:cli_swift"

[project.urls]
Homepage = "https://github.com/lightsail-network/stellar-contract-bindings"
Expand Down
21 changes: 21 additions & 0 deletions stellar_contract_bindings/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from stellar_contract_bindings import __version__
from stellar_contract_bindings.python import command as python_command
from stellar_contract_bindings.java import command as java_command
from stellar_contract_bindings.flutter import command as flutter_command
from stellar_contract_bindings.php import command as php_command
from stellar_contract_bindings.swift import command as swift_command


@click.group()
Expand All @@ -13,6 +16,9 @@ def cli():

cli.add_command(python_command)
cli.add_command(java_command)
cli.add_command(flutter_command)
cli.add_command(php_command)
cli.add_command(swift_command)


# https://github.com/lightsail-network/stellar-contract-bindings/issues/14
Expand All @@ -26,5 +32,20 @@ def cli_java():
java_command()


def cli_flutter():
"""CLI for generating Stellar contract bindings (Flutter)."""
flutter_command()


def cli_php():
"""CLI for generating Stellar contract bindings (PHP)."""
php_command()


def cli_swift():
"""CLI for generating Stellar contract bindings (Swift)."""
swift_command()


if __name__ == "__main__":
cli()
Loading
Loading