Different XCM builders, default one requires fee payment (#2253)

Adding on top of the new builder pattern for creating XCM programs, I'm
adding some more APIs:

```rust
let paying_fees: Xcm<()> = Xcm::builder() // Only allow paying for fees
  .withdraw_asset() // First instruction has to load the holding register
  .buy_execution() // Second instruction has to be `buy_execution`
  .build();

let paying_fees_invalid: Xcm<()> = Xcm::builder()
  .withdraw_asset()
  .build(); // Invalid, need to pay for fees

let not_paying_fees: Xcm<()> = Xcm::builder_unpaid()
  .unpaid_execution() // Needed
  .withdraw_asset()
  .deposit_asset()
  .build();

let all_goes: Xcm<()> = Xcm::builder_unsafe() // You can do anything
  .withdraw_asset()
  .deposit_asset()
  .build();
```

The invalid bits are because the methods don't even exist on the types
that you'd want to call them on.

---------

Co-authored-by: command-bot <>
This commit is contained in:
Francisco Aguirre
2023-11-21 16:09:40 +01:00
committed by GitHub
parent b25d29a502
commit b3841b6b71
25 changed files with 625 additions and 54 deletions
@@ -0,0 +1,32 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test error when using a badly formatted attribute.
use xcm_procedural::Builder;
struct Xcm<Call>(pub Vec<Instruction<Call>>);
#[derive(Builder)]
enum Instruction<Call> {
#[builder(funds_holding = 2)]
WithdrawAsset(u128),
BuyExecution { fees: u128 },
UnpaidExecution { weight_limit: (u32, u32) },
Transact { call: Call },
}
fn main() {}
@@ -0,0 +1,5 @@
error: Expected `builder(loads_holding)`
--> tests/ui/builder_pattern/badly_formatted_attribute.rs:25:5
|
25 | #[builder(funds_holding = 2)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -0,0 +1,30 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test error when the `BuyExecution` instruction doesn't take named fields.
use xcm_procedural::Builder;
struct Xcm<Call>(pub Vec<Instruction<Call>>);
#[derive(Builder)]
enum Instruction<Call> {
BuyExecution(u128),
UnpaidExecution { weight_limit: (u32, u32) },
Transact { call: Call },
}
fn main() {}
@@ -0,0 +1,5 @@
error: BuyExecution should have named fields
--> tests/ui/builder_pattern/buy_execution_named_fields.rs:25:5
|
25 | BuyExecution(u128),
| ^^^^^^^^^^^^^^^^^^
@@ -0,0 +1,32 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test error when an instruction that loads the holding register doesn't take operands.
use xcm_procedural::Builder;
struct Xcm<Call>(pub Vec<Instruction<Call>>);
#[derive(Builder)]
enum Instruction<Call> {
#[builder(loads_holding)]
WithdrawAsset,
BuyExecution { fees: u128 },
UnpaidExecution { weight_limit: (u32, u32) },
Transact { call: Call },
}
fn main() {}
@@ -0,0 +1,6 @@
error: Instructions that load the holding register should take operands
--> tests/ui/builder_pattern/loads_holding_no_operands.rs:25:5
|
25 | / #[builder(loads_holding)]
26 | | WithdrawAsset,
| |_________________^
@@ -0,0 +1,29 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test error when there's no `BuyExecution` instruction.
use xcm_procedural::Builder;
struct Xcm<Call>(pub Vec<Instruction<Call>>);
#[derive(Builder)]
enum Instruction<Call> {
UnpaidExecution { weight_limit: (u32, u32) },
Transact { call: Call },
}
fn main() {}
@@ -0,0 +1,6 @@
error: No BuyExecution instruction
--> tests/ui/builder_pattern/no_buy_execution.rs:25:5
|
25 | / UnpaidExecution { weight_limit: (u32, u32) },
26 | | Transact { call: Call },
| |____________________________^
@@ -0,0 +1,29 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test error when there's no `UnpaidExecution` instruction.
use xcm_procedural::Builder;
struct Xcm<Call>(pub Vec<Instruction<Call>>);
#[derive(Builder)]
enum Instruction<Call> {
BuyExecution { fees: u128 },
Transact { call: Call },
}
fn main() {}
@@ -0,0 +1,6 @@
error: No UnpaidExecution instruction
--> tests/ui/builder_pattern/no_unpaid_execution.rs:25:5
|
25 | / BuyExecution { fees: u128 },
26 | | Transact { call: Call },
| |____________________________^
@@ -0,0 +1,32 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test error when using wrong attribute.
use xcm_procedural::Builder;
struct Xcm<Call>(pub Vec<Instruction<Call>>);
#[derive(Builder)]
enum Instruction<Call> {
#[builder(funds_holding)]
WithdrawAsset(u128),
BuyExecution { fees: u128 },
UnpaidExecution { weight_limit: (u32, u32) },
Transact { call: Call },
}
fn main() {}
@@ -0,0 +1,5 @@
error: Expected `builder(loads_holding)`
--> tests/ui/builder_pattern/unexpected_attribute.rs:25:5
|
25 | #[builder(funds_holding)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -0,0 +1,30 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test error when the `BuyExecution` instruction doesn't take named fields.
use xcm_procedural::Builder;
struct Xcm<Call>(pub Vec<Instruction<Call>>);
#[derive(Builder)]
enum Instruction<Call> {
BuyExecution { fees: u128 },
UnpaidExecution(u32, u32),
Transact { call: Call },
}
fn main() {}
@@ -0,0 +1,5 @@
error: UnpaidExecution should have named fields
--> tests/ui/builder_pattern/unpaid_execution_named_fields.rs:26:5
|
26 | UnpaidExecution(u32, u32),
| ^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -0,0 +1,25 @@
// Copyright (C) Parity Technologies (UK) Ltd.
// This file is part of Polkadot.
// Polkadot is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// Polkadot is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>.
//! Test error when attaching the derive builder macro to something
//! other than the XCM `Instruction` enum.
use xcm_procedural::Builder;
#[derive(Builder)]
struct SomeStruct;
fn main() {}
@@ -0,0 +1,5 @@
error: Expected the `Instruction` enum
--> tests/ui/builder_pattern/wrong_target.rs:23:1
|
23 | struct SomeStruct;
| ^^^^^^^^^^^^^^^^^^