--- a/runtime/src/chain_extension.rs +++ b/runtime/src/chain_extension.rs @@ -78,10 +78,13 @@ /// The chain Extension of NFT pallet pub struct NFTExtension; +pub type NftWeightInfoOf = ::WeightInfo; + impl ChainExtension for NFTExtension { fn call(func_id: u32, env: Environment) -> Result where E: Ext, + C: pallet_nft::Config, ::AccountId: UncheckedFrom<::Hash> + AsRef<[u8]>, { // The memory of the vm stores buf in scale-codec @@ -89,6 +92,7 @@ 0 => { let mut env = env.buf_in_buf_out(); let input: NFTExtTransfer = env.read_as()?; + env.charge_weight(NftWeightInfoOf::::transfer())?; let collection = pallet_nft::Module::::get_collection(input.collection_id)?; @@ -101,12 +105,13 @@ )?; pallet_nft::Module::::submit_logs(collection)?; - Ok(RetVal::Converging(func_id)) + Ok(RetVal::Converging(0)) }, 1 => { // Create Item let mut env = env.buf_in_buf_out(); let input: NFTExtCreateItem = env.read_as()?; + env.charge_weight(NftWeightInfoOf::::create_item(input.data.len()))?; let collection = pallet_nft::Module::::get_collection(input.collection_id)?; @@ -118,12 +123,17 @@ )?; pallet_nft::Module::::submit_logs(collection)?; - Ok(RetVal::Converging(func_id)) + Ok(RetVal::Converging(0)) }, 2 => { // Create multiple items let mut env = env.buf_in_buf_out(); let input: NFTExtCreateMultipleItems = env.read_as()?; + env.charge_weight(NftWeightInfoOf::::create_item( + input.data.iter() + .map(|i| i.len()) + .sum() + ))?; let collection = pallet_nft::Module::::get_collection(input.collection_id)?; @@ -135,12 +145,13 @@ )?; pallet_nft::Module::::submit_logs(collection)?; - Ok(RetVal::Converging(func_id)) + Ok(RetVal::Converging(0)) }, 3 => { // Approve let mut env = env.buf_in_buf_out(); let input: NFTExtApprove = env.read_as()?; + env.charge_weight(NftWeightInfoOf::::approve())?; let collection = pallet_nft::Module::::get_collection(input.collection_id)?; @@ -153,12 +164,13 @@ )?; pallet_nft::Module::::submit_logs(collection)?; - Ok(RetVal::Converging(func_id)) + Ok(RetVal::Converging(0)) }, 4 => { // Transfer from let mut env = env.buf_in_buf_out(); let input: NFTExtTransferFrom = env.read_as()?; + env.charge_weight(NftWeightInfoOf::::transfer_from())?; let collection = pallet_nft::Module::::get_collection(input.collection_id)?; @@ -172,12 +184,13 @@ )?; pallet_nft::Module::::submit_logs(collection)?; - Ok(RetVal::Converging(func_id)) + Ok(RetVal::Converging(0)) }, 5 => { // Set variable metadata let mut env = env.buf_in_buf_out(); let input: NFTExtSetVariableMetaData = env.read_as()?; + env.charge_weight(NftWeightInfoOf::::set_variable_meta_data())?; let collection = pallet_nft::Module::::get_collection(input.collection_id)?; @@ -189,12 +202,13 @@ )?; pallet_nft::Module::::submit_logs(collection)?; - Ok(RetVal::Converging(func_id)) + Ok(RetVal::Converging(0)) }, 6 => { // Toggle whitelist let mut env = env.buf_in_buf_out(); let input: NFTExtToggleWhiteList = env.read_as()?; + env.charge_weight(NftWeightInfoOf::::add_to_white_list())?; let collection = pallet_nft::Module::::get_collection(input.collection_id)?; @@ -206,10 +220,10 @@ )?; pallet_nft::Module::::submit_logs(collection)?; - Ok(RetVal::Converging(func_id)) + Ok(RetVal::Converging(0)) } _ => { - panic!("Passed unknown func_id to test chain extension: {}", func_id); + Err(DispatchError::Other("unknown chain_extension func_id")); } } }