Skip to content

Commit

Permalink
Add documentation for some entrypoints
Browse files Browse the repository at this point in the history
  • Loading branch information
aumetra committed May 6, 2024
1 parent 18f70dc commit f55e52d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/pages/core/entrypoints.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ these entrypoints, each one different from the last.
In this section we want to give you a quick overview over all the entrypoints
and when they are called.

## Define entrypoints
## Defining entrypoints

While you will learn all about entrypoints in the next sections, we want to give
you an idea on how to define an entrypoint in the first place.
Expand All @@ -31,14 +31,22 @@ VM: "Hey! This is an entrypoint, please use it when needed!"
including the correct function signature.
</Callout>

<Callout>
Even though the sections will show you to use `#[entry_point]`, it is
recommended to define your endpoints as `#[cfg_attr(not(feature = "library"),
entry_point)]`.
<br /> The reason behind that is that it allows you to reuse your contract as a
library.
</Callout>

```rust template="core"
#[entry_point]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, StdError> {
) -> StdResult<Response> {
// Do some logic here
Ok(Response::default())
}
Expand Down
22 changes: 22 additions & 0 deletions src/pages/core/entrypoints/execute.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,25 @@ tags: ["core", "entrypoints"]
import { Callout } from "nextra/components";

# Execute

Execute pretty much does what it says on the tin: It executes some routine in
your contract after a remote (either another contract or some client) sent a
message.
What sets it apart from other functions is that the contract has mutable access
to the underlying storage.
This allows you to, for example, increment a counter or add a user to a lottery.

## Definition

```rust filename="contract.rs"
#[entry_point]
pub fn execute(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: ExecuteMsg,
) -> StdResult<Response> {
// Increment some counter, register a user, spread liberty for super earth!
Ok(Response::new())
}
```
22 changes: 22 additions & 0 deletions src/pages/core/entrypoints/instantiate.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,25 @@ tags: ["core", "entrypoints"]
import { Callout } from "nextra/components";

# Instantiate

This is one of the most fundamental entrypoints. This entrypoint is called once
during the contract lifecycle.
It essentially is there to initialise the state of your contract (for example,
initialising a counter in storage, etc.).

You can imagine it as the constructor of your contract.

The most basic definition of this endpoint looks like this:

```rust filename="contract.rs"
#[entry_point]
pub fn instantiate(
_deps: DepsMut,
_env: Env,
_info: MessageInfo,
_msg: InstantiateMsg,
) -> StdResult<Response> {
// TODO: Initialise storage, send out metrics, do mischief
Ok(Response::new())
}
```
20 changes: 20 additions & 0 deletions src/pages/core/entrypoints/query.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,23 @@ tags: ["core", "entrypoints"]
import { Callout } from "nextra/components";

# Query

In the previous section we talked about the
[`execute` endpoint](./execute.mdx).
The `query` endpoint is actually pretty similar to execute with one key
difference: The storage is only accessible immutably.

This means you can only _read_ from the storage but not _write_ to it.

## Definition

```rust filename="contract.rs"
#[entry_point]
pub fn query(
_deps: Deps,
_env: Env,
_msg: QueryMsg,
) -> StdResult<QueryResponse> {
Ok(QueryResponse::default())
}
```

0 comments on commit f55e52d

Please sign in to comment.