Clarity Smart contract function loop if error

46 Views Asked by At

can someone help me understand why my 'get-employee-2nd...' function seems to loop if the check index u1 and returns (ok none) for other indexes.

(define-public (get-employee-2nd-club-

name-from-tuple)
            (let (
                    (employeeT (get employee TUPLE_INPUT))
                    (clubsT (get clubs employeeT))
                    (clubU (element-at clubsT u1))
                    (clubUN (get name clubU))
                    )
                    (ok (print clubUN))
            )
    )

Can you also advise as to why the stx-transfer? call in the transfer function from my 'balances' contract is returning (err u4)?

   (define-public (transfer (amount uint) (recipient principal))
        (begin
            (try! (stx-transfer? amount recipient CALLER))
            (map-set balances recipient (+ (var-get totalTransfers) amount))
            (map-set balances CALLER (- (var-get totalTransfers) amount))
            (var-set totalTransfers (+ amount (var-get totalTransfers)))
            (ok ""SUCCESS"")
        )
    )
1

There are 1 best solutions below

0
On

err u4 for stx-transfer? means that the sender is not the current tx-sender https://docs.stacks.co/docs/write-smart-contracts/clarity-language/language-functions#description-80

The principal that is calling into the function to transfer must be the one sending the STX. Currently you have the sender parameter defined as recipient who is not the user calling into the function, but the one receiving the transferred balance.

As for the first question, I would need to see the rest of the code to provide an answer, there isn't enough context here with what these data-vars are doing and how you are passing data.