1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use crate::num_bigint::BigUint;
use dharitri_sc::{
    api::DCT_NFT_ADD_QUANTITY_FUNC_NAME,
    codec::{top_encode_to_vec_u8, TopDecode},
};

use crate::{
    tx_mock::{BlockchainUpdate, TxCache, TxInput, TxLog, TxResult},
    world_mock::DctInstanceMetadata,
};

use super::super::builtin_func_trait::BuiltinFunction;

pub struct DCTNftAddQuantity;

impl BuiltinFunction for DCTNftAddQuantity {
    fn name(&self) -> &str {
        DCT_NFT_ADD_QUANTITY_FUNC_NAME
    }

    fn execute(&self, tx_input: TxInput, tx_cache: TxCache) -> (TxResult, BlockchainUpdate) {
        if tx_input.args.len() != 3 {
            let err_result = TxResult::from_vm_error("DCTNFTAddQuantity expects 3 arguments");
            return (err_result, BlockchainUpdate::empty());
        }

        let token_identifier = tx_input.args[0].clone();
        let nonce = u64::top_decode(tx_input.args[1].as_slice()).unwrap();
        let value = BigUint::from_bytes_be(tx_input.args[2].as_slice());

        tx_cache.increase_dct_balance(
            &tx_input.to,
            &token_identifier,
            nonce,
            &value,
            DctInstanceMetadata::default(),
        );

        let dct_nft_create_log = TxLog {
            address: tx_input.from,
            endpoint: DCT_NFT_ADD_QUANTITY_FUNC_NAME.into(),
            topics: vec![
                token_identifier.to_vec(),
                top_encode_to_vec_u8(&nonce).unwrap(),
                value.to_bytes_be(),
            ],
            data: vec![],
        };

        let tx_result = TxResult {
            result_status: 0,
            result_logs: vec![dct_nft_create_log],
            ..Default::default()
        };

        (tx_result, tx_cache.into_blockchain_updates())
    }
}