Webhooks
An HTTP endpoint is generated for each Webhook Handler. The handler receives a WebhookEvent which contains information about the request (path, headers, etc), and the data returned by the handler is forwarded as the response.
Use cases
- Creating an HTTP API to interact with specific contracts.
- Serve a frontend directly from Solidity.
- Recursively register/remove other handler functions (of any type).
Examples
| function handle(WebhookEvent calldata ev) external returns(JsonObject memory) {
JsonObject memory data = json
.create(string(ev.body))
.expect("Failed to parse json body");
return json
.create()
.set("timestamp", ev.timestamp)
.set("data", data)
.set("path", ev.path);
}
|
Query a token's balance
| function handle(WebhookEvent calldata ev) external returns(JsonObject memory) {
JsonObject memory data = json
.create(string(ev.body))
.expect("Failed to parse json body");
IERC20 token = IERC20(data.getAddress("token"));
address wallet = data.getAddress("wallet");
return json.create().set("balance", token.balanceOf(wallet));
}
|