Skip to content

Commit 2105863

Browse files
committed
add CheckBalance contract for querying token balances via Chainlink for other chain token balances
1 parent f2e3a41 commit 2105863

File tree

1 file changed

+95
-0
lines changed

1 file changed

+95
-0
lines changed

src/CheckBalance.sol

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.26;
3+
4+
import {FunctionsClient} from "@chainlink/contracts/src/v0.8/functions/v1_0_0/FunctionsClient.sol";
5+
import {ConfirmedOwner} from "@chainlink/contracts/src/v0.8/shared/access/ConfirmedOwner.sol";
6+
import {FunctionsRequest} from "@chainlink/contracts/src/v0.8/functions/v1_0_0/libraries/FunctionsRequest.sol";
7+
8+
contract CheckBalance is FunctionsClient, ConfirmedOwner {
9+
using FunctionsRequest for FunctionsRequest.Request;
10+
11+
bytes32 private s_lastRequestId;
12+
uint256 private s_lastResponse;
13+
bytes private s_lastError;
14+
uint32 private s_gasLimit = 300000;
15+
bytes32 private s_donID;
16+
string private s_chainBaseUrl;
17+
address private s_tokenAddress;
18+
address private s_subscriber;
19+
string private s_source =
20+
"const chainBaseUrl = args[0];"
21+
"const tokenAddress = args[1];"
22+
"const subscriber = args[2];"
23+
"const apiResponse = await Functions.makeHttpRequest({"
24+
" url: `https://${chainBaseUrl}//api?module=account&action=tokenbalance&contractaddress=${tokenAddress}&address=${subscriber}`"
25+
"});"
26+
"if (apiResponse.error) {"
27+
"throw Error('Request failed');"
28+
"}"
29+
"const { data } = apiResponse;"
30+
"return Functions.encodeString(data.result);";
31+
32+
error UnexpectedRequestID(bytes32 requestId);
33+
34+
event Response(bytes32 indexed requestId, uint256 response, bytes err);
35+
36+
constructor(
37+
string memory _chainBaseUrl,
38+
address _tokenAddress,
39+
address _subscriber,
40+
address router,
41+
bytes32 donID
42+
) FunctionsClient(router) ConfirmedOwner(msg.sender) {
43+
s_chainBaseUrl = _chainBaseUrl;
44+
s_tokenAddress = _tokenAddress;
45+
s_subscriber = _subscriber;
46+
s_donID = donID;
47+
}
48+
49+
/*//////////////////////////////////////////////////////////////
50+
OVERRIDE FUNCTIONS
51+
//////////////////////////////////////////////////////////////*/
52+
53+
function sendRequest(
54+
uint64 subscriptionId,
55+
string[] calldata args
56+
) external onlyOwner returns (bytes32 requestId) {
57+
FunctionsRequest.Request memory req;
58+
req.initializeRequestForInlineJavaScript(s_source); // Initialize the request with JS code
59+
if (args.length > 0) req.setArgs(args); // Set the arguments for the request
60+
61+
// Send the request and store the request ID
62+
s_lastRequestId = _sendRequest(
63+
req.encodeCBOR(),
64+
subscriptionId,
65+
s_gasLimit,
66+
s_donID
67+
);
68+
69+
return s_lastRequestId;
70+
}
71+
72+
function fulfillRequest(
73+
bytes32 requestId,
74+
bytes memory response,
75+
bytes memory err
76+
) internal override {
77+
if (s_lastRequestId != requestId) {
78+
revert UnexpectedRequestID(requestId); // Check if request IDs match
79+
}
80+
// Update the contract's state variables with the response and any errors
81+
s_lastResponse = abi.decode(response, (uint256));
82+
s_lastError = err;
83+
84+
// Emit an event to log the response
85+
emit Response(requestId, s_lastResponse, s_lastError);
86+
}
87+
88+
function getResponse() external view returns (uint256) {
89+
return s_lastResponse;
90+
}
91+
92+
function getLastError() external view returns (bytes memory) {
93+
return s_lastError;
94+
}
95+
}

0 commit comments

Comments
 (0)