Encoder
This exercise assumes you know how abi encoding works.
-
In the
createEncodedData
function below, write the logic that creates an encoded data of astring
anduint256
, based on the function parameters -
Assign the encoded data to the
encoded
state variable and return it
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// SPDX-License-Identifier: BUSL-1.1
pragma solidity ^0.8.13;
contract Encoder {
/* This exercise assumes you know how abi encoding works.
1. In the `createEncodedData` function below, write the logic that creates an encoded data of a `string` and `uint256`, based on the function parameters
2. Assign the encoded data to the `encoded` state variable and return it
*/
bytes public encoded;
function createEncodedData(
string memory _word,
uint256 _num
) public returns (bytes memory) {
}
}
Coding
1
2
3
4
5
6
7
function createEncodedData(
string memory _word,
uint256 _num
) public returns (bytes memory) {
encoded = abi.encode(_word, _num);
return encoded;
}
Test
1
2
3
4
5
6
7
8
9
10
11
➜ Encoder git:(main) ✗ forge test -vvv
[⠊] Compiling...
[⠊] Compiling 19 files with Solc 0.8.25
[⠒] Solc 0.8.25 finished in 920.57ms
Compiler run successful!
Ran 1 test for test/Encoder.t.sol:EncoderTest
[PASS] testEncodedData() (gas: 157766)
Suite result: ok. 1 passed; 0 failed; 0 skipped; finished in 4.72ms (399.54µs CPU time)
Ran 1 test suite in 205.15ms (4.72ms CPU time): 1 tests passed, 0 failed, 0 skipped (1 total tests)
Reference: Solidity Exercise - Encoder