Skip to content

Commit

Permalink
add isc20 ui
Browse files Browse the repository at this point in the history
  • Loading branch information
abenso committed Dec 11, 2024
1 parent c5d366a commit 97a57a2
Show file tree
Hide file tree
Showing 7 changed files with 763 additions and 769 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ file(GLOB_RECURSE LIB_SRC
${CMAKE_CURRENT_SOURCE_DIR}/app/src/memo.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/swap.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/c_api/rust.c
${CMAKE_CURRENT_SOURCE_DIR}/app/src/plan/ics20_withdrawal.c
)

add_library(app_lib STATIC ${LIB_SRC})
Expand Down
6 changes: 5 additions & 1 deletion app/src/constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,8 @@

// Constant to use to allocate a buffer on the stack to hold the formatting of an swap action
#define SWAP_DISPLAY_MAX_LEN \
(2*VALUE_DISPLAY_MAX_LEN + SHORT_ADDRESS_VISIBLE_CHARS + sizeof(ELLIPSIS) + 6) // = 355
(2*VALUE_DISPLAY_MAX_LEN + SHORT_ADDRESS_VISIBLE_CHARS + sizeof(ELLIPSIS) + 6) // = 355

// Constant to use to allocate a buffer on the stack to hold the formatting of an ics20 withdrawal action
#define ICS20_WITHDRAWAL_DISPLAY_MAX_LEN \
(VALUE_DISPLAY_MAX_LEN + 300 + 36) // = 498 -> 300 bytes for the channel and destination address
8 changes: 8 additions & 0 deletions app/src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include "tx_metadata.h"
#include "memo.h"
#include "swap.h"
#include "ics20_withdrawal.h"

static uint8_t action_idx = 0;

Expand Down Expand Up @@ -90,6 +91,9 @@ parser_error_t parser_getNumItems(const parser_context_t *ctx, uint8_t *num_item
case penumbra_core_transaction_v1_ActionPlan_swap_tag:
CHECK_ERROR(swap_getNumItems(ctx, &action_num_items));
break;
case penumbra_core_transaction_v1_ActionPlan_ics20_withdrawal_tag:
CHECK_ERROR(ics20_withdrawal_getNumItems(ctx, &action_num_items));
break;
default:
return parser_unexpected_error;
}
Expand Down Expand Up @@ -160,6 +164,10 @@ parser_error_t parser_getItem(const parser_context_t *ctx, uint8_t displayIdx, c
CHECK_ERROR(swap_getItem(ctx, &ctx->tx_obj->actions_plan[action_idx].action.swap, 0, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount))
break;
case penumbra_core_transaction_v1_ActionPlan_ics20_withdrawal_tag:
CHECK_ERROR(ics20_withdrawal_getItem(ctx, &ctx->tx_obj->actions_plan[action_idx].action.ics20_withdrawal, 0, outKey, outKeyLen,
outVal, outValLen, pageIdx, pageCount))
break;
default:
return parser_unexpected_error;
}
Expand Down
78 changes: 78 additions & 0 deletions app/src/plan/ics20_withdrawal.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
********************************************************************************/
#include "ics20_withdrawal.h"

#include "parser_pb_utils.h"
#include "zxformat.h"
Expand Down Expand Up @@ -57,3 +58,80 @@ parser_error_t decode_ics20_withdrawal_plan(const bytes_t *data, ics20_withdrawa

return parser_ok;
}


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

parser_error_t ics20_withdrawal_getItem(const parser_context_t *ctx, const ics20_withdrawal_plan_t *ics20_withdrawal,
uint8_t displayIdx, char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen, uint8_t pageIdx,
uint8_t *pageCount) {

parser_error_t err = parser_no_data;
if (ics20_withdrawal == NULL || outKey == NULL || outVal == NULL || outKeyLen == 0 || outValLen == 0) {
return err;
}


if (displayIdx != 0) {
return parser_no_data;
}

char bufferUI[ICS20_WITHDRAWAL_DISPLAY_MAX_LEN] = {0};

snprintf(outKey, outKeyLen, "Action");
CHECK_ERROR(ics20_withdrawal_printValue(ctx, ics20_withdrawal, bufferUI, sizeof(bufferUI)));
pageString(outVal, outValLen, bufferUI, pageIdx, pageCount);

return parser_ok;

}

parser_error_t ics20_withdrawal_printValue(const parser_context_t *ctx, const ics20_withdrawal_plan_t *ics20_withdrawal, char *outVal, uint16_t outValLen) {
if (ctx == NULL || ics20_withdrawal == NULL || outVal == NULL) {
return parser_no_data;
}

if (outValLen < ICS20_WITHDRAWAL_DISPLAY_MAX_LEN) {
return parser_unexpected_buffer_end;
}

MEMZERO(outVal, outValLen);

// add action title
uint16_t written_value = snprintf(outVal, outValLen, "ICS20Withdrawal ");

// add "channel"
snprintf(outVal + written_value, outValLen - written_value, "Channel ");
written_value = strlen(outVal);

MEMCPY(outVal + written_value, ics20_withdrawal->source_channel.ptr, ics20_withdrawal->source_channel.len);
written_value += ics20_withdrawal->source_channel.len;

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

uint8_t asset_id_bytes[ASSET_ID_LEN] = {0};
rs_get_asset_id_from_metadata(&ics20_withdrawal->denom.inner, asset_id_bytes, ASSET_ID_LEN);

value_t ics20_withdrawal_value = {0};
ics20_withdrawal_value.amount.hi = ics20_withdrawal->amount.hi;
ics20_withdrawal_value.amount.lo = ics20_withdrawal->amount.lo;
ics20_withdrawal_value.asset_id.inner.ptr = asset_id_bytes;
ics20_withdrawal_value.asset_id.inner.len = ASSET_ID_LEN;
ics20_withdrawal_value.has_amount = true;
ics20_withdrawal_value.has_asset_id = true;
CHECK_ERROR(printValue(ctx, &ics20_withdrawal_value, &ctx->tx_obj->parameters_plan.chain_id, outVal + written_value, outValLen - written_value));
written_value = strlen(outVal);

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

MEMCPY(outVal + written_value, ics20_withdrawal->destination_chain_address.ptr, ics20_withdrawal->destination_chain_address.len);

return parser_ok;
}
7 changes: 7 additions & 0 deletions app/src/plan/ics20_withdrawal.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ extern "C" {
#endif

parser_error_t decode_ics20_withdrawal_plan(const bytes_t *data, ics20_withdrawal_plan_t *withdrawal);
parser_error_t ics20_withdrawal_getNumItems(const parser_context_t *ctx, uint8_t *num_items);
parser_error_t ics20_withdrawal_getItem(const parser_context_t *ctx, const ics20_withdrawal_plan_t *ics20_withdrawal,
uint8_t displayIdx, char *outKey, uint16_t outKeyLen,
char *outVal, uint16_t outValLen, uint8_t pageIdx,
uint8_t *pageCount);

parser_error_t ics20_withdrawal_printValue(const parser_context_t *ctx, const ics20_withdrawal_plan_t *ics20_withdrawal, char *outVal, uint16_t outValLen);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion app/src/plan/swap.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ parser_error_t swap_printValue(const parser_context_t *ctx, const swap_plan_t *s
return parser_no_data;
}

if (outValLen < OUTPUT_DISPLAY_MAX_LEN) {
if (outValLen < SWAP_DISPLAY_MAX_LEN) {
return parser_unexpected_buffer_end;
}

Expand Down
Loading

0 comments on commit 97a57a2

Please sign in to comment.