Deploy Smarts Contracts with imports by a local compiler

Hi there.
I'm trying to create, compile and deploy smart contracts from an Angular project and I'm using a smart contract which import OpenZeppelin smart contracts like ( @openzeppelin/contracts/token/ERC20/ERC20.sol, @openzeppelin/contracts/security/Pausable.sol or @openzeppelin/contracts/access/Ownable.sol). But, for compile it I have to pass the smart contract as a string parameter between " `` " to the compiler . Because of that if I pass the smart contract incluiding the imports the compiler doesn't find it, even having the openzeppelin folder in the node modules of the Angular project.

I don't know if I explained it well enough to understand the problem, but if I did and someone can help me, I would really appreciate it.

I also attach the file where I pass the smart contract to the compiler in case it helps:

import { Component } from '@angular/core';
import { solidityCompiler, getCompilerVersions } from '@agnostico/browser-solidity-compiler';

@Component({
  selector: 'app-root',
  templateUrl: 'app.component.html',
  styleUrls: ['app.component.css']
})

export class AppComponent {

  version: string = 'soljson-v0.8.19+commit.7dd6d404.js'

  constructor() {
  }

  ngOnInit() {

    (async () => {
      let result = await this.compiler();
      console.log("RESULT: \n" + JSON.stringify(result));
    })();
  }

  /*async getVersions() {
    let resultVersions = await getCompilerVersions();
    console.log("Result1 " + JSON.stringify(resultVersions));
  }*/

  async compiler() {
    let compilerResponse = await solidityCompiler({
      version: `https://binaries.soliditylang.org/bin/${this.version}`,

      contractBody: 
      `// SPDX-License-Identifier: MIT
      pragma solidity ^0.8.9;

      import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
      import "@openzeppelin/contracts/security/Pausable.sol";
      import "@openzeppelin/contracts/access/Ownable.sol";

      contract KHCoin is ERC20, Pausable, Ownable {
        
          constructor() ERC20("KHCoin", "KHC") {
              _mint(msg.sender, 10000 * 10 ** decimals());
          }


          function mint(address to, uint256 amount) public onlyOwner {
              _mint(to, amount);
          }


          function burn(uint256 amount) public onlyOwner {
              _burn(_msgSender(), amount);
          }


          function burnFrom(address account, uint256 amount) public onlyOwner {
              _burn(account, amount);
          }


          function pause() public onlyOwner {
              _pause();
          }


          function unpause() public onlyOwner {
              _unpause();
          }


          function _beforeTokenTransfer(address from, address to, uint256 amount)
              internal
              whenNotPaused
              override
          {
              super._beforeTokenTransfer(from, to, amount);
          }
      }`
    });

    return compilerResponse;
  }
}

Thanks a million :slight_smile:

Indeed, the example on their GitHub repository does not included any import statement as part of the source code being tested:

    `
    contract C { 
      function f() public { } 
    }

    contract D {
      function g() public { }
    }
    `

So perhaps they do not support this (a bit weird, but who knows).
Try to flatten the source code before pasting it into your script...

First of all thanks for your answer. Yes, I saw that part of the github repository and tried to flatten it with remix, but I don't know if it kept giving me an error because of the length, I'm going to try to flatten it manually, which will take me a long time but maybe it's profitable.

Thanks again

1 Like

have you resolved the issues? If not, I can help you.

I think I did. I flatterned the smart contract and It's working, but idk if it's the optimal solution.