...

/

Defining More Functions in Our Smart Contract

Defining More Functions in Our Smart Contract

Learn to add other important functions to our smart contract.

We’ve looked at functions and learned how to define them, so now let’s extend the functionality of our current smart contract and add a few more important functions.

Implementing read-update-delete

Here’s the current state of our contract.

Press + to interact
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Contact
* @dev Store & retrieve contacts of friends and family
*/
contract Contact {
enum Relationship {
Family,
Friend,
Acquaintance
}
struct ContactDetail {
uint256 number;
string name;
address payable _address;
bool isEmergencyContact;
Relationship relationship;
}
address owner;
mapping (uint => ContactDetail) public contacts;
uint public numberOfContacts;
constructor() {
owner = msg.sender;
}
/**
* @dev Save new contact
* @param number_ number of contact to store
* @param name_ name of contact to store
* @param address_ blockchain address of contact to store
* @param relationship_ relationship of contact to store
*/
function save(uint256 number_, string calldata name_, address payable address_, Relationship relationship_) public {
// save contact
uint newContactID = ++numberOfContacts;
ContactDetail storage newContact = contacts[newContactID];
newContact.number = number_;
newContact.name = name_;
newContact._address = address_;
newContact.isEmergencyContact = false;
newContact.relationship = relationship_;
}
}

Our idea for the contract is a basic CRUD that manages contact details. Right now, our contract only has the create feature available with the save function. We’ll implement the remaining read, update, and delete portions by creating three extra functions: get, update, and deleteContact. We’ll add one extra feature as a function: the ability to send our contacts some money. Let’s call this sendFunds.

Press + to interact
CRUD
CRUD

get

Here, we want to look up a contact with a specified id. We’ve already specified our contacts state variable as public, so we can perform a lookup using the GUI button on Remix. But we’ll still create this function to do something similar.

Press + to interact
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
/**
* @title Contact
* @dev Store & retrieve contacts of friends and family
*/
contract Contact {
enum Relationship {
Family,
Friend,
Acquaintance
}
struct ContactDetail {
uint256 number;
string name;
address payable _address;
bool isEmergencyContact;
Relationship relationship;
}
address owner;
mapping(uint256 => ContactDetail) public contacts;
uint256 public numberOfContacts;
constructor() {
owner = msg.sender;
}
modifier isOwner {
require(msg.sender == owner, "Caller is not contract owner");
_;
}
/**
* @dev Save new contact
* @param number_ number of contact to store
* @param name_ name of contact to store
* @param address_ blockchain address of contact to store
* @param relationship_ relationship of contact to store
*/
function save(
uint256 number_,
string calldata name_,
address payable address_,
Relationship relationship_
) public isOwner {
// save contact
uint256 newContactID = ++numberOfContacts;
ContactDetail storage newContact = contacts[newContactID];
newContact.number = number_;
newContact.name = name_;
newContact._address = address_;
newContact.isEmergencyContact = false;
newContact.relationship = relationship_;
}
function get(uint id) public view returns (ContactDetail memory contact) {
require(id > 0 && numberOfContacts >= id, "Contact does not exist");
contact = contacts[id];
}
}

We made this function accept an argument called id, which we’ll use for the lookup. We first check if this contact exists by seeing if the provided id is greater than 0, since the first contact will have an id of 1. Then, we check if the provided id is greater than the number of contacts. This isn’t possible, so it serves as a good ...