0

I implemented contract-pallet in runtime:

use pallet_contracts::migration;

pub struct Migrations;
impl OnRuntimeUpgrade for Migrations {
fn on_runtime_upgrade() -> Weight {
 migration::migrate::<Runtime>()
}
}

parameter_types! {
    pub const DepositPerItem: Balance = deposit(1, 0);
    pub const DepositPerByte: Balance = deposit(0, 1);
    pub const MaxValueSize: u32 = 16 * 1024;
    pub const DeletionQueueDepth: u32 = 128;
    pub DeletionWeightLimit: Weight = AVERAGE_ON_INITIALIZE_RATIO * BlockWeights::get().max_block;
    pub Schedule: pallet_contracts::Schedule<Runtime> = Default::default();
}

impl pallet_contracts::Config for Runtime {
    type Time = Timestamp;
    type Randomness = RandomnessCollectiveFlip;
    type Currency = Balances;
    type Event = Event;
    type Call = Call;
    type CallFilter = Nothing;
    type DepositPerItem = DepositPerItem;
    type DepositPerByte = DepositPerByte;
    type CallStack = [pallet_contracts::Frame<Self>; 31];
    type WeightPrice = pallet_transaction_payment::Pallet<Self>;
    type WeightInfo = pallet_contracts::weights::SubstrateWeight<Self>;
    type ChainExtension = ();
    type DeletionQueueDepth = DeletionQueueDepth;
    type DeletionWeightLimit = DeletionWeightLimit;
    type Schedule = Schedule;
    type AddressGenerator = pallet_contracts::DefaultAddressGenerator;
    type ContractAccessWeight = pallet_contracts::DefaultContractAccessWeight<BlockWeights>;
    type MaxCodeLen = ConstU32<{ 128 * 1024 }>;
    type RelaxedMaxCodeLen = ConstU32<{ 256 * 1024 }>;
    type MaxStorageKeyLen = ConstU32<128>;
}

When I upload .contract file, console log 'f.rpc.contracts.instantiate is not a function' error. Then I click next after filling parameter for constructor, console log 'The runtime does not expose api.tx.contracts.instantiateWithCode with storageDepositLimit' error. I tried polkadotjs and contract-ui but both returned the same result. How can I fix these errors? Thanks

1 Answer 1

1

In addition to what you added to the runtime, there are some other code snippets you need as well. I recommend finding all areas where pallet_contracts is used in the substrate-contracts-node.

In your runtime, inside the impl_runtime_apis! you will need to add the following snippet (found here):

impl pallet_contracts_rpc_runtime_api::ContractsApi<Block, AccountId, Balance, BlockNumber, Hash>
        for Runtime
    {
        fn call(
            origin: AccountId,
            dest: AccountId,
            value: Balance,
            gas_limit: u64,
            storage_deposit_limit: Option<Balance>,
            input_data: Vec<u8>,
        ) -> pallet_contracts_primitives::ContractExecResult<Balance> {
            Contracts::bare_call(origin, dest, value, Weight::from_ref_time(gas_limit), storage_deposit_limit, input_data, CONTRACTS_DEBUG_OUTPUT)
        }

        fn instantiate(
            origin: AccountId,
            value: Balance,
            gas_limit: u64,
            storage_deposit_limit: Option<Balance>,
            code: pallet_contracts_primitives::Code<Hash>,
            data: Vec<u8>,
            salt: Vec<u8>,
        ) -> pallet_contracts_primitives::ContractInstantiateResult<AccountId, Balance>
        {
            Contracts::bare_instantiate(origin, value,  Weight::from_ref_time(gas_limit), storage_deposit_limit, code, data, salt, CONTRACTS_DEBUG_OUTPUT)
        }

        fn upload_code(
            origin: AccountId,
            code: Vec<u8>,
            storage_deposit_limit: Option<Balance>,
        ) -> pallet_contracts_primitives::CodeUploadResult<Hash, Balance>
        {
            Contracts::bare_upload_code(origin, code, storage_deposit_limit)
        }

        fn get_storage(
            address: AccountId,
            key: Vec<u8>,
        ) -> pallet_contracts_primitives::GetStorageResult {
            Contracts::get_storage(address, key)
        }
    }

Additionally, you will need to slightly modify the client RPC as well. Inside of node/src/rpc.rs modify create_full (found here):

pub fn create_full<C, P>(
    deps: FullDeps<C, P>,
) -> Result<RpcModule<()>, Box<dyn std::error::Error + Send + Sync>>
where
   // ... snip ...

    // **** ADD THIS ****
    C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId,Balance, BlockNumber, Hash>,
{
    // **** ADD THIS ****
    use pallet_contracts_rpc::{Contracts, ContractsApiServer};

    // ... snip ...

    // Contracts RPC API extension
    // **** ADD THIS ****
    module.merge(Contracts::new(client.clone()).into_rpc())?;

   // ... snip ...
}
4
  • Yeah I have these codes but it's just the same as tutorial I'm following so I didn't post it. I only modified ContractAccessWeight and DeletionWeightLimit type there because of some compile error. Do you think the error is from these type?
    – marethyu
    Commented Sep 17, 2022 at 1:01
  • @marethyu Hmm, okay. You shouldn't need to modify ContractAccessWeight or DeletionWeightLimit from their defaults to get the node to build. Do you have a link to share to the code? Commented Sep 19, 2022 at 19:53
  • If you are only wanting to test ink! smart contracts, you can use the preconfigured contracts node: github.com/paritytech/substrate-contracts-node. These tutorials walk through how to use it: docs.substrate.io/tutorials/smart-contracts Commented Sep 19, 2022 at 19:54
  • Use node template tag polkadot-v0.9.28 works just fine
    – marethyu
    Commented Nov 15, 2022 at 10:44

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.