referrerpolicy=no-referrer-when-downgrade

snowbridge_test_utils/
mock_xcm.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <hello@snowfork.com>

use codec::Encode;
use core::cell::RefCell;
use xcm::prelude::*;
use xcm_executor::{
	traits::{FeeManager, FeeReason, TransactAsset},
	AssetsInHolding,
};

thread_local! {
	pub static IS_WAIVED: RefCell<Vec<FeeReason>> = RefCell::new(vec![]);
	pub static SENDER_OVERRIDE: RefCell<Option<(
		fn(
			&mut Option<Location>,
			&mut Option<Xcm<()>>,
		) -> Result<(Xcm<()>, Assets), SendError>,
		fn(
			Xcm<()>,
		) -> Result<XcmHash, SendError>,
	)>> = RefCell::new(None);
	pub static CHARGE_FEES_OVERRIDE: RefCell<Option<
		fn(Location, Assets) -> xcm::latest::Result
	>> = RefCell::new(None);
}

#[allow(dead_code)]
pub fn set_fee_waiver(waived: Vec<FeeReason>) {
	IS_WAIVED.with(|l| l.replace(waived));
}

#[allow(dead_code)]
pub fn set_sender_override(
	validate: fn(&mut Option<Location>, &mut Option<Xcm<()>>) -> SendResult<Xcm<()>>,
	deliver: fn(Xcm<()>) -> Result<XcmHash, SendError>,
) {
	SENDER_OVERRIDE.with(|x| x.replace(Some((validate, deliver))));
}

#[allow(dead_code)]
pub fn clear_sender_override() {
	SENDER_OVERRIDE.with(|x| x.replace(None));
}

#[allow(dead_code)]
pub fn set_charge_fees_override(charge_fees: fn(Location, Assets) -> xcm::latest::Result) {
	CHARGE_FEES_OVERRIDE.with(|x| x.replace(Some(charge_fees)));
}

#[allow(dead_code)]
pub fn clear_charge_fees_override() {
	CHARGE_FEES_OVERRIDE.with(|x| x.replace(None));
}

/// Mock XCM sender with an overridable `validate` and `deliver` function.
pub struct MockXcmSender;

impl SendXcm for MockXcmSender {
	type Ticket = Xcm<()>;

	fn validate(
		dest: &mut Option<Location>,
		xcm: &mut Option<Xcm<()>>,
	) -> SendResult<Self::Ticket> {
		let r: SendResult<Self::Ticket> = SENDER_OVERRIDE.with(|s| {
			if let Some((ref f, _)) = &*s.borrow() {
				f(dest, xcm)
			} else {
				Ok((xcm.take().unwrap(), Assets::default()))
			}
		});
		r
	}

	fn deliver(ticket: Self::Ticket) -> Result<XcmHash, SendError> {
		let r: Result<XcmHash, SendError> = SENDER_OVERRIDE.with(|s| {
			if let Some((_, ref f)) = &*s.borrow() {
				f(ticket)
			} else {
				let hash = ticket.using_encoded(sp_core::hashing::blake2_256);
				Ok(hash)
			}
		});
		r
	}
}

/// Mock XCM transactor that always succeeds
pub struct SuccessfulTransactor;
impl TransactAsset for SuccessfulTransactor {
	fn can_check_in(_origin: &Location, _what: &Asset, _context: &XcmContext) -> XcmResult {
		Ok(())
	}

	fn can_check_out(_dest: &Location, _what: &Asset, _context: &XcmContext) -> XcmResult {
		Ok(())
	}

	fn deposit_asset(_what: &Asset, _who: &Location, _context: Option<&XcmContext>) -> XcmResult {
		Ok(())
	}

	fn withdraw_asset(
		_what: &Asset,
		_who: &Location,
		_context: Option<&XcmContext>,
	) -> Result<AssetsInHolding, XcmError> {
		Ok(AssetsInHolding::default())
	}

	fn internal_transfer_asset(
		_what: &Asset,
		_from: &Location,
		_to: &Location,
		_context: &XcmContext,
	) -> Result<AssetsInHolding, XcmError> {
		Ok(AssetsInHolding::default())
	}
}

pub enum Weightless {}
impl PreparedMessage for Weightless {
	fn weight_of(&self) -> Weight {
		unreachable!();
	}
}

/// Mock the XCM executor with an overridable `charge_fees` function.
pub struct MockXcmExecutor;
impl<C> ExecuteXcm<C> for MockXcmExecutor {
	type Prepared = Weightless;
	fn prepare(_: Xcm<C>) -> Result<Self::Prepared, Xcm<C>> {
		unreachable!()
	}
	fn execute(_: impl Into<Location>, _: Self::Prepared, _: &mut XcmHash, _: Weight) -> Outcome {
		unreachable!()
	}
	fn charge_fees(location: impl Into<Location>, assets: Assets) -> xcm::latest::Result {
		let r: xcm::latest::Result = CHARGE_FEES_OVERRIDE.with(|s| {
			if let Some(ref f) = &*s.borrow() {
				f(location.into(), assets)
			} else {
				Ok(())
			}
		});
		r
	}
}

impl FeeManager for MockXcmExecutor {
	fn is_waived(_: Option<&Location>, r: FeeReason) -> bool {
		IS_WAIVED.with(|l| l.borrow().contains(&r))
	}

	fn handle_fee(_: Assets, _: Option<&XcmContext>, _: FeeReason) {}
}