When using OpenZeppelin SDK how can you use `truffle console`?

Thanks a lot for this great tutorial. (Deploy to a public testnet using OpenZeppelin SDK)
I have a question for you once a contract have been deployed, I tried to interact with it using Truffle console but it did not work. I got: “MyContract has not been deployed on selected network”.
I checked MyContract artifact and the networks entry is still empty.
Is there a way to tell OpenZeppelin SDK to write deployment data in the artifact file?
Thank you!

2 Likes

Hi @nicob927

Welcome to the community :wave:

With the OpenZeppelin SDK I just use send-tx and call.
Is there something that you can only do in truffle console?

I don’t know if there is a better way, but if you need truffle with an openzeppelin project, then install truffle and init truffle first and then install OpenZeppelin SDK and init OpenZeppelin. This will create a truffle-config.js file that OpenZeppelin SDK will also use (rather than an OpenZeppelin SDK network.js file).

npm init -y
npm install truffle
npx truffle init
npm install @openzeppelin/cli
npx openzeppelin init

You can then setup the network configuration in truffle-config.js (similar to the guide Deploy to a public testnet using OpenZeppelin SDK).

You can deploy your contract using npx openzeppelin create.

$ npx openzeppelin create
✓ Compiling contracts with Truffle, using settings from truffle.js file
Truffle output:

Compiling your contracts...
===========================
> Compiling ./contracts/Counter.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /mnt/c/Users/andre/Documents/projects/forum/console/build/contracts
> Compiled successfully using:
   - solc: 0.5.8+commit.23d335f2.Emscripten.clang


? Pick a contract to instantiate Counter
? Pick a network development
✓ Added contract Counter
✓ Contract Counter deployed
All contracts have been deployed
? Do you want to call a function on the instance after creating it? No
✓ Setting everything up to create contract instances
✓ Instance created at 0xCfEB869F69431e42cdB54A4F4f105C19C080A601
0xCfEB869F69431e42cdB54A4F4f105C19C080A601

You can then use truffle console to interact with the contract.

$ npx truffle console
truffle(development)> counter = await Counter.deployed()
truffle(development)> counter.increase()
truffle(development)> (await counter.value()).toString()
'1'

You can also use OpenZeppelin SDK interactive commands openzeppelin send-tx and openzeppelin call to interact with the contract.

$ npx openzeppelin send-tx
? Pick a network development
? Pick an instance Counter at 0xCfEB869F69431e42cdB54A4F4f105C19C080A601
? Select which function increase()
✓ Transaction successful. Transaction hash: 0x08e66950a600f600e5a9134339c2d59885463756fc08264c43c963f788413787
$ npx openzeppelin call
? Pick a network development
? Pick an instance Counter at 0xCfEB869F69431e42cdB54A4F4f105C19C080A601
? Select which function value()
✓ Method 'value()' returned: 2
2

I have moved the question into it’s own topic so that it is easier for the community to find.

Hi @abcoathup,

Thanks a lot for this detailed answer, it works like a charm!

1 Like

Glad you hear you're using the SDK @nicob927! I wanted to ask the same thing as @abcoathup here:

In my experience oz create, oz send-tx and oz call are more than enough to perform the tasks I used to use truffle console for, albeit much more easily. If your workflow is not covered by these, we'd love to hear more about it!

1 Like