1.0.3 • Published 1 year ago

ethers-sdk v1.0.3

Weekly downloads
-
License
ISC
Repository
github
Last release
1 year ago

ethers-sdk

Pre-Install Hardhat Repo

mkdir appName && cd appName
npx hardhat   // options: typescript-project, hardhat-toolbox

Install

npm i -D ethers-sdk
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'

Using the SDK

  • RUN npx hardhat compile
  • Create deploy and contract scripts with ethers-sdk
// File: scripts/runTest.ts
import sdk from "ethers-sdk";

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

SDK Documentation

FULL DOCUMENTATION

deployContract

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()
);

Parameters

NameTypeDescription
contractAbistringAbsolute or Relative (to project root) path of ABI JSON file
contractNamestringName of Contract to index deployed addresses
deployArgsany[]Array of arguments to be deconstructed
signerSignerSigner used to sign transactions

Returns

Promise<string>

Address of Deployed Contract


deployContractFromArtifacts

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", []);

Parameters

NameTypeDescription
fileNamestringName of Contract file to find ABI
contractNamestringName of Contract to index deployed addresses
deployArgsany[]Array of arguments to be deconstructed
artifactLocation?string(Optional) File location of ABIs

Returns

Promise<string>

Address of Deployed Contract


getContract

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()
);

Parameters

NameTypeDescription
contractAbistringAbsolute or Relative (to project root) path of ABI JSON file
contractAddressstringAddress of Deployed Contract
signerSignerSigner used to sign transactions

Returns

Promise<Contract>

Deployed Contract ethers Interface


getContractFromArtifacts

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");

Parameters

NameTypeDescription
fileNamestringName of Contract file to find ABI
contractNamestringName of Contract to index deployed addresses
artifactLocation?string(Optional) File location of ABIs

Returns

Promise<Contract>

Deployed Contract ethers Interface


readJson

readJson(type?, name?, file?): Promise<undefined | string | ObjectAny>

This reads json file given type and name

Example

await readJson("addresses", "goerli-utility");

Parameters

NameTypeDescription
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

Returns

Promise<undefined | string | ObjectAny>

Object or string, depending on input


rootFolder

rootFolder(): string

Get rootFolder absolute path

Example

rootFolder();

Returns

string

rootFolder absolute path


saveAddress

saveAddress(name, value, file?): Promise<void>

This function saves the address given a contract name

Example

await saveAddress("utility", "0x4d391169EcF040072d8Da35d70166f70254B32C7");

Parameters

NameTypeDescription
namestringName of Contract to index deployed addresses
valuestringValue of address to save
file?string(Optional) File to save address in

Returns

Promise<void>

Promise to save address of given contract


saveJson

saveJson(type, name, value, file?): Promise<void>

This saves to json file given type, name, and value

Example

await saveJson(
  "addresses",
  "goerli-utility",
  "0x65B165C17a8660e84e4427c4024fcB784577AB05"
);

Parameters

NameTypeDescription
typestringType of saved data (eg. addresses)
namestringName of saved data (eg. goerli-utility)
valuestringValue of saved data (eg. 0x65B165C17a8660e84e4427c4024fcB784577AB05)
file?string(Optional) File that data is saved in

Returns

Promise<void>

Promise to finish writing to file


setNetwork

setNetwork(networkName): Promise<string>

This function changes the current NETWORK

Example

await setNetwork("goerli");

Parameters

NameTypeDescription
networkNamestringname of NETWORK name to change to

Returns

Promise<string>

current network (localhost / hardhat / goerli)


wallet

wallet(): Promise<Signer>

This function returns a Signer connected to a provider, given the appropriate network

Example

await wallet();

Returns

Promise<Signer>

Signer used to sign transactions

SDK Expansion

  • Unit / Integration Testing
  • Set up hardhat JSONRPC
  • Set up IPFS calls
  • Event Listener and Logger
  • Syntactic security analysis of contracts