DEV Community

Amy's Vue on this
Amy's Vue on this

Posted on

Web3 Solidity + JavaScript 32 hour course recap #10

timestamps: 3:30:30 - 3:57:00

This is a recap of me making my way through the 32 hour course by Patrick Collins posted on FreeCodeCamp.

The first few minutes went over what had been learned in the previous lesson. Which is always a convenient place to stop.

Let's take a moment to emphasize that in order to override a function from a child contract, the parent function has to have the keyword virtual, and the child function has to have override.

I also spent some time trying to figure out a good way to export the desktop remix workspace to my local machine. The best bet is to either export it into a gist, and/or create a new workspace as soon as you start a new project.

Patrick Collins explained really well how chainlink and things like that work to allow decentralized apps have access to real-world information in a decentralized way. It involved pretty graphs, and I recommend that you watch that part if you're interested in the specifics.

tl;dr use Chainlink.

In order to make a function payable, you have to add the keyword... payable.

function fund() public payable {}

It also has the added benefit of making the button in the deploy section of remix red.

The value of the etherium (or whatever currency) is at the top level of the request, so the value can be accessed through msg.value

When you have a function that has a revert function (payable), put the stuff that has the option to fail at the top of the function to help the user save on gas fees if the transaction is cancelled.

I also went on a bit of a tangent to get the revert message to show a concatenated message that was descriptive with the proper amount that was expected:

function uint2str(uint _i) internal pure returns (string memory _uintAsString) {
if (_i == 0) {
return "0";
}
uint j = _i;
uint len;
while (j != 0) {
len++;
j /= 10;
}
bytes memory bstr = new bytes(len);
uint k = len;
while (_i != 0) {
k = k-1;
uint8 temp = (48 + uint8(_i - _i / 10 * 10));
bytes1 b1 = bytes1(temp);
bstr[k] = b1;
_i /= 10;
}
return string(bstr);
}

string public insufficientFundsMessage = string.concat("A minimum of $", uint2str(minimumUsd), " is required");

Top comments (0)