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