Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SC-967] Deploy contract from bytecode #94

Merged
merged 3 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@1inch/solidity-utils",
"version": "3.1.0",
"version": "3.2.0",
"main": "dist/src/index.js",
"types": "dist/src/index.d.ts",
"repository": {
Expand Down
10 changes: 9 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import '@nomicfoundation/hardhat-ethers'; // required to populate the HardhatRuntimeEnvironment with ethers
import hre, { ethers } from 'hardhat';
import { time } from '@nomicfoundation/hardhat-network-helpers';
import { BaseContract, BigNumberish, Contract, ContractTransactionReceipt, ContractTransactionResponse, JsonRpcProvider, Wallet } from 'ethers';
import { BaseContract, BigNumberish, BytesLike, Contract, ContractTransactionReceipt, ContractTransactionResponse, JsonRpcProvider, Signer, Wallet } from 'ethers';
import { DeployOptions, DeployResult } from 'hardhat-deploy/types';

import { constants } from './prelude';
Expand Down Expand Up @@ -81,6 +81,14 @@ export async function deployContract(name: string, parameters: Array<BigNumberis
return instance;
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
export async function deployContractFromBytecode(abi: any[], bytecode: BytesLike, parameters: Array<BigNumberish> = [], signer?: Signer) : Promise<BaseContract> {
const ContractFactory = await ethers.getContractFactory(abi, bytecode, signer);
const instance = await ContractFactory.deploy(...parameters);
await instance.waitForDeployment();
return instance;
}

type Token = {
balanceOf: (address: string) => Promise<bigint>;
getAddress: () => Promise<string>;
Expand Down
18 changes: 17 additions & 1 deletion test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { expect, ether, time, constants } from '../src/prelude';
import { timeIncreaseTo, fixSignature, signMessage, trackReceivedTokenAndTx, countInstructions, deployContract, deployAndGetContract } from '../src/utils';
import { timeIncreaseTo, fixSignature, signMessage, trackReceivedTokenAndTx, countInstructions, deployContract, deployAndGetContract, deployContractFromBytecode } from '../src/utils';
import hre, { deployments, ethers } from 'hardhat';
import { loadFixture } from '@nomicfoundation/hardhat-network-helpers';
import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers';
Expand Down Expand Up @@ -182,6 +182,22 @@ describe('utils', function () {
});
});

describe('deployContractFromBytecode', function () {
it('should deploy new contract instance', async function () {
const contractArtifact = await hre.artifacts.readArtifact('TokenMock');
const token = <TokenMock> await deployContractFromBytecode(contractArtifact.abi, contractArtifact.bytecode, ['SomeToken', 'STM']);
expect(await token.getAddress()).to.be.not.eq(constants.ZERO_ADDRESS);
expect(await token.name()).to.be.eq('SomeToken');
});

it('can be used without arguments', async function () {
const contractArtifact = await hre.artifacts.readArtifact('WETH');
const weth = <WETH> await deployContractFromBytecode(contractArtifact.abi, contractArtifact.bytecode);
expect(await weth.getAddress()).to.be.not.eq(constants.ZERO_ADDRESS);
expect(await weth.name()).to.be.eq('Wrapped Ether');
});
});

describe('deployAndGetContract', function () {
it('should deploy new contract instance', async function () {
const tokenName = 'SomeToken';
Expand Down
Loading