DEV Community

Madhav Jha
Madhav Jha

Posted on

Solidity Basics

Let's start with a simple setup.

// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Fundraiser {
    string public name;

    constructor(string memory _name) {
        name = _name;
    }
}

Enter fullscreen mode Exit fullscreen mode

Notice the constructor parameter is labeled string memory. What does it mean? Parameters containing reference data types such as arrays or strings have two choices.

  1. storage
  2. memory

These reference types must tell if they are pointing to persisted data (storage) or a local copy of that data (memory).

In most cases memory is the correct choice.

There is also calldata which we can get into later.

Solidity offers two different types for address:

  1. the address type and
  2. the address payable type.

The distinction is that payable addresses can receive ether, and will have the transfer and send methods available.

const FundraiserContract = artifacts.require("Fundraiser");

contract("Fundraiser", accounts => {
    let fundraiser;
    const name = "My Fundraiser";
    const beneficiary = accounts[1];
    const owner = accounts[0];

    describe("initialization", () => {
        beforeEach(async () => {
            fundraiser = await FundraiserContract.new(name, beneficiary, owner);
        });

        it("gets the beneficiary name", async () => {
            const actualName = await fundraiser.name();
            assert.equal(actualName, name);
        });

        it("gets the beneficiary address", async () => {
            const actualBeneficiary = await fundraiser.beneficiary();
            assert.equal(actualBeneficiary, beneficiary);
        });

        it("gets the owner address", async () => {
            const actualOwner = await fundraiser.owner();
            assert.equal(actualOwner, owner);
        });
    });
});
Enter fullscreen mode Exit fullscreen mode
// SPDX-License-Identifier: MIT
pragma solidity >=0.4.22 <0.9.0;

contract Fundraiser {
    string public name;
    address payable public beneficiary;
    address public owner;

    constructor(
        string memory _name,
        address payable _beneficiary,
        address _owner
    ) {
        name = _name;
        beneficiary = _beneficiary;
        owner = _owner;
    }
}
Enter fullscreen mode Exit fullscreen mode

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay