Writing Smart Contracts

EigenKub is 100% compatible with Ethereum, which means you can write smart contracts using Solidity just like you would for Ethereum. You can use all the same tools, libraries, and frameworks, including OpenZeppelin, Hardhat, Truffle, and more.

Here's an example of a simple ERC-20 token contract:

solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.17;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract MyToken is ERC20, Ownable {
    constructor(uint256 initialSupply) ERC20("MyToken", "MTK") {
        _mint(msg.sender, initialSupply * 10 ** decimals());
    }
    
    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}

Deploying Smart Contracts

Once you've written your smart contract, you can deploy it to the EigenKub network using Truffle, Hardhat, or Remix. Here's an example of a Truffle migration script:

javascript
// migrations/1_deploy_token.js
const MyToken = artifacts.require("MyToken");

module.exports = function (deployer) {
  // Deploy with an initial supply of 1,000,000 tokens
  deployer.deploy(MyToken, 1000000);
};

To deploy your contract, run the following command:

bash
truffle migrate --network eigenkub

Testing Smart Contracts

It's important to thoroughly test your smart contracts before deploying them to the mainnet. You can use Truffle or Hardhat to write and run tests. Here's an example of a Truffle test for our ERC-20 token:

javascript
const MyToken = artifacts.require("MyToken");

contract("MyToken", accounts => {
  it("should put 1,000,000 tokens in the first account", async () => {
    const instance = await MyToken.deployed();
    const balance = await instance.balanceOf(accounts[0]);
    assert.equal(
      balance.toString(), 
      "1000000000000000000000000", 
      "1,000,000 tokens were not in the first account"
    );
  });
  
  it("should allow the owner to mint new tokens", async () => {
    const instance = await MyToken.deployed();
    const initialBalance = await instance.balanceOf(accounts[1]);
    
    await instance.mint(accounts[1], "1000000000000000000000");
    
    const newBalance = await instance.balanceOf(accounts[1]);
    assert.equal(
      newBalance.toString(),
      "1000000000000000000000",
      "1000 tokens were not minted to the second account"
    );
  });
});

To run your tests, use the following command:

bash
truffle test

Gas Optimization

While gas fees on EigenKub are lower than on Ethereum, it's still important to optimize your contracts to minimize gas usage. Here are some tips:

  • Use the latest Solidity compiler version
  • Minimize on-chain storage
  • Batch operations when possible
  • Use libraries for common functions
  • Avoid loops with unbounded iterations

Security Best Practices

Security is critical when developing smart contracts. Follow these best practices:

  • Use established libraries like OpenZeppelin
  • Follow the checks-effects-interactions pattern
  • Be aware of reentrancy attacks
  • Use proper access control
  • Consider having your contracts audited