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

227 migrate smart contract course from learnworlds #233

Draft
wants to merge 7 commits into
base: main
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Finality Importance in Interoperabile Systems
description: Learn about interoperability and it's importance in multichain systems
updated: 2024-06-10
authors: [andyvargtz]
authors: [Andrea Vargas]
icon: BookOpen
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Incentivizing a Relayer
description: Learn how to add Relayer Incentives.
updated: 2024-06-09
authors: [andyvargtz]
authors: [Andrea Vargas]
icon: Book
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Fee Data Flow
description: Learn the logic behind the fee incentivization.
updated: 2024-06-09
authors: [andyvargtz]
authors: [Andrea Vargas]
icon: BookOpen
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Determining the Fee
description: Calculate a fair incentive amount.
updated: 2024-06-09
authors: [andyvargtz]
authors: [Andrea Vargas]
icon: BookOpen
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Setting Incentives
description: Learn where incentives details need to be included
updated: 2024-06-09
authors: [andyvargtz]
authors: [Andrea Vargas]
icon: BookOpen
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Deploy Fee Token Contract
description: Deploy an ERC20 to work as the Fee Token for incentives.
updated: 2024-06-09
authors: [andyvargtz]
authors: [Andrea Vargas]
icon: Terminal
---
import { Steps, Step } from 'fumadocs-ui/components/steps';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Incentivize an AWM relayer
description: Add incentives to the basic send-receive contracts.
updated: 2024-06-09
authors: [andyvargtz]
authors: [Andrea Vargas]
icon: Terminal
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
title: Interaction Flow With Fees
description: Go trhough the complete deployment flow and send an incentivized message.
updated: 2024-06-09
authors: [andyvargtz]
authors: [Andrea Vargas]
icon: Terminal
---
import { Step, Steps } from 'fumadocs-ui/components/steps';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: Avalanche Starter Kit
description: Environment Setup
updated: 2024-05-31
authors: [martineckardt]
icon: Book
---

To make easier your journey through this course, we have prepared a Starter Kit repo consisting of everything you will need to start developing your own dApps on Avalanche. This repo will provide a self-contained environment with Avalanche-CLI, and Foundry so you can follow the course without the need of installing anything else other than launching the environment.

## What You Will Learn

In this section, you will go through the following topics:

- How to launch your own Codespace
- Create your own Interchain Messaging Enabled Custom Blockchain
- Foundry configuration

At the end of this section, you will have your environment ready to follow with the Cross-Chain dApp development with Interchain Messaging
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Set Up Avalanche Starter Kit
description: Environment Setup
updated: 2024-05-31
authors: [martineckardt]
icon: Terminal
---

import SetUp from "@/content/common/avalanche-starter-kit/set-up.mdx";

<SetUp />
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Close and Reopen Codespace
description: Environment Setup
updated: 2024-05-31
authors: [martineckardt]
icon: Terminal
---

import CloseAndReopen from "@/content/common/codespaces/close-and-reopen-codespace.mdx";

<CloseAndReopen />
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Create a Blockchain
description: Environment Setup
updated: 2024-05-31
authors: [martineckardt]
icon: Terminal
---

import CreateDefaultBlockchain from "@/content/common/avalanche-starter-kit/create-default-blockchain.mdx";

In order to send messages between two chains, we need to create a blockchain. In this guide, we will spin up a local Avalanche network that has it's own Primary Network (C-, P- and X-Chain) using the Avalanche CLI. Furthermore, we will create a blockchain in that Avalanche network, so we can send messages between the C-Chain and the newly created blockchain in the next chapters.

<CreateDefaultBlockchain/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Networks
description: Environment Setup
updated: 2024-05-31
authors: [martineckardt]
icon: BookOpen
---

import Networks from "@/content/common/avalanche-starter-kit/networks.mdx";

You just deployed and interacted with your first local network. But what does that mean? You can interact with your Avalanche L1, but you will notice that no one else can. Therefore, we have to understand what different Networks are in Avalanche.

<Networks/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: Pause and Resume
description: If you've deployed an Avalanche L1, you can preserve and restore the state of your deployed Avalanche L1s.
updated: 2024-10-07
authors: [0xstt]
icon: BookOpen
---

import PauseAndResume from "@/content/common/avalanche-starter-kit/pause-and-resume.mdx";

<PauseAndResume />
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: Building Programs on Blockchain
description: Learn what a Smart Contract is
updated: 2024-06-28
authors: [Andrea Vargas, Ash]
icon: Book
---

First off, thank you for enrolling in this course! We assume that you are taking this course in order to learn how to write smart contracts. This begs the question - what are smart contracts?

From a high-level, you may have heard that smart contracts are just code that automate a process. Want to create a decentralized insurance protocol? You can build a smart contract for that. Want to create and distribute NFTs? You can also build a smart contract for that as well. So while we might understand the capabilities of a smart contract, this course will be focused on defining the actual code (and therefore, business logic) required for such intents.

## The EVM Model
In this course, we will learn Solidity, a high-level programming language that we will use to design smart contracts. Before we delve into learning Solidity, it is important to understand how our smart contracts operate in the grand scheme of things.

Smart contracts are defined by the following: their behaviors and their state. Focusing first on the behaviors of a smart contract, this is simply the functions that one can call on the smart contracts. Examples of such behaviors can be found below:

- __Tokens__: behaviors include creating tokens, transferring them, getting the balances of token holders
- __Decentralized Exchange__: behaviors include swapping tokens, adding liquidity to a pool, getting the adresses of both tokens of a liquidity pools
- __DAO__: behaviors include submitting a proposal, voting on a proposal, checking if someone is a governance member

We now focus on the state of a contract. Abstractly, we can define the state of a contract as being the values and stateful data structures that are associated with said contract. Examples of the state of a smart contract are listed below:

- __Tokens__: the name of the token, the symbol of the token, the balances of the users
- __Decentralized Exchange__: the number of tokens of a liquidity pool, the amount of tokens a liquidity provider is entitled to
- __DAO__: a list of all governance members, a list of all outstanding proposals

Understanding what a smart contract under the EVM model consists of, we are now ready to understand the relationship between Solidity and the underlying blockchain.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
title: What is Solidity
description: Learn what a Smart Contract is
updated: 2024-06-28
authors: [Andrea Vargas, Ash]
icon: Book
---
Our objective throughout this course is to learn how to define the code required to implement a smart contract that matches our intent. The previous section, furthermore, told us that all smart contracts are defined by their state and their behaviors. Therefore, we claim the following:

> Solidity is a high-level programming language which allows us to define both the state and behaviors of a smart contract.

Although these behaviors can be almost anything, the majority of the time, such behaviors manipulate the state of the contract.

## Solidity v.s. Other Programming Languages

The first question one might have when learning a new language is the following: what does the language even look like? To start, we consider the following code snippet:

```solidity
contract A {
function getOne() public pure returns(uint) {
return 1;
}
}
```

In the above, we have a contract named A. Furthermore, we see that A contains a function getOne() that, when called, returns 1. Even though you might not understand everything that is going on in the code snippet, web developers may realize that the code snippet looks similar to JavaScript. The Solidity syntax, as a matter of fact, is actually inspired by JavaScript, and is a testament to the fact that Solidity itself is a high-level programming language.

Now that we have an idea of what both Solidity allows us to do and what the actually syntax of Solidity looks like, its time to learn the actual language itself.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Foundry Quickstart
description: Environment Setup
updated: 2024-05-31
authors: [martineckardt]
icon: BookOpen
---

import FoundryQuickstart from "@/content/common/avalanche-starter-kit/foundry-quickstart.mdx";

In this chapter, we will look into the Foundry that will be used to deploy and interact with smart contracts on the Avalanche network in our Avalanche Starter kit.

<FoundryQuickstart/>
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
---
title: Create a new Smart Contract
description: Learn what a Smart Contract is
updated: 2024-06-28
authors: [Andrea Vargas, Ash]
icon: Book
---

Alright, let's create our first contract. Head to the Avalanche Starter Kit Codespace and create a new folder called my-contracts in the _src_ folder.

Now in there create a new file called HelloWorld.sol and paste the following contents:

```solidity
contract HelloWorld {
function sayHello() public pure returns (string memory) {
return "Hello World";
}
}
```

Now let's deploy the contract:

```bash
forge create --rpc-url local-c --private-key $PK src/my-contracts/HelloWorld.sol:HelloWorld
```

If deployed successfully we should see something like this:

```bash
[⠒]
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0x52C84043CD9c865236f11d9Fc9F56aa003c1f922
Transaction hash: 0x28247e1292e9489c3b51456e2b848eeb6b82ccbcda18836a638f5d81605ac508
```

This means we have deployed our contract to the address 0x52C84043CD9c865236f11d9Fc9F56aa003c1f922. Using this address we can now call our contract:

```bash
cast call --rpc-url local-c 0x52C84043CD9c865236f11d9Fc9F56aa003c1f922 "sayHello()(string)"
```

Don't forget to replace the contract address with the one your contract was deployed to. The result should look like this:

```solidity
"Hello World"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
title: Hello World! Part 1
description: Learn about Solidity
updated: 2024-06-28
authors: [Andrea Vargas, Ash]
icon: Book
---
In this chapter, our main objective will be to learn about the necessary components require to compile and deploy a smart contract. In particular, we want to be able to:

- Properly create a new Solidity file
- Understand how to organize the business logic of our contract

After learning the two main points above, you will be tasked with building your first smart contract! With the above in mind, let's get started.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
title: Primitive Values and Types
description: Learn about Solidity
updated: 2024-06-28
authors: [Andrea Vargas, Ash, martineckardt]
icon: Book
---

All code, regardless of the language it is written in, can be described (in very very simple terms) as the manipulation of values. Therefore, if we want to learn a new language, we should start at the type of values we will be manipulating; this is where we will start our Solidity journey.

## Declaring Variable

We first note that Solidity is a statically-typed language; this means that for any variable that we declare, we must declare its type at the time of initialization. Therefore, the general syntax for declaring a variable is as follows:

```solidity
<type> <name>;
```

The following are examples of us declaring variables in Solidity:

```solidity
address addr;
uint256 num;
bool b;
```

For right now, we won't focus on what the types mentioned above actually mean. However, the biggest takeaway is that for any variable that we initialize, we must declare its type.

## Defining Variables

Now that we know how to declare a variable in Solidity, the other half of the puzzle that we want to solve for is actually assigning a value to these variables. The following code snippet is an example of how we would assign values to variables:

```solidity
addr = 0x7f610402ccc4CC1BEbcE9699819200f5f28ED6e3;
num = 0;
b = false;
```

Rather than having to take the two separate steps of declaring a variable and then defining said variable, we can do these two steps in just line; the following code shows how we would do this:

```solidity
address addr = 0x7f610402ccc4CC1BEbcE9699819200f5f28ED6e3;
uint256 num = 0;
bool b = false;
```

## Types

Now that we've discussed the process of both declaring and defining a variable, let's return to the topic of types. In Solidity, the following is a (non-exhaustive) list of elementary types that are available:

- Unsigned Integers: any value which is an unsigned integer is a nonnegative integer. Furthermore, the unsigned integer type is not a singular type, but rather a group of unsigned integers types differentiated by their bit size. Any unsigned integer type has a bit size that is a multiple of 8. So the following types are valid unsigned integer types: uint8, uint16, uint24, uint32, ..., uint256 (uint256 is the maximum unsigned integer bit size). Furthermore, for any unsigned integer uintx (where x is a multiple of 8 and between 0 and 256), we have that the range of uintx is from 0 up to (but not including) 2^(x).
- Signed Integers: any value which is a signed integer is a integer that can be either negative or positive. Like the unsigned integer type, the signed integer type is a group of signed integer types differentiated by their bit size. Any signed integer type has a bit size that is a multipe of 8. Examples of signed integer types are the following: int8, int16, int24, ..., int256 (int256 is the maximum signed integer size). Furthermore, for any signed integer intx (where x is a multiple of 8 and between 0 and 256), we have that the range of intx is from -2^(x - 1) up to (but not including) 2^(x - 1)
- Addresses: the address type consists of a 20-byte value. This type, as its name might suggest, represents the address of an EVM account.
- Booleans: can be either true or false. Treating booleans as integers, a true value is equal to 1 while a false value is equal to 0.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
title: Functions
description: Learn about Solidity
updated: 2024-05-31
authors: [martineckardt]
icon: BookOpen
---

In this section, we will look at functions. Recall from the beginning of this course that all smart contracts consist of a state and their behaviors. With regards to the latter, functions allow us to define the behavior of a smart contract.
## Structure of a Function
Similar to other programming languages, all functions consist of two sections: their header and their body. An example of the following can be found below:

```solidity
function getOne() public pure returns(uint) {
return 1;
}
```
In the above, we have the code function getOne() public pure returns(uint) as the header of the function, while the code return 1; is the body of the function (the body of a function is always encapsulated by curly brackets).

## Function Header
Focusing first on the header of a function, below is the required syntax that all function must have:

```solidity
function <function_name>(<args>) <visibility>
```

In the parentheses, we list the parameters of the function. In addition to having a name, each parameter of a function must also be marked with its type. The only concept that might be new for most developers is the visibility of a function. In Solidity, we can mark a function with the following visibility traits:

- Public
- Private
- Internal
- External

## Function Body
As of right now, the only thing we know what to do inside the body of a function is declaring/defining local variables. Inside the bodies of functions, we can also use regular mathematical operations like in other programming languages; the following code demonstrates this:

```solidity
function getSquare(uint num) public returns(uint) {
uint square = num ** 2;
return square;
}
```
Loading