在Substrate链上跑evm智能合约

在Substrate链上跑evm智能合约,第1张

文章目录 简介部署使用rpc接口查看账户余额连接metamask连接polkadot-js使用polkadot-js查看账户余额使用polkadot-js部署智能合约

简介

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

通过兼容层运行一个普通的 web3 应用程序,使用本地节点,其中额外的网桥二进制文件是可以接受的。能够从以太坊主网导入状态 部署 clone代码
git clone https://github.com/paritytech/frontier.git
cd template
构建(比较耗时)
cargo build --release
添加账户(已导入到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
			},
		}
启动
./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 , 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",
		"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: 
value: 0
gas_limit: 210000
gas_price: 1
nonce:  {None}

点击提交sudo

打开f12 查看console。点击签名并提交
可以看到已经返回合约地址

根据blockId查看

检查合约存储,返回值应该是0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

合约调用: 交易-》evm->call

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


填好参数 提交

查看关联的事件

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/943580.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-18
下一篇 2022-05-18

发表评论

登录后才能评论

评论列表(0条)

保存