This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 81
Upgrade getLedgerEntries
examples to latest pre-stable releases of the SDKs
#635
Merged
Merged
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -42,80 +42,84 @@ The example above is querying a deployment of the [`increment` example contract] | |
|
||
:::note | ||
|
||
If you are using the Python `stellar_sdk` to generate these keys, you will need to install the latest version of the `soroban` branch of the SDK. This can be done like so: | ||
If you are using the Python `stellar_sdk` to generate these keys, you will need to install the latest pre-release version of the SDK. This can be done like so: | ||
|
||
```bash | ||
pip install git+https://github.com/StellarCN/py-stellar-base.git@soroban | ||
pip install stellar-sdk==9.0.0-beta0 | ||
``` | ||
|
||
::: | ||
|
||
```python | ||
from stellar_sdk import xdr | ||
from stellar_sdk.soroban import types as soroban_types | ||
from stellar_sdk.strkey import StrKey | ||
from stellar_sdk.soroban import Symbol | ||
|
||
def get_ledger_key_symbol(contract_id: str, symbol_text: str) -> str: | ||
ledger_key = xdr.LedgerKey( | ||
type=xdr.LedgerEntryType.CONTRACT_DATA, | ||
contract_data=xdr.LedgerKeyContractData( | ||
contract_id=xdr.hash.Hash(bytes.fromhex(contract_id)), | ||
key=soroban_types.Symbol(symbol_text).to_xdr_sc_val(), | ||
), | ||
) | ||
return ledger_key.to_xdr() | ||
ledger_key = xdr.LedgerKey( | ||
type=xdr.LedgerEntryType.CONTRACT_DATA, | ||
contract_data=xdr.LedgerKeyContractData( | ||
contract_id=xdr.hash.Hash(StrKey.decode_contract(contract_id)), | ||
key=Symbol(symbol_text).to_xdr_sc_val(), | ||
), | ||
) | ||
return ledger_key.to_xdr() | ||
|
||
print( | ||
get_ledger_key_symbol( | ||
"58b53604fda60524fa7097fca030a6e840c4a84dccf7a4fe909790245e31a86f", "COUNTER" | ||
) | ||
get_ledger_key_symbol( | ||
"CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE", | ||
"COUNTER" | ||
) | ||
) | ||
``` | ||
|
||
#### JavaScript | ||
|
||
If you are using the JavaScript `stellar-sdk` to generate these keys, you will need to install the latest pre-release version of the SDK. This can be done like so: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above for linking to the SDK |
||
|
||
```bash | ||
yarn add stellar-sdk@beta | ||
``` | ||
|
||
```js | ||
const SorobanClient = require('soroban-client') | ||
const xdr = SorobanClient.xdr | ||
import { xdr, StrKey } from 'stellar-sdk'; | ||
|
||
const getLedgerKeySymbol = (contractId, symbolText) => { | ||
const ledgerKey = xdr.LedgerKey.contractData( | ||
new xdr.LedgerKeyContractData({ | ||
contractId: Buffer.from(contractId, 'hex'), | ||
contractId: StrKey.decodeContract(contractId), | ||
key: xdr.ScVal.scvSymbol(symbolText) | ||
}) | ||
); | ||
return ledgerKey.toXDR('base64'); | ||
}; | ||
|
||
console.log( | ||
getLedgerKeySymbol( | ||
'CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE', | ||
'COUNTER' | ||
) | ||
return ledgerKey.toXDR('base64') | ||
} | ||
console.log(getLedgerKeySymbol( | ||
'58b53604fda60524fa7097fca030a6e840c4a84dccf7a4fe909790245e31a86f', | ||
'COUNTER' | ||
)) | ||
); | ||
``` | ||
|
||
### Requesting an Account | ||
|
||
:::note | ||
|
||
This functionality is included in the js [soroban-client](https://www.npmjs.com/package/soroban-client) package as `Server.getAccount(address)`. | ||
This functionality is included in the JavaScript [stellar-sdk](https://www.npmjs.com/package/stellar-sdk) package as `SorobanRpc.Server.getAccount(address)`. | ||
|
||
::: | ||
|
||
Accounts are stored as ledger entries, so we can use this method to look up an account along with it's current sequence number. | ||
|
||
```js | ||
const SorobanClient = require('soroban-client') | ||
const xdr = SorobanClient.xdr | ||
import { xdr, Keypair, StrKey } from 'stellar-sdk' | ||
|
||
const getLedgerKeyAccount = (address) => { | ||
// We can use the `StrKey` library here to decode the address into the public | ||
// key. | ||
const publicKey = SorobanClient.StrKey.decodeEd25519PublicKey(address); | ||
|
||
const publicKey = StrKey.decodeEd25519PublicKey(address) | ||
const ledgerKey = xdr.LedgerKey.account( | ||
new xdr.LedgerKeyAccount({ | ||
accountId: xdr.PublicKey.publicKeyTypeEd25519( | ||
publicKey | ||
), | ||
accountId: Keypair.fromPublicKey(publicKey).xdrPublicKey(), | ||
}) | ||
) | ||
return ledgerKey.toXDR('base64') | ||
|
@@ -190,42 +194,36 @@ When you deploy a contract, first the code is "installed" (i.e. it is uploaded o | |
|
||
```python | ||
from stellar_sdk import xdr | ||
from stellar_sdk.strkey import StrKey | ||
|
||
def get_ledger_key_contract_code(contract_id: str) -> str: | ||
ledger_key = xdr.LedgerKey( | ||
type=xdr.LedgerEntryType.CONTRACT_DATA, | ||
contract_data=xdr.LedgerKeyContractData( | ||
contract_id=xdr.Hash(bytes.fromhex(contract_id)), | ||
key=xdr.SCVal( | ||
type=xdr.SCValType.SCV_LEDGER_KEY_CONTRACT_EXECUTABLE, | ||
), | ||
), | ||
) | ||
return ledger_key.to_xdr() | ||
ledger_key = xdr.LedgerKey( | ||
type=xdr.LedgerEntryType.CONTRACT_DATA, | ||
contract_data=xdr.LedgerKeyContractData( | ||
contract_id=xdr.Hash(StrKey.decodeContract(contract_id)), | ||
key=xdr.SCVal( | ||
type=xdr.SCValType.SCV_LEDGER_KEY_CONTRACT_EXECUTABLE, | ||
), | ||
), | ||
) | ||
return ledger_key.to_xdr() | ||
|
||
print( | ||
get_ledger_key_contract_code( | ||
"af9a2527e3b3b5571d63b0246ba32b7d31a5323766df7c60dfc0b3e3ba6fdf23" | ||
) | ||
get_ledger_key_contract_code( | ||
"CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE" | ||
) | ||
) | ||
# OUTPUT: AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFA== | ||
``` | ||
|
||
##### JavaScript | ||
|
||
```javascript | ||
const { Address, xdr } = require("soroban-client"); | ||
import { Address, xdr } from 'stellar-sdk'; | ||
|
||
function getLedgerKeyContractCode(contractId) { | ||
let ledgerKey = xdr.LedgerKey.contractData( | ||
new xdr.LedgerKeyContractData({ | ||
contract: new Address(contractId).toScAddress(), | ||
key: new xdr.ScVal.scvLedgerKeyContractInstance(), | ||
durability: xdr.ContractDataDurability.persistent() | ||
}) | ||
); | ||
|
||
return ledgerKey.toXDR('base64'); | ||
const [ _, instance ] = new ContractId(contractId).getFootprint(); | ||
return instance.toXDR('base64'); | ||
} | ||
|
||
console.log( | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be nice to link to the relevant SDK when mentioned like you did below:
"If you are using the Python
stellar_sdk
..."