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

Top comments (0)