0

While testing on smart contract file by adding scenario,

@sp.add_test("IntegrationTest")
def test():
    sc = sp.test_scenario()
         helper_contract = deploy_helper_contract()
         sc += helper_contract
         helper_contract.originatContract()

this function originateContract of helper, originates a new contract (contractB) from a smart contract, Now I need to do some test on the originated contract (ContractB) from the test. I have the address of the new originated contract i.e contractB.

How do I call the functions of contract B inside test scenario

1 Answer 1

2

You can use Dynamic Contracts. It is a way to retrieve the contract instances that are originated from other contracts while running in a scenario.

It takes three arguments:

  • contractId is the ID of the originated dynamic contract. It starts from 0 and keeps incrementing.
  • tcontract is the contract class
  • tparameter is the parameter type of the contract.
import smartpy as sp


class ContractB(sp.Contract):
    def __init__(self, **kargs):
        self.init(**kargs)


class ContractA(sp.Contract):
    def __init__(self, **kargs):
        self.init(**kargs)
        self.contractB = ContractB()

    @sp.entry_point
    def create_b(self):
        sp.create_contract(self.contractB, storage=())


@sp.add_test(name = "Dynamic Contract")
def test():
    scenario = sp.test_scenario()
    contractA = ContractA()
    scenario += contractA
    scenario += contractA.contractB

    admin = sp.test_account('admin')
    contractA.create_b().run(sender = admin)

    contractB = scenario.dynamic_contract(0, contractA.contractB)
    scenario.h1('ContractB Retrieved')
    scenario.show(contractB.address)

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.