Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add input/output type constants to std-lib #2187

Merged
merged 18 commits into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions sway-lib-std/src/token.sw
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ pub fn force_transfer_to_contract(amount: u64, asset_id: ContractId, to: Contrac
/// Transfer `amount` coins of type `asset_id` and send them to
/// the address `to`.
pub fn transfer_to_output(amount: u64, asset_id: ContractId, to: Address) {
const OUTPUT_VARIABLE_TYPE: u8 = 4;

// maintain a manual index as we only have `while` loops in sway atm:
let mut index = 0;
let mut output_index = 0;
Expand All @@ -99,7 +97,7 @@ pub fn transfer_to_output(amount: u64, asset_id: ContractId, to: Address) {
// variable output with a value of zero is by definition unused.
let outputs_count = tx_outputs_count();
while index < outputs_count {
if tx_output_type(index) == OUTPUT_VARIABLE_TYPE && tx_output_amount(index) == 0 {
if tx_output_type(index) == OUTPUT_VARIABLE && tx_output_amount(index) == 0 {
output_index = index;
output_found = true;
break; // break early and use the output we found
Expand Down
13 changes: 13 additions & 0 deletions sway-lib-std/src/tx.sw
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,19 @@ const TX_WITNESSES_COUNT_OFFSET = 10312;
const TX_RECEIPTS_ROOT_OFFSET = 10320;
const TX_SCRIPT_START_OFFSET = 10352;

// Input types
const INPUT_COIN = 0u8;
const INPUT_CONTRACT = 1u8;
const INPUT_MESSAGE = 2u8;

// Output types
const OUTPUT_COIN = 0u8;
const OUTPUT_CONTRACT = 1u8;
adlerjohn marked this conversation as resolved.
Show resolved Hide resolved
const OUTPUT_MESSAGE = 2u8;
const OUTPUT_CHANGE = 3u8;
const OUTPUT_VARIABLE = 4u8;
const OUTPUT_CONTRACT_CREATED = 5u8;

/// Get the transaction type.
pub fn tx_type() -> u8 {
asm(r1, r2: TX_TYPE_OFFSET) {
Expand Down