use super::transaction::*;
use crate::crypto::prelude::*;
use borsh::{BorshDeserialize, BorshSerialize};
use serde::{Deserialize, Serialize};
use std::{borrow::Borrow, collections::HashMap, fmt, hash::Hash};
use near_primitives_core::{
hash::CryptoHash,
serialize::{dec_format, option_base64_format, to_base58},
types::{AccountId, Balance, ShardId},
};
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct Receipt {
pub predecessor_id: AccountId,
pub receiver_id: AccountId,
pub receipt_id: CryptoHash,
pub receipt: ReceiptEnum,
}
impl Borrow<CryptoHash> for Receipt {
fn borrow(&self) -> &CryptoHash {
&self.receipt_id
}
}
impl Receipt {
pub fn get_hash(&self) -> CryptoHash {
self.receipt_id
}
pub fn new_balance_refund(receiver_id: &AccountId, refund: Balance) -> Self {
Receipt {
predecessor_id: "system".parse().unwrap(),
receiver_id: receiver_id.clone(),
receipt_id: CryptoHash::default(),
receipt: ReceiptEnum::Action(ActionReceipt {
signer_id: "system".parse().unwrap(),
signer_public_key: Ed25519PublicKey::try_from_bytes(&[0; 32]).unwrap(),
gas_price: 0,
output_data_receivers: vec![],
input_data_ids: vec![],
actions: vec![Action::Transfer(TransferAction { deposit: refund })],
}),
}
}
pub fn new_gas_refund(
receiver_id: &AccountId,
refund: Balance,
signer_public_key: Ed25519PublicKey,
) -> Self {
Receipt {
predecessor_id: "system".parse().unwrap(),
receiver_id: receiver_id.clone(),
receipt_id: CryptoHash::default(),
receipt: ReceiptEnum::Action(ActionReceipt {
signer_id: receiver_id.clone(),
signer_public_key,
gas_price: 0,
output_data_receivers: vec![],
input_data_ids: vec![],
actions: vec![Action::Transfer(TransferAction { deposit: refund })],
}),
}
}
}
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
#[allow(clippy::large_enum_variant)]
pub enum ReceiptEnum {
Action(ActionReceipt),
Data(DataReceipt),
}
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Debug, PartialEq, Eq, Clone)]
pub struct ActionReceipt {
pub signer_id: AccountId,
pub signer_public_key: Ed25519PublicKey,
#[serde(with = "dec_format")]
pub gas_price: Balance,
pub output_data_receivers: Vec<DataReceiver>,
pub input_data_ids: Vec<CryptoHash>,
pub actions: Vec<Action>,
}
#[derive(BorshSerialize, BorshDeserialize, Serialize, Deserialize, Hash, PartialEq, Eq, Clone)]
pub struct DataReceipt {
pub data_id: CryptoHash,
#[serde(with = "option_base64_format")]
pub data: Option<Vec<u8>>,
}
impl fmt::Debug for DataReceipt {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("DataReceipt")
.field("data_id", &self.data_id)
.field(
"data",
&format_args!("{:?}", self.data.as_deref().map(to_base58)),
)
.finish()
}
}
#[derive(
BorshSerialize, BorshDeserialize, Serialize, Deserialize, Hash, Clone, Debug, PartialEq, Eq,
)]
pub struct DataReceiver {
pub data_id: CryptoHash,
pub receiver_id: AccountId,
}
#[derive(BorshSerialize, BorshDeserialize, Hash, PartialEq, Eq, Clone)]
pub struct ReceivedData {
pub data: Option<Vec<u8>>,
}
impl fmt::Debug for ReceivedData {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("ReceivedData")
.field(
"data",
&format_args!("{:?}", self.data.as_deref().map(to_base58)),
)
.finish()
}
}
#[derive(Default, BorshSerialize, BorshDeserialize, Clone, PartialEq, Eq, Debug)]
pub struct DelayedReceiptIndices {
pub first_index: u64,
pub next_available_index: u64,
}
pub type ReceiptResult = HashMap<ShardId, Vec<Receipt>>;