1

In this answer, the answerer suggested that CREATE_CONTRACT could be used to generate a contract address which could then be used as a seed for a pseudorandom number in a contract.

What would a simple example of this look like when using Smartpy?

For context, my end goal would be to provide an endpoint that receives payment, generates a pseudo-random number (via the above method) and then mints an NFT with attributes based partially on the random result.

3

For the example provided by Rafael, you can do the following in SmartPy.

import smartpy as sp

class Creator(sp.Contract):
    def __init__(self):
        self.init(address = sp.none)
        
    @sp.entry_point
    def generate_address(self):
        # Generate the address but don't append the operation
        contract = sp.create_contract_operation(sp.Contract(), sp.unit, sp.tez(0), None);
        self.data.address = sp.some(sp.pack(contract.address))
        
@sp.add_test(name = "generate_address")
def test():
    scenario = sp.test_scenario()
    c1 = Creator()
    scenario += c1
    c1.generate_address()

this.storage.address will contain the "random" value.

Something similar to: 0x050a0000001601294ce4766fe8c9a9aa375b5413de86998127ffd100

2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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