Skip to content

Commit

Permalink
complete abi encoded payload
Browse files Browse the repository at this point in the history
  • Loading branch information
claravanstaden committed Jan 23, 2025
1 parent 61e2a28 commit 1d7ffa0
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 23 deletions.
72 changes: 59 additions & 13 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/envelope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,26 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
use snowbridge_core::inbound::Log;

use sp_core::{RuntimeDebug, H160};
use alloy_core::{
primitives::B256,
sol,
sol_types::{SolEvent, SolType},
};
use snowbridge_router_primitives::inbound::v2::{
Asset::{ForeignTokenERC20, NativeTokenERC20},
Message as MessageV2,
};
use sp_core::{RuntimeDebug, H160, H256};
use sp_std::prelude::*;

use alloy_core::{primitives::B256, sol, sol_types::SolEvent};

/**
struct AsNativeTokenERC20 {
sol! {
struct AsNativeTokenERC20 {
address token_id;
uint128 value;
}
struct AsForeignTokenERC20 {
bytes32 token_id;
uint128 value;
}
**/
sol! {
struct EthereumAsset {
uint8 kind;
bytes data;
Expand All @@ -42,7 +46,7 @@ pub struct Envelope {
/// A nonce for enforcing replay protection and ordering.
pub nonce: u64,
/// The inner payload generated from the source application.
pub payload: Payload,
pub message: MessageV2,
}

#[derive(Copy, Clone, RuntimeDebug)]
Expand All @@ -55,6 +59,8 @@ impl TryFrom<&Log> for Envelope {
// Convert to B256 for Alloy decoding
let topics: Vec<B256> = log.topics.iter().map(|x| B256::from_slice(x.as_ref())).collect();

let mut substrate_assets = alloc::vec![];

// Decode the Solidity event from raw logs
let event = OutboundMessageAccepted::decode_raw_log(topics, &log.data, true).map_err(
|decode_err| {
Expand All @@ -68,9 +74,46 @@ impl TryFrom<&Log> for Envelope {
},
)?;

// event.nonce is a `u64`
// event.payload is already the typed `Payload` struct
Ok(Self { gateway: log.address, nonce: event.nonce, payload: event.payload })
let payload = event.payload;

for asset in payload.assets {
match asset.kind {
0 => {
let native_data = AsNativeTokenERC20::abi_decode(&asset.data, true)
.map_err(|_| EnvelopeDecodeError)?;
substrate_assets.push(NativeTokenERC20 {
token_id: H160::from(native_data.token_id.as_ref()),
value: native_data.value,
});
},
1 => {
let foreign_data = AsForeignTokenERC20::abi_decode(&asset.data, true)
.map_err(|_| EnvelopeDecodeError)?;
substrate_assets.push(ForeignTokenERC20 {
token_id: H256::from(foreign_data.token_id.as_ref()),
value: foreign_data.value,
});
},
_ => return Err(EnvelopeDecodeError),
}
}

let mut claimer = None;
if payload.claimer.len() > 0 {
claimer = Some(payload.claimer.to_vec());
}

let message = MessageV2 {
origin: H160::from(payload.origin.as_ref()),
assets: substrate_assets,
xcm: payload.xcm.to_vec(),
claimer,
value: payload.value,
execution_fee: payload.executionFee,
relayer_fee: payload.relayerFee,
};

Ok(Self { gateway: log.address, nonce: event.nonce, message })
}
}

Expand Down Expand Up @@ -117,6 +160,9 @@ mod tests {
let envelope = result.unwrap();

assert_eq!(H160::from(hex!("b8ea8cb425d85536b158d661da1ef0895bb92f1d")), envelope.gateway);
assert_eq!(hex!("B8EA8cB425d85536b158d661da1ef0895Bb92F1D"), envelope.payload.origin);
assert_eq!(
H160::from(hex!("B8EA8cB425d85536b158d661da1ef0895Bb92F1D")),
envelope.message.origin
);
}
}
16 changes: 6 additions & 10 deletions bridges/snowbridge/pallets/inbound-queue-v2/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ mod mock;
#[cfg(test)]
mod test;

use codec::{Decode, DecodeAll, Encode};
use codec::{Decode, Encode};
use envelope::Envelope;
use frame_support::{
traits::{
Expand Down Expand Up @@ -220,14 +220,10 @@ pub mod pallet {
// Verify the message has not been processed
ensure!(!Nonce::<T>::get(envelope.nonce.into()), Error::<T>::InvalidNonce);

// Decode payload into `MessageV2`
//let message = MessageV2::decode_all(&mut envelope.payload.as_ref())
// .map_err(|_| Error::<T>::InvalidPayload)?;
let origin_account_location = Self::account_to_location(who)?;

//let origin_account_location = Self::account_to_location(who)?;

//let (xcm, _relayer_reward) =
// Self::do_convert(message, origin_account_location.clone())?;
let (xcm, _relayer_reward) =
Self::do_convert(envelope.message, origin_account_location.clone())?;

// Todo: Deposit fee(in Ether) to RewardLeger which should cover all of:
// T::RewardLeger::deposit(who, relayer_reward.into())?;
Expand All @@ -242,8 +238,8 @@ pub mod pallet {
// Set nonce flag to true
Nonce::<T>::set(envelope.nonce.into());

//let message_id = Self::send_xcm(xcm, T::AssetHubParaId::get())?;
//Self::deposit_event(Event::MessageReceived { nonce: envelope.nonce, message_id });
let message_id = Self::send_xcm(xcm, T::AssetHubParaId::get())?;
Self::deposit_event(Event::MessageReceived { nonce: envelope.nonce, message_id });

Ok(())
}
Expand Down

0 comments on commit 1d7ffa0

Please sign in to comment.