2

I would like to deploy a contract written in pure Yul with Hardhat but I'm not sure how to do it.

Is there some plugin for it, or do I just need to add special fields to the hardhat.config.js file?

Thanks.

1 Answer 1

0

It's absolutely possible, the only pre-requisite is to have the Yul compiler set-up already.


As long as you manage to compile the .yul contract down to the bytecode and ABI, you can just pass them directly to the getContractFactory function, like this (I used the Remix IDE Yul compiler to compile the Token.yul file):

// scripts/deploy_Token.js

// We require the Hardhat Runtime Environment explicitly here. This is optional
// but useful for running the script in a standalone fashion through `node <script>`.
//
// You can also run a script with `npx hardhat run <script>`. If you do that, Hardhat
// will compile your contracts, add the Hardhat Runtime Environment's members to the
// global scope, and execute the script.
const hre = require("hardhat");
const fs = require("fs")
const path = require("path")

async function main() {
  // paste the bytecode from the compiler output:bytecode:object argument as a string literal
  const bytecode = '335f556103a36100115f396103a35ff3fe6100093415610398565b6100116101ce565b806370a082311461011857806318160ddd1461010b578063a9059cbb146100eb57806323b872dd146100c1578063095ea7b3146100a1578063dd62ed3e14610081576340c10f1914610061575f80fd5b61007c61006e60016101f7565b6100775f6101d9565b61012e565b610216565b61009c61008e60016101d9565b6100975f6101d9565b610325565b61020f565b6100bc6100ae60016101f7565b6100b75f6101d9565b61016b565b610216565b6100e66100ce60026101f7565b6100d860016101d9565b6100e15f6101d9565b61018c565b610216565b6101066100f860016101f7565b6101015f6101d9565b61015e565b610216565b6101136102b5565b61020f565b6101296101245f6101d9565b6102dd565b61020f565b9061015c9161014361013e610380565b610398565b61014c826102c1565b61015682826102ea565b5f61021d565b565b9061016991336101a4565b565b9061018a916101798161038d565b610184828233610333565b3361024b565b565b906101a2929161019d833383610340565b6101a4565b565b906101cc92916101b38261038d565b6101bd8382610302565b6101c783836102ea565b61021d565b565b600160e01b5f350490565b6101e2906101f7565b9060018060a01b031982166101f357565b5f80fd5b60200260040160208101361061020b573590565b5f80fd5b5f5260205ff35b600161020f565b9061024992917fddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef610279565b565b9061027792917f8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925610279565b565b9091925f5260205fa3565b5f90565b600190565b6110000190565b61029d9061028d565b5f5260205260405f2090565b6102b1610284565b5490565b6102bd610288565b5490565b6102d2906102cd6102b5565b610369565b6102da610288565b55565b6102e69061028d565b5490565b6102f66102fe9161028d565b918254610369565b9055565b61030b9061028d565b90815461032061031b8284610364565b610398565b039055565b9061032f91610294565b5490565b9061033d91610294565b55565b9061034a91610294565b90815461035f61035a8284610364565b610398565b039055565b111590565b9190918281019283109083101761037c57565b5f80fd5b336103896102a9565b1490565b61039690610398565b565b1561039f57565b5f80fd'; // require('../contracts/Token.json');
  // paste ABI as a const or load the abi from a JSON file
  const abi = require("../contracts/TokenABI.json").abi;

  const ERC1155Token = await hre.ethers.getContractFactory(abi, bytecode);
  const deployed = await ERC1155Token.deploy();

  await deployed.waitForDeployment();

  console.log(
    `🚢 Done! Contract deployed to ${deployed.target}!`
  );
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Then you should see something like this logged after npx hardhat run scripts/deploy_Token.js:

Done! Contract deployed to 0x5FbDB2315678afecb367f032d93F642f64180aa3!

That approach with instantiating a contract factory based on the ABI and the bytecode is just a bit more complicated than just doing something like that (something that you'd normally do for Solidity files):

const MyContract = await ethers.getContractFactory("MyContract");
const contract = MyContract.attach(
  "0x..." // The deployed contract address
);

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.