patractlabs / redspot

Redspot is an Substrate pallet-contracts (ink!) development environment. Compile your contracts and run them on a different networks. Redspot's core forks from Hardhat but changed a lot to suit substrate.
https://redspot.patract.io/
Other
67 stars 22 forks source link

[Question] ContractFactory: put code without instantiation #136

Closed tk-o closed 2 years ago

tk-o commented 2 years ago

I've been playing with a use-case where a smart contract (A) needs to instantiate another smart contract (B). I was not able to have it done using ContractFactory.

Deploying A requires a code hash to B, but ContractFactory.#putCode is only called from inside the ContractFactory.deploy which also instantiates the contract (I don't need it to happen for B).

It would be great if #putCode was public, so there is an explicit way to upload a WASM on-chain without having it instantiated.

What do you think?


Context

From what I see:

I understand that I need to call ContractFactory.#putCode, but it's a private method, so how do I do that?

Originally posted by @tk-o in https://github.com/paritytech/ink/issues/875#issuecomment-905901177

ii-ii-ii commented 2 years ago

As far as I know, putcode is deprecated, as determined by the chain, so there is no way you can just upload wasmcode without instantiating the contract. redspot's putcode function is just for compatibility with older versions of the chain (which had putcode method)

ii-ii-ii commented 2 years ago

The new version of the chain has only instantiateWithCode (which contains both putcode and instantiate) and no separate putcode.

atenjin commented 2 years ago

@tk-o parity does not think they need put_code, though I think it's a bad idea. Look at this issue: https://github.com/paritytech/substrate/issues/8671

I try to convince them to recover put_code, but they do not agree.

So at here, the way in practice is first deploying a useless contract, let the code already on the chain, then you contract which uses the deployed contract can do the normal thing.

parity thinks the 3rd party can hide the process, but we do not think of a good idea to do this. For initing a contract may need some parameters, we can neither identify whether the developer wants to instantiate the contract or just need code, nor can we guess what parameters the developer needs to instantiate the contract.

So at now, developer needs to init the contract by himself to put the code on the chain.

tk-o commented 2 years ago

Thanks for all the details, folks 🤝

I'll be looking into CosmWasm now to see if it's more approachable tech for dApp developers.

athei commented 2 years ago

The solution for this problem is to not instantiate (B) from within (A). You embed the contract address of (B) in (A) and assume it to be instantiated. Contract addresses are deterministic and guarantee to be derived from a specific code hash. So you can even instantiate (B) after instantiating (A).