Skip to content

Commit

Permalink
chore: update onRequestHook
Browse files Browse the repository at this point in the history
  • Loading branch information
shekohex committed Dec 4, 2024
1 parent 3c581b1 commit c137d7b
Show file tree
Hide file tree
Showing 7 changed files with 114 additions and 82 deletions.
1 change: 1 addition & 0 deletions pallets/services/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ std = [
"pallet-ethereum/std",
"pallet-evm/std",
"pallet-evm-chain-id/std",
"pallet-assets/std",

"pallet-evm-precompile-modexp/std",
"pallet-evm-precompile-sha3fips/std",
Expand Down
94 changes: 43 additions & 51 deletions pallets/services/src/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use frame_support::dispatch::{DispatchErrorWithPostInfo, PostDispatchInfo};
use sp_core::{H160, U256};
use sp_runtime::traits::{UniqueSaturatedInto, Zero};
use tangle_primitives::services::{
BlueprintServiceManager, Field, MasterBlueprintServiceManagerRevision, OperatorPreferences,
Service, ServiceBlueprint,
Asset, BlueprintServiceManager, Field, MasterBlueprintServiceManagerRevision,
OperatorPreferences, Service, ServiceBlueprint,
};

use super::*;
Expand Down Expand Up @@ -449,6 +449,7 @@ impl<T: Config> Pallet<T> {
permitted_callers: &[T::AccountId],
_assets: &[T::AssetId],
ttl: BlockNumberFor<T>,
paymet_asset: Asset<T::AssetId>,
value: BalanceOf<T>,
) -> Result<(bool, Weight), DispatchErrorWithPostInfo> {
#[allow(deprecated)]
Expand All @@ -463,40 +464,27 @@ impl<T: Config> Pallet<T> {
internal_type: None,
},
ethabi::Param {
name: String::from("requestId"),
kind: ethabi::ParamType::Uint(64),
internal_type: None,
},
ethabi::Param {
name: String::from("requester"),
kind: ethabi::ParamType::Address,
internal_type: None,
},
ethabi::Param {
name: String::from("operatorsWithPreferences"),
kind: ethabi::ParamType::Array(Box::new(
OperatorPreferences::to_ethabi_param_type(),
)),
internal_type: Some(String::from("OperatorPreferences[]")),
},
ethabi::Param {
name: String::from("requestInputs"),
kind: ethabi::ParamType::Bytes,
internal_type: None,
},
ethabi::Param {
name: String::from("permittedCallers"),
kind: ethabi::ParamType::Array(Box::new(ethabi::ParamType::Address)),
internal_type: Some(String::from("address[]")),
},
// ethabi::Param {
// name: String::from("assets"),
// kind: ethabi::ParamType::Array(Box::new(ethabi::ParamType::Address)),
// internal_type: Some(String::from("address[]")),
// },
ethabi::Param {
name: String::from("ttl"),
kind: ethabi::ParamType::Uint(64),
name: String::from("params"),
kind: ethabi::ParamType::Tuple(vec![
// requestId
ethabi::ParamType::Uint(64),
// requester
ethabi::ParamType::Address,
// operatorsWithPreferences
ethabi::ParamType::Array(Box::new(
OperatorPreferences::to_ethabi_param_type(),
)),
// requestInputs
ethabi::ParamType::Bytes,
// permittedCallers
ethabi::ParamType::Array(Box::new(ethabi::ParamType::Address)),
// ttl
ethabi::ParamType::Uint(64),
// payment asset
Asset::<T::AssetId>::to_ethabi_param_type(),
// value
ethabi::ParamType::Uint(256),
]),
internal_type: None,
},
],
Expand All @@ -506,21 +494,25 @@ impl<T: Config> Pallet<T> {
},
&[
Token::Uint(ethabi::Uint::from(blueprint_id)),
Token::Uint(ethabi::Uint::from(request_id)),
Token::Address(T::EvmAddressMapping::into_address(requester.clone())),
Token::Array(operators.iter().map(OperatorPreferences::to_ethabi).collect()),
Token::Bytes(Field::encode_to_ethabi(request_args)),
Token::Array(
permitted_callers
.iter()
.map(|caller| {
Token::Address(T::EvmAddressMapping::into_address(caller.clone()))
.clone()
})
.collect(),
),
// Token::Array(vec![]),
Token::Uint(ethabi::Uint::from(ttl.into())),
Token::Tuple(vec![
Token::Uint(ethabi::Uint::from(request_id)),
Token::Address(T::EvmAddressMapping::into_address(requester.clone())),
Token::Array(operators.iter().map(OperatorPreferences::to_ethabi).collect()),
Token::Bytes(Field::encode_to_ethabi(request_args)),
Token::Array(
permitted_callers
.iter()
.map(|caller| {
Token::Address(T::EvmAddressMapping::into_address(caller.clone()))
.clone()
})
.collect(),
),
// Token::Array(vec![]),
Token::Uint(ethabi::Uint::from(ttl.into())),
paymet_asset.to_ethabi(),
Token::Uint(ethabi::Uint::from(value.using_encoded(U256::from_little_endian))),
]),
],
value,
)
Expand Down
56 changes: 29 additions & 27 deletions pallets/services/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -870,34 +870,35 @@ pub mod module {
preferences.push(prefs);
}

// Payment transfer
match payment_asset {
// Handle the case of native currency.
Asset::Custom(asset_id) if asset_id == Zero::zero() => {
T::Currency::transfer(
&caller,
&Self::account_id(),
value,
ExistenceRequirement::KeepAlive,
)?;
},
Asset::Custom(asset_id) => {
T::Fungibles::transfer(
asset_id,
&caller,
&Self::account_id(),
value,
Preservation::Preserve,
)?;
},
Asset::Erc20(token) => {
let (success, _weight) =
Self::erc20_transfer(token, &caller, Self::address(), value)?;
ensure!(success, Error::<T>::ERC20TransferFailed);
},
};
if value != Zero::zero() {
// Payment transfer
match payment_asset {
// Handle the case of native currency.
Asset::Custom(asset_id) if asset_id == Zero::zero() => {
T::Currency::transfer(
&caller,
&Self::account_id(),
value,
ExistenceRequirement::KeepAlive,
)?;
},
Asset::Custom(asset_id) => {
T::Fungibles::transfer(
asset_id,
&caller,
&Self::account_id(),
value,
Preservation::Preserve,
)?;
},
Asset::Erc20(token) => {
let (success, _weight) =
Self::erc20_transfer(token, &caller, Self::address(), value)?;
ensure!(success, Error::<T>::ERC20TransferFailed);
},
};
}

// Transfer the request value to the pallet
let service_id = Self::next_instance_id();
let (allowed, _weight) = Self::on_request_hook(
&blueprint,
Expand All @@ -909,6 +910,7 @@ pub mod module {
&permitted_callers,
&assets,
ttl,
payment_asset,
value,
)?;

Expand Down
2 changes: 1 addition & 1 deletion pallets/services/src/test-artifacts/CGGMP21Blueprint.hex
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0x60806040526004361061012a5760003560e01c8063884673ac116100ab578063a4d91fe91161006f578063a4d91fe9146102e3578063bb43abd914610301578063d7deb4821461030f578063e926cbd11461012f578063f84076621461032f578063fe0dd3711461034f57600080fd5b8063884673ac146102585780639838caa314610280578063987ab9db1461028e5780639d0410ee146102b5578063a24e8a90146102c857600080fd5b8063434698bb116100f2578063434698bb146101e15780635d79ea291461020157806365ce59fa1461022557806374ceeb55146101ae578063821c7be21461023857600080fd5b80630af7d7431461012f5780630b6535d7146101515780630d0dd3991461017157806314b4df4c146101ae57806337c29662146101ce575b600080fd5b34801561013b57600080fd5b5061014f61014a366004610638565b61035d565b005b34801561015d57600080fd5b5061014f61016c3660046106bb565b6103a9565b34801561017d57600080fd5b50600054610191906001600160a01b031681565b6040516001600160a01b0390911681526020015b60405180910390f35b3480156101ba57600080fd5b506101916101c93660046106fe565b503090565b61014f6101dc366004610738565b610432565b3480156101ed57600080fd5b5061014f6101fc3660046107fd565b610478565b34801561020d57600080fd5b5061021760015481565b6040519081526020016101a5565b61014f61023336600461087d565b6104b7565b34801561024457600080fd5b5061014f610253366004610947565b6104fe565b34801561026457600080fd5b5061019173111111111111111111111111111111111111111181565b61014f61014a366004610994565b34801561029a57600080fd5b50731111111111111111111111111111111111111111610191565b61014f6102c3366004610a09565b61053e565b3480156102d457600080fd5b5061014f610253366004610a71565b3480156102ef57600080fd5b506000546001600160a01b0316610191565b61014f6102c3366004610a9b565b34801561031b57600080fd5b5061014f61032a366004610aef565b61057f565b34801561033b57600080fd5b50600254610191906001600160a01b031681565b61014f6101fc3660046107fd565b6000546001600160a01b031633146103a257600054604051630c423fcf60e01b81526103999133916001600160a01b0390911690600401610b73565b60405180910390fd5b5050505050565b33731111111111111111111111111111111111111111146103f557337311111111111111111111111111111111111111116040516359afe8af60e11b8152600401610399929190610b73565b6001600160401b0392909216600155600280546001600160a01b039283166001600160a01b03199182161790915560008054929093169116179055565b6000546001600160a01b0316331461046e57600054604051630c423fcf60e01b81526103999133916001600160a01b0390911690600401610b73565b5050505050505050565b6000546001600160a01b031633146104b457600054604051630c423fcf60e01b81526103999133916001600160a01b0390911690600401610b73565b50565b6000546001600160a01b031633146104f357600054604051630c423fcf60e01b81526103999133916001600160a01b0390911690600401610b73565b505050505050505050565b6000546001600160a01b0316331461053a57600054604051630c423fcf60e01b81526103999133916001600160a01b0390911690600401610b73565b5050565b6000546001600160a01b0316331461057a57600054604051630c423fcf60e01b81526103999133916001600160a01b0390911690600401610b73565b505050565b6000546001600160a01b031633146105bb57600054604051630c423fcf60e01b81526103999133916001600160a01b0390911690600401610b73565b505050505050565b80356001600160401b03811681146105da57600080fd5b919050565b60008083601f8401126105f157600080fd5b5081356001600160401b0381111561060857600080fd5b60208301915083602082850101111561062057600080fd5b9250929050565b803560ff811681146105da57600080fd5b60008060008060006080868803121561065057600080fd5b610659866105c3565b945060208601356001600160401b0381111561067457600080fd5b610680888289016105df565b9095509350610693905060408701610627565b949793965091946060013592915050565b80356001600160a01b03811681146105da57600080fd5b6000806000606084860312156106d057600080fd5b6106d9846105c3565b92506106e7602085016106a4565b91506106f5604085016106a4565b90509250925092565b60006020828403121561071057600080fd5b610719826105c3565b9392505050565b600060c0828403121561073257600080fd5b50919050565b60008060008060008060008060c0898b03121561075457600080fd5b61075d896105c3565b975061076b60208a01610627565b965061077960408a016105c3565b955060608901356001600160401b038082111561079557600080fd5b6107a18c838d01610720565b965060808b01359150808211156107b757600080fd5b6107c38c838d016105df565b909650945060a08b01359150808211156107dc57600080fd5b506107e98b828c016105df565b999c989b5096995094979396929594505050565b60006020828403121561080f57600080fd5b81356001600160401b0381111561082557600080fd5b61083184828501610720565b949350505050565b60008083601f84011261084b57600080fd5b5081356001600160401b0381111561086257600080fd5b6020830191508360208260051b850101111561062057600080fd5b600080600080600080600080600060c08a8c03121561089b57600080fd5b6108a48a6105c3565b98506108b260208b016106a4565b975060408a01356001600160401b03808211156108ce57600080fd5b6108da8d838e01610839565b909950975060608c01359150808211156108f357600080fd5b6108ff8d838e016105df565b909750955060808c013591508082111561091857600080fd5b506109258c828d01610839565b9094509250610938905060a08b016105c3565b90509295985092959850929598565b6000806040838503121561095a57600080fd5b82356001600160401b0381111561097057600080fd5b61097c85828601610720565b92505061098b602084016105c3565b90509250929050565b6000806000806000608086880312156109ac57600080fd5b6109b5866105c3565b94506109c360208701610627565b93506109d1604087016105c3565b925060608601356001600160401b038111156109ec57600080fd5b6109f8888289016105df565b969995985093965092949392505050565b600080600060408486031215610a1e57600080fd5b83356001600160401b0380821115610a3557600080fd5b610a4187838801610720565b94506020860135915080821115610a5757600080fd5b50610a64868287016105df565b9497909650939450505050565b60008060408385031215610a8457600080fd5b610a8d836105c3565b915061098b602084016106a4565b600080600060608486031215610ab057600080fd5b83356001600160401b03811115610ac657600080fd5b610ad286828701610720565b935050610ae1602085016105c3565b91506106f560408501610627565b60008060008060008060a08789031215610b0857600080fd5b610b11876105c3565b9550610b1f602088016105c3565b9450610b2d604088016106a4565b935060608701356001600160401b03811115610b4857600080fd5b610b5489828a01610839565b9094509250610b679050608088016105c3565b90509295509295509295565b6001600160a01b039283168152911660208201526040019056fea164736f6c6343000814000a
0x6080604052600436106101355760003560e01c80638b248065116100ab578063a4d91fe91161006f578063a4d91fe914610314578063bb43abd914610332578063d7deb48214610340578063e926cbd114610182578063f840766214610360578063fe0dd3711461038057600080fd5b80638b248065146102a35780639838caa3146102b1578063987ab9db146102bf5780639d0410ee146102e6578063a24e8a90146102f957600080fd5b806337c29662116100fd57806337c2966214610204578063434698bb146102175780635d79ea291461023757806374ceeb55146101e4578063821c7be21461025b578063884673ac1461027b57600080fd5b806308179f351461013a5780630af7d743146101825780630b6535d7146101a45780630d0dd399146101c457806314b4df4c146101e4575b600080fd5b34801561014657600080fd5b506101656101553660046105e3565b506002546001600160a01b031690565b6040516001600160a01b0390911681526020015b60405180910390f35b34801561018e57600080fd5b506101a261019d36600461065f565b61038e565b005b3480156101b057600080fd5b506101a26101bf3660046106e3565b6103da565b3480156101d057600080fd5b50600054610165906001600160a01b031681565b3480156101f057600080fd5b506101656101ff3660046105e3565b503090565b6101a261021236600461073e565b610468565b34801561022357600080fd5b506101a2610232366004610804565b6104b2565b34801561024357600080fd5b5061024d60015481565b604051908152602001610179565b34801561026757600080fd5b506101a2610276366004610841565b6104f5565b34801561028757600080fd5b5061016573111111111111111111111111111111111111111181565b6101a261023236600461088f565b6101a261019d3660046108cb565b3480156102cb57600080fd5b50731111111111111111111111111111111111111111610165565b6101a26102f4366004610941565b610539565b34801561030557600080fd5b506101a26102763660046109aa565b34801561032057600080fd5b506000546001600160a01b0316610165565b6101a26102f43660046109d4565b34801561034c57600080fd5b506101a261035b366004610a29565b61057e565b34801561036c57600080fd5b50600254610165906001600160a01b031681565b6101a2610232366004610804565b6000546001600160a01b031633146103d357600054604051630c423fcf60e01b81523360048201526001600160a01b0390911660248201526044015b60405180910390fd5b5050505050565b337311111111111111111111111111111111111111111461042a576040516359afe8af60e11b815233600482015273111111111111111111111111111111111111111160248201526044016103ca565b67ffffffffffffffff92909216600155600280546001600160a01b039283166001600160a01b03199182161790915560008054929093169116179055565b6000546001600160a01b031633146104a857600054604051630c423fcf60e01b81523360048201526001600160a01b0390911660248201526044016103ca565b5050505050505050565b6000546001600160a01b031633146104f257600054604051630c423fcf60e01b81523360048201526001600160a01b0390911660248201526044016103ca565b50565b6000546001600160a01b0316331461053557600054604051630c423fcf60e01b81523360048201526001600160a01b0390911660248201526044016103ca565b5050565b6000546001600160a01b0316331461057957600054604051630c423fcf60e01b81523360048201526001600160a01b0390911660248201526044016103ca565b505050565b6000546001600160a01b031633146105be57600054604051630c423fcf60e01b81523360048201526001600160a01b0390911660248201526044016103ca565b505050505050565b803567ffffffffffffffff811681146105de57600080fd5b919050565b6000602082840312156105f557600080fd5b6105fe826105c6565b9392505050565b60008083601f84011261061757600080fd5b50813567ffffffffffffffff81111561062f57600080fd5b60208301915083602082850101111561064757600080fd5b9250929050565b803560ff811681146105de57600080fd5b60008060008060006080868803121561067757600080fd5b610680866105c6565b9450602086013567ffffffffffffffff81111561069c57600080fd5b6106a888828901610605565b90955093506106bb90506040870161064e565b949793965091946060013592915050565b80356001600160a01b03811681146105de57600080fd5b6000806000606084860312156106f857600080fd5b610701846105c6565b925061070f602085016106cc565b915061071d604085016106cc565b90509250925092565b600060c0828403121561073857600080fd5b50919050565b60008060008060008060008060c0898b03121561075a57600080fd5b610763896105c6565b975061077160208a0161064e565b965061077f60408a016105c6565b9550606089013567ffffffffffffffff8082111561079c57600080fd5b6107a88c838d01610726565b965060808b01359150808211156107be57600080fd5b6107ca8c838d01610605565b909650945060a08b01359150808211156107e357600080fd5b506107f08b828c01610605565b999c989b5096995094979396929594505050565b60006020828403121561081657600080fd5b813567ffffffffffffffff81111561082d57600080fd5b61083984828501610726565b949350505050565b6000806040838503121561085457600080fd5b823567ffffffffffffffff81111561086b57600080fd5b61087785828601610726565b925050610886602084016105c6565b90509250929050565b6000602082840312156108a157600080fd5b813567ffffffffffffffff8111156108b857600080fd5b820161012081850312156105fe57600080fd5b6000806000806000608086880312156108e357600080fd5b6108ec866105c6565b94506108fa6020870161064e565b9350610908604087016105c6565b9250606086013567ffffffffffffffff81111561092457600080fd5b61093088828901610605565b969995985093965092949392505050565b60008060006040848603121561095657600080fd5b833567ffffffffffffffff8082111561096e57600080fd5b61097a87838801610726565b9450602086013591508082111561099057600080fd5b5061099d86828701610605565b9497909650939450505050565b600080604083850312156109bd57600080fd5b6109c6836105c6565b9150610886602084016106cc565b6000806000606084860312156109e957600080fd5b833567ffffffffffffffff811115610a0057600080fd5b610a0c86828701610726565b935050610a1b602085016105c6565b915061071d6040850161064e565b60008060008060008060a08789031215610a4257600080fd5b610a4b876105c6565b9550610a59602088016105c6565b9450610a67604088016106cc565b9350606087013567ffffffffffffffff80821115610a8457600080fd5b818901915089601f830112610a9857600080fd5b813581811115610aa757600080fd5b8a60208260051b8501011115610abc57600080fd5b602083019550809450505050610ad4608088016105c6565b9050929550929550929556fea164736f6c6343000814000a
Loading

0 comments on commit c137d7b

Please sign in to comment.