Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Contract factory example #2233

Closed
lock9 opened this issue Jan 14, 2021 · 13 comments · Fixed by #2240
Closed

Contract factory example #2233

lock9 opened this issue Jan 14, 2021 · 13 comments · Fixed by #2240
Labels
Question Used in questions

Comments

@lock9
Copy link
Contributor

lock9 commented Jan 14, 2021

Hi,
How can I create a contract that creates other contract?
I want to allow my smart-contract to vote, but I want to be able to split their votes into multiple candidates. For that to work, I need to deploy one smart contract per candidate.

I would like to know the best way to create and deploy these 'child' contracts. Is there any kind of constructor concept or equivalent feature? Is there an example?

@lock9 lock9 added the Question Used in questions label Jan 14, 2021
@shargon
Copy link
Member

shargon commented Jan 14, 2021

The problem that the hash is based on NefFile and sender, so if you have the NefFile static, you won't be able to deploy multiple contracts with the same file/sender, I think that we can change from sender to TX.Hash

@erikzhang
Copy link
Member

My solution is that we can change the way of generating contract hash.

First, we can add a nonce to nef file. Then the contract hash will be:

contract_hash = hash160(ABORT, sender, checksum(nef))

@lock9
Copy link
Contributor Author

lock9 commented Jan 15, 2021

Will that work Erik? How can we change the nonce before deploying the new contract? (from a smart contract)

Can't we add some sort of constructor? We need a way to customize the deployed contract. I know that we can dismiss the need for a constructor if we add the initialization methods ourselves and call it when we deploy the contract.

@erikzhang
Copy link
Member

How can we change the nonce before deploying the new contract? (from a smart contract)

Use NEWBUFFER, MEMCPY, CAT, SUBSTR.

Or we can add a new method CreateChild() in ContractManagement?

@lock9
Copy link
Contributor Author

lock9 commented Jan 15, 2021

Use NEWBUFFER, MEMCPY, CAT, SUBSTR.

I'm in favor of a more 'developer-friendly' solution. It is too dangerous to change bytes 'manually'. Maybe the second option is better.

@roman-khimov
Copy link
Contributor

Maybe we just take the name from the manifest and add it into the hash mix? This will require preparing a number of manifests, but that should be feasible and I think one would like to have these contracts have different names anyway (like NeoFS alphabet contracts, https://github.com/nspcc-dev/neofs-spec/blob/master/02-blockchain/01-governance.md). Though it makes upgrade somewhat more complicated.

Another option could be adding an explicit name parameter to override (and replace) the one we have in the manifest.

@shargon
Copy link
Member

shargon commented Jan 15, 2021

Why not use the Transaction hash?

@erikzhang
Copy link
Member

Why not use the Transaction hash?

Because you can't compute the hash at the compile time.

@shargon
Copy link
Member

shargon commented Jan 15, 2021

Because you can't compute the hash at the compile time.

Then we can use a nonce based on the data (#2239).

@erikzhang
Copy link
Member

Then we can use a nonce based on the data (#2239).

But there is no standard on the data.

@shargon
Copy link
Member

shargon commented Jan 15, 2021

Add a new field on Deploy?

@erikzhang
Copy link
Member

Too many fields. What about add CreateChild()? It create a cloned contract with different hash.

@Tommo-L
Copy link
Contributor

Tommo-L commented Jan 15, 2021

Maybe we can do it like this.

Upload -> Contract Nef file ---- instantiation(+add params to invoke deploy method) --->  Contract A
                            ---- instantiation ----> Contract B

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question Used in questions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants