在Substrate链上跑evm智能合约(Frontier)

简介

Frontier 是 Substrate 的以太坊兼容层。它允许您运行未经修改以太坊 dapp。
以太坊兼容层的目标是能够:

  • 通过兼容层运行一个普通的 web3 应用程序,使用本地节点,其中额外的网桥二进制文件是可以接受的。
  • 能够从以太坊主网导入状态

部署

  1. clone代码
git clone https://github.com/paritytech/frontier.git
cd template
  1. 构建(比较耗时)
cargo build --release
  1. 添加账户(已导入到metamask)
    修改template/node/src/chain_spec.rs,这里最后一个账号5763e8fea803CAa7b2D6C18709Ef36b1b54fc1Cf是我们新添加的
evm: EVMConfig {
			accounts: {
				let mut map = BTreeMap::new();
				......
				......
				map.insert(
					H160::from_str("5763e8fea803CAa7b2D6C18709Ef36b1b54fc1Cf")
						.expect("internal H160 is valid; qed"),
					pallet_evm::GenesisAccount {
						balance: U256::from_str("0xffffffffffffffffffffffffffffffff")
							.expect("internal U256 is valid; qed"),
						code: Default::default(),
						nonce: Default::default(),
						storage: Default::default(),
					},
				);
				map
			},
		}
  1. 启动
./target/release/frontier-template-node --dev --tmp --ws-external
2021-10-20 12:28:44 Running in --dev mode, RPC CORS has been disabled.
2021-10-20 12:28:44 Frontier Node
2021-10-20 12:28:44 ✌️  version 0.0.0-8b34fb3-x86_64-linux-gnu
2021-10-20 12:28:44 ❤️  by Substrate DevHub <https://github.com/substrate-developer-hub>, 2021-2021
2021-10-20 12:28:44 📋 Chain specification: Development
2021-10-20 12:28:44 🏷 Node name: regular-smash-8365
2021-10-20 12:28:44 👤 Role: AUTHORITY
2021-10-20 12:28:44 💾 Database: RocksDb at /tmp/substrateThe7EF/chains/dev/db/full
2021-10-20 12:28:44 ⛓  Native runtime: node-frontier-template-1 (node-frontier-template-1.tx1.au1)
2021-10-20 12:28:44 🔨 Initializing Genesis block/state (state: 0x68e5…2119, header-hash: 0xaf46…4ef5)
2021-10-20 12:28:44 👴 Loading GRANDPA authority set from genesis on what appears to be first startup.
2021-10-20 12:28:45 ⏱  Loaded block-time = 6s from block 0xaf467f5c8683e1317ff3e426253f5d53f15adc9227c67f2ea483cca6db964ef5
2021-10-20 12:28:45 Using default protocol ID "sup" because none is configured in the chain specs
2021-10-20 12:28:45 🏷 Local node identity is: 12D3KooWRmxzyA7K1mDvemBTW2GuqS3GeW2XcVkjenmjygw9fLD6
2021-10-20 12:28:45 📦 Highest known block at #0
2021-10-20 12:28:45 〽️ Prometheus exporter started at 127.0.0.1:9615
2021-10-20 12:28:45 Listening for new connections on 0.0.0.0:9944.
2021-10-20 12:28:48 🙌 Starting consensus session on top of parent 0xaf467f5c8683e1317ff3e426253f5d53f15adc9227c67f2ea483cca6db964ef5
2021-10-20 12:28:48 🎁 Prepared block for proposing at 1 [hash: 0xcd08fddbf3574f5207548efa138bb8f303b58b08f803f6c8a01d70ff4bb78dca; parent_hash: 0xaf46…4ef5; extrinsics (2): [0x8542…22c3, 0x549b…7957]]
2021-10-20 12:28:48 🔖 Pre-sealed block for proposal at 1. Hash now 0xfe92b68fb7ce5ffe92d0cbf74ca3b5efe07638a8ea6d79cc5ff3e82cce73a7d3, previously 0xcd08fddbf3574f5207548efa138bb8f303b58b08f803f6c8a01d70ff4bb78dca.
2021-10-20 12:28:48 ✨ Imported #1 (0xfe92…a7d3)
2021-10-20 12:28:48 Accepted a new tcp connection from 119.123.221.119:52362.
2021-10-20 12:28:50 💤 Idle (0 peers), best: #1 (0xfe92…a7d3), finalized #0 (0xaf46…4ef5), ⬇ 0 ⬆ 0
2021-10-20 12:28:54 🙌 Starting consensus session on top of parent 0xfe92b68fb7ce5ffe92d0cbf74ca3b5efe07638a8ea6d79cc5ff3e82cce73a7d3

使用rpc接口查看账户余额

在这里插入图片描述

连接metamask

设置网络,网络id为42
在这里插入图片描述
可以看到余额不能正常显示,这样remix等工具就不能使用了
在这里插入图片描述

连接polkadot-js

可以本地启动一个,也可以直接使用https://polkadot.js.org/apps/#/explorer

docker run --rm -it --name polkadot-ui -e WS_URL=ws://yourip:9944 -p 80:80 jacogr/polkadot-js-apps:latest

打开浏览器访问,可以正常使用
在这里插入图片描述

使用polkadot-js查看账户余额

进入设置,开发者,配置以下json

{
	"Address": "MultiAddress",
	"LookupSource": "MultiAddress",
	"Account": {
		"nonce": "U256",
		"balance": "U256"
	},
	"Transaction": {
		"nonce": "U256",
		"action": "String",
		"gas_price": "u64",
		"gas_limit": "u64",
		"value": "U256",
		"input": "Vec<u8>",
		"signature": "Signature"
	},
	"Signature": {
		"v": "u64",
		"r": "H256",
		"s": "H256"
	}
}

在这里插入图片描述
RPC calls选项卡查询 eth > getBalance(address, number) 查看账户余额
在这里插入图片描述

使用polkadot-js部署智能合约

打开polkadot-js,进入超级管理
在这里插入图片描述

选择evm,再选择create
在这里插入图片描述

填入参数
bytecode来自这里

source: 0xd43593c715fdd31c61141abd04a99fd6822c8558
init: <raw contract bytecode, a very long hex value>
value: 0
gas_limit: 210000
gas_price: 1
nonce: <empty> {None}

在这里插入图片描述

点击提交sudo
在这里插入图片描述

打开f12 查看console。点击签名并提交
可以看到已经返回合约地址
在这里插入图片描述

根据blockId查看
在这里插入图片描述
检查合约存储,返回值应该是0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff
在这里插入图片描述
合约调用: 交易-》evm->call

target: 0xd43593c715fdd31c61141abd04a99fd6822c8558
source: 0x6be02d1d3665660d22ff9624b7be0551ee1ac91b
input: 0xa9059cbb0000000000000000000000008eaf04151687736326c9fea17e25fc528761369300000000000000000000000000000000000000000000000000000000000000dd
value: 0
gas_limit: 210000
gas_price: 1

在这里插入图片描述
填好参数 提交

查看关联的事件
在这里插入图片描述

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值