Created
March 2, 2022 16:05
-
-
Save yann300/6ec29d3d7531b9e773c03e4c0965de4b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Right click on the script name and hit "Run" to execute | |
const { expect } = require("chai"); | |
const { ethers } = require("hardhat"); | |
(async () => { | |
try { | |
// function getContractFactoryFromArtifact(artifact: Artifact, signer?: ethers.Signer): Promise<ethers.ContractFactory>; | |
// function getContractFactoryFromArtifact(artifact: Artifact, factoryOptions: FactoryOptions): Promise<ethers.ContractFactory>; | |
const metadata = JSON.parse(await remix.call('fileManager', 'readFile', 'contracts/artifacts/Storage.json')) | |
/* | |
export interface Artifact { | |
_format: string; | |
contractName: string; | |
sourceName: string; | |
abi: any[]; | |
bytecode: string; // "0x"-prefixed hex string | |
deployedBytecode: string; // "0x"-prefixed hex string | |
linkReferences: LinkReferences; | |
deployedLinkReferences: LinkReferences; | |
} | |
*/ | |
/* | |
interface Libraries { | |
[libraryName: string]: string; | |
} | |
interface FactoryOptions { | |
signer?: ethers.Signer; | |
libraries?: Libraries; | |
} | |
*/ | |
const artifact = { | |
contractName: '', | |
sourceName: 'contracts/1_Storage.sol', | |
abi: metadata.abi, | |
bytecode: '0x' + metadata.data.bytecode.object, | |
deployedBytecode: '0x' + metadata.data.deployedBytecode.object, | |
linkReferences: metadata.data.bytecode.linkReferences, | |
deployedLinkReferences: metadata.data.deployedBytecode.linkReferences, | |
} | |
const options = { | |
libraries: metadata.deploy['VM:-'].linkReferences['contracts/1_Storage.sol'] | |
} | |
console.log(options) | |
const factory = await ethers.getContractFactoryFromArtifact(artifact, options) | |
const storage = await factory.deploy(); | |
await storage.deployed(); | |
expect((await storage.retrieve()).toString()).to.equal('0') | |
const storeValue = await storage.store(333); | |
// wait until the transaction is mined | |
await storeValue.wait(); | |
expect((await storage.retrieve()).toString()).to.equal('333') | |
expect((await storage.getFromLib()).toString()).to.equal('34'); | |
} catch (e) { | |
console.log(e.message) | |
} | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment