Skip to content

Commit 4adde2a

Browse files
feat(kusama-asset-hub): Add ability to force to use ChargeAssetTxPayment (#4541)
* feat(kusama-asset-hub): Add ability to force to use `ChargeAssetTxPayment` for native tip * feat(kusama-asset-hub): Add a unit test
1 parent fe280e4 commit 4adde2a

File tree

5 files changed

+88
-10
lines changed

5 files changed

+88
-10
lines changed

rust/chains/tw_polkadot/src/entry.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ impl PolkadotEntry {
5656
builder.extension(CheckEra { era, current_hash });
5757
builder.extension(CheckNonce::new(input.nonce as u32));
5858
if let Some(fee_asset_id) = ctx.fee_asset_id {
59-
builder.extension(ChargeAssetTxPayment::new(tip, fee_asset_id));
59+
builder.extension(ChargeAssetTxPayment::new(tip, fee_asset_id.as_u32()));
6060
} else {
6161
builder.extension(ChargeTransactionPayment::new(tip));
6262
}

rust/chains/tw_polkadot/src/lib.rs

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,22 @@ pub fn network_id_from_tw(input: &'_ Proto::SigningInput<'_>) -> EncodeResult<Ne
2525
Ok(NetworkId::try_from(input.network as u16).map_err(|_| EncodeError::InvalidNetworkId)?)
2626
}
2727

28-
pub fn fee_asset_id_from_tw(input: &'_ Proto::SigningInput<'_>) -> Option<u32> {
28+
pub fn fee_asset_id_from_tw(input: &'_ Proto::SigningInput<'_>) -> Option<FeeAssetId> {
2929
// Special case for batches.
30-
match &input.message_oneof {
31-
SigningVariant::balance_call(b) => match &b.message_oneof {
32-
BalanceVariant::asset_transfer(at) => Some(at.fee_asset_id),
33-
BalanceVariant::batch_asset_transfer(bat) => Some(bat.fee_asset_id),
34-
_ => None,
35-
},
36-
_ => None,
30+
if let SigningVariant::balance_call(b) = &input.message_oneof {
31+
match &b.message_oneof {
32+
BalanceVariant::asset_transfer(at) => return Some(FeeAssetId::Asset(at.fee_asset_id)),
33+
BalanceVariant::batch_asset_transfer(bat) => {
34+
return Some(FeeAssetId::Asset(bat.fee_asset_id))
35+
},
36+
_ => {},
37+
}
38+
}
39+
40+
if input.charge_native_as_asset_tx_payment {
41+
Some(FeeAssetId::Native)
42+
} else {
43+
None
3744
}
3845
}
3946

rust/frameworks/tw_substrate/src/lib.rs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,29 @@ impl From<EncodeError> for SigningErrorType {
4444
pub type EncodeResult<T> = Result<T, TWError<EncodeError>>;
4545
pub type WithCallIndexResult<T> = Result<WithCallIndex<T>, TWError<EncodeError>>;
4646

47+
#[derive(Debug, Clone)]
48+
pub enum FeeAssetId {
49+
Native,
50+
Asset(u32),
51+
}
52+
53+
impl FeeAssetId {
54+
pub fn as_u32(&self) -> u32 {
55+
match self {
56+
FeeAssetId::Native => 0,
57+
FeeAssetId::Asset(id) => *id,
58+
}
59+
}
60+
}
61+
4762
#[derive(Debug, Clone)]
4863
pub struct SubstrateContext {
4964
pub multi_address: bool,
5065
pub check_metadata: bool,
5166
pub network: NetworkId,
5267
pub spec_version: u32,
5368
pub transaction_version: u32,
54-
pub fee_asset_id: Option<u32>,
69+
pub fee_asset_id: Option<FeeAssetId>,
5570
}
5671

5772
impl SubstrateContext {

rust/tw_tests/tests/chains/polkadot/polkadot_sign.rs

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,58 @@ fn test_sign_transfer_kusama_new_spec() {
740740
assert_eq!(signed.as_deref(), Some("450284009dca538b7a925b8ea979cc546464a3c5f81d2398a3a272f6f93bdf4803f2f78300fc5a463d3b6972ac7e0b701110f9d95d377be5b6a2f356765553104c04765fc0066c235c11dabde650d487760dc310003d607abceaf85a0a0f47f1a90e3680029501590200000400001a2447c661c9b168bba4a2a178baef7d79eee006c1d145ffc832be76ff6ee9ce0300943577"));
741741
}
742742

743+
#[test]
744+
fn test_sign_transfer_kusama_asset_hub() {
745+
let block_hash = "0xa08d580076533e7262904ea3105c7abb1923e10b4a44c2b8e2121fca23c99d63"
746+
.decode_hex()
747+
.unwrap();
748+
let block_number = 11410063;
749+
let genesis_hash = "0x48239ef607d7928874027a43a67689209727dfb3d3dc5e5b03a39bdc2eda771a"
750+
.decode_hex()
751+
.unwrap();
752+
// GtrrH11FcafS3wNr59t5PwZSr94Zatc3nj1dTN1ET3drJ5k
753+
let private_key = "f5636ddc5583a9d3f95748328b56f36b5a4197f9b8473c546378eee3648b070f"
754+
.decode_hex()
755+
.unwrap();
756+
757+
let input = Proto::SigningInput {
758+
network: 2,
759+
private_key: private_key.into(),
760+
multi_address: false,
761+
nonce: 1,
762+
block_hash: block_hash.into(),
763+
genesis_hash: genesis_hash.into(),
764+
spec_version: 1009002,
765+
transaction_version: 15,
766+
charge_native_as_asset_tx_payment: true,
767+
era: Some(Proto::Era {
768+
block_number,
769+
period: 64,
770+
}),
771+
message_oneof: balance_call(Proto::mod_Balance::OneOfmessage_oneof::transfer(
772+
Proto::mod_Balance::Transfer {
773+
to_address: "DrRsYwWQN4QH6RCqyw5xLJbq8V37NodsJjuWKhMhx1GnJm1".into(),
774+
value: Cow::Owned(U256::from(90_000_000_000u64).to_big_endian().to_vec()), // 0.09
775+
call_indices: Some(Proto::CallIndices {
776+
variant: Proto::mod_CallIndices::OneOfvariant::custom(
777+
Proto::CustomCallIndices {
778+
module_index: 0x0A, // Balances pallet
779+
method_index: 0x00, // transfer_allow_death
780+
},
781+
),
782+
}),
783+
..Default::default()
784+
},
785+
)),
786+
..Default::default()
787+
};
788+
789+
let (_preimage, signed) = helper_encode_and_maybe_sign(CoinType::Polkadot, input);
790+
// Successfully broadcasted tx:
791+
// https://assethub-kusama.subscan.io/extrinsic/0xc3dd4b245cbd31ca0b6ea17ad1f7aad12b5984e705da1bee8b127e1d338301a4
792+
assert_eq!(signed.as_deref(), Some("49028400bf14d379a6d161a3cfcbb12dc1ae6c9a5e89c9c22924060e8fecab41e6124acf00a26875c8f4f1760319ffaa1a4a44d0841fec3a7d7abf0c820ac931574e18cda960bedbc32886fc9d050b4ebf291db1ded94e6ee55cfb83372cc800fceeacea03f500040000000a000038858d284516bcf0991d66e09c18815afb33c22f1d0d29cf43be56debd5777610700046bf414"));
793+
}
794+
743795
// TEST(PolkadotExtrinsic, Polkadot_EncodePayloadWithNewSpec)
744796
#[test]
745797
fn test_encode_payload_with_new_spec() {

src/proto/Polkadot.proto

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,10 @@ message SigningInput {
269269
// Whether enable MultiAddress
270270
bool multi_address = 10;
271271

272+
// Whether to use `ChargeAssetTxPayment` extrinsic extension instead of `ChargeTransactionPayment`
273+
// when paying transaction tip in native chain token.
274+
bool charge_native_as_asset_tx_payment = 13;
275+
272276
// Payload message
273277
oneof message_oneof {
274278
Balance balance_call = 11;

0 commit comments

Comments
 (0)