0

So I have this code which uses a modified version of the [guess game] (https://github.com/agocorona/DAppFlow/blob/main/ContractExample/GuessGame.hs) which parameterizes the validateGuess script with a parameter depending on the user hash, so that each user can make a lock to his own scrip instance

I create two instances and the corresponding wallets make a lock of funds to his own instance:

main = Simulator.runSimulationWith handlers $ do

     cid  <- Simulator.activateContract (Wallet 1) GuessGame
     cid2 <- Simulator.activateContract (Wallet 2) GuessGame

    
     waitNSlots 3
     callEndpointOnInstance  cid  "lock" LockParams{secretWord="world", amount= Ada.adaValueOf 200}
     waitNSlots 3
     callEndpointOnInstance  cid2 "lock" LockParams{secretWord="world", amount= Ada.adaValueOf 200}
     waitNSlots 3
     Simulator.logString @(Builtin TestContracts) "Balances at the end of the simulation"
     b <- Simulator.currentBalances
     Simulator.logBalances @(Builtin TestContracts) b 

The balances reflect the operations:

[INFO] Balances at the end of the simulation
[INFO] Wallet 1: 
[INFO]     {, ""}: 99799999990
[INFO] Wallet 2: 
[INFO]     {, ""}: 99799999990
[INFO] Wallet 3: 
[INFO]     {, ""}: 100000000000
[INFO] Wallet 4: 
.....
.....
[INFO] Script 4c7d92f39ce436db8cf07bb207ca4272b255673ce988d9642403d7b8: 
[INFO]     {, ""}: 200000000
[INFO] Script 8d558e1a3cda0d447eb4d2d5ffe0837c355dc1c8f2519e205b37c439: 
[INFO]     {, ""}: 200000000

The scripts have the 200000000 funds allocated and the wallets lost 200000000 + 20 of fees

Now the question is: How I invoke the guess to one of these two instances with a third wallet ?

If I use:

callEndpointOnInstance cid "guess" GuessParams{guessWord="world",userHash=.....}

Then the funds are returned to wallet 1, since it is the wallet with which I initiated the cid contract. How I introduce a third wallet that may guess to each of these two instances?

1
  • The script of the link is the non-parameterized version
    – agocorona
    Commented Oct 29, 2021 at 23:00

1 Answer 1

1

You need to define contracts for every endpoint that you need to call. So, an endpoint for Lock and other one for Guess

Something like this:

data GuessGameContracts = Lock
                     | Guess
    deriving (Eq, Ord, Show, Generic, FromJSON, ToJSON)

instance Pretty GuessGameContracts where
    pretty = viaShow

instance HasDefinitions GuessGameContracts where
    getDefinitions = [Lock, Guess]
    getContract = getGuessGameContracts
    getSchema = getGuessGameSchema

getGuessGameSchema :: GuessGameContracts -> [FunctionSchema FormSchema]
getGuessGameSchema = \case
    Lock   -> Builtin.endpointsToSchemas @GameSchema
    Guess  -> Builtin.endpointsToSchemas @GameSchema

getGuessGameContracts :: GuessGameContracts -> SomeBuiltin
getGuessGameContracts = \case
    Lock   -> SomeBuiltin $ lock
    Guess  -> SomeBuiltin $ guess

handlers :: SimulatorEffectHandlers (Builtin GuessGameContracts)
handlers =
    Simulator.mkSimulatorHandlers def def (interpret (contractHandler Builtin.handleBuiltin))

And use it like this to activate every contract:

cid1  <- Simulator.activateContract (Wallet 1) Lock
cid2  <- Simulator.activateContract (Wallet 2) Lock
cid3  <- Simulator.activateContract (Wallet 3) Guess

You can find an example in Lesson 10 of PPP - Uniswap

1
  • Thanks a lot. I will try it out
    – agocorona
    Commented Oct 31, 2021 at 8:25

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