Search code examples
smartcontractsscrypto

Scrypto: ResourceCheckFailure when calling method


I have this method in my component that is supposed to return a GumBall tokens after the user sends a payment:

pub fn buy_gumball(&self, payment: Bucket) -> Bucket {
    self.payments.put(payment.take(self.gumball_cost));
            
    self.gumball_vault.take(1)
}

When I call that method, I get a ResourceCheckFailure:

> resim call-method [component_address] buy_gumball 10,030000000000000000000000000000000000000000000000000004

Instructions:
├─ DeclareTempBucket
├─ CallMethod { component_address: 02c1897261516ff0597fded2b19bf2472ff97b2d791ea50bd02ab2, method: "withdraw", args: [10, 030000000000000000000000000000000000000000000000000004] }
├─ TakeFromContext { amount: 10, resource_address: 030000000000000000000000000000000000000000000000000004, to: Bid(0) }
├─ CallMethod { component_address: 0268709f61e9f60d5d8b8157b5d4939511f194a9f6cfd8656db600, method: "buy_gumball", args: [Bid(0)] }
├─ DropAllBucketRefs
├─ DepositAllBuckets { account: 02c1897261516ff0597fded2b19bf2472ff97b2d791ea50bd02ab2 }
└─ End { signers: [04005feceb66ffc86f38d952786c6d696c79c2dbc239dd4e91b46729d73a27fb57e9] }
Results:
├─ Ok(None)
├─ Ok(Some(Bid(1)))
├─ Ok(None)
└─ Err(ResourceCheckFailure)
Logs: 0
New Entities: 0

Any idea why I get this ?


Solution

  • The Radix Engine makes sure that all buckets are either returned, stored in a vault or burned at the end of a transaction. This is to be confident that no resources will ever get lost because a developer forgot to put the bucket content somewhere.

    Imagine you would have sent more than the required payment. The extra XRD would then be lost if the RE didn't do those checks.

    In your case, I would suggest to return the payment bucket back to the caller:

    pub fn buy_gumball(&self, payment: Bucket) -> (Bucket, Bucket) {
        self.payments.put(payment.take(self.gumball_cost));
    
        (self.gumball_vault.take(1), payment)
    }
    

    That way, if the user sends more XRD than required, they will get their change back and RE will be happy.