pchain_sdk/
transaction.rs

1/*
2    Copyright © 2023, ParallelChain Lab 
3    Licensed under the Apache License, Version 2.0: http://www.apache.org/licenses/LICENSE-2.0
4*/
5 
6//! Defines functions for getting information about the Transaction that triggered this call, e.g. the
7//! calling account (Transaction's `signer`) and `transaction_hash`, etc.
8
9use crate::imports;
10
11/// Get the address of this contract call
12pub fn calling_account() -> [u8;32] {
13    let mut args_ptr: u32 = 0;
14    let args_ptr_ptr = &mut args_ptr;
15    
16    let arguments =
17    unsafe {
18        imports::calling_account(args_ptr_ptr);
19        Vec::<u8>::from_raw_parts(args_ptr as *mut u8, 32, 32)
20    };
21    TryInto::<[u8;32]>::try_into(arguments).unwrap()
22}
23
24/// Get current address (equivalent to this contract address)
25pub fn current_account() -> [u8;32] {
26    let mut args_ptr: u32 = 0;
27    let args_ptr_ptr = &mut args_ptr;
28
29    let arguments =
30    unsafe {
31        imports::current_account(args_ptr_ptr);
32        Vec::<u8>::from_raw_parts(args_ptr as *mut u8, 32, 32)
33    };
34    TryInto::<[u8;32]>::try_into(arguments).unwrap()
35}
36
37/// Get transferring amount in this contract call
38pub fn amount() -> u64 {
39    unsafe { imports::amount() }
40}
41
42/// Returns whether it is an internal call
43pub fn is_internal_call() -> bool {
44    unsafe { imports::is_internal_call() != 0 }
45}
46
47/// Get transaction hash of this contract call
48pub fn transaction_hash() -> [u8;32] {
49    let mut args_ptr: u32 = 0;
50    let args_ptr_ptr = &mut args_ptr;
51
52    let arguments =
53    unsafe {
54        imports::transaction_hash(args_ptr_ptr);
55        Vec::<u8>::from_raw_parts(args_ptr as *mut u8,32, 32)
56    };
57    TryInto::<[u8;32]>::try_into(arguments).unwrap()
58}
59
60/// Get method name of the invoking method in this contract call
61pub fn method() -> String {
62    let mut args_ptr: u32 = 0;
63    let args_ptr_ptr = &mut args_ptr;
64
65    let arguments = 
66    unsafe {
67        let args_len = imports::method(args_ptr_ptr);
68        Vec::<u8>::from_raw_parts(args_ptr as *mut u8,args_len as usize, args_len as usize)
69    };
70    String::from_utf8(arguments).unwrap()
71}
72
73/// Get method arguments of the invoking method in this contract call
74pub fn arguments() -> Vec<u8> {
75    let mut args_ptr: u32 = 0;
76    let args_ptr_ptr = &mut args_ptr;
77
78    unsafe {
79        let args_len = imports::arguments(args_ptr_ptr);
80        Vec::<u8>::from_raw_parts(args_ptr as *mut u8,args_len as usize, args_len as usize)
81    }
82}