Skip to content

Commit

Permalink
adding builder method
Browse files Browse the repository at this point in the history
  • Loading branch information
dzmitry-lahoda committed Mar 12, 2024
1 parent 56e4657 commit 9740290
Showing 1 changed file with 46 additions and 5 deletions.
51 changes: 46 additions & 5 deletions crates/cvm-runtime/src/shared.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use core::panic;

use crate::{prelude::*, AssetId};
use cosmwasm_std::{from_json, to_json_binary, Api, Binary, CanonicalAddr, StdError, StdResult};
use cvm::NetworkId;
use serde::{de::DeserializeOwned, Serialize};

pub use cvm::shared::*;
Expand All @@ -13,6 +16,9 @@ pub type CvmInstruction = crate::Instruction<Vec<u8>, XcAddr, CvmFundsFilter>;
pub type CvmPacket = crate::Packet<CvmProgram>;
pub type CvmProgram = crate::Program<Vec<CvmInstruction>>;


pub type CvmSpawnRef<'a> = (&'a CvmProgram,&'a CvmFundsFilter);

impl CvmProgram {
pub fn new(instructions: Vec<CvmInstruction>) -> Self {
Self {
Expand All @@ -21,16 +27,27 @@ impl CvmProgram {
}
}

pub fn last_spawns(&self) -> Vec<&CvmInstruction> {
pub fn will_spawn(&self) -> bool {
self.instructions
.iter()
.any(|i| matches!(i, CvmInstruction::Spawn { .. }))
}

pub fn last_spawns(&self) -> Vec<CvmSpawnRef> {
self.instructions
.iter()
.filter_map(|i|
if let CvmInstruction::Spawn { program, .. } = i {
Some(program.last_spawns())
.filter_map(|i| {
if let CvmInstruction::Spawn { program, assets, .. } = i {
if program.will_spawn() {
Some(program.last_spawns())
} else {
Some(vec![(program, assets)])
}
} else {
None
}
)
})
.flatten()
.collect()
}
}
Expand All @@ -42,6 +59,15 @@ impl CvmInstruction {
assets: CvmFundsFilter::one(asset_id.into(), crate::Amount::new(amount, 0)),
}
}

pub fn spawn(network_id: NetworkId, program: CvmProgram ) -> Self {
Self::Spawn {
network_id,
salt: vec![],
assets: vec![],
program,
}
}
}

pub fn to_json_base64<T: Serialize>(x: &T) -> StdResult<String> {
Expand All @@ -51,3 +77,18 @@ pub fn to_json_base64<T: Serialize>(x: &T) -> StdResult<String> {
pub fn decode_base64<S: AsRef<str>, T: DeserializeOwned>(encoded: S) -> StdResult<T> {
from_json::<T>(&Binary::from_base64(encoded.as_ref())?)
}


#[cfg(test)]
mod tests {
use super::{CvmInstruction, CvmProgram};


#[test]
pub fn spawn() {
let spawn = CvmInstruction::Spawn { network_id: (), salt: (), assets: (), program: () }
let program = CvmProgram::new(vec![

]);
}
}

0 comments on commit 9740290

Please sign in to comment.