Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update new readStorage and getDataEntry signature #411

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
10 changes: 8 additions & 2 deletions docs/build/hello-world-dapp.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,9 @@ function App() {
async function getGreeting() {
if (client) {
const dataStoreVal = await client.getDatastoreEntry(GREETING_KEY, sc_addr, false)
if (!dataStoreVal) {
throw new Error(`datastore key ${GREETING_KEY} doesn't exist`);
}
const greetingDecoded = bytesToStr(dataStoreVal);
setGreeting(greetingDecoded);
}
Expand Down Expand Up @@ -306,7 +309,10 @@ The buildnet is a test network that is used to test smart contracts before deplo
```tsx
async function getGreeting() {
if (client) {
const dataStoreVal = await client.getDatastoreEntry(GREETING_KEY, sc_addr, false)
const dataStoreVal = await client.getDatastoreEntry(GREETING_KEY, sc_addr, false);
if (!dataStoreVal) {
throw new Error(`datastore key ${GREETING_KEY} doesn't exist`);
}
const greetingDecoded = bytesToStr(dataStoreVal);
setGreeting(greetingDecoded);
}
Expand All @@ -316,7 +322,7 @@ In the code above, we are retrieving the "Hello, World!" greeting message by dir

To do so, we are using the `getDatastoreEntry` method.
In our case, the address of the smart contract is the one we got during contract deployment. The key is the string "greeting_key" [that we used to store the greeting message in the smart contract's storage](#step-1-writing-the-smart-contract).
We retrieve the current greeting from the smart contract's storage and decode it using the `bytesToStr` function. Finally, we set the greeting to the `greeting` state variable using the `setGreeting` function.
We retrieve the current greeting from the smart contract's storage, check if the value exists and decode it using the `bytesToStr` function. Finally, we set the greeting to the `greeting` state variable using the `setGreeting` function.

:::info
The storage of the smart contract is a key value store that stores data in bytes. Thus, we need to decode the bytes returned by the smart contract to get the actual greeting message. That is why we have to use massa-web3 `bytesToStr` function.
Expand Down
2 changes: 1 addition & 1 deletion docs/build/massa-web3/provider.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ Retrieves all storage keys registered at a given address.
- `final`: Defaults to true.

```typescript
readStorage(address: string, keys: Uint8Array[] | string[], final?: boolean): Promise<Uint8Array[]>
readStorage(address: string, keys: Uint8Array[] | string[], final?: boolean): Promise<(Uint8Array | null)[]>
```

Retrieves data associated with given storage keys of a given address.
Expand Down