Skip to content

Commit

Permalink
Move the optimized build instructions inside the build page to shorte…
Browse files Browse the repository at this point in the history
…n the list of tutorials (#190)
  • Loading branch information
leighmcculloch authored Oct 9, 2022
1 parent 6b0b019 commit 2c2d793
Show file tree
Hide file tree
Showing 4 changed files with 175 additions and 89 deletions.
88 changes: 0 additions & 88 deletions docs/tutorials/build-optimized.mdx

This file was deleted.

92 changes: 92 additions & 0 deletions docs/tutorials/build.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ To build a Soroban contract to deploy or run, use the `cargo build` command.
cargo build --target wasm32-unknown-unknown --release
```

:::tip
It is highly recommended to use the `release` profile outlined in [Configure the
Release Profile], otherwise the contract will exceed Soroban's size limits.
:::

A `.wasm` file will be outputted in the `target` directory. The `.wasm` file is
the built contract.

Expand All @@ -20,3 +25,90 @@ The `.wasm` file contains the logic of the contract, as well as a the contracts
specification that can be imported into other contracts who wish to call it.
This is the only artifact needed to deploy the contract, share the interface
with others, or integration test against the contract.

<details><summary>Optimizing Builds</summary>
<p>

import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';

Building optimized contracts to be as small as possible requires some additional
tools, and requires installing the `nightly` Rust toolchain and `wasm-opt`.

Building optimized contracts is only necessary when deploying to a network with
fees or when analyzing and profiling a contract to get it as small as possible.
If you're just starting out writing a contract, these steps are not necessary.

[Configure the Release Profile]: create-a-project#configure-the-release-profile

## Install Rust `nightly`

To install the nightly Rust toolchain use [`rustup`].

[`rustup`]: ../getting-started/setup#install-rust

```sh
rustup install nightly
rustup target add --toolchain nightly wasm32-unknown-unknown
rustup component add --toolchain nightly rust-src
```

## Install `wasm-opt`

To install `wasm-opt`, install [`binaryen`]. Depending on your operating system
there may be different ways to install it.

[`binaryen`]: https://github.com/WebAssembly/binaryen

<Tabs>
<TabItem value="macos" label="macOS" default>

```sh
brew install binaryen
```

</TabItem>
<TabItem value="linux" label="Linux">

For distributions that use `apt`:

```sh
sudo apt install binaryen
```

For other distributions see:
https://github.com/WebAssembly/binaryen/releases

</TabItem>
<TabItem value="windows" label="Windows">

For install options see:
https://github.com/WebAssembly/binaryen/releases

</TabItem>
</Tabs>

## Build with `nightly` and `wasm-opt`

Build your contract using nightly. The additional options instruct the compiler
to remove unnecessary information from the contract.

```sh
cargo +nightly build \
--target wasm32-unknown-unknown \
--release \
-Z build-std=std,panic_abort \
-Z build-std-features=panic_immediate_abort
```

Use `wasm-opt` to further minimize the size of the `.wasm`.

```sh
wasm-opt -Oz \
target/wasm32-unknown-unknown/release/first_project.wasm \
-o target/wasm32-unknown-unknown/release/first_project_optimized.wasm
```


</p>
</details>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
sidebar_position: 8
title: Run on Local Network
title: Deploy to Local Network
---

If you have Docker installed, you can run a local standalone network with the
Expand Down
82 changes: 82 additions & 0 deletions docs/tutorials/invoking-contracts-with-transactions.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
---
sidebar_position: 11
title: Invoking Contracts with Stellar Transactions
---

Stellar supports invoking and deploying contracts with a new Operation named `InvokeHostFunctionOp`. The [`soroban-cli`] abstracts these details away from the
user, but SDKs do not yet and if you're building a dapp you'll probably find yourself building the XDR transaction to submit to
the network.

The `InvokeHostFunctionOp` can be used to invoke host functions. Host functions are built-in to Soroban and give the invoker access to:
- Invoking contract functions.
- Deploying new contracts.
- Deploying token contracts for Stellar Assets.

[`soroban-cli`]: ../getting-started/setup#install-the-soroban-cli

## InvokeHostFunctionOp

The XDR of `HostFunction` and `InvokeHostFunctionOp` below can be found
[here](https://github.com/stellar/stellar-xdr-next/blob/161e2e5b64425a49f9ccfef7f732ae742ed5eec4/Stellar-transaction.x#L477-L496)
```c++
enum HostFunction
{
HOST_FN_INVOKE_CONTRACT = 0,
HOST_FN_CREATE_CONTRACT_WITH_ED25519 = 1,
HOST_FN_CREATE_CONTRACT_WITH_SOURCE_ACCOUNT = 2,
HOST_FN_CREATE_TOKEN_CONTRACT_WITH_SOURCE_ACCOUNT = 3,
HOST_FN_CREATE_TOKEN_CONTRACT_WITH_ASSET = 4
};

struct InvokeHostFunctionOp
{
// The host function to invoke
HostFunction function;

// Parameters to the host function
SCVec parameters;

// The footprint for this invocation
LedgerFootprint footprint;
};
```

### Function
The `parameters` in `InvokeHostFunctionOp` will be forwarded to the
`HostFunction` specifed in `function`. The options are -
1. `HOST_FN_INVOKE_CONTRACT`
- This will call the `call_n` host function, invoking a contract function.
- `parameters` is
expected to contain the contract id, contract function name, and the
parameters to the contract function being invoked.
2. `HOST_FN_CREATE_CONTRACT_WITH_ED25519`
- This is disabled for now. Calling it will result in an error.
3. `HOST_FN_CREATE_CONTRACT_WITH_SOURCE_ACCOUNT`
- This will call the `create_contract_from_source_account` host function,
creating a contract using the stellar source account that submitted the
transaction. The contract id will be the SHA256 hash of
[this](https://github.com/stellar/stellar-xdr-next/blob/161e2e5b64425a49f9ccfef7f732ae742ed5eec4/Stellar-transaction.x#L594) `HashIDPreimage`.
- `parameters` is expected to contain two elements: the WASM contract code and a
salt.
4. `HOST_FN_CREATE_TOKEN_CONTRACT_WITH_SOURCE_ACCOUNT`
- This will call the `create_token_from_source_account` host function,
creating a [built-in token contract] using the stellar source account that submitted the
transaction. The contract id will be the SHA256 hash of
[this](https://github.com/stellar/stellar-xdr-next/blob/161e2e5b64425a49f9ccfef7f732ae742ed5eec4/Stellar-transaction.x#L594) `HashIDPreimage`.
- `parameters` is expected to contain a salt.
5. `HOST_FN_CREATE_TOKEN_CONTRACT_WITH_ASSET`
- This will call the `create_token_from_asset` host function, creating a
[built-in token contract] that wraps a classic Stellar asset.
The contract id will be the SHA256 hash of
[this](https://github.com/stellar/stellar-xdr-next/blob/161e2e5b64425a49f9ccfef7f732ae742ed5eec4/Stellar-transaction.x#L592) `HashIDPreimage`. `create_token_from_asset` will also initialize the token
contract, so the user is not expected to call any intialize functions.
- `parameters` is expected to contain a xdr `Asset`.

[built-in token contract]: ../built-in-contracts/token-contract.mdx

### Parameters
This `SCVec` contains the parameters that will be passed to the host function that is being invoked.

### Footprint
The footprint must contain the `LedgerKeys` that will be read and/or written. More
information about the footprint can be found in the advanced section of [interacting with contracts](interacting-with-contracts#storage-footprint-and-preflight).

0 comments on commit 2c2d793

Please sign in to comment.