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

Feat/evm compatible substrate #12

Merged
merged 5 commits into from
Aug 25, 2024
Merged
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
1 change: 1 addition & 0 deletions pages/_meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"coding_challenge_1": "Coding Challenge 1",
"install_and_run_substrate_node" : "Install and Run Substrate Node",
"client_libraries" : "Thư viện client",
"evm_compatible_layer_substrate": "Tích hợp EVM trong Substrate",
"rakiapp": {
"title": "Polkadot Bootcamp - Video ↗",
"type": "page",
Expand Down
Binary file added pages/assets/compile_contract.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/contract_address.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/deploy_contract.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/deploy_remix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/encode_extrinsic.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/frontier.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/get_chain_id.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/get_current_block.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/interact_remix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/moonbeam.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/on_chain_storage.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/provider.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/storage_code.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pages/assets/structure_frontier.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Empty file.
5 changes: 5 additions & 0 deletions pages/evm_compatible_layer_substrate/_meta.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"introduction_to_frontier" : "Giới thiệu Frontier",
"introduction_to_moonbeam": "Giới thiệu Moonbeam",
"interact_contract_on_moonbeam": "Tương tác contract trên mạng moonbeam"
}
217 changes: 217 additions & 0 deletions pages/evm_compatible_layer_substrate/interact_contract_on_moonbeam.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,217 @@
# Tương tác hợp đồng thông minh trên Moonbeam



## Chuẩn bị các tool hỗ trợ

### RPC Endpoints và cách thêm network vào Metamask

+ RPC endpoints: https://rpc.api.moonbase.moonbeam.network
+ Chain id: 1287


+ Kết nối metamask: https://docs.moonbeam.network/tokens/connect/metamask/

### Faucet coins

https://faucet.moonbeam.network/

### Remix IDE

https://remix.ethereum.org/#lang=en&optimize=false&runs=200&evmVersion=null&version=soljson-v0.8.26+commit.8a97fa7a.js



### Contract `Storage`

```javascript
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract Storage {
uint256 private data;

// Store a value
function set(uint256 _data) public {
data = _data;
}

// Retrieve the stored value
function get() public view returns (uint256) {
return data;
}
}
```


## Deploy/Interact Contract trên Moonbeam

### Compile contract
#### Sử dụng remix

![](../assets/compile_contract.png)


#### Sử dụng script
Lưu ý: Cài đặt thư viện
```bash
npm install ethers solc
```

<details>
<summary>compile.js</summary>
```javascript
const fs = require("fs");
const solc = require("solc");

const source = fs.readFileSync("Storage.sol", "utf8");

const input = {
language: "Solidity",
sources: {
"Storage.sol": {
content: source,
},
},
settings: {
outputSelection: {
"*": {
"*": ["abi", "evm.bytecode"],
},
},
},
};

const output = JSON.parse(solc.compile(JSON.stringify(input)));
const abi = output.contracts["Storage.sol"].Storage.abi;
const bytecode = output.contracts["Storage.sol"].Storage.evm.bytecode.object;

fs.writeFileSync("StorageABI.json", JSON.stringify(abi));
fs.writeFileSync("StorageBytecode.json", bytecode);
console.log("Contract compiled and ABI/bytecode saved.");
```
</details>



### Deploy contract
#### Sử dụng remix

1. Chọn `injected provider` là Moonbase


![](../assets/provider.png)

2. Chọn account và Deploy contract

![](../assets/deploy_remix.png)

3. Metamask sẽ pop up và thực hiện deploy



#### Sử dụng script


<details>
<summary>deploy.js</summary>
```javascript
const { ethers } = require("ethers");
const fs = require("fs");

async function main() {
// Load the ABI and bytecode
const abi = JSON.parse(fs.readFileSync("StorageABI.json", "utf8"));
const bytecode = fs.readFileSync("StorageBytecode.json", "utf8");

// Set up provider and wallet
const provider = new ethers.JsonRpcProvider("https://rpc.api.moonbase.moonbeam.network");
const wallet = new ethers.Wallet("your private key", provider);

const contractFactory = new ethers.ContractFactory(abi, bytecode, wallet);

const contract = await contractFactory.deploy();

console.log("Contract deployed at:", contract.target);
}

main().catch((error) => {
console.error("Error:", error);
process.exit(1);
});
```
</details>

Kết quả

```bash
Contract deployed at: 0x7d4567B7257cf869B01a47E8cf0EDB3814bDb963
```



### Kiểm tra địa chỉ contract trên explorer

https://moonbase.moonscan.io/tx/0xed3507458ecaf40fb67f815cbd70cbbcb9a1d8f4bd001b37839db4a99be14f46



### Tương tác contract

#### Sử dụng remix

Remix IDE sẽ show 2 method `set` (WRITE) và `get` (READ)

![](../assets/interact_remix.png)

#### Sử dụng script


<details>
<summary>interact.js</summary>
```javascript
const { ethers } = require("ethers");
const fs = require("fs");

async function main() {

const abi = JSON.parse(fs.readFileSync("StorageABI.json", "utf8"));

const provider = new ethers.JsonRpcProvider("https://rpc.api.moonbase.moonbeam.network");
const wallet = new ethers.Wallet("your private key", provider);

const contractAddress = "0x7d4567B7257cf869B01a47E8cf0EDB3814bDb963";

const storageContract = new ethers.Contract(contractAddress, abi, wallet);

const setTx = await storageContract.set(42);
await setTx.wait();
console.log("Value set to 42");

const value = await storageContract.get();
console.log("Stored value:", value.toString());
}


main().catch((error) => {
console.error("Error:", error);
process.exit(1);
});

```
</details>

Kết quả:
```bash
Value set to 42
Stored value: 42
```









Loading
Loading