ChristoferVikstroem / FaTS

An open-source repository for a smart contract implementation in the KTH course DD2485.
MIT License
1 stars 0 forks source link

Resources #8

Open vivi365 opened 9 months ago

vivi365 commented 9 months ago

Basics 😎

vivi365 commented 9 months ago

How to interact with a contract deployed locally in hardhat

Set up

  1. Start the network with npx hardhat node
  2. Create a deployment script
  3. run deployment script with localhost network: npx hardhat run --network localhost scripts/deploy.js
  4. Get contract address from deployment on localhost
  5. Open the hardhat console in localhost: npx hardhat console --network localhost
  6. Interact!

How to interact:

We are using the ethers api

  1. const Company = await ethers.getContractFactory('CONTRACT NAME')
  2. const company = Company.attach('HEXADDRESS HERE');
  3. Use any contract addresses using async await to avoid promises. save data in e.g. a var.- var greeting = await company.register("hello");
  4. Use toString when displaying unit256 which is too big for java... eg company.retrieve()).toString()

Namespaces in ethers API

vivi365 commented 9 months ago

Calling a contract from another contract

Example

import "./access-control/Auth.sol";
contract Hello {
  Auth private _auth;
  constructor() {
        _auth = new Auth(msg.sender);
    }
  function hi() {
    require(_auth.isAdministrator(msg.sender), "Unauthorized");}
}