1

I was trying to use views in the v2 contract of quipuswap. This is my code.

But getting the error while executing. Can someone spot where I am going wrong?

Code:

    @sp.entry_point
    def get_swap_details(self):
        swap_details = sp.view('get_swap_min_res', 
                                self.data.dex_address, 
                                sp.record(
                                    swaps = [
                                                sp.record(
                                                    direction = sp.variant("a_to_b", sp.unit),
                                                    pair_id = sp.nat(0)
                                                )
                                            ],
                                    amount_in = sp.nat(1000000)
                                ), 
                                t = sp.TNat
                        ).open_some("Something is Wrong here")

        self.data.current_price = swap_details

Error I am getting is

[
  {
    "kind": "temporary",
    "id": "proto.016-PtMumbai.michelson_v1.runtime_error",
    "contract_handle": "KT1EcQZB7kU3ihQbJqss7ypknbATM7j6Cbuz"
  },
  {
    "kind": "temporary",
    "id": "proto.016-PtMumbai.michelson_v1.script_rejected",
    "location": 36,
    "with": {
      "string": "Something is Wrong here"
    }
  }
]

Please tell me where I am going wrong?

1 Answer 1

1

It's the issue of layouts.

The get_swap_min_res view in this contract takes the input in the following Michelson type:

(pair
    (list %swaps
        (pair (or %direction (unit %a_to_b) (unit %b_to_a))
        (nat %pair_id)))
    (nat %amount_in)
)

But in the above SmartPy contract, there is no mention of the Pair tree structure of the view's params record. So, SmartPy automatically creates a structure of the following form: (line no. 10 here)

(pair nat (list (pair unit nat))) (Pair 1000000 { Pair Unit 0 })

This can be solved by explicitly telling SmartPy to use the exact layout which the other contract is expecting. Like this:

@sp.entry_point
def get_swap_details(self):
    get_swap_min_res_param_type = sp.TRecord(
        swaps = sp.TList(
            sp.TRecord(
                direction = sp.TVariant(
                    a_to_b = sp.TUnit,
                    b_to_a = sp.TUnit
                ),
                pair_id = sp.TNat
            ).layout(("direction", "pair_id"))
        ),
        amount_in = sp.TNat
    ).layout(("swaps", "amount_in"))

    get_swap_min_res_param = sp.record(
        swaps = [
            sp.record(
                direction = sp.variant("a_to_b", sp.unit),
                pair_id = sp.nat(0)
            )
        ],
        amount_in = sp.nat(1000000)
    )

    swap_details = sp.view('get_swap_min_res',
        self.data.dex_address,
        sp.set_type_expr(get_swap_min_res_param, get_swap_min_res_param_type), 
        t = sp.TNat
    ).open_some("Something is Wrong here")
    self.data.current_price = swap_details

Here, we set the type of this record using sp.set_type_expr(), more on this here. This way it send the exact structure which we can check here on line 10

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.