Skip to content

Commit

Permalink
remove caller key (#1098)
Browse files Browse the repository at this point in the history
  • Loading branch information
iFrostizz authored Jul 5, 2024
1 parent 93a9e45 commit c9aa3f1
Show file tree
Hide file tree
Showing 7 changed files with 249 additions and 372 deletions.
2 changes: 0 additions & 2 deletions x/programs/cmd/simulator/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ const (
)

type Step struct {
// The key of the caller used.
CallerKey string `json:"callerKey"`
// The API endpoint to call. (required)
Endpoint Endpoint `json:"endpoint"`
// The method to call on the endpoint.
Expand Down
12 changes: 1 addition & 11 deletions x/programs/cmd/simulator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,6 @@ pub struct Step {
pub params: Vec<Param>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SimulatorStep<'a> {
/// The key of the caller used in each step of the plan.
pub caller_key: &'a str,
#[serde(flatten)]
pub step: &'a Step,
}

impl Step {
/// Create a [`Step`] that creates a key.
#[must_use]
Expand Down Expand Up @@ -357,11 +348,10 @@ where
W: Write,
R: Iterator<Item = StepResultItem>,
{
pub fn run_step(&mut self, caller_key: &str, step: &Step) -> StepResultItem {
pub fn run_step(&mut self, step: &Step) -> StepResultItem {
let run_command = b"run --step '";
self.writer.write_all(run_command)?;

let step = SimulatorStep { caller_key, step };
let input = serde_json::to_vec(&step).map_err(StepError::Serde)?;
self.writer.write_all(&input)?;
self.writer.write_all(b"'\n")?;
Expand Down
59 changes: 25 additions & 34 deletions x/programs/cmd/simulator/test/context_injection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@ mod tests {
let owner = String::from("owner");

simulator
.run_step(&owner, &Step::create_key(Key::Ed25519(owner.clone())))
.run_step(&Step::create_key(Key::Ed25519(owner.clone())))
.unwrap();

let program_id = simulator
.run_step(&owner, &Step::create_program(PROGRAM_PATH))
.run_step(&Step::create_program(PROGRAM_PATH))
.unwrap()
.id;

Expand All @@ -42,15 +42,12 @@ mod tests {
test_context.timestamp = timestamp;

let response = simulator
.run_step(
&owner,
&Step {
endpoint: Endpoint::Execute,
method: "get_timestamp".into(),
max_units: 1000000,
params: vec![test_context.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "get_timestamp".into(),
max_units: 1000000,
params: vec![test_context.into()],
})
.unwrap()
.result
.response::<u64>()
Expand All @@ -66,11 +63,11 @@ mod tests {
let owner = String::from("owner");

simulator
.run_step(&owner, &Step::create_key(Key::Ed25519(owner.clone())))
.run_step(&Step::create_key(Key::Ed25519(owner.clone())))
.unwrap();

let program_id = simulator
.run_step(&owner, &Step::create_program(PROGRAM_PATH))
.run_step(&Step::create_program(PROGRAM_PATH))
.unwrap()
.id;

Expand All @@ -79,15 +76,12 @@ mod tests {
test_context.height = height;

let response = simulator
.run_step(
&owner,
&Step {
endpoint: Endpoint::Execute,
method: "get_height".into(),
max_units: 1000000,
params: vec![test_context.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "get_height".into(),
max_units: 1000000,
params: vec![test_context.into()],
})
.unwrap()
.result
.response::<u64>()
Expand All @@ -103,33 +97,30 @@ mod tests {
let owner = String::from("owner");

simulator
.run_step(&owner, &Step::create_key(Key::Ed25519(owner.clone())))
.run_step(&Step::create_key(Key::Ed25519(owner.clone())))
.unwrap();

let program_id = simulator
.run_step(&owner, &Step::create_program(PROGRAM_PATH))
.run_step(&Step::create_program(PROGRAM_PATH))
.unwrap()
.id;

let actor_key = Key::Ed25519(String::from("actor"));

simulator
.run_step(&owner, &Step::create_key(actor_key.clone()))
.run_step(&Step::create_key(actor_key.clone()))
.unwrap();

let mut test_context = TestContext::from(program_id);
test_context.actor_key = Some(actor_key);

let response = simulator
.run_step(
&owner,
&Step {
endpoint: Endpoint::Execute,
method: "get_actor".into(),
max_units: 1000000,
params: vec![test_context.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "get_actor".into(),
max_units: 1000000,
params: vec![test_context.into()],
})
.unwrap()
.result
.response::<Address>()
Expand Down
123 changes: 51 additions & 72 deletions x/programs/rust/examples/automated-market-maker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,29 +130,26 @@ mod tests {
fn init_state() {
let mut simulator = simulator::ClientBuilder::new().try_build().unwrap();

let owner = "owner";
let owner = String::from("owner");

let program_id = simulator
.run_step(owner, &Step::create_program(PROGRAM_PATH))
.run_step(&Step::create_program(PROGRAM_PATH))
.unwrap()
.id;

simulator
.run_step(owner, &Step::create_key(Key::Ed25519(owner.to_string())))
.run_step(&Step::create_key(Key::Ed25519(owner)))
.unwrap();

let test_context = TestContext::from(program_id);

let resp_err = simulator
.run_step(
owner,
&Step {
endpoint: Endpoint::Execute,
method: "remove_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.clone().into(), 100000u64.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "remove_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.clone().into(), 100000u64.into()],
})
.unwrap()
.result
.response::<(u64, u64)>()
Expand All @@ -165,15 +162,12 @@ mod tests {
assert!(matches!(call_err, ExternalCallError::CallPanicked));

let resp_err = simulator
.run_step(
owner,
&Step {
endpoint: Endpoint::Execute,
method: "swap".to_string(),
max_units: u64::MAX,
params: vec![test_context.into(), 100000u64.into(), true.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "swap".to_string(),
max_units: u64::MAX,
params: vec![test_context.into(), 100000u64.into(), true.into()],
})
.unwrap()
.result
.response::<u64>()
Expand All @@ -190,29 +184,26 @@ mod tests {
fn add_liquidity_same_ratio() {
let mut simulator = simulator::ClientBuilder::new().try_build().unwrap();

let owner = "owner";
let owner = String::from("owner");

let program_id = simulator
.run_step(owner, &Step::create_program(PROGRAM_PATH))
.run_step(&Step::create_program(PROGRAM_PATH))
.unwrap()
.id;

simulator
.run_step(owner, &Step::create_key(Key::Ed25519(owner.to_string())))
.run_step(&Step::create_key(Key::Ed25519(owner)))
.unwrap();

let test_context = TestContext::from(program_id);

let resp = simulator
.run_step(
owner,
&Step {
endpoint: Endpoint::Execute,
method: "add_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.clone().into(), 1000u64.into(), 1000u64.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "add_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.clone().into(), 1000u64.into(), 1000u64.into()],
})
.unwrap()
.result
.response::<u64>()
Expand All @@ -221,15 +212,12 @@ mod tests {
assert_eq!(resp, 1000);

let resp = simulator
.run_step(
owner,
&Step {
endpoint: Endpoint::Execute,
method: "add_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.into(), 1000u64.into(), 1001u64.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "add_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.into(), 1000u64.into(), 1001u64.into()],
})
.unwrap()
.result
.response::<u64>()
Expand All @@ -246,29 +234,26 @@ mod tests {
fn swap_changes_ratio() {
let mut simulator = simulator::ClientBuilder::new().try_build().unwrap();

let owner = "owner";
let owner = String::from("owner");

let program_id = simulator
.run_step(owner, &Step::create_program(PROGRAM_PATH))
.run_step(&Step::create_program(PROGRAM_PATH))
.unwrap()
.id;

simulator
.run_step(owner, &Step::create_key(Key::Ed25519(owner.to_string())))
.run_step(&Step::create_key(Key::Ed25519(owner)))
.unwrap();

let test_context = TestContext::from(program_id);

let resp = simulator
.run_step(
owner,
&Step {
endpoint: Endpoint::Execute,
method: "add_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.clone().into(), 1000u64.into(), 1000u64.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "add_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.clone().into(), 1000u64.into(), 1000u64.into()],
})
.unwrap()
.result
.response::<u64>()
Expand All @@ -277,27 +262,21 @@ mod tests {
assert_eq!(resp, 1000);

simulator
.run_step(
owner,
&Step {
endpoint: Endpoint::Execute,
method: "swap".to_string(),
max_units: u64::MAX,
params: vec![test_context.clone().into(), 10u64.into(), true.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "swap".to_string(),
max_units: u64::MAX,
params: vec![test_context.clone().into(), 10u64.into(), true.into()],
})
.unwrap();

let (amount_x, amount_y) = simulator
.run_step(
owner,
&Step {
endpoint: Endpoint::Execute,
method: "remove_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.into(), 1000.into()],
},
)
.run_step(&Step {
endpoint: Endpoint::Execute,
method: "remove_liquidity".to_string(),
max_units: u64::MAX,
params: vec![test_context.into(), 1000.into()],
})
.unwrap()
.result
.response::<(u64, u64)>()
Expand Down
Loading

0 comments on commit c9aa3f1

Please sign in to comment.