使用truffle控制台
使用truffle控制台
相关链接
truffle -h
truffle -h
Truffle v5.4.29 - a development framework for Ethereum
Usage: truffle <command> [options]
Commands:
build Execute build pipeline (if configuration present)
compile Compile contract source files
config Set user-level configuration options
console Run a console with contract abstractions and commands available
create Helper to create new contracts, migrations and tests
db Database interface commands
debug Interactively debug any transaction on the blockchain
deploy (alias for migrate)
develop Open a console with a local development blockchain
exec Execute a JS module within this Truffle environment
help List all commands or provide information about a specific command
init Initialize new and empty Ethereum project
install Install a package from the Ethereum Package Registry
migrate Run migrations to deploy contracts
networks Show addresses for deployed contracts on each network
obtain Fetch and cache a specified compiler
opcode Print the compiled opcodes for a given contract
preserve Save data to decentralized storage platforms like IPFS and Filecoin
publish Publish a package to the Ethereum Package Registry
run Run a third-party command
test Run JavaScript and Solidity tests
unbox Download a Truffle Box, a pre-built Truffle project
version Show version number and exit
watch Watch filesystem for changes and rebuild the project automatically
See more at http://trufflesuite.com/docs
常用的有:
truffle compile
: 编译合约源文件truffle migrage
: 部署合约truffle deploy
: 与truffle migrage
一致
truffle 控制台
truffle console
: 启动一个控制台,其中包含有效的合约抽象 并且可以运行truffle命令。
命令格式:
truffle console [--network <name>] [--verbose-rpc]
truffle console
命令提供一个在命令行访问合约的用户接口,而且可以在控制台 中使用众多的truffle命令.
truffle console
命令需要一个外部以太坊客户端,例如Ganache
或geth
。如果需要 一个仅用于开发和测试的控制台,可以使用 truffle develop
命令,
命令选项:
--network
: 指定要使用的网络。该网络必须在配置文件中存在--verbose-rpc
:是否记录并显示Truffle和以太坊客户端之间的通信
truffle develop
启动一个包含了开发用区块链的控制台。
命令格式如下:
truffle develop [--log]
- –log:启动truffle开发会话并记录全部RPC活动。
与合约交互
使用这个合约 为例:
1.启动ganache :
truffle migrate
- 启动控制台:
truffle console
- 首先建立已部署的 MetaCoin 合约实例和由 Truffle 的内置区块链或 Ganache 创建的帐户:
truffle(development)> let instance = await MetaCoin.deployed()
truffle(development)> let accounts = await web3.eth.getAccounts()
4. 检查部署合约账户的元币余额:
truffle(development)> let balance = await instance.getBalance(accounts[0])
undefined
truffle(development)> balance.toNumber()
10000
5.查看该余额值多少以太币(并注意合约将meta币定义为价值 2 以太币):
truffle(development)> let ether = await instance.getBalanceInEth(accounts[0])
undefined
truffle(development)> ether.toNumber()
20000
- 将一些meta币从一个账户转移到另一个账户:
truffle(development)> instance.sendCoin(accounts[1], 500)
{
tx: '0xe74be8b5498e4cc16d3930a58e33b07eeedd8f508754dd376ffd3882d937d9d3',
receipt: {
transactionHash: '0xe74be8b5498e4cc16d3930a58e33b07eeedd8f508754dd376ffd3882d937d9d3',
transactionIndex: 0,
blockHash: '0xa5ba982c7f6df4067d4b331b52d8d6a1bb7946df13e55ce05cb1827c50e21e19',
blockNumber: 6,
from: '0x14c595f138745b6b3588659c44dc3182313a7532',
to: '0x508ce51477b8d605435b4ffadb888d7cd6c039cf',
gasUsed: 51520,
cumulativeGasUsed: 51520,
contractAddress: null,
logs: [ [Object] ],
status: true,
logsBloom: '0x00000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000100000020000000000000000000000000000000000808000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000040000000000000000000002000000000000000000000000010000000000020000000000000000000000000000000000000100000000000000000000000000000000000000000000',
rawLogs: [ [Object] ]
},
logs: [
{
logIndex: 0,
transactionIndex: 0,
transactionHash: '0xe74be8b5498e4cc16d3930a58e33b07eeedd8f508754dd376ffd3882d937d9d3',
blockHash: '0xa5ba982c7f6df4067d4b331b52d8d6a1bb7946df13e55ce05cb1827c50e21e19',
blockNumber: 6,
address: '0x508CE51477B8D605435b4ffaDB888D7cd6C039CF',
type: 'mined',
id: 'log_7c43b3ea',
event: 'Transfer',
args: [Result]
}
]
}
查看ganache有一个转账交易:
可以看到这个交易hash和上面控制台输出的是一致的。
- 查看收到meta币的账户余额:
truffle(development)> let received = await instance.getBalance(accounts[1])
undefined
truffle(development)> received.toNumber()
500
8. 检查发送meta币的账户余额:
truffle(development)> let newBalance = await instance.getBalance(accounts[0])
undefined
truffle(development)> newBalance.toNumber()
9500