Coder Social home page Coder Social logo

learn-ethereum-second-edition's Introduction

Packt Conference

3 Days, 20+ AI Experts, 25+ Workshops and Power Talks

Code: USD75OFF

Learn-Ethereum-Second-Edition

Errata

  • Page 388: The function removeAdmin should be:
    function removeAdmin(address account) external onlyOwner {
      require(account != address(0) && admins[account]);
      admins[account] = false;
    }
    

learn-ethereum-second-edition's People

Contributors

brian8151 avatar jubit-packt avatar nathanya-packt avatar packt-itservice avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

learn-ethereum-second-edition's Issues

Gas limit problem

I keep running this code from chapter 6 on remix IDE version 0.46.0:-
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

contract LeaseContract {

// Define the structure for Rental property Lease Agreement
struct Lease {
address payable landlord; // the landlord
address payable tenant; // the tenant
string location; // property location
uint term; // lease term
uint rent; // monthly rent
uint securityDeposit; // security deposit
uint earlyPenalty; // early termination penalty
uint creationTimestamp; // creation timestamp
uint signedTimestamp; // contract retification timestamp
uint moveinTimestamp; // The tenant occupation timestamp;
}

// Define the state machine for leasing
enum LeaseState {Created, Signed, Occupied, Terminated}

// Lease as the state variable
Lease public lease;

// Keep a record of all payments and account balance
struct Deposit
{
uint sequence;
uint amount;
}

Deposit[] public deposits;
uint public balance = 0;
uint public totalReceived = 0;

// keep track of security deposit received;
uint public securityDeposited;

//keep track of state transition of leasing application
LeaseState public state;

/* start the lease */
constructor(uint _rent, uint _term, uint _securityDeposit, uint _earlyPenalty, string memory _location) {
lease.landlord = payable(msg.sender);
lease.location = _location;
lease.rent = _rent;
lease.term = _term;
lease.securityDeposit = _securityDeposit;
lease.earlyPenalty = _earlyPenalty;
lease.creationTimestamp = block.timestamp;
state = LeaseState.Created;
}

// define function modifier restricting actions per state
modifier inState(LeaseState _state) {
if (state != _state) revert();
_;
}

// define function modifier restricting to landlord only
modifier onlyLandlord() {
if (msg.sender != lease.landlord) revert();
_;
}

// define function modifier excluding the landlord
modifier notLandlord() {
if (msg.sender == lease.landlord) revert();
_;
}

// define function modifier restricting to tenant only
modifier onlyTenant() {
if (msg.sender != lease.tenant) revert();
_;
}

// define function modifier requiring pay in full
modifier payInFull(uint _rent) {
if (_rent < lease.rent) revert();
_;
}

event securityDepositPaid(address indexed _tenant, uint _amount, uint _timestamp);

event leaseSigned(address indexed _tenant, uint _signedTimestamp);

event rentPaid(address indexed _tenant, uint _timestamp);

event leaseTerminated(address indexed _by, string _reason, uint _timestamp);

/* Lease signed by the tenant */
function signLease() public payable
inState(LeaseState.Created)
notLandlord
{
lease.tenant = payable(msg.sender);
securityDeposited = msg.value;
require(securityDeposited >= lease.securityDeposit);
lease.signedTimestamp = block.timestamp;
state = LeaseState.Signed;

emit leaseSigned(lease.tenant, lease.signedTimestamp);

}

/* tenant move in */
function moveIn() public
onlyTenant
inState(LeaseState.Signed)
onlyTenant
{
lease.moveinTimestamp = block.timestamp;
state = LeaseState.Occupied;
}

/* pay the monthly rent, and keep a record */
function payRent() public payable
onlyTenant
inState(LeaseState.Occupied)
payInFull(msg.value + balance)
{
emit rentPaid(lease.tenant, block.timestamp);
totalReceived++;
balance += msg.value - lease.rent; // keep track of balance;
deposits.push(Deposit({sequence : totalReceived, amount : msg.value}));
lease.landlord.transfer(msg.value);
}

/* terminate the lease when it is mature*/
function leaseDue() public
inState(LeaseState.Occupied)
onlyLandlord
{
emit leaseTerminated(lease.landlord, "lease due", block.timestamp);
//if lease term is due, return security deposit to the tenant, and the rest to landlord;
require (totalReceived >= lease.term);
state = LeaseState.Terminated;
lease.tenant.transfer(securityDeposited);
lease.landlord.transfer(address(this).balance);
}

/* evict the tenant for missing pay*/
function evict() public
inState(LeaseState.Occupied)
onlyLandlord
{
emit leaseTerminated(lease.landlord, "eviction", block.timestamp);
//if missing rent pay, start the eviction; return the balance to the landlord;
require (totalReceived < lease.term && balance < lease.rent);
state = LeaseState.Terminated;
lease.landlord.transfer(address(this).balance);
}

/* terminiate the lease early by the tenant*/
function terminateEarly() public payable
inState(LeaseState.Occupied)
onlyTenant
{
emit leaseTerminated(lease.tenant, "early termination", block.timestamp);
//tenant termintes the lease early, pay penalty; return the balance to landlord
require (totalReceived < lease.term && msg.value >= lease.earlyPenalty);
state = LeaseState.Terminated;
lease.landlord.transfer(address(this).balance);
}
}
With these parameters for the constructor:- 100000,12,100000,100000,"Red"
So far i've been stting value to :- 1,000,000
and leaving the gas limit at 300000000.
It keeps telling me "transact to LeaseContract.signLease errored: Error occurred: out of gas.

out of gas
The transaction ran out of gas. Please increase the Gas Limit."
and the only way i don't get this error is lowering the value lower than the securityDeposited which in this case is 10000 and results in an error being thrown due to the requirement in the signLease function not being met, also the value denomination is wei and i use remix vm(cancun).
No matter how much i seem to increase the gas limit, i still get the same error, could i get help with this?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.