Skip to content

Commit

Permalink
Merge pull request #6 from CosmWasm/init-core
Browse files Browse the repository at this point in the history
Initial docs for core
  • Loading branch information
aumetra authored Apr 29, 2024
2 parents 4a3e0a2 + 28ed779 commit 4e0c5f5
Show file tree
Hide file tree
Showing 15 changed files with 263 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/pages/_meta.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"index": "Welcome",
"core": "CosmWasm Core",
"sylvia": "Sylvia",
"cw-multi-test": "MultiTest",
"how-to-doc": "How to doc"
Expand Down
41 changes: 41 additions & 0 deletions src/pages/core.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
---
tags: ["core"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Introduction

This chapter will give you an overview over CosmWasm from a contract developer
perspective, its capabilities, and guide you through initializing your first
smart contract.

<Callout>
We assume you have basic knowledge of Rust and Cargo (its standard build
system)
</Callout>

## What is CosmWasm?

CosmWasm is a platform for writing smart contracts for Cosmos chains using Rust
and WebAssembly.
Meaning CosmWasm is your one-stop shop for developing, testing, and running
smart contracts on enabled chains.

## What does CosmWasm provide?

For you, a contract developer, CosmWasm provides a set of high-quality
primitives through our standard library. These primitives include:

- extended precision arithmetic (128, 256, 512-bit integers)
- cryptographic primitives (for example, secp256k1 verification)
- interaction with the Cosmos SDK

## What does CosmWasm _not_ provide?

- Abstractions to simplify contract development (for this, check out
[Sylvia](/sylvia))
- Storage abstractions (for this, check out [Storey](/storey))
5 changes: 5 additions & 0 deletions src/pages/core/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"installation": "Installation",
"entrypoints": "Entrypoints",
"standard-library": "Standard Library"
}
48 changes: 48 additions & 0 deletions src/pages/core/entrypoints.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
tags: ["core"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Entrypoints

Entrypoints are where your contract can be called from the outside world. You
can equate that to your `main` function in C, Rust, Java, etc.
However, there is one _small_ difference: In CosmWasm, you have multiple of
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

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.

CosmWasm defines the handy `#[entry_point]` attribute macro. You simply annotate
a function with it, and it automatically generates code that communicates to the
VM: "Hey! This is an entrypoint, please use it when needed!"

<Callout>
When defining an entrypoint, it is important to use the correct types for the
parameters and return type. Incorrect types will cause errors when trying to
call the contract.
<br /> In the following sections we will take a look at all possible entrypoints,
including the correct function signature.
</Callout>

```rust
#[entry_point]
pub fn instantiate(
deps: DepsMut,
env: Env,
info: MessageInfo,
msg: InstantiateMsg,
) -> Result<Response, StdError> {
// Do some logic here
Ok(Response::default())
}
```
7 changes: 7 additions & 0 deletions src/pages/core/entrypoints/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"instantiate": "Instantiate",
"execute": "Execute",
"query": "Query",
"migrate": "Migrate",
"sudo": "Sudo"
}
10 changes: 10 additions & 0 deletions src/pages/core/entrypoints/execute.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
tags: ["core", "entrypoints"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Execute
10 changes: 10 additions & 0 deletions src/pages/core/entrypoints/instantiate.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
tags: ["core", "entrypoints"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Instantiate
10 changes: 10 additions & 0 deletions src/pages/core/entrypoints/migrate.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
tags: ["core", "entrypoints"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Migrate
10 changes: 10 additions & 0 deletions src/pages/core/entrypoints/query.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
tags: ["core", "entrypoints"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Query
10 changes: 10 additions & 0 deletions src/pages/core/entrypoints/sudo.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
tags: ["core", "entrypoints"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Sudo
77 changes: 77 additions & 0 deletions src/pages/core/installation.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
---
tags: ["core"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Installation

## Setting up the environment

Before diving right into writing code, you need to install some tooling in order
to compile your contract.

CosmWasm is luckily rather self-contained and therefore needs little external
tooling to compile.
Our only external dependency is Rust, which you need to install for your
platform.

<Callout>
We recommend installing Rust using the official [rustup
installer](https://rustup.rs). This makes it easy to stay on the most recent
Rust version and to install compiler targets.
</Callout>

<Callout>
For production builds you probably also want to install
[Docker](https://www.docker.com/), too.
<br /> This is because we offer the [CosmWasm Optimizing Compiler](https://github.com/CosmWasm/optimizer),
which uses a Docker image to build the smallest contract possible in a deterministic
fashion.
</Callout>

After installing Rust, you need to add the WebAssembly target. This is needed so
Rust knows how to build your code to WebAssembly.

To install the target using `rustup`, run the following command:

```sh
rustup target add wasm32-unknown-unknown
```

Perfect!
Now that we set up the foundation we just need two more tools:

1. `cargo-generate`
2. `cargo-run-script` (this is required to later optimize your contract for
production)

To install those, run the following commands:

```sh
cargo install cargo-generate --features vendored-openssl
cargo install cargo-run-script
```

## Setting up the contract

Now that the environment is all done, let's create the project!

Luckily you don't need to start from scratch, we already took care of the most
tedious parts of setting up a new project in form of a template!

In order to generate a fresh project, run this command and off we go:

<Callout>
Make sure to change `PROJECT_NAME` to the name of your contract!
</Callout>

```sh
cargo generate --git https://github.com/CosmWasm/cw-template.git --name PROJECT_NAME
```

Now you should have a ready contract project in a new folder called
`PROJECT_NAME` (or whatever you changed it to).
10 changes: 10 additions & 0 deletions src/pages/core/standard-library.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
tags: ["core"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Standard Library
4 changes: 4 additions & 0 deletions src/pages/core/standard-library/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"math": "Math",
"cryptography": "Cryptography"
}
10 changes: 10 additions & 0 deletions src/pages/core/standard-library/cryptography.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
tags: ["core"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Cryptography
10 changes: 10 additions & 0 deletions src/pages/core/standard-library/math.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
tags: ["core"]
---

import { Callout } from "nextra/components";
import Tags from "@/components/Tags";

<Tags />

# Math

0 comments on commit 4e0c5f5

Please sign in to comment.