This npm package is an ethers helper to create a structured and safe framework around contract deployment and testing. Future works in syntactic code analysis and unit testing helper code will help ensure safer standards when creating and deploying smart contracts.
- Features
- JSON based address and constants management
- Feature Expansion (Future Work)
- Export Contract Details with ABI
- Unit Testing (SDK code) and Corner Case Exception Handling
- Unit Testing Helper (eg. test-coverage analysis, testing functions, etc)
- Syntactic security analysis of contracts
- hardhat-jsonRPC (importable package extension)
- hardhat json RPC setup
- Event Listener and Logger
mkdir appName && cd appName
npx hardhat // options: typescript-project, hardhat-toolbox
npm i -D ethers-methods
touch .env
- Add the following into the
.env
file
// File: .env
GOERLI_RPC_URL='https://goerli.infura.io/v3/api-key-here'
PRIVATE_KEY_1='key'
JSON_LOCATION='utils/json/constants.json'
- RUN
npx hardhat compile
- Create deploy and contract scripts with ethers-methods
// File: scripts/runTest.ts
import sdk from "ethers-methods";
const main = async () => {
await sdk.setNetwork("goerli");
await sdk.createIfNotExist(sdk.rootFolder(), sdk.JSON_LOCATION);
const addr = await sdk.deployContractFromArtifacts("Lock", "lock", [
new Date().getTime(),
{ value: 1 },
]);
await sdk.wait(10000);
const lockContract = await sdk.getContractFromArtifacts("Lock", "lock");
const ret = await lockContract.withdraw();
return ret;
};
main()
.then((val) => console.log(val))
.catch((err) => console.error(err));
- Run scripts in order
npx ts-node scripts/runTest.ts
▸ deployContract(contractAbi
, contractName
, deployArgs
, signer
): Promise
<string
>
This function deploys contract, with ABI path, deployArgs, and a Signer
Example
await sdk.deployContract(
"artifacts/contracts/Utility.sol/Utility.json",
"utility",
[],
await sdk.wallet()
);
Name | Type | Description |
---|---|---|
contractAbi |
string |
Absolute or Relative (to project root) path of ABI JSON file |
contractName |
string |
Name of Contract to index deployed addresses |
deployArgs |
any [] |
Array of arguments to be deconstructed |
signer |
Signer |
Signer used to sign transactions |
Promise
<string
>
Address of Deployed Contract
▸ deployContractFromArtifacts(fileName
, contractName
, deployArgs
, artifactLocation?
): Promise
<string
>
This function deploys contract, looking through Artifacts for the ABI matching the fileName provided.
Example
await sdk.deployContractFromArtifacts("Utility", "utility", []);
Name | Type | Description |
---|---|---|
fileName |
string |
Name of Contract file to find ABI |
contractName |
string |
Name of Contract to index deployed addresses |
deployArgs |
any [] |
Array of arguments to be deconstructed |
artifactLocation? |
string |
(Optional) File location of ABIs |
Promise
<string
>
Address of Deployed Contract
▸ getContract(contractAbi
, contractAddress
, signer
): Promise
<Contract
>
This function creates a contract interface with a deployed contract.
Example
await sdk.getContract(
"artifacts/contracts/Utility.sol/Utility.json",
"0x65B165C17a8660e84e4427c4024fcB784577AB05",
await sdk.wallet()
);
Name | Type | Description |
---|---|---|
contractAbi |
string |
Absolute or Relative (to project root) path of ABI JSON file |
contractAddress |
string |
Address of Deployed Contract |
signer |
Signer |
Signer used to sign transactions |
Promise
<Contract
>
Deployed Contract ethers Interface
▸ getContractFromArtifacts(fileName
, contractName
, artifactLocation?
): Promise
<Contract
>
This function creates a contract interface with a deployed contract, looking through Artifacts for the ABI matching the fileName provided.
Example
await sdk.getContractFromArtifacts("Utility", "utility");
Name | Type | Description |
---|---|---|
fileName |
string |
Name of Contract file to find ABI |
contractName |
string |
Name of Contract to index deployed addresses |
artifactLocation? |
string |
(Optional) File location of ABIs |
Promise
<Contract
>
Deployed Contract ethers Interface
▸ readJson(type?
, name?
, file?
): Promise
<undefined
| string
| ObjectAny
>
This reads json file given type and name
Example
await readJson("addresses", "goerli-utility");
Name | Type | Description |
---|---|---|
type? |
string |
(Optional) Type of saved data (eg. addresses) |
name? |
string |
(Optional) Name of saved data (eg. goerli-utility) |
file? |
string |
(Optional) File that data is saved in |
Promise
<undefined
| string
| ObjectAny
>
Object or string, depending on input
▸ rootFolder(): string
Get rootFolder absolute path
Example
rootFolder();
string
rootFolder absolute path
▸ saveAddress(name
, value
, file?
): Promise
<void
>
This function saves the address given a contract name
Example
await saveAddress("utility", "0x4d391169EcF040072d8Da35d70166f70254B32C7");
Name | Type | Description |
---|---|---|
name |
string |
Name of Contract to index deployed addresses |
value |
string |
Value of address to save |
file? |
string |
(Optional) File to save address in |
Promise
<void
>
Promise to save address of given contract
▸ saveJson(type
, name
, value
, file?
): Promise
<void
>
This saves to json file given type, name, and value
Example
await saveJson(
"addresses",
"goerli-utility",
"0x65B165C17a8660e84e4427c4024fcB784577AB05"
);
Name | Type | Description |
---|---|---|
type |
string |
Type of saved data (eg. addresses) |
name |
string |
Name of saved data (eg. goerli-utility) |
value |
string |
Value of saved data (eg. 0x65B165C17a8660e84e4427c4024fcB784577AB05) |
file? |
string |
(Optional) File that data is saved in |
Promise
<void
>
Promise to finish writing to file
▸ setNetwork(networkName
): Promise
<string
>
This function changes the current NETWORK
Example
await setNetwork("goerli");
Name | Type | Description |
---|---|---|
networkName |
string |
name of NETWORK name to change to |
Promise
<string
>
current network (localhost / hardhat / goerli)
▸ wallet(): Promise
<Signer
>
This function returns a Signer connected to a provider, given the appropriate network
Example
await wallet();
Promise
<Signer
>
Signer used to sign transactions