我有一个从ChainlinkClient继承来向API发送请求的契约,该契约有三个主要功能:
function onTokenTransfer(
address _sender,
uint256 _fee,
bytes calldata _data
) public {
require(_fee >= fee, "NOT ENOUGH FUNDS");
bytes memory data = _data[4:];
uint256 _id = abi.decode(data, (uint256));
requestVolumeData(_id);
}function requestVolumeData(uint256 _id) public returns (bytes32 requestId) {
Chainlink.Request memory req = buildChainlinkRequest(
jobId,
address(this),
this.fulfill.selector
);
// do somthing with _id
req.add(
"get",
"https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD"
);
req.add("path", "RAW,ETH,USD,VOLUME24HOUR");
int256 timesAmount = 10 ** 18;
req.addInt("times", timesAmount);
return sendChainlinkRequest(req, fee);
}function fulfill(
bytes32 _requestId,
uint256 _volume
) public recordChainlinkFulfillment(_requestId) {
emit RequestVolume(_requestId, _volume);
volume = _volume;
}当用户使用link.transferAndCall使用_id调用我的契约时,onTokenTransfer函数使用相同的_id调用requestVolumeData,该函数向节点发出请求,节点将请求的数据返回给fulfill函数。
My的问题是:如何在实现函数中获得requestVolumeData中使用的_id的值?
我的结构和(id=>struct)映射:
struct Collab {
address promoter;
address client;
string apiUrl;
uint256 amount;
}
mapping(uint256 => Collab) public collabById;发布于 2023-02-03 10:54:49
您可以将_id存储到以下状态变量中:
uint256 id;
function requestVolumeData(uint256 _id) public returns (bytes32 requestId) {
Collab memory collab;
// You can set the values that you are having
collab.apiUrl = "https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD";
collab.promoter = msg.sender; // Or any other address that goes here
collab.client = msg.sender; // Or any other address that goes here
collab.amount = 0; // Setting this 0 for now, you can leave that too because by-default it's 0 only
id = _id;
collabById[id] = collab;
Chainlink.Request memory req = buildChainlinkRequest(
jobId,
address(this),
this.fulfill.selector
);
// do somthing with _id
req.add(
"get",
"https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD"
);
req.add("path", "RAW,ETH,USD,VOLUME24HOUR");
int256 timesAmount = 10 ** 18;
req.addInt("times", timesAmount);
return sendChainlinkRequest(req, fee);
}然后,在fulfill()函数中,可以使用id状态变量访问最新的D4值:
function fulfill(
bytes32 _requestId,
uint256 _volume
) public recordChainlinkFulfillment(_requestId) {
emit RequestVolume(_requestId, _volume);
volume = _volume;
// You can set the remaining values of the mapping struct here
collabById[id].amount = volume;
}https://ethereum.stackexchange.com/questions/144199
复制相似问题