-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set state initial implementation for chain simulator, test in adder
- Loading branch information
1 parent
7409cb8
commit 04e59ef
Showing
6 changed files
with
156 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
use anyhow::anyhow; | ||
use serde::{Deserialize, Serialize}; | ||
use std::collections::HashMap; | ||
|
||
use crate::data::account::Account; | ||
|
||
use super::{GatewayRequest, GatewayRequestType, SET_STATE_ENDPOINT}; | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize)] | ||
pub struct SetStateResponse { | ||
pub data: serde_json::Value, | ||
pub error: String, | ||
pub code: String, | ||
} | ||
|
||
#[derive(Debug, Clone, Serialize, Deserialize, Default)] | ||
pub struct SetStateAccount { | ||
pub address: String, | ||
pub nonce: u64, | ||
pub balance: String, | ||
pub keys: HashMap<String, String>, | ||
pub code: String, | ||
#[serde(default)] | ||
pub code_hash: String, | ||
#[serde(default)] | ||
pub root_hash: String, | ||
#[serde(default)] | ||
pub code_metadata: String, | ||
#[serde(default)] | ||
pub owner_address: String, | ||
#[serde(default)] | ||
pub developer_reward: String, | ||
} | ||
|
||
impl From<Account> for SetStateAccount { | ||
fn from(value: Account) -> Self { | ||
Self { | ||
address: value.address.to_bech32_string().unwrap_or_default(), | ||
nonce: value.nonce, | ||
balance: value.balance.to_string(), | ||
keys: HashMap::new(), | ||
code: value.code, | ||
code_hash: value.code_hash.unwrap_or_default(), | ||
root_hash: value.root_hash.unwrap_or_default(), | ||
code_metadata: value.code_metadata.unwrap_or_default(), | ||
owner_address: value.owner_address.unwrap_or_default(), | ||
developer_reward: value.developer_reward.unwrap_or_default(), | ||
} | ||
} | ||
} | ||
|
||
impl SetStateAccount { | ||
pub fn with_keys(mut self, keys: HashMap<String, String>) -> Self { | ||
self.keys = keys; | ||
|
||
self | ||
} | ||
} | ||
|
||
/// Sets state for a list of accounts using the chain simulator API. | ||
pub struct ChainSimulatorSetStateRequest { | ||
pub account: Vec<SetStateAccount>, | ||
} | ||
|
||
impl ChainSimulatorSetStateRequest { | ||
pub fn for_account(account: Vec<SetStateAccount>) -> Self { | ||
Self { account } | ||
} | ||
} | ||
|
||
impl GatewayRequest for ChainSimulatorSetStateRequest { | ||
type Payload = Vec<SetStateAccount>; | ||
type DecodedJson = SetStateResponse; | ||
type Result = String; | ||
|
||
fn get_payload(&self) -> Option<&Self::Payload> { | ||
Some(&self.account) | ||
} | ||
|
||
fn request_type(&self) -> GatewayRequestType { | ||
GatewayRequestType::Post | ||
} | ||
|
||
fn get_endpoint(&self) -> String { | ||
SET_STATE_ENDPOINT.to_owned() | ||
} | ||
|
||
fn process_json(&self, decoded: Self::DecodedJson) -> anyhow::Result<Self::Result> { | ||
match decoded.code.as_str() { | ||
"successful" => Ok(decoded.code), | ||
_ => Err(anyhow!("{}", decoded.error)), | ||
} | ||
} | ||
} |