Skip to content

Commit

Permalink
action dutch auction schedule support
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Jan 23, 2025
1 parent dba6b2f commit cda92ee
Show file tree
Hide file tree
Showing 25 changed files with 8,372 additions and 6,287 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/position_open.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/position_close.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/position_withdraw.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/action_dutch_auction_schedule.c
)

add_library(app_lib STATIC ${LIB_SRC})
Expand Down
2 changes: 1 addition & 1 deletion app/rust/src/keys/payload_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl<'a> TryFrom<&'a [u8]> for PayloadKey<'a> {
}
}

impl<> TryFrom<PayloadKey<'_>> for Key {
impl TryFrom<PayloadKey<'_>> for Key {
type Error = ParserError;

fn try_from(slice: PayloadKey) -> Result<Self, Self::Error> {
Expand Down
5 changes: 2 additions & 3 deletions app/rust/src/parser/balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,11 @@ impl Balance {
let mut new_balance = self.clone();

for existing_imbalance in &mut new_balance.imbalances.iter_mut().flatten() {
if existing_imbalance.value.asset_id == rhs.asset_id
&& existing_imbalance.sign == sign
if existing_imbalance.value.asset_id == rhs.asset_id && existing_imbalance.sign == sign
{
existing_imbalance.value.amount = existing_imbalance.value.amount + rhs.amount;
return Ok(new_balance);
}
}
}

new_balance.insert(Imbalance {
Expand Down
6 changes: 6 additions & 0 deletions app/rust/src/parser/plans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,12 @@ pub unsafe extern "C" fn rs_generic_action_hash(
data_to_hash,
);
}
ActionPlan::ActionDutchAuctionSchedule => {
effect_hash = EffectHash::from_proto_effecting_data(
"/penumbra.core.component.auction.v1.ActionDutchAuctionSchedule",
data_to_hash,
);
}
_ => {
return ParserError::UnexpectedData as u32;
}
Expand Down
3 changes: 3 additions & 0 deletions app/src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,6 @@

// Constant to use to allocate a buffer on the stack to hold the formatting of an position_withdraw action
#define POSITION_WITHDRAW_DISPLAY_MAX_LEN 140 // = 140

// Constant to use to allocate a buffer on the stack to hold the formatting of an dutch_auction_schedule action
#define DUTCH_AUCTION_SCHEDULE_DISPLAY_MAX_LEN (4 * VALUE_DISPLAY_MAX_LEN + 154) // = 802
10 changes: 9 additions & 1 deletion app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <zxmacros.h>
#include <zxtypes.h>

#include "action_dutch_auction_schedule.h"
#include "coin.h"
#include "crypto.h"
#include "delegate.h"
Expand All @@ -39,7 +40,6 @@
#include "tx_metadata.h"
#include "undelegate.h"
#include "undelegate_claim.h"

static uint8_t action_idx = 0;

parser_error_t parser_init_context(parser_context_t *ctx, const uint8_t *buffer, uint16_t bufferSize) {
Expand Down Expand Up @@ -128,6 +128,9 @@ parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_item
case penumbra_core_transaction_v1_ActionPlan_position_withdraw_tag:
CHECK_ERROR(position_withdraw_getNumItems(ctx, &action_num_items));
break;
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_schedule_tag:
CHECK_ERROR(action_dutch_auction_schedule_getNumItems(ctx, &action_num_items));
break;
default:
return parser_unexpected_error;
}
Expand Down Expand Up @@ -240,6 +243,11 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
action_idx + 1, outKey, outKeyLen, outVal, outValLen, pageIdx,
pageCount))
break;
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_schedule_tag:
CHECK_ERROR(action_dutch_auction_schedule_getItem(
ctx, &ctx->tx_obj->actions_plan[action_idx].action.action_dutch_auction_schedule, action_idx + 1, outKey,
outKeyLen, outVal, outValLen, pageIdx, pageCount))
break;
default:
return parser_unexpected_error;
}
Expand Down
6 changes: 6 additions & 0 deletions app/src/parser_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "parser_impl.h"

#include "action_dutch_auction_schedule.h"
#include "delegate.h"
#include "delegator_vote.h"
#include "ics20_withdrawal.h"
Expand Down Expand Up @@ -133,6 +134,11 @@ bool decode_action(pb_istream_t *stream, const pb_field_t *field, void **arg) {
CHECK_ACTION_ERROR(
decode_position_withdraw_plan(&action_data_4, &decode_arg[actions_qty].action.position_withdraw));
break;
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_schedule_tag:
decode_arg[actions_qty].action_data = action_data_4;
CHECK_ACTION_ERROR(decode_action_dutch_auction_schedule_plan(
&action_data_4, &decode_arg[actions_qty].action.action_dutch_auction_schedule));
break;
default:
return false;
}
Expand Down
1 change: 1 addition & 0 deletions app/src/parser_interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ zxerr_t compute_action_hash(action_t *action, bytes_t *memo_key, hash_t *output)
case penumbra_core_transaction_v1_ActionPlan_undelegate_tag:
case penumbra_core_transaction_v1_ActionPlan_position_open_tag:
case penumbra_core_transaction_v1_ActionPlan_position_close_tag:
case penumbra_core_transaction_v1_ActionPlan_action_dutch_auction_schedule_tag:
if (rs_generic_action_hash(&action->action_data, action->action_type, (uint8_t *)output, 64) != parser_ok) {
return zxerr_encoding_failed;
}
Expand Down
21 changes: 21 additions & 0 deletions app/src/parser_txdef.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,21 @@ typedef struct {
bytes_t inner;
} position_id_t;

typedef struct {
bool has_input;
value_t input;
bool has_output_id;
asset_id_t output_id;
bool has_max_output;
amount_t max_output;
bool has_min_output;
amount_t min_output;
uint64_t start_height;
uint64_t end_height;
uint64_t step_count;
bytes_t nonce;
} dutch_auction_description_t;

typedef struct {
note_t note;
uint64_t position;
Expand Down Expand Up @@ -306,6 +321,11 @@ typedef struct {
uint8_t rewards_qty;
} position_withdraw_plan_t;

typedef struct {
bool has_description;
dutch_auction_description_t description;
} action_dutch_auction_schedule_plan_t;

typedef struct {
address_plan_t return_address;
bytes_t text;
Expand Down Expand Up @@ -341,6 +361,7 @@ typedef struct {
position_open_plan_t position_open;
position_close_plan_t position_close;
position_withdraw_plan_t position_withdraw;
action_dutch_auction_schedule_plan_t action_dutch_auction_schedule;
} action;
} action_t;

Expand Down
207 changes: 207 additions & 0 deletions app/src/plan/action_dutch_auction_schedule.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,207 @@
/*******************************************************************************
* (c) 2018 - 2023 Zondax AG
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include "action_dutch_auction_schedule.h"

#include "note.h"
#include "parser_pb_utils.h"
#include "ui_utils.h"
#include "zxformat.h"

parser_error_t decode_action_dutch_auction_schedule_plan(
const bytes_t *data, action_dutch_auction_schedule_plan_t *action_dutch_auction_schedule) {
penumbra_core_component_auction_v1_ActionDutchAuctionSchedule action_dutch_auction_schedule_pb =
penumbra_core_component_auction_v1_ActionDutchAuctionSchedule_init_default;

pb_istream_t stream = pb_istream_from_buffer(data->ptr, data->len);
CHECK_APP_CANARY()

// Set up fixed size fields
fixed_size_field_t nonce_arg, input_asset_id_arg, output_asset_id_arg;

setup_decode_fixed_field(&action_dutch_auction_schedule_pb.description.nonce, &nonce_arg,
&action_dutch_auction_schedule->description.nonce, 32);
setup_decode_fixed_field(&action_dutch_auction_schedule_pb.description.input.asset_id.inner, &input_asset_id_arg,
&action_dutch_auction_schedule->description.input.asset_id.inner, ASSET_ID_LEN);
setup_decode_fixed_field(&action_dutch_auction_schedule_pb.description.output_id.inner, &output_asset_id_arg,
&action_dutch_auction_schedule->description.output_id.inner, ASSET_ID_LEN);

if (!pb_decode(&stream, penumbra_core_component_auction_v1_ActionDutchAuctionSchedule_fields,
&action_dutch_auction_schedule_pb)) {
return parser_output_plan_error;
}

action_dutch_auction_schedule->has_description = action_dutch_auction_schedule_pb.has_description;
if (action_dutch_auction_schedule_pb.has_description) {
action_dutch_auction_schedule->description.has_input = action_dutch_auction_schedule_pb.description.has_input;
if (action_dutch_auction_schedule->description.has_input) {
action_dutch_auction_schedule->description.input.has_amount =
action_dutch_auction_schedule_pb.description.input.has_amount;
if (action_dutch_auction_schedule_pb.description.input.has_amount) {
action_dutch_auction_schedule->description.input.amount.hi =
action_dutch_auction_schedule_pb.description.input.amount.hi;
action_dutch_auction_schedule->description.input.amount.lo =
action_dutch_auction_schedule_pb.description.input.amount.lo;
}
action_dutch_auction_schedule->description.input.has_asset_id =
action_dutch_auction_schedule_pb.description.input.has_asset_id;
}
action_dutch_auction_schedule->description.has_output_id =
action_dutch_auction_schedule_pb.description.has_output_id;
action_dutch_auction_schedule->description.has_max_output =
action_dutch_auction_schedule_pb.description.has_max_output;
if (action_dutch_auction_schedule_pb.description.has_max_output) {
action_dutch_auction_schedule->description.max_output.hi =
action_dutch_auction_schedule_pb.description.max_output.hi;
action_dutch_auction_schedule->description.max_output.lo =
action_dutch_auction_schedule_pb.description.max_output.lo;
}
action_dutch_auction_schedule->description.has_min_output =
action_dutch_auction_schedule_pb.description.has_min_output;
if (action_dutch_auction_schedule_pb.description.has_min_output) {
action_dutch_auction_schedule->description.min_output.hi =
action_dutch_auction_schedule_pb.description.min_output.hi;
action_dutch_auction_schedule->description.min_output.lo =
action_dutch_auction_schedule_pb.description.min_output.lo;
}
action_dutch_auction_schedule->description.start_height = action_dutch_auction_schedule_pb.description.start_height;
action_dutch_auction_schedule->description.end_height = action_dutch_auction_schedule_pb.description.end_height;
action_dutch_auction_schedule->description.step_count = action_dutch_auction_schedule_pb.description.step_count;
}

return parser_ok;
}

parser_error_t action_dutch_auction_schedule_getNumItems(const parser_context_t *ctx, uint8_t *num_items) {
UNUSED(ctx);
*num_items = 1;
return parser_ok;
}

parser_error_t action_dutch_auction_schedule_getItem(
const parser_context_t *ctx, const action_dutch_auction_schedule_plan_t *action_dutch_auction_schedule,
uint8_t actionIdx, char *outKey, uint16_t outKeyLen, char *outVal, uint16_t outValLen, uint8_t pageIdx,
uint8_t *pageCount) {
parser_error_t err = parser_no_data;
if (action_dutch_auction_schedule == NULL || outKey == NULL || outVal == NULL || outKeyLen == 0 || outValLen == 0) {
return err;
}

char bufferUI[DUTCH_AUCTION_SCHEDULE_DISPLAY_MAX_LEN] = {0};

snprintf(outKey, outKeyLen, "Action_%d", actionIdx);
CHECK_ERROR(action_dutch_auction_schedule_printValue(ctx, action_dutch_auction_schedule, bufferUI, sizeof(bufferUI)));
pageString(outVal, outValLen, bufferUI, pageIdx, pageCount);

return parser_ok;
}

parser_error_t action_dutch_auction_schedule_printValue(
const parser_context_t *ctx, const action_dutch_auction_schedule_plan_t *action_dutch_auction_schedule, char *outVal,
uint16_t outValLen) {
if (ctx == NULL || action_dutch_auction_schedule == NULL || outVal == NULL) {
return parser_no_data;
}

if (outValLen < DUTCH_AUCTION_SCHEDULE_DISPLAY_MAX_LEN) {
return parser_unexpected_buffer_end;
}

MEMZERO(outVal, outValLen);

// add action title
snprintf(outVal, outValLen, "DutchAuctionSchedule Selling: ");
uint16_t written_value = strlen(outVal);

// Selling value
CHECK_ERROR(printValue(ctx, &action_dutch_auction_schedule->description.input, &ctx->tx_obj->parameters_plan.chain_id,
true, outVal + written_value, outValLen - written_value));
written_value = strlen(outVal);

snprintf(outVal + written_value, outValLen - written_value, " For: ");
written_value = strlen(outVal);

// for asset
value_t for_asset = {.amount = 0,
.asset_id.inner = {.ptr = action_dutch_auction_schedule->description.output_id.inner.ptr,
.len = action_dutch_auction_schedule->description.output_id.inner.len},
.has_amount = false,
.has_asset_id = true};
CHECK_ERROR(printValue(ctx, &for_asset, &ctx->tx_obj->parameters_plan.chain_id, true, outVal + written_value,
outValLen - written_value));
written_value = strlen(outVal);

// Format starting price
snprintf(outVal + written_value, outValLen - written_value, " Starting price: ");
written_value = strlen(outVal);

value_t start_price_1 = {.amount = action_dutch_auction_schedule->description.max_output,
.asset_id.inner = {.ptr = action_dutch_auction_schedule->description.output_id.inner.ptr,
.len = action_dutch_auction_schedule->description.output_id.inner.len},
.has_amount = true,
.has_asset_id = true};
CHECK_ERROR(printValue(ctx, &start_price_1, &ctx->tx_obj->parameters_plan.chain_id, false, outVal + written_value,
outValLen - written_value));
written_value = strlen(outVal);

snprintf(outVal + written_value, outValLen - written_value, " for ");
written_value = strlen(outVal);

CHECK_ERROR(printValue(ctx, &action_dutch_auction_schedule->description.input, &ctx->tx_obj->parameters_plan.chain_id,
false, outVal + written_value, outValLen - written_value));
written_value = strlen(outVal);

// Format ending price
snprintf(outVal + written_value, outValLen - written_value, " Ending price: ");
written_value = strlen(outVal);

value_t end_price_1 = {.amount = action_dutch_auction_schedule->description.min_output,
.asset_id.inner = {.ptr = action_dutch_auction_schedule->description.output_id.inner.ptr,
.len = action_dutch_auction_schedule->description.output_id.inner.len},
.has_amount = true,
.has_asset_id = true};
CHECK_ERROR(printValue(ctx, &end_price_1, &ctx->tx_obj->parameters_plan.chain_id, false, outVal + written_value,
outValLen - written_value));
written_value = strlen(outVal);

snprintf(outVal + written_value, outValLen - written_value, " for ");
written_value = strlen(outVal);

CHECK_ERROR(printValue(ctx, &action_dutch_auction_schedule->description.input, &ctx->tx_obj->parameters_plan.chain_id,
false, outVal + written_value, outValLen - written_value));
written_value = strlen(outVal);

// Start block height
snprintf(outVal + written_value, outValLen - written_value, " Start block height: ");
written_value = strlen(outVal);
snprintf(outVal + written_value, outValLen - written_value, "%llu",
action_dutch_auction_schedule->description.start_height);
written_value = strlen(outVal);

// End block height
snprintf(outVal + written_value, outValLen - written_value, " End block height: ");
written_value = strlen(outVal);
snprintf(outVal + written_value, outValLen - written_value, "%llu",
action_dutch_auction_schedule->description.end_height);
written_value = strlen(outVal);

// Step count
snprintf(outVal + written_value, outValLen - written_value, " Steps: ");
written_value = strlen(outVal);
snprintf(outVal + written_value, outValLen - written_value, "%llu",
action_dutch_auction_schedule->description.step_count);

return parser_ok;
}
Loading

0 comments on commit cda92ee

Please sign in to comment.