If this is a challenge, you should try to tackle it yourself. I understand that maybe you don't understand the problem well, and I will clarify it to you, then I will spell out the logic and then I'll show you a solution.
But when I clarify what you need to do, try to solve it yourself. If you can't, then check the logic I will show you. If you still can't figure it out, then check my solution:
Clarification
They want you to create a contract that has a function "with no function selector" (meaning, it's the fallback
function). That function should receive calldata
parameter of length 32 bytesthat will contain data that you need to treat as a
uint256number (by decoding it). Let's call this number
n`.
Then, you need to calculate the Fibonacci sequence up to that n
number and return it.
As we know, the Fibonacci sequence is as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, ...
The first is 0
, the second is 1
, the third is 1
, and the fourth is 2
. Basically, the Fibonacci sequence is a sequence of numbers where each number is composed of the sum of the previous 2 numbers, starting with 0, 1
.
Compile it (maybe with Remix), take the runtime bytecode, and submit it.
Check the fallback
function docs: https://docs.soliditylang.org/en/v0.8.14/contracts.html?highlight=fallback#fallback-function
Check the abi.encode
, abi.encodePacked
and abi.decode
functions docs: https://docs.soliditylang.org/en/v0.8.15/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions
Think about it and try to solve it yourself now.
Logic
Implement the following fallback
function:
js fallback(bytes calldata _input) external returns(bytes memory output)
Inside it, decode the _input
as a uint256
type (the uint256
type has 32 bytes), in a variable called n
.
Calculate the Fibonacci sequence up to n
.
Return the result encoding it as bytes (with abi.encode
or abi.encodePacked
).
Compile it (maybe with Remix), take the runtime bytecode, and submit it.
That's it!
Try to solve it yourself.
If you can't, then take a look at my solution below.
Solution
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
contract Fibonacci {
uint256 public input;
uint256 public result;
fallback(bytes calldata _input) external returns(bytes memory output) {
(uint256 n) = abi.decode(_input, (uint256));
uint256 v1 = 0;
uint256 v2 = 1;
for (uint256 i = 0; i < n - 1; i++) {
v2 = v1 + v2;
v1 = v2 - v1;
}
input = n; // No need to do this, just for debugging.
result = v2; // No need to do this, just for debugging.
// If called from another contract, the contract will receive this as the bytes response
return abi.encode(v2);
}
}
Compile it (maybe with Remix), take the runtime bytecode, and submit it. In my case, it's:
608060405234801561001057600080fd5b506004361061003a5760003560e01c806365372147146100df578063eaed3f4f146100fd5761003b565b5b6000366060600083838101906100519190610162565b90506000806001905060005b60018461006a91906101be565b8110156100a057818361007d91906101f2565b9150828261008b91906101be565b9250808061009890610226565b91505061005d565b508260008190555080600181905550806040516020016100c0919061027d565b6040516020818303038152906040529350505050915050805190602001f35b6100e761011b565b6040516100f4919061027d565b60405180910390f35b610105610121565b604051610112919061027d565b60405180910390f35b60015481565b60005481565b600080fd5b6000819050919050565b61013f8161012c565b811461014a57600080fd5b50565b60008135905061015c81610136565b92915050565b60006020828403121561017857610177610127565b5b60006101868482850161014d565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006101c98261012c565b91506101d48361012c565b92508282039050818111156101ec576101eb61018f565b5b92915050565b60006101fd8261012c565b91506102088361012c565b92508282019050808211156102205761021f61018f565b5b92915050565b60006102318261012c565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82036102635761026261018f565b5b600182019050919050565b6102778161012c565b82525050565b6000602082019050610292600083018461026e565b9291505056fea26469706673582212200a07d1acb375662d0b29a5f80dc05e9effdf73118008d05c193b7417094b1c8064736f6c63430008100033
In Remix, click on Compilation Details and click on Runtime Bytecode:

And get it:

There are many ways to solve the Fibonacci sequence. The solution that I used is very efficient in terms of time and space complexity. This solution uses little gas. Solving it with recursion in Solidity would be really gas expensive. You can try other solutions if you want.