soneso / stellar-php-sdk
Stellar Network的PHP SDK
1.5.6
2024-09-05 10:30 UTC
Requires
- ext-bcmath: *
- ext-pcntl: *
- christian-riesen/base32: ~1.6
- guzzlehttp/guzzle: ~7.3
- paragonie/sodium_compat: ~1.17
- phpseclib/phpseclib: ^3.0
- yosymfony/toml: ~1.0
Requires (Dev)
- phpunit/phpunit: ~9.5
- dev-main
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.9
- 1.3.8
- 1.3.7
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.5
- 1.2.4
- 1.2.3
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.6
- 1.0.5
- 1.0.4
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
- 0.7.4
- 0.7.3
- 0.7.2
- 0.7.1
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.0
- 0.2.0
- 0.1.0
- 0.0.1
- dev-soroban_20.0.0_prep
This package is not auto-updated.
Last update: 2024-09-19 10:38:45 UTC
README
Soneso开源的PHP Stellar SDK提供了构建和签名交易、连接和查询Horizon的API。
安装
composer require soneso/stellar-php-sdk
快速开始
1. 创建Stellar密钥对
随机生成
// create a completely new and unique pair of keys. $keyPair = KeyPair::random(); print($keyPair->getAccountId()); // GCFXHS4GXL6BVUCXBWXGTITROWLVYXQKQLF4YH5O5JT3YZXCYPAFBJZB print($keyPair->getSecretSeed()); // SAV76USXIJOBMEQXPANUOQM6F5LIOTLPDIDVRJBFFE2MDJXG24TAPUU7
2. 创建账户
密钥对生成后,您已经获得了地址,但它必须至少有1卢门的转账才能激活。
2.1 测试网
如果您想在Stellar测试网上进行操作,SDK可以像下面这样请求Friendbot为您创建账户
$funded = FriendBot::fundTestAccount($keyPair->getAccountId()); print ($funded ? "account funded" : "account not funded");
2.2 公网
另一方面,如果您想在公网上创建账户,您应该从交易所购买一些Stellar Lumens (XLM)。当您将卢门提现到新账户时,交易所会自动为您创建账户。但是,如果您想从自己的另一个账户创建账户,您可以运行以下代码
/// Init sdk for public net $sdk = StellarSDK::getPublicNetInstance(); /// Create a key pair for your existing account. $keyA = KeyPair::fromSeed("SAPS66IJDXUSFDSDKIHR4LN6YPXIGCM5FBZ7GE66FDKFJRYJGFW7ZHYF"); /// Load the data of your account from the stellar network. $accA = $sdk->requestAccount($keyA->getAccountId()); /// Create a keypair for a new account. $keyB = KeyPair::random(); /// Create the operation builder. $createAccBuilder = new CreateAccountOperationBuilder($keyB->getAccountId(), "3"); // send 3 XLM (lumen) // Create the transaction. $transaction = (new TransactionBuilder($accA)) ->addOperation($createAccBuilder->build()) ->build(); /// Sign the transaction with the key pair of your existing account. $transaction->sign($keyA, Network::public()); /// Submit the transaction to the stellar network. $response = $sdk->submitTransaction($transaction); if ($response->isSuccessful()) { printf (PHP_EOL."account %s created", $keyB->getAccountId()); }
3. 检查账户
3.1 基本信息检查
创建账户后,我们可以检查账户的基本信息。
$accountId = "GCQHNQR2VM5OPXSTWZSF7ISDLE5XZRF73LNU6EOZXFQG2IJFU4WB7VFY"; // Request the account data. $account = $sdk->requestAccount($accountId); // You can check the `balance`, `sequence`, `flags`, `signers`, `data` etc. foreach ($account->getBalances() as $balance) { switch ($balance->getAssetType()) { case Asset::TYPE_NATIVE: printf (PHP_EOL."Balance: %s XLM", $balance->getBalance() ); break; default: printf(PHP_EOL."Balance: %s %s Issuer: %s", $balance->getBalance(), $balance->getAssetCode(), $balance->getAssetIssuer()); } } print(PHP_EOL."Sequence number: ".$account->getSequenceNumber()); foreach ($account->getSigners() as $signer) { print(PHP_EOL."Signer public key: ".$signer->getKey()); }
3.2 检查付款
您可以检查与账户相关的付款
$accountId = $account->getAccountId(); $operationsPage = $sdk->payments()->forAccount($accountId)->order("desc")->execute(); foreach ($operationsPage->getOperations() as $payment) { if ($payment->isTransactionSuccessful()) { print(PHP_EOL."Transaction hash: ".$payment->getTransactionHash()); } }
您可以使用limit
、order
和cursor
来定制查询。获取账户、账本和交易的最新付款。
Horizon支持SSE推送数据。您可以像这样使用它
$accountId = "GCDBA6GFGEHAMVAMRL6R2733EXUENJ35EMYNA2LE7WWJPVANORVC4UNA"; $sdk->payments()->forAccount($accountId)->cursor("now")->stream(function(OperationResponse $response) { if ($response instanceof PaymentOperationResponse) { switch ($response->getAsset()->getType()) { case Asset::TYPE_NATIVE: printf("Payment of %s XLM from %s received.", $response->getAmount(), $response->getSourceAccount()); break; default: printf("Payment of %s %s from %s received.", $response->getAmount(), $response->getAsset()->getCode(), $response->getSourceAccount()); } if (floatval($response->getAmount()) > 0.5) { exit; } } });
另请参阅流付款示例
3.3 检查其他信息
就像付款一样,您可以检查assets
、transactions
、effects
、offers
、operations
、ledgers
等。
$sdk->assets() $sdk->transactions() $sdk->effects() $sdk->offers() $sdk->operations() $sdk->orderBook() $sdk->trades() // add so on ...
4. 构建和提交交易
示例 "发送本地付款"
$senderKeyPair = KeyPair::fromSeed("SA52PD5FN425CUONRMMX2CY5HB6I473A5OYNIVU67INROUZ6W4SPHXZB"); $destination = "GCRFFUKMUWWBRIA6ABRDFL5NKO6CKDB2IOX7MOS2TRLXNXQD255Z2MYG"; // Load sender account data from the stellar network. $sender = $sdk->requestAccount($senderKeyPair->getAccountId()); // Build the transaction to send 100 XLM native payment from sender to destination $paymentOperation = (new PaymentOperationBuilder($destination,Asset::native(), "100"))->build(); $transaction = (new TransactionBuilder($sender))->addOperation($paymentOperation)->build(); // Sign the transaction with the sender's key pair. $transaction->sign($senderKeyPair, Network::testnet()); // Submit the transaction to the stellar network. $response = $sdk->submitTransaction($transaction); if ($response->isSuccessful()) { print(PHP_EOL."Payment sent"); }
文档和示例
示例
更多示例可以在测试中找到。
实现的SEPs
- SEP-0001: stellar.toml
- SEP-0002: Federation
- SEP-0005: Key derivation
- SEP-0006: 存款和取款API
- SEP-0007: URI方案以促进委托签名
- SEP-0008: 受监管资产
- SEP-0009: 标准KYC字段
- SEP-0010: Stellar Web身份验证
- SEP-0011: Txrep
- SEP-0012: KYC API
- SEP-0023: Strkeys
- SEP-0024: 主办存款和取款
- SEP-0029: 账户备忘录要求
- SEP-0030: 账户恢复
- SEP-0038: 报价
- SEP-0031: 跨境支付
Soroban支持
此SDK提供了对Soroban的支持。