Skip to content
This repository has been archived by the owner on Aug 21, 2024. It is now read-only.

Commit

Permalink
Update outdated code.
Browse files Browse the repository at this point in the history
  • Loading branch information
overcat committed Feb 29, 2024
1 parent f96a059 commit 4c88ae0
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 118 deletions.
128 changes: 65 additions & 63 deletions api/methods/getLedgerEntries.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,24 @@ pip install --upgrade stellar-sdk
:::

```python
from stellar_sdk import xdr
from stellar_sdk.strkey import StrKey
from stellar_sdk.soroban import Symbol
from stellar_sdk import xdr, scval, Address

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(StrKey.decode_contract(contract_id)),
key=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=Address(contract_id).to_xdr_sc_address(),
key=scval.to_symbol(symbol_text),
durability=xdr.ContractDataDurability.PERSISTENT
),
)
return ledger_key.to_xdr()

print(
get_ledger_key_symbol(
"CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE",
"COUNTER"
)
get_ledger_key_symbol(
"CCPYZFKEAXHHS5VVW5J45TOU7S2EODJ7TZNJIA5LKDVL3PESCES6FNCI",
"COUNTER"
)
)
```

Expand All @@ -55,21 +54,22 @@ yarn add @stellar/stellar-sdk
```

```js
import { xdr, StrKey } from "@stellar/stellar-sdk";
import { xdr, Address } from "@stellar/stellar-sdk";

const getLedgerKeySymbol = (contractId, symbolText) => {
const ledgerKey = xdr.LedgerKey.contractData(
new xdr.LedgerKeyContractData({
contractId: StrKey.decodeContract(contractId),
contract: new Address(contractId).toScAddress(),
key: xdr.ScVal.scvSymbol(symbolText),
durability: xdr.ContractDataDurability.persistent(),
}),
);
return ledgerKey.toXDR("base64");
};

console.log(
getLedgerKeySymbol(
"CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE",
"CCPYZFKEAXHHS5VVW5J45TOU7S2EODJ7TZNJIA5LKDVL3PESCES6FNCI",
"COUNTER",
),
);
Expand All @@ -86,13 +86,12 @@ This functionality is included in the JavaScript [`stellar-sdk`](https://www.npm
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
import { xdr, Keypair, StrKey } from '@stellar/stellar-sdk'
import { xdr, Keypair } from '@stellar/stellar-sdk'

const getLedgerKeyAccount = (address) => {
const publicKey = StrKey.decodeEd25519PublicKey(address)
const ledgerKey = xdr.LedgerKey.account(
new xdr.LedgerKeyAccount({
accountId: Keypair.fromPublicKey(publicKey).xdrPublicKey(),
accountId: Keypair.fromPublicKey(address).xdrPublicKey(),
})
)
return ledgerKey.toXDR('base64')
Expand Down Expand Up @@ -128,11 +127,11 @@ And the response we get contains the `LedgerEntryData` with the current informat
"entries": [
{
"key": "AAAAAAAAAACp3BPIqFxM9XSnW6aHvavD3GWlJGfuylOt5tZL6CQtdQ==",
"xdr": "AAAAAAAAAACp3BPIqFxM9XSnW6aHvavD3GWlJGfuylOt5tZL6CQtdQAAABdIdugAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAA",
"xdr": "AAAAAAAAAACp3BPIqFxM9XSnW6aHvavD3GWlJGfuylOt5tZL6CQtdQAAABdIdugAAAWpygAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAA",
"lastModifiedLedgerSeq": "164303"
}
],
"latestLedger": "246819"
"latestLedger": 246819
}
}
```
Expand All @@ -141,7 +140,7 @@ We can then parse this result as an `xdr.LedgerEntryData` type.

```js
const parsed = xdr.LedgerEntryData.fromXDR(
"AAAAAAAAAACp3BPIqFxM9XSnW6aHvavD3GWlJGfuylOt5tZL6CQtdQAAABdIdugAAAAADAAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAA",
"AAAAAAAAAACp3BPIqFxM9XSnW6aHvavD3GWlJGfuylOt5tZL6CQtdQAAABdIdugAAAWpygAAAAAAAAAAAAAAAAAAAAAAAAAAAQAAAAAAAAAAAAAA",
"base64",
);
console.log(parsed);
Expand All @@ -165,45 +164,43 @@ When you deploy a contract, first the code is "installed" (i.e. it is uploaded o
##### Python

```python
from stellar_sdk import xdr
from stellar_sdk.strkey import StrKey
from stellar_sdk import xdr, Address

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(StrKey.decodeContract(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=Address(contract_id).to_xdr_sc_address(),
key=xdr.SCVal(xdr.SCValType.SCV_LEDGER_KEY_CONTRACT_INSTANCE),
durability=xdr.ContractDataDurability.PERSISTENT
)
)
return ledger_key.to_xdr()

print(
get_ledger_key_contract_code(
"CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE"
)
get_ledger_key_contract_code(
"CCPYZFKEAXHHS5VVW5J45TOU7S2EODJ7TZNJIA5LKDVL3PESCES6FNCI"
)
)
# OUTPUT: AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFA==
# OUTPUT: AAAABgAAAAGfjJVEBc55drW3U87N1Py0Rw0/nlqUA6tQ6r28khEl4gAAABQAAAAB
```

##### JavaScript

```javascript
import { Address, xdr } from "@stellar/stellar-sdk";
import { Contract } from "@stellar/stellar-sdk";

function getLedgerKeyContractCode(contractId) {
const instance = new ContractId(contractId).getFootprint();
const instance = new Contract(contractId).getFootprint();
return instance.toXDR("base64");
}

console.log(
getLedgerKeyContractCode(
"CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE",
"CCPYZFKEAXHHS5VVW5J45TOU7S2EODJ7TZNJIA5LKDVL3PESCES6FNCI",
),
);
// OUTPUT: AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFA==
// OUTPUT: AAAABgAAAAGfjJVEBc55drW3U87N1Py0Rw0/nlqUA6tQ6r28khEl4gAAABQAAAAB
```

We then take our output from this function, and use it as the element in the `keys` array parameter in our call to the `getLedgerEntries` method.
Expand All @@ -214,7 +211,7 @@ We then take our output from this function, and use it as the element in the `ke
"id": 8675309,
"method": "getLedgerEntries",
"params": {
"keys": ["AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFA=="]
"keys": ["AAAABgAAAAGfjJVEBc55drW3U87N1Py0Rw0/nlqUA6tQ6r28khEl4gAAABQAAAAB"]
}
}
```
Expand All @@ -228,12 +225,12 @@ And the response we get contains the `LedgerEntryData` that can be used to find
"result": {
"entries": [
{
"key": "AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFA==",
"xdr": "AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFAAAABIAAAAAZBYoEJT3IaPMMk3FoRmnEQHoDxewPZL+Uor+xWI4uII=",
"key": "AAAABgAAAAGfjJVEBc55drW3U87N1Py0Rw0/nlqUA6tQ6r28khEl4gAAABQAAAAB",
"xdr": "AAAABgAAAAAAAAABn4yVRAXOeXa1t1POzdT8tEcNP55alAOrUOq9vJIRJeIAAAAUAAAAAQAAABMAAAAA5DNtbckOGVRsNVb8L7X/lIhAOy2o5G6GkLKXvc7W8foAAAAA",
"lastModifiedLedgerSeq": "261603"
}
],
"latestLedger": "262322"
"latestLedger": 262322
}
}
```
Expand All @@ -251,40 +248,45 @@ def get_ledger_key_wasm_id(contract_code_ledger_entry_data: str) -> str:
# First, we dig the wasm_id hash out of the xdr we received from RPC
contract_code_wasm_hash = xdr.LedgerEntryData.from_xdr(
contract_code_ledger_entry_data
).contract_data.val.exec.wasm_id.hash
).contract_data.val.instance.executable.wasm_hash
# Now, we can create the `LedgerKey` as we've done in previous examples
ledger_key = xdr.LedgerKey(
type=xdr.LedgerEntryType.CONTRACT_CODE,
contract_code=xdr.LedgerKeyContractCode(
hash=xdr.hash.Hash(contract_code_wasm_hash)
hash=contract_code_wasm_hash
),
)
return ledger_key.to_xdr()

print(
get_ledger_key_wasm_id(
"AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFAAAABIAAAAAZBYoEJT3IaPMMk3FoRmnEQHoDxewPZL+Uor+xWI4uII="
"AAAABgAAAAAAAAABn4yVRAXOeXa1t1POzdT8tEcNP55alAOrUOq9vJIRJeIAAAAUAAAAAQAAABMAAAAA5DNtbckOGVRsNVb8L7X/lIhAOy2o5G6GkLKXvc7W8foAAAAA"
)
)
# OUTPUT: AAAAB2QWKBCU9yGjzDJNxaEZpxEB6A8XsD2S/lKK/sViOLiC
# OUTPUT: AAAAB+QzbW3JDhlUbDVW/C+1/5SIQDstqORuhpCyl73O1vH6
```

##### JavaScript

```javascript
const { xdr } = require("@stellar/stellar-sdk");
import { xdr } from "@stellar/stellar-sdk";

function getLedgerKeyWasmId(contractCodeLedgerEntryData) {
const entry = xdr.LedgerEntryData.fromXDR(
contractCodeLedgerEntryData,
"base64",
);

const instance = entry.contractData().val().instance();
const wasmHash = entry
.contractData()
.val()
.instance()
.executable()
.wasmHash();

let ledgerKey = xdr.LedgerKey.contractCode(
new xdr.LedgerKeyContractCode({
hash: instance.wasmHash(),
hash: wasmHash,
}),
);

Expand All @@ -293,10 +295,10 @@ function getLedgerKeyWasmId(contractCodeLedgerEntryData) {

console.log(
getLedgerKeyWasmId(
"AAAABq+aJSfjs7VXHWOwJGujK30xpTI3Zt98YN/As+O6b98jAAAAFAAAABIAAAAAZBYoEJT3IaPMMk3FoRmnEQHoDxewPZL+Uor+xWI4uII=",
"AAAABgAAAAAAAAABn4yVRAXOeXa1t1POzdT8tEcNP55alAOrUOq9vJIRJeIAAAAUAAAAAQAAABMAAAAA5DNtbckOGVRsNVb8L7X/lIhAOy2o5G6GkLKXvc7W8foAAAAA",
),
);
// OUTPUT: AAAAB2QWKBCU9yGjzDJNxaEZpxEB6A8XsD2S/lKK/sViOLiC
// OUTPUT: AAAAB+QzbW3JDhlUbDVW/C+1/5SIQDstqORuhpCyl73O1vH6
```

Now, finally we have a `LedgerKey` that correspond to the Wasm byte-code that has been deployed under the `ContractId` we started out with so very long ago. This `LedgerKey` can be used in a final request to the Soroban-RPC endpoint.
Expand All @@ -307,7 +309,7 @@ Now, finally we have a `LedgerKey` that correspond to the Wasm byte-code that ha
"id": 8675309,
"method": "getLedgerEntries",
"params": {
"keys": ["AAAAB2QWKBCU9yGjzDJNxaEZpxEB6A8XsD2S/lKK/sViOLiC"]
"keys": ["AAAAB+QzbW3JDhlUbDVW/C+1/5SIQDstqORuhpCyl73O1vH6"]
}
}
```
Expand All @@ -321,13 +323,13 @@ And the response we get contains (even more) `LedgerEntryData` that we can decod
"result": {
"entries": [
{
"key": "AAAAB2QWKBCU9yGjzDJNxaEZpxEB6A8XsD2S/lKK/sViOLiC",
"xdr": "AAAABwAAAABkFigQlPcho8wyTcWhGacRAegPF7A9kv5Siv7FYji4ggAAAlQAYXNtAQAAAAEPA2ACfn4BfmABfgF+YAAAAgcBAXYBRwAAAwQDAQICBQMBABEGGQN/AUGAgMAAC38AQYWAwAALfwBBkIDAAAsHMQUGbWVtb3J5AgAFaGVsbG8AAQFfAAMKX19kYXRhX2VuZAMBC19faGVhcF9iYXNlAwIK4QID1gIDAn8CfgF/I4CAgIAAQSBrIgEkgICAgAACQAJAIACnQf8B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QgEQgoCAgAAaQoSAgICgBkKEgICAwAwQg4CAgAAaIAEPCwAACxCFgICAAAALCQAQhoCAgAAACwQAAAALAgALAHMOY29udHJhY3RzcGVjdjAAAAAAAAAAQEluY3JlbWVudCBpbmNyZW1lbnRzIGFuIGludGVybmFsIGNvdW50ZXIsIGFuZCByZXR1cm5zIHRoZSB2YWx1ZS4AAAAJaW5jcmVtZW50AAAAAAAAAAAAAAEAAAAEAB4RY29udHJhY3RlbnZtZXRhdjAAAAAAAAAAFAAAAAAAbw5jb250cmFjdG1ldGF2MAAAAAAAAAAFcnN2ZXIAAAAAAAAGMS43Ni4wAAAAAAAAAAAACHJzc2RrdmVyAAAALzIwLjMuMSNiYTA0NWE1N2FmOTcxZmM4M2U0NzU3NDZiNTlhNTAzYjdlZjQxNjQ5AA==",
"lastModifiedLedgerSeq": 368441,
"liveUntilLedgerSeq": 2442040
}
],
"latestLedger": "262384"
"latestLedger": 370940
}
}
```
Expand Down
11 changes: 5 additions & 6 deletions docs/guides/rpc/generate-ledger-keys-python.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,22 @@ hide_table_of_contents: true
In the [`increment` example contract] stores an integer value in a ledger entry that is identified by a key with the symbol `COUNTER`. The value of this ledger key can be derived using the following code snippets.

```python
from stellar_sdk import xdr
from stellar_sdk.strkey import StrKey
from stellar_sdk.soroban import Symbol
from stellar_sdk import xdr, scval, Address

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(StrKey.decode_contract(contract_id)),
key=Symbol(symbol_text).to_xdr_sc_val(),
contract=Address(contract_id).to_xdr_sc_address(),
key=scval.to_symbol(symbol_text),
durability=xdr.ContractDataDurability.PERSISTENT
),
)
return ledger_key.to_xdr()

print(
get_ledger_key_symbol(
"CA3D5KRYM6CB7OWQ6TWYRR3Z4T7GNZLKERYNZGGA5SOAOPIFY6YQGAXE",
"CCPYZFKEAXHHS5VVW5J45TOU7S2EODJ7TZNJIA5LKDVL3PESCES6FNCI",
"COUNTER"
)
)
Expand Down
Loading

0 comments on commit 4c88ae0

Please sign in to comment.