3

I am developing a Pallet and I defined this type:

type BalanceOf<T> =
        <<T as Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance;

and my question is how to add value of this tmp value with input amount:

let mut tmp:BalanceOf<T>;
pub fn mint(
            owner: &T::AccountId,
            amount: BalanceOf<T>,
        ) -> Result<[u8; 3], DispatchError> {
   // How to increase tmp value with input amount
}

my Cargo.toml of pallet: and it caused: Caused by: Dependency 'sp-runtime' has different source paths depending on the build target. Each dependency must have a single canonical source path irrespective of build target.

[package]
name = 'pallet-nft-wcn'
version = '4.0.0-dev'
description = 'FRAME pallet template for defining custom runtime logic.'
authors = ['Substrate DevHub <https://github.com/substrate-developer-hub>']
homepage = 'https://substrate.io/'
edition = '2021'
license = 'Unlicense'
publish = false
repository = "https://github.com/substrate-developer-hub/substrate-node-template/"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = [
    "derive",
] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
frame-support = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.17"}
frame-system = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.17" }
frame-benchmarking = { default-features = false, version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.17", optional = true }
sp-runtime = { version = "5.0.0"}
serde = { version = "1.0", features = ["derive"] }
sp-std = { default-features = false, version = '3.0.0' }


[dev-dependencies]
sp-core = { default-features = false, version = "5.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.17" }
sp-io = { default-features = false, version = "5.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.17" }
sp-runtime = { default-features = false, version = "5.0.0", git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.17" }

[features]
default = ["std"]
std = [
    "codec/std",
    "scale-info/std",
    "frame-support/std",
    "frame-system/std",
    "frame-benchmarking/std",
]

runtime-benchmarks = ["frame-benchmarking/runtime-benchmarks"]
try-runtime = ["frame-support/try-runtime"]
3
  • 1
    In rust, variables are generally defined inside functions unless they are statics or constants.
    – Squirrel
    Mar 29, 2022 at 10:26
  • @Squirrel yes, it's consider as global variable #[pallet::storage] Mar 29, 2022 at 10:28
  • 1
    My apologies - I did not know that that was possible. Those sneaky macros!
    – Squirrel
    Mar 29, 2022 at 10:48

2 Answers 2

3

I think you need to provide more clarity what is failing for you, but this works fine:

fn add_balance(balance1: BalanceOf<T>, balance2: BalanceOf<T>) -> BalanceOf<T> {
    use sp_runtime::traits::Saturating;
    balance1.saturating_add(balance2)
}
8
  • I have to declared sp_runtime in dependencies in Cargo.toml, but I encounter this error: Dependency 'sp-runtime' has different source paths depending on the build target. Each dependency must have a single canonical source path irrespective of build target. Mar 30, 2022 at 2:27
  • You need to share your TOML file. Maybe you want to ask a new question and we can help you there.
    – Shawn Tabrizi
    Mar 30, 2022 at 2:35
  • I'm using the latest substrate-node-template, and please see my updated question Mar 30, 2022 at 2:39
  • please take a look at my answer, I have updated. Mar 31, 2022 at 2:13
  • Sorry Rong, but you didnt even spend a second to think about how you imported this crate, and to compare it to any other crate in that file and how it is being imported. Please follow patterns outlined by the other imports of substrate crates.
    – Shawn Tabrizi
    Mar 31, 2022 at 2:18
7

There is a Saturating math trait in the arithmetic primitives - you can use that:

use sp_arithmetic::traits::Saturating;
tmp.saturating_accrue(amount);

You can also use AddAssign or CheckedAdd traits if you either don't care about overflow(!) or want to handle it other than by saturating.

2
  • I encounted this error: error[E0599]: the method 'saturating_accrue' exists for associated type '<<T as pallet::Config>::Currency as Currency<<T as frame_system::Config>::AccountId>>::Balance', but its trait bounds were not satisfied. Thank for your answer! Mar 29, 2022 at 11:23
  • I defined BalanceOf<T> as Type, So I cannot implement trait for it Mar 29, 2022 at 11:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.