I am learning the Drizzle framework after I did a project using TruffleContract abstractions in Javascript (DApp).

I have multiple instances of a contract and I am not able to access at the instances "older" than the last deployed one. With TruffleContract I have the at(address) function that (slowly) retrieves the contract with address address.

For example:

I deploy 3 contracts "MyContract" c1, c2, c3, each one with a different storage variables. Then with TruffleContract I can call these functions:

const getContract = require('truffle-contract')
const contractJSON = require('./MyContract.json')

const contract = getContract(contractJSON)

const instance_c3 = await contract.deployed() // returns the last deployed, c3
const instance_c2 = await contract.at(address_c2) // retrieve c2

How does it work with Drizzle?


1 Answer 1


Consider the following way:

Get contract JSON data:

const contract_artifacts = require('../../../build/contracts/contract.json');

Create Truffle Contract object:

const truffleContract = await this.web3Service.artifactsToContract(contract_artifacts);

Get the contract deployed to a specific address:

const contractInstance = await truffleContract.at(CONTRACTADDRESS);

Fill contract name (you can modify the function to pass it as a third parameter):

contractInstance.contractName = truffleContract.contractName;

  await this.drizzleService.addContract(contractInstance, EventsArray);

Bellow is the dynamic addition of the smart contract to Drizzle:

  async addContract(truffleContract: any, events: string[]) {

    if(!truffleContract.contractName) {
      throw new Error('When Calling Drizzle.addContract(...), TruffleContract.contractName is not defined.');
    var contractConfig = {
      contractName: truffleContract.contractName, 
      web3Contract: truffleContract.contract

    // Add using the Drizzle context object
    this.drizzle.addContract(contractConfig, events)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.