Skip to content

Commit

Permalink
Try-out some C magical function pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
gabmontes committed Nov 20, 2022
1 parent 4916687 commit ebd7c8e
Showing 1 changed file with 15 additions and 19 deletions.
34 changes: 15 additions & 19 deletions src/handle_finalize.c
Original file line number Diff line number Diff line change
@@ -1,35 +1,33 @@
#include "vesper_plugin.h"
#include "vesper_metadata.h"

const pool_metadata_t *find_pool_metadata(uint8_t *pool_address) {
const pool_metadata_t *find_pool_metadata(const uint8_t *(*get_pool_prop)(const pool_metadata_t *),
uint8_t *address) {
uint8_t i;
for (i = 0; i < NUM_VESPER_POOLS; i++) {
const pool_metadata_t *current = &VESPER_POOLS[i];
if (memcmp(current->address, pool_address, ADDRESS_LENGTH) == 0) {
if (memcmp(get_pool_prop(current), address, ADDRESS_LENGTH) == 0) {
return current;
}
}
return NULL;
}

const pool_metadata_t *find_pool_rewards(uint8_t *rewards_address) {
uint8_t i;
for (i = 0; i < NUM_VESPER_POOLS; i++) {
const pool_metadata_t *current = &VESPER_POOLS[i];
if (memcmp(current->rewards, rewards_address, ADDRESS_LENGTH) == 0) {
return current;
}
}
return NULL;
const uint8_t *get_pool_address(const pool_metadata_t *pool) {
return pool->address;
}

const uint8_t *get_pool_rewards_address(const pool_metadata_t *pool) {
return pool->rewards;
}

void handle_finalize(void *parameters) {
ethPluginFinalize_t *msg = (ethPluginFinalize_t *) parameters;
context_t *context = (context_t *) msg->pluginContext;

uint8_t *destination = msg->pluginSharedRO->txContent->destination;
bool pool_data_found = false;
uint8_t *pool_address;
uint8_t *rewards_address;

switch (context->selectorIndex) {
case DEPOSIT:
case DEPOSIT_AND_CLAIM:
Expand All @@ -39,19 +37,17 @@ void handle_finalize(void *parameters) {
case WITHDRAW_AND_CLAIM:
case WITHDRAW_ETH:
case WITHDRAW_ETH_AND_CLAIM:
pool_address = msg->pluginSharedRO->txContent->destination;
context->pool_metadata = find_pool_metadata(pool_address);
context->pool_metadata = find_pool_metadata(get_pool_address, destination);
pool_data_found = context->pool_metadata != NULL;
break;
case CLAIM_REWARD:
rewards_address = msg->pluginSharedRO->txContent->destination;
context->pool_metadata = find_pool_rewards(rewards_address);
context->pool_metadata = find_pool_metadata(get_pool_rewards_address, destination);
pool_data_found = context->pool_metadata != NULL;
break;
case SIMPLE_MIGRATE:
case SIMPLE_MIGRATE_WITH_PERMIT:
context->pool_metadata = find_pool_metadata(context->pool_from);
context->pool_metadata_to = find_pool_metadata(context->pool_to);
context->pool_metadata = find_pool_metadata(get_pool_address, context->pool_from);
context->pool_metadata_to = find_pool_metadata(get_pool_address, context->pool_to);
pool_data_found = context->pool_metadata != NULL && context->pool_metadata_to != NULL;
break;
}
Expand Down

0 comments on commit ebd7c8e

Please sign in to comment.