Contract Interactions (ERC Contracts)

npm versionLicense: GPL-3.0GitHub stars

Smart contract interaction is one of the most fundamental building blocks in any decentralized application (dApp). With this library, developers can seamlessly interact with deployed Ethereum contracts β€” whether calling read-only functions (view/pure) or executing state-changing transactions. This library simplifies smart contract communication by wrapping common read and write operations with a streamlined interface. This includes

  • Abstracted contract creation via createContract()

  • Unified read() and write() methods

  • Built-in support for gas estimation

  • Optional transaction configuration (e.g., gasLimit, from)

This guide explores how to interact with smart contracts β€” without directly using ethers.js β€” using a clean and minimal library.

Creates a smart contract instance scoped to the provided provider and ABI. The returned contract object supports:

  • .read(methodName, args)

  • .write(methodName, args, txOptions)

  • .estimateGas(methodName, args)

🧱 1. Framework Usage (ES6/ npm modules)

πŸ“ Installation

If you haven’t already:

Or if you use Yarn:

πŸ“˜ Example: Reading and Writing Values

🧠 Contract ABI

βœ… Full Interaction Script

For typescript users, declare as a module in your types file:

πŸ“Œ Code Breakdown

πŸ”Ή contract.read(methodName, args[])

Reads a view function from the contract.

  • Returns: Resolved value from the contract.

  • Use Case: Free, read-only queries.

πŸ”Έcontract.estimateGas(methodName, args[])

Estimates the gas usage for a function call before executing it.

  • Returns: Estimated gas (number).

  • Use Case: Pre-transaction cost estimation.

✴️ contract.write(methodName, args[], options)

Sends a transaction to the contract.

  • Returns: Transaction hash on submission.

  • Requires: Wallet connection and user approval.

  • Options:

    • from: Wallet address

    • gasLimit: (optional) Maximum gas to spend

    • [123456] : 123456 the parameter the function is accepting, which would be used to change the state of the contract.

🌐 2. CommonJS via CDN (HTML/Vanilla JS)

βœ… Embed Script

Ensure this is added before your custom scripts in the HTML body.

πŸ” Implementation on VanillaJs via CDN

For production readiness, always wrap async calls with try/catch:

πŸ“š Summary

Operation
Method
Signature Required
Notes

Read

❌

Executes a view or pure function declared in the contract.

Estimate Gas

❌

Simulates gas use without sending

Write

βœ…

Executes a function that changes the state of a contract and returns tx hash

πŸš€ Conclusion

This library contract abstraction provides a zero-boilerplate way to interact with Ethereum contracts. With unified read/write calls, gas estimation, and support for CDN usage, it empowers frontend developers to build dApps without the complexity of manually managing web3 libraries.

Last updated