Skip to content

Commit 08f59c1

Browse files
committed
refactor: Change make_nft_withdrawal_event() -> make_withdrawal_event() so that can handle fungible tokens also
1 parent 5ec836b commit 08f59c1

File tree

1 file changed

+48
-35
lines changed

1 file changed

+48
-35
lines changed

src/chainstate/stacks/db/blocks.rs

+48-35
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,51 @@ impl Streamer for MicroblockStreamData {
751751
}
752752
}
753753

754+
enum Token {
755+
Nft { id: u128 },
756+
Ft { amount: u128 },
757+
}
758+
759+
fn make_withdrawal_event(
760+
subnet_contract_id: QualifiedContractIdentifier,
761+
sender: PrincipalData,
762+
token: Token,
763+
mainnet: bool,
764+
) -> StacksTransactionEvent {
765+
let (withdrawal_type, mut values) = match token {
766+
Token::Nft { id } => ("nft", vec![("id".into(), Value::UInt(id))]),
767+
Token::Ft { amount } => ("ft", vec![("amount".into(), Value::UInt(amount))]),
768+
};
769+
770+
values.extend(
771+
vec![
772+
(
773+
"event".into(),
774+
Value::string_ascii_from_bytes("withdraw".into())
775+
.expect("Supplied string was not ASCII"),
776+
),
777+
(
778+
"type".into(),
779+
Value::string_ascii_from_bytes(withdrawal_type.into())
780+
.expect("Supplied string was not ASCII"),
781+
),
782+
("sender".into(), Value::Principal(sender)),
783+
(
784+
"asset-contract".into(),
785+
Value::Principal(PrincipalData::Contract(subnet_contract_id)),
786+
),
787+
]
788+
.into_iter(),
789+
);
790+
791+
StacksTransactionEvent::SmartContractEvent(SmartContractEventData {
792+
key: (boot_code_id("subnet", mainnet), "print".into()),
793+
value: TupleData::from_data(values)
794+
.expect("Failed to create tuple data.")
795+
.into(),
796+
})
797+
}
798+
754799
impl StacksChainState {
755800
fn get_index_block_pathbuf(blocks_dir: &str, index_block_hash: &StacksBlockId) -> PathBuf {
756801
let block_hash_bytes = index_block_hash.as_bytes();
@@ -4710,45 +4755,13 @@ impl StacksChainState {
47104755
.collect()
47114756
}
47124757

4713-
fn make_nft_withdrawal_event(
4714-
subnet_contract_id: QualifiedContractIdentifier,
4715-
sender: PrincipalData,
4716-
id: u128,
4717-
mainnet: bool,
4718-
) -> StacksTransactionEvent {
4719-
StacksTransactionEvent::SmartContractEvent(SmartContractEventData {
4720-
key: (boot_code_id("subnet", mainnet), "print".into()),
4721-
value: TupleData::from_data(vec![
4722-
(
4723-
"event".into(),
4724-
Value::string_ascii_from_bytes("withdraw".into())
4725-
.expect("Supplied string was not ASCII"),
4726-
),
4727-
(
4728-
"type".into(),
4729-
Value::string_ascii_from_bytes("nft".into())
4730-
.expect("Supplied string was not ASCII"),
4731-
),
4732-
("sender".into(), Value::Principal(sender)),
4733-
("id".into(), Value::UInt(id)),
4734-
(
4735-
"asset-contract".into(),
4736-
Value::Principal(PrincipalData::Contract(subnet_contract_id)),
4737-
),
4738-
])
4739-
.expect("Failed to create tuple data.")
4740-
.into(),
4741-
})
4742-
}
4743-
47444758
/// Process any deposit NFT operations that haven't been processed in this
47454759
/// subnet fork yet.
47464760
pub fn process_deposit_nft_ops(
47474761
clarity_tx: &mut ClarityTx,
47484762
operations: Vec<DepositNftOp>,
47494763
) -> Vec<StacksTransactionReceipt> {
47504764
let mainnet = clarity_tx.config.mainnet;
4751-
let boot_addr = PrincipalData::from(boot_code_addr(mainnet));
47524765
let cost_so_far = clarity_tx.cost_so_far();
47534766
// return valid receipts
47544767
operations
@@ -4764,7 +4777,7 @@ impl StacksChainState {
47644777
} = deposit_nft_op.clone();
47654778
let result = clarity_tx.connection().as_transaction(|tx| {
47664779
tx.run_contract_call(
4767-
&boot_addr,
4780+
&boot_code_addr(mainnet).into(),
47684781
None,
47694782
&subnet_contract_id,
47704783
DEPOSIT_FUNCTION_NAME,
@@ -4792,10 +4805,10 @@ impl StacksChainState {
47924805
// If deposit fails, create a withdrawal event to send NFT back to user
47934806
if deposit_op_failed {
47944807
info!("DepositNft op failed. Issue withdrawal tx");
4795-
events.push(Self::make_nft_withdrawal_event(
4808+
events.push(make_withdrawal_event(
47964809
subnet_contract_id,
47974810
sender,
4798-
id,
4811+
Token::Nft { id },
47994812
mainnet,
48004813
));
48014814
};

0 commit comments

Comments
 (0)