Skip to content

Commit

Permalink
Merge branch 'main' of github.com:asset3/Dapp-Learning
Browse files Browse the repository at this point in the history
  • Loading branch information
jhfnetboy committed Jan 22, 2022
2 parents eb63b64 + a702b90 commit 4762044
Show file tree
Hide file tree
Showing 35 changed files with 1,214 additions and 15 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ DAPP架构请参考文章--[从架构维度看Web2.0与Web3.0应用之别](https
32. [chainlink-keeper](basic/32-chainlink-keeper/README.md)
33. [pooltogether](basic/33-pooltogether/README.md)
34. [subgraph](basic/34-subgraph/readme.md)
35. [StarkNet](https://starkware.co/starknet/)
欢迎提交 PR,[添加新的基础任务或者更新上面的任务](https://github.com/rebase-network/Dapp-Learning/issues/new)
35. [StarkNet](basic/35-starkNet/readme.md)
36. [NFT FileCoin](basic/36-nft-ipfs/README.md)
37. [Charm.fi](https://github.com/charmfinance/alpha-vaults-contracts)
38. [Flashbots provider with ethers.js](https://github.com/flashbots/ethers-provider-flashbots-bundle)
Expand Down Expand Up @@ -186,9 +185,15 @@ DAPP架构请参考文章--[从架构维度看Web2.0与Web3.0应用之别](https
开发者可以在开发群里发起项目研究倡议,建立小组,进行协作。

## DeFi 进阶
建议先阅读DEFI经典书籍:
**建议先阅读DEFI经典书籍:**
[How to DeFi](https://assets.coingecko.com/books/how-to-defi/How_to_DeFi_Chinese.pdf)
[How to DeFi:Advanced](https://nigdaemon.gitbook.io/how-to-defi-advanced-zhogn-wen-b/)
**DEFI学习路线图:**
交易 -> 借贷 ->衍生品
可以参考我们学习小组的学习路线图,配合视频学习更佳:
- 交易: uniV1(task13) -> uniV2 -> uniV3 -> CurveV1 -> CurveV2
- 借贷: Aave -> Compound -> Liquity -> Euler
- 衍生品: SNX -> YFI -> Perpetual
01. [UniswapV2](defi/Uniswap-V2/readme.md)
02. [UniswapV3](defi/Uniswap-V3/readme.md)
03. [Compound](defi/Compound/readme.md)
Expand Down
5 changes: 2 additions & 3 deletions basic/22-zk-snarkjs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,5 @@ npx snarkjs zkey export soliditycalldata public.json proof.json

## 参考资料

https://learnblockchain.cn/article/1078
https://iden3.io/blog/circom-and-snarkjs-tutorial2.html
https://github.com/iden3/circom/blob/master/TUTORIAL.md
- 创建第一个零知识 snark 电路: https://learnblockchain.cn/article/1078
- circom2 doc:https://docs.circom.io/circom-language/basic-operators/
Binary file added basic/30-zksync-layer2/imgs/zkrollup.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
15 changes: 11 additions & 4 deletions basic/30-zksync-layer2/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

[主页](https://github.com/matter-labs/zksync)

## ZK-Rollup
zkRollup在链下利用Merkle tree存储账户状态,由Operator收集用户的交易,交易收集完成后Operator会执行每个交易(校验余额,校验nonce,校验签名,执行状态转换),当交易执行完成后会产生一个新的Merkle tree Root,为了证明链下状态转移是正确的,Operator会在交易执行完成后生成一个零知识证明的proof。
Operator执行交易后本地的merkle tree root会由prev state root转换成post state root。
![zkrollup](./imgs/zkrollup.png)



## 基本架构

zkSync 的基本组成有:
Expand All @@ -24,10 +31,10 @@ Server application 的职能主要有:

## 参考链接

https://zhuanlan.zhihu.com/p/363029544
- 一文读懂 Layer2 方案 zkSync 基本原理https://zhuanlan.zhihu.com/p/363029544

https://www.jianshu.com/u/ac3aed07477e
- zksync 源码分析: https://www.jianshu.com/u/ac3aed07477e

https://zhuanlan.zhihu.com/p/343212894
- 李星L2 - zkSync源代码导读: https://zhuanlan.zhihu.com/p/343212894

https://mp.weixin.qq.com/s/TxZ5W9rx6OF8qB4ZU9XrKA
- 以太坊 Layer 2 扩容方案及用例综述: https://mp.weixin.qq.com/s/TxZ5W9rx6OF8qB4ZU9XrKA
12 changes: 11 additions & 1 deletion basic/31-dune-analytics-nansen/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ SELECT DISTINCT(tablename) FROM pg_catalog.pg_tables
## 参考链接

- 视频:https://www.bilibili.com/video/BV1ZK4y137Ce

- nansen说明书: https://github.com/rebase-network/Dapp-Learning-Arsenal/blob/main/papers/7-tool/nansen中文说明书.pdf

- https://qiita.com/shooter/items/3b66fc6400bc49854ffe
Expand All @@ -292,4 +293,13 @@ SELECT DISTINCT(tablename) FROM pg_catalog.pg_tables
- https://app.flipsidecrypto.com/velocity

- https://glassnode.com/
- 视频: https://ournetwork.mirror.xyz/gP16wLY-9BA1E_ZuOSv1EUAgYGfK9mELNza8cfgMWPQ

- 视频: https://ournetwork.mirror.xyz/gP16wLY-9BA1E_ZuOSv1EUAgYGfK9mELNza8cfgMWPQ

- Dune Youtube channel: <https://www.youtube.com/channel/UCPrm9d2hLd_YxSExH7oRyAg>

- Dune docs: <https://docs.dune.xyz/>

- Dune abstractions 常用查询语句: <https://github.com/duneanalytics/abstractions>

- PostgreSQL freeCodeCamp教程: <https://www.youtube.com/watch?v=qw--VYLpxG4>
3 changes: 3 additions & 0 deletions basic/35-starkNet/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
contract_abi.json
contract_compiled.json
starknet-artifacts/
184 changes: 184 additions & 0 deletions basic/35-starkNet/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,184 @@
# StarkNet

## intro

StarkNet 是一个无需许可的去中心化 ZK-Rollup,作为以太坊上的 L2 网络运行,任何 dApp 都可以在不影响以太坊的可组合性和安全性的情况下实现无限规模的计算。

`Cairo` 是一种用于编写可证明程序的编程语言,其中一方可以向另一方证明某个计算已正确执行。

StarkNet 将 Cairo 编程语言用于其基础设施和编写 StarkNet 合约。

## Setting up the environment

具体安装可以查看官方文档,这里以 Linux 环境为例 (Mac M1 芯片安装一直存在问题)

1. 为 Cairo 创建一个独立的 python 虚拟环境

```sh
python3.7 -m venv ~/cairo_venv
source ~/cairo_venv/bin/activate
```

建议使用 python3.7 版本,其他版本可能有依赖安装问题

2. 安装 pip 依赖

```sh
pip3 install ecdsa fastecdsa sympy
```

3. 安装 cairo-lang

```sh
pip3 install cairo-lang
```

验证是否安装成功

```sh
# (cairo_venv) (python37)
cairo-compile --version # cairo-compile 0.6.2
```

## Your first contract

### 创建 `contract.cairo` 文件

```python
# Declare this file as a StarkNet contract and set the required
# builtins.
%lang starknet
%builtins pedersen range_check

from starkware.cairo.common.cairo_builtins import HashBuiltin

# Define a storage variable.
@storage_var
func balance() -> (res : felt):
end

# Increases the balance by the given amount.
@external
func increase_balance{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
range_check_ptr}(amount : felt):
let (res) = balance.read()
balance.write(res + amount)
return ()
end

# Returns the current balance.
@view
func get_balance{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
range_check_ptr}() -> (res : felt):
let (res) = balance.read()
return (res)
end
```

- `%lang starknet` 声明该文件为 StarkNet contract, 需要使用 `starknet-compile` 命令来编译,而非 `cairo-compile`
- `%builtins pedersen range_check` 引入两个内建函数
- `@storage_var` 声明 storage 变量
- `balance.read()` 读取变量值
- `balance.write(newValue)` 将 newValue 值写入 balance
- 部署合约时,所有 storage 变量都将初始化为 0
- `@external` 外部调用方法
- `func increase_balance{pedersen_ptr:HashBuiltin*}`
- 大括号内为声明的隐式参数 `Implicit arguments`
- 隐式参数会自动向函数添加参数和返回值,而不用显式的在 return 语句中添加返回值
- `let (res) = ..` 声明局部变量

### 部署合约

命令行引入网络配置

```sh
export STARKNET_NETWORK=alpha-goerli
```

部署合约命令

```sh
starknet deploy --contract contract_compiled.json
```

部署成功后输出:

```sh
Deploy transaction was sent.
Contract address: 0x032e72fd53f838b7d4479fe38e4e33a8e95e06b3afaa197995e0046db2f5b97d
Transaction hash: 0x50b37bd192aed1fa131b83f1b034bc41f2b46174a2b93d8fb5ec326ca8b4679
```

### 与合约交互

`CONTRACT_ADDRESS` 替换为你的合约地址

```sh
starknet invoke \
--address CONTRACT_ADDRESS \
--abi contract_abi.json \
--function increase_balance \
--inputs 1234
```

调用成功,输出

```sh
Invoke transaction was sent.
Contract address: 0x032e72fd53f838b7d4479fe38e4e33a8e95e06b3afaa197995e0046db2f5b97d
Transaction hash: 0x139207f26d5e14507f62ff0f2eb68dcff43c107f1ee0a75489e8def0fbcc5bd
```

查询交易信息, `TRANSACTION_HASH` 替换为你的交易 hash

```sh
starknet tx_status --hash TRANSACTION_HASH
```

输出

```sh
{
"block_hash": "0x6170597aaf501317aa8cdf762bca4295c7f3a2ab03e1b2e6811a257acc6d026",
"tx_status": "ACCEPTED_ON_L2"
}
```

The possible statuses are:

- NOT_RECEIVED: The transaction has not been received yet (i.e., not written to storage).
- RECEIVED: The transaction was received by the sequencer.
- PENDING: The transaction passed the validation and entered the pending block.
- REJECTED: The transaction failed validation and thus was skipped.
- ACCEPTED_ON_L2: The transaction passed the validation and entered an actual created block.
- ACCEPTED_ON_L1: The transaction was accepted on-chain.

### Query the balance

`CONTRACT_ADDRESS` 替换为合约地址

```sh
starknet call \
--address CONTRACT_ADDRESS \
--abi contract_abi.json \
--function get_balance
```

输出

```sh
1234
```

## to-do

- starknet 合约语法解析

## reference

- official website <https://starkware.co/starknet/>
- develop docs <https://starknet.io/docs/index.html>
- blockscan <https://voyager.online/>
- StarkNet AMM demo <https://amm-demo.starknet.starkware.co/swap>
30 changes: 30 additions & 0 deletions basic/35-starkNet/contract.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Declare this file as a StarkNet contract and set the required
# builtins.
%lang starknet
%builtins pedersen range_check

from starkware.cairo.common.cairo_builtins import HashBuiltin

# Define a storage variable.
@storage_var
func balance() -> (res : felt):
end
# Increases the balance by the given amount.
@external
func increase_balance{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
range_check_ptr}(amount : felt):
let (res) = balance.read()
balance.write(res + amount)
return ()
end

# Returns the current balance.
@view
func get_balance{
syscall_ptr : felt*, pedersen_ptr : HashBuiltin*,
range_check_ptr}() -> (res : felt):
let (res) = balance.read()
return (res)
end
87 changes: 87 additions & 0 deletions basic/35-starkNet/starknet-hardhat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# starknet-hardhat-plugin

starknet 的 hardhat 插件使用

## starknet-dev-net

starknet 的本地测试网络

安装

```sh
pip3 install starknet-devnet
```

启动本地服务(localhost:5000)

```sh
starknet-devnet -p 5000
```

## usage

安装依赖

```sh
yarn install
```

设置 `starknet` 路径,即安装cairo环境的python venv路径

- `cairo.version``cairo.venv` 两个字段只能出现一个,若设置version,则需要手动编译合约
- `mocha.starknetNetwork` 设置为 alpha 将使用默认的starknet测试网配置 (alpha-goerli),设置为devnet则需要本地运行starknet-dev-net作为本地测试网络

```ts
// hardhat.config.js
...
const config: HardhatUserConfig = {
cairo: {
// version: "0.6.2", // alternatively choose one of the two venv options below

// uses (my-venv) defined by `python -m venv path/to/my-venv`
venv: "path/to/my-venv" // <-- put your dir

// uses the currently active Python environment (hopefully with available Starknet commands!)
// venv: "active"
},
networks: {
devnet: {
url: "http://localhost:5000"
}
},
mocha: {
starknetNetwork: "devnet"
// starknetNetwork: "alpha"
}
};
```

编译合约

```sh
yarn compile
```

运行测试文件

```sh
yarn test
```


```sh
npx hardhat test ./test/...
```

## to-do

- 使用 hardhat 部署到测试网,一直连接超时
- 使用 hardhat verify 在区块链浏览器上做源码认证

## reference

- hardhat 插件 <https://github.com/Shard-Labs/starknet-hardhat-plugin>
- 插件使用示例 <https://github.com/Shard-Labs/starknet-hardhat-example>
- starkNet-dev-net <https://github.com/Shard-Labs/starknet-devnet>

Loading

0 comments on commit 4762044

Please sign in to comment.