Skip to content

Commit 156b626

Browse files
committed
add stringToUint256 function to convert string responses to uint256 and update response handling in CheckBalance contract
1 parent bb58153 commit 156b626

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/CheckBalance.sol

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,13 +78,28 @@ contract CheckBalance is FunctionsClient, ConfirmedOwner {
7878
revert UnexpectedRequestID(requestId); // Check if request IDs match
7979
}
8080
// Update the contract's state variables with the response and any errors
81-
s_lastResponse = abi.decode(response, (uint256));
81+
string memory responseString = abi.decode(response, (string));
82+
s_lastResponse = stringToUint256(responseString);
8283
s_lastError = err;
8384

8485
// Emit an event to log the response
8586
emit Response(requestId, s_lastResponse, s_lastError);
8687
}
8788

89+
function stringToUint256(
90+
string memory s
91+
) public pure returns (uint256 result) {
92+
bytes memory b = bytes(s);
93+
uint256 i;
94+
result = 0;
95+
for (i = 0; i < b.length; i++) {
96+
uint256 c = uint256(uint8(b[i]));
97+
if (c >= 48 && c <= 57) {
98+
result = result * 10 + (c - 48);
99+
}
100+
}
101+
}
102+
88103
function getResponse() external view returns (uint256) {
89104
return s_lastResponse;
90105
}

0 commit comments

Comments
 (0)