Skip to content

Commit c0efdaf

Browse files
committed
sns-testing: make ICP treasury configurable
Currently, the identity that transfer ICPs to the swap participants or the user provided account is hardcoded. Which limits the usage of the 'sns-testing' to the network created by 'sns-testing-init'. This commit adds an optional argument to choose the dfx identity to be used as an ICP treasury to 'sns-testing-init' binary and 'swap-complete' and 'transfer-icp' subcommands of 'sns-testing'.
1 parent fe95bc9 commit c0efdaf

File tree

5 files changed

+69
-16
lines changed

5 files changed

+69
-16
lines changed

rs/sns/testing/src/bin/init.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,18 @@ async fn nns_init(args: NnsInitArgs) {
2323
let initial_mutations = load_registry_mutations(registry_proto_path);
2424
let dev_principal_id = get_identity_principal(&args.dev_identity).unwrap();
2525

26+
let treasury_principal_id = if let Some(icp_treasury_identity) = args.icp_treasury_identity {
27+
get_identity_principal(&icp_treasury_identity).unwrap()
28+
} else {
29+
*TREASURY_PRINCIPAL_ID
30+
};
31+
2632
bootstrap_nns(
2733
&pocket_ic,
2834
vec![initial_mutations],
2935
vec![
3036
(
31-
(*TREASURY_PRINCIPAL_ID).into(),
37+
treasury_principal_id.into(),
3238
Tokens::from_tokens(10_000_000).unwrap(),
3339
),
3440
(dev_principal_id.into(), Tokens::from_tokens(100).unwrap()),

rs/sns/testing/src/bin/main.rs

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,9 @@ async fn validate_network(network: String) {
8787
}
8888

8989
async fn swap_complete(network: String, args: SwapCompleteArgs) {
90-
let agent = get_agent(&network, None).await.unwrap();
90+
let agent = get_agent(&network, args.icp_treasury_identity.clone())
91+
.await
92+
.unwrap();
9193

9294
// Normally, SNSes would have different names, so the vector below would have a single element.
9395
let target_snses = find_sns_by_name(&agent, args.sns_name.clone()).await;
@@ -113,7 +115,14 @@ async fn swap_complete(network: String, args: SwapCompleteArgs) {
113115
};
114116
neurons_to_follow.extend(principal_neurons);
115117
}
116-
if let Err(e) = complete_sns_swap(&agent, sns.swap, sns.governance, neurons_to_follow).await
118+
if let Err(e) = complete_sns_swap(
119+
&agent,
120+
args.icp_treasury_identity.is_none(),
121+
sns.swap,
122+
sns.governance,
123+
neurons_to_follow,
124+
)
125+
.await
117126
{
118127
eprintln!("Failed to complete swap for SNS {}: {}", args.sns_name, e);
119128
exit(1);
@@ -154,17 +163,24 @@ async fn sns_proposal_upvote(network: String, args: SnsProposalUpvoteArgs) {
154163
}
155164

156165
pub async fn transfer_icp(network: String, args: TransferICPArgs) {
157-
let agent = get_agent(&network, None).await.unwrap();
166+
let agent = get_agent(&network, args.icp_treasury_identity.clone())
167+
.await
168+
.unwrap();
158169
let recipient = match args.recipient {
159170
TransferRecipientArg::Account { account } => account.0,
160171
TransferRecipientArg::Principal {
161172
principal_id,
162173
subaccount,
163174
} => AccountIdentifier::new(principal_id, subaccount.map(|a| Subaccount(a.0))).to_address(),
164175
};
165-
transfer_icp_from_treasury(&agent, recipient, args.amount.0)
166-
.await
167-
.unwrap();
176+
transfer_icp_from_treasury(
177+
&agent,
178+
args.icp_treasury_identity.is_none(),
179+
recipient,
180+
args.amount.0,
181+
)
182+
.await
183+
.unwrap();
168184
}
169185

170186
#[tokio::main]

rs/sns/testing/src/lib.rs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ pub struct ValidateNetworkArgs {}
4141

4242
#[derive(Debug, Parser)]
4343
pub struct RunBasicScenarioArgs {
44-
/// The name of the 'dfx' identity to use for the scenario. This identity is used to submit NNS
45-
/// proposal to create the new SNS and is added as an initial neuron in the new SNS.
44+
/// The name of the 'dfx' identity to use for the scenario. The principal of this identity
45+
/// is used to submit NNS proposal to create the new SNS and is added as an initial neuron in the new SNS.
4646
#[arg(long)]
4747
pub dev_identity: Option<String>,
4848
/// The ID of the canister to be controlled by the SNS created in the scenario.
@@ -56,6 +56,12 @@ pub struct SwapCompleteArgs {
5656
/// The name of the SNS to complete the swap for.
5757
#[arg(long)]
5858
pub sns_name: String,
59+
/// The name of the 'dfx' identity. The principal of this identity is supposed to have
60+
/// an account in the ICP ledger with sufficient balance to provide direct participations
61+
/// for the SNS swap.
62+
/// If not provided, the ephemeral identity with hardcoded principal will be used.
63+
#[arg(long)]
64+
pub icp_treasury_identity: Option<String>,
5965
/// The neuron that swap participants will follow.
6066
#[clap(long, group = "neuron-follow-selection")]
6167
pub follow_neuron: Option<ParsedSnsNeuron>,
@@ -128,6 +134,11 @@ pub struct TransferICPArgs {
128134
/// The amount of e8s to transfer.
129135
#[arg(long)]
130136
pub amount: ParsedTokens,
137+
/// The name of the 'dfx' identity. The principal of this identity is supposed to
138+
/// have an account in the ICP ledger with sufficient balance to perform the transfer.
139+
/// If not provided, the ephemeral identity with hardcoded principal will be used.
140+
#[arg(long)]
141+
pub icp_treasury_identity: Option<String>,
131142
}
132143

133144
/// A wrapper to parse ICP tokens from a decimal number.
@@ -220,4 +231,11 @@ pub struct NnsInitArgs {
220231
/// hotkey for the NNS neuron with the majority voting power.
221232
#[arg(long)]
222233
pub dev_identity: String,
234+
/// The name of the 'dfx' identity. The principal of this identity will be added to the
235+
/// ICP ledger with 10_000_000 ICP. This identity will be used to transfer ICP
236+
/// to provide sufficient direct participations for the SNS swap or to transfer ICP
237+
/// to the user provided account.
238+
/// If not provided, the ephemeral identity with hardcoded principal will be used.
239+
#[arg(long)]
240+
pub icp_treasury_identity: Option<String>,
223241
}

rs/sns/testing/src/sns.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ pub async fn create_sns<C: CallCanisters + ProgressNetwork + BuildEphemeralAgent
124124
let sns_swap = sns.swap;
125125
complete_sns_swap(
126126
neuron_agent,
127+
true,
127128
sns_swap,
128129
sns_governance,
129130
if follow_dev_neuron {
@@ -186,16 +187,24 @@ async fn get_current_participation<C: CallCanisters>(
186187

187188
// Completes the swap by transferring the required amount of ICP from the "treasury" account
188189
// and participating in the swap for each participant using agents provided as arguments:
189-
// 1) swap_treasury_agent - Agent for the identity that has sufficient amout of ICP tokens to close the swap.
190-
// 2) swap_participants_agents - Agents for the identities that will participate in the swap.
191-
// 3) swap_canister - SNS Swap canister IDs.
190+
// 1) agent - Agent that is used to provide IC network settings.
191+
// 2) use_ephemeral_icp_treasury - defines whether the identity of the 'agent' or 'TREASURY_PRINCIPAL_ID'.
192+
// is used to transfer ICP to the swap participants.
193+
// 2) swap_canister - SNS Swap canister ID.
194+
// 3) governance_canister - SNS Governance canister ID.
195+
// 4) neurons_to_follow - SNS Neuron IDs that will be followed by the swap participants.
192196
pub async fn complete_sns_swap<C: CallCanisters + ProgressNetwork + BuildEphemeralAgent>(
193197
agent: &C,
198+
use_ephemeral_icp_treasury: bool,
194199
swap_canister: SwapCanister,
195200
governance_canister: GovernanceCanister,
196201
neurons_to_follow: Vec<SnsNeuronId>,
197202
) -> Result<(), String> {
198-
let swap_treasury_agent = &agent.build_ephemeral_agent(TREASURY_SECRET_KEY.clone());
203+
let swap_treasury_agent = if use_ephemeral_icp_treasury {
204+
&agent.build_ephemeral_agent(TREASURY_SECRET_KEY.clone())
205+
} else {
206+
agent
207+
};
199208

200209
println!("Waiting for the swap to be open...");
201210
await_swap_lifecycle(agent, swap_canister, Lifecycle::Open, true).await?;
@@ -514,7 +523,6 @@ pub async fn propose_sns_controlled_test_canister_upgrade<C: CallCanisters + Pro
514523
.unwrap()
515524
}
516525

517-
518526
// Waits for the upgrade proposal to be adopted and executed and then waits for the canister to become available
519527
// after upgrade using arguments:
520528
// 1) agent - Agent that will be used to check the status of the canister.

rs/sns/testing/src/utils.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,17 @@ pub async fn validate_target_canister<C: CallCanisters>(
252252

253253
pub async fn transfer_icp_from_treasury<C: CallCanisters + BuildEphemeralAgent>(
254254
agent: &C,
255+
use_ephemeral_icp_treasury: bool,
255256
to: AccountIdBlob,
256257
amount: Tokens,
257258
) -> Result<u64, TransferError> {
258-
let treasury_agent = agent.build_ephemeral_agent(TREASURY_SECRET_KEY.clone());
259+
let treasury_agent = if use_ephemeral_icp_treasury {
260+
&agent.build_ephemeral_agent(TREASURY_SECRET_KEY.clone())
261+
} else {
262+
agent
263+
};
259264
transfer(
260-
&treasury_agent,
265+
treasury_agent,
261266
TransferArgs {
262267
to,
263268
amount,

0 commit comments

Comments
 (0)