简体   繁体   中英

How to use truffle console

I have a Truffle example app and then when I try to interact with it in truffle console, I don't understand why it is not deploying or working.

I activate testrpc and then after that, I type:

> truffle console

> migrate --reset

> MetaCoin.new();

After that:

truffle(development)> MetaCoin.name
'TruffleContract'
truffle(development)> MetaCoin.country
undefined
truffle(development)> a1 = web3.eth.accounts[0];
'0x0a3d66a80b50875770fd264dd7c905f21395037f'
truffle(development)> MetaCoin.sendCoin(a1, 100);
TypeError: MetaCoin.sendCoin is not a function
    at evalmachine.<anonymous>:1:10
    at ContextifyScript.Script.runInContext (vm.js:59:29)
    at Object.runInContext (vm.js:120:6)
    at Console.interpret (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:199314:17)
    at ReplManager.interpret (/usr/local/lib/node_modules/truffle/build/cli.bundled.js:200019:18)
    at bound (domain.js:301:14)
    at REPLServer.runBound [as eval] (domain.js:314:12)
    at REPLServer.onLine (repl.js:440:10)
    at emitOne (events.js:115:13)
    at REPLServer.emit (events.js:210:7)
truffle(development)> MetaCoin.sendCoin(a1, 100);

It says that the sendCoin function is not a function, so I'm not sure how to interact with the contract. How can I call that?

First deploy the contract using truffle migrate.

Then interact with the contract in the following way from truffle console

MetaCoin.deployed().then(function(instance) {return instance.sendCoin(a1, 100);}).then(function(value) {console.log(value);});

Looks like you want to access to contract with web3. This is how u interact with web3. since truffle is using web3 internally, u can use web3 inside truffle console. Inside the directory of the truffle config

$ truffle console

then

const instance=new web3.eth.Contract(Metacoin.abi,"addressOfDeployedContract")

const name=await instance.methods.name.call()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM