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 contract getting started #520

Merged
merged 5 commits into from
May 2, 2024
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
34 changes: 19 additions & 15 deletions docs/smart-contracts/getting-started/create-an-app.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ From our `getting-started-tutorial` directory, run the following command to add

```sh
soroban contract init ./ \
--frontend-template https://github.com/AhaLabs/soroban-astro-template
--frontend-template https://github.com/stellar/soroban-astro-template
```

This will add the following to your project, which we'll go over in more detail below.
Expand Down Expand Up @@ -55,7 +55,7 @@ This is going to use the CLI command `soroban contract bindings typescript`:
```bash
soroban contract bindings typescript \
--network testnet \
--contract-id $(cat .soroban/contract-ids/soroban_hello_world_contract.txt) \
--contract-id $(cat .soroban/contract-ids/hello_world.txt) \
--output-dir packages/hello_world
```

Expand All @@ -65,7 +65,7 @@ We attempt to keep the code in these generated libraries readable, so go ahead a

## Generate an NPM package for the Increment contract

Though we can run `soroban contract bindings typescript` for each of our contracts individually, the [soroban-astro-template](https://github.com/AhaLabs/soroban-astro-template) that we used as our template includes a very handy `initialize.js` script that will handle this for all of the contracts in our `contracts` directory.
Though we can run `soroban contract bindings typescript` for each of our contracts individually, the [soroban-astro-template](https://github.com/stellar/soroban-astro-template) that we used as our template includes a very handy `initialize.js` script that will handle this for all of the contracts in our `contracts` directory.

In addition to generating the NPM packages, `initialize.js` will also:

Expand Down Expand Up @@ -139,13 +139,13 @@ As mentioned above, this script attempts to build and deploy our contracts, whic

Now let's open up `src/pages/index.astro` and take a look at how the frontend code integrates with the NPM package we created for our contracts.

Here we can see that we're importing our generated `helloWorld` client from `../contracts/soroban_hello_world_contract`. We're then invoking the `hello` method and adding the result to the page.
Here we can see that we're importing our generated `helloWorld` client from `../contracts/hello_world`. We're then invoking the `hello` method and adding the result to the page.

```ts title="src/pages/index.astro"
---
import Layout from "../layouts/Layout.astro";
import Card from "../components/Card.astro";
import helloWorld from "../contracts/soroban_hello_world_contract";
import helloWorld from "../contracts/hello_world";
const { result } = await helloWorld.hello({ to: "you" });
const greeting = result.join(" ");
---
Expand Down Expand Up @@ -255,20 +255,21 @@ Now let's add a new component to the `src/components` directory called `ConnectF
return publicKey;
}

async function setLoggedIn() {
const publicKey = await getPk();
async function setLoggedIn(publicKey: string) {
ellipsis.innerHTML = `Signed in as ${publicKey}`;
ellipsis.title = publicKey;
}

if (await isAllowed()) {
if (await getPk()) setLoggedIn();
const publicKey = await getPk();
if (publicKey) setLoggedIn(publicKey);
BlaineHeffron marked this conversation as resolved.
Show resolved Hide resolved
else wrap.innerHTML = 'Freighter is locked.<br>Sign in & refresh the page.';
} else {
button.addEventListener('click', async () => {
button.disabled = true;
await setAllowed();
await setLoggedIn();
const publicKey = await getPk();
await setLoggedIn(publicKey);
});
}
</script>
Expand All @@ -290,7 +291,7 @@ Now we can import the component in the frontmatter of `pages/index.astro`:
---
import Layout from '../layouts/Layout.astro';
import Card from '../components/Card.astro';
import helloWorld from "../contracts/soroban_hello_world_contract";
import helloWorld from "../contracts/hello_world";
+import ConnectFreighter from '../components/ConnectFreighter.astro';
...
```
Expand Down Expand Up @@ -324,10 +325,13 @@ Current value: <strong id="current-value" aria-live="polite">???</strong><br />

<script>
import incrementor from "../contracts/soroban_increment_contract";

import { isAllowed, getUserInfo, signTransaction } from '@stellar/freighter-api';
const button = document.querySelector("[data-increment]");
const currentValue = document.querySelector("#current-value");

if (await isAllowed()) {
const publicKey = await getPublicKey();
if (publicKey) incrementor.options.publicKey = publicKey;
}
button.addEventListener("click", async () => {
button.disabled = true;
button.classList.add("loading");
Expand All @@ -336,11 +340,11 @@ Current value: <strong id="current-value" aria-live="polite">???</strong><br />
'<span class="visually-hidden"> – updating…</span>';

const tx = await incrementor.increment();
const { result } = await tx.signAndSend();
const { result } = await tx.signAndSend({signTransaction});

// Only use `innerHTML` with contract values you trust!
// Blindly using values from an untrusted contract opens your users to script injection attacks!
currentValue.innerHTML = result;
currentValue.innerHTML = result.toString();

button.disabled = false;
button.classList.remove("loading");
Expand Down Expand Up @@ -370,7 +374,7 @@ Now let's use this component. In `pages/index.astro`, first import it:
---
import Layout from '../layouts/Layout.astro';
import Card from '../components/Card.astro';
import helloWorld from "../contracts/soroban_hello_world_contract";
import helloWorld from "../contracts/hello_world";
import ConnectFreighter from '../components/ConnectFreighter.astro';
+import Counter from '../components/Counter.astro';
...
Expand Down
2 changes: 1 addition & 1 deletion docs/smart-contracts/getting-started/storing-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Check that it built:
ls target/wasm32-unknown-unknown/release/*.wasm
```

You should see both `soroban_hello_world_contract.wasm` and `soroban_increment_contract.wasm`.
You should see both `hello_world.wasm` and `soroban_increment_contract.wasm`.

## Tests

Expand Down
Loading