安装环境
Truffle && Ganache
Truffle是最全面的智能合约开发套件
# 安装node
nvm install 18
# 安装Truffle
npm install -g truffle
# 查看Truffle版本
truffle version
# 安卓ganache
https://trufflesuite.com/docs/ganache/quickstart/
安装IDE插件
https://marketplace.visualstudio.com/items?itemName=trufflesuite-csi.truffle-vscode
https://trufflesuite.com/blog/build-on-web3-with-truffle-vs-code-extension/
vscode安装Truffle for VS Code
使用说明
#新建项目
make dir fcoin
cd fcoin
truffle init
init后会创建
配置文件truffle-config.js 里的network是要部署的链。其中"development"部分是命令"truffle develop"对应的网络
development: {
host: "127.0.0.1", // Localhost (default: none)
port: 9545, // Standard Ethereum port (default: none)
network_id: "*", // Any network (default: none)
},
可以跟ganache对应
开始编码
参考:https://blog.whiteprompt.com/solidity-development-environment-setup-explained-b4e613503f8d
合约
写个sol代码Counter.sol
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract Counter {
uint private count;
constructor(uint _initialCount) public {
count = _initialCount;
}
function incrementBy(uint _increment) public {
count = count + _increment;
}
function getCount() public view returns (uint) {
return count;
}
}
migrations
执行命令
ruffle create migration deploy_counter
增加部署代码
const Counter = artifacts.require('Counter');
module.exports = function(_deployer) {
// Use deployer to state migration tasks.
_deployer.deploy(Counter, 1);
};
部署
truffle migrate
部署成功,合约地址
> contract address: 0xA54d15330a0fFA3C2578c72aa0A3Be537d485D27
交互
#注意,你已经打开了 truffle develop
#初始化,调用合约
let instance = await Counter.at("0xA54d15330a0fFA3C2578c72aa0A3Be537d485D27")
#获取合约里的初始化数字
instance.getCount()
返回内容,word后面的1是值
BN { negative: 0, words: [ 1, <1 empty item> ], length: 1, red: null }
instance.incrementBy(88)
# 返回89
instance.getCount()
其他案例
https://blog.csdn.net/VictorXSS/article/details/127852997
这个文档中,要注意migrations里,要加2_deploy_contracts.js
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;
contract Pair{
string private _name;
uint private _liquidity;
constructor(string memory name){
_name = name;
}
function addLiquidity(uint amount) public {
_liquidity+=amount;
}
function getLiquidity() public view returns(uint){
return _liquidity;
}
function getName() public view returns(string memory){
return _name;
}
}
contract Dex {
address private _owner;
uint private _index = 0;
mapping(address => Pair) private _pairs;
mapping(uint => address) private _indexs;
modifier onlyOwner(){
require(msg.sender == _owner,"must be owner");
_;
}
event PairCreated(string name, address pairAddress);
event AddLiquidity(address pairAddress, uint liquidity);
function createPair(string memory name) public {
Pair pair = new Pair(name);
_pairs[address(pair)] = pair;
_indexs[_index] = address(pair);
_index++;
emit PairCreated(name, address(pair));
}
function addLiquidity(address pairedAddress,uint liquidity) public {
_pairs[pairedAddress].addLiquidity(liquidity);
emit AddLiquidity(pairedAddress,liquidity);
}
function getCount() public view returns(uint){
return _index;
}
function pairAddress(uint index) public view returns(address){
return _indexs[index];
}
function getPairName(address pairedAddress) public view returns(string memory){
return _pairs[pairedAddress].getName();
}
function getLiquidity(address pairedAddress) public view returns(uint liquidity){
return _pairs[pairedAddress].getLiquidity();
}
}
核心代码
# solidity 里的代码
emit PairCreated(name, address(pair));
emit AddLiquidity(pairAddress,liquidity);
# subgraph.yaml
eventHandlers:
- event: PairCreated(string,address)
handler: handleNewPairCreated
- event: AddLiquidity(address,uint256)
handler: handleAddLiquidity
import {AddLiquidity, PairCreated} from '../generated/study/Dex'
import {Pair} from '../generated/schema'
export function handleNewPairCreated(event: PairCreated): void {
let pair = new Pair(event.params.pairAddress.toHexString())
pair.displayName = event.params.name
pair.liquidity = 0
pair.save()
}
export function handleAddLiquidity(event: AddLiquidity): void {
let id = event.params.pairAddress.toHexString()
let pair = Pair.load(id)
if (pair == null) {
return
}
pair.liquidity += event.params.liquidity.toI32()
pair.save()
}
调用
https://blog.csdn.net/qq_41882147/article/details/82966783
参考
https://blog.csdn.net/u013705066/article/details/123573546
https://blog.quicknode.com/the-importance-of-web3-provider-redundancy
https://ethereum.stackexchange.com/questions/31218/what-are-providers-in-the-ethereum
https://techfi.tech/introduction-to-the-graph-the-google-of-web3/
https://thegraph.academy/developers/local-development/
https://blog.whiteprompt.com/solidity-development-environment-setup-explained-b4e613503f8d?gi=cac56d9e3cc4