Im working on a smart contract in solidity and im having issues with Chainlink function execution. I have this source code for the function:
string private functionCode = "return Functions.encodeString('Hello, World!');";
Then i have this function for making the call:
function requestMatchResult(
string memory sport,
string memory eventId,
uint32 gasLimit
) external onlyOwner returns (bytes32) {
if (bytes(sport).length == 0) revert InvalidInput("Sport cannot be empty");
if (bytes(eventId).length == 0) revert InvalidInput("Event ID cannot be empty");
if (gasLimit < 100000) revert InvalidInput("Gas limit too low");
if (gasLimit > 500000) revert InvalidInput("Gas limit too high");
FunctionsRequest.Request memory req;
req.initializeRequestForInlineJavaScript(functionCode);
string[] memory args = new string[](2);
args[0] = sport;
args[1] = eventId;
req.setArgs(args);
bytes32 requestId = _sendRequest(
req.encodeCBOR(),
subscriptionId,
gasLimit,
donId
);
lastRequestId = requestId;
pendingRequests[requestId] = true;
emit RequestSent(requestId, sport, eventId);
return requestId;
}
And this is my fufillRequest:
function fulfillRequest(
bytes32 requestId,
bytes memory response,
bytes memory err
) internal override {
if (!pendingRequests[requestId]) {
revert UnexpectedRequestID(requestId);
}
pendingRequests[requestId] = false;
if (err.length > 0) {
string memory errorMsg = string(err);
requestResults[requestId] = string(abi.encodePacked("Error: ", errorMsg));
emit RequestFulfilled(requestId, "Error occurred");
return;
}
if (response.length > 0) {
string memory result = abi.decode(response, (string));
winner = result;
requestResults[requestId] = result;
emit RequestFulfilled(requestId, result);
} else {
winner = "No response";
requestResults[requestId] = "No response";
emit RequestFulfilled(requestId, "No response");
}
}
But i can never successfully make the call.
Looking at the chainlink dashboard i see this (the image) (https://ibb.co/Pv8VmmH9)
The image shows the chainlink functions console that the Computation was sucessfull and it shows Hello World!, but the callback failed, The on-chain callback failed.
This is how im calling the function from python:
def call_chainlink_function():
"""Call Chainlink function to get winner"""
txn = contract.functions.requestMatchResult(
"some_string",
"some_string",
100000
).build_transaction({
"from": account.address,
"nonce": web3.eth.get_transaction_count(account.address),
"gas": 300000,
"gasPrice": web3.eth.gas_price,
})
signed_txn = account.sign_transaction(txn)
tx_hash = web3.eth.send_raw_transaction(signed_txn.raw_transaction)
tx_receipt = web3.eth.wait_for_transaction_receipt(tx_hash)
if tx_receipt.status == 1:
last_request_id = contract.functions.lastRequestId().call()
request_id_hex = "0x" + last_request_id.hex()
for _ in range(30):
is_pending = contract.functions.isRequestPending(request_id_hex).call()
if not is_pending:
return True
time.sleep(10)
print("Request timeout after 5 minutes")
return False
return False
Calling other functions is okay (for example getting value of a string, setting value etc.)
I've already tried changing the gas value.