Giovanni S
Giovanni S
FFlow
Created by Giovanni S on 3/3/2023 in #developer-forums
Updating resource method pre-conditions
Hey all! 👋 I thought of a possibly silly question that others might also wonder about - does updating a resource method's pre-condition in a contract update also update the pre-conditions within existing instances of that resource. The short answer is yes, but here's how I quickly went about confirming... I set up a contract, PizzaPlace with the following:
pub contract PizzaPlace {
pub event OrderPlaced(order: String, customer: Address?)
pub resource Menu {
pub fun submitOrder(_ order: String): String {
pre {
order == "pizza" : "We only serve pizza!"
}
emit OrderPlaced(order: order, customer: self.owner?.address)
}
}

pub fun getMenu(): @Menu {
return <-create Menu()
}
}
pub contract PizzaPlace {
pub event OrderPlaced(order: String, customer: Address?)
pub resource Menu {
pub fun submitOrder(_ order: String): String {
pre {
order == "pizza" : "We only serve pizza!"
}
emit OrderPlaced(order: order, customer: self.owner?.address)
}
}

pub fun getMenu(): @Menu {
return <-create Menu()
}
}
I then deployed my contract, configured a Menu in account storage, and placed an order. Of course, based on the pre-condition, I could only order "pizza" for a successful transaction. PizzaPlace now also makes sandwiches, so I updated Menu.submitOrder() pre-condition to:
pub fun submitOrder(_ order: String) {
pre {
order == "pizza" || order == "sandwich": "We only serve pizza & sandwiches!"
}
emit OrderPlaced(order: order, customer: self.owner?.address)
}
pub fun submitOrder(_ order: String) {
pre {
order == "pizza" || order == "sandwich": "We only serve pizza & sandwiches!"
}
emit OrderPlaced(order: order, customer: self.owner?.address)
}
I then updated the contract. Using the same Menu resource I configured previously, I submitted a transaction with Menu.submitOrder("sandwich") and behold it worked!
Events:
Index 0
Type A.f8d6e0586b0a20c7.PizzaPlace.OrderPlaced
Tx ID 0210d9b89e549decf944ddfb36034d04890e1671197840bb03f7b56ca90c8edf
Values
- order (String): "sandwich"
- customer (Address?): 0xf8d6e0586b0a20c7
Events:
Index 0
Type A.f8d6e0586b0a20c7.PizzaPlace.OrderPlaced
Tx ID 0210d9b89e549decf944ddfb36034d04890e1671197840bb03f7b56ca90c8edf
Values
- order (String): "sandwich"
- customer (Address?): 0xf8d6e0586b0a20c7
In hindsight, it makes total sense this would be the case. Hope this simple example helps some of my fellow devs! 😃
6 replies
FFlow
Created by Giovanni S on 11/21/2022 in #developer-forums
Preventing a function from being called in the same transaction
In the instance two parties have the same Capability on a method, how would you all recommend ensuring that a resource method cannot be called in the same transaction? My current thought is to track the block that the method is called the first time and asserting that the block height of the successive call is greater than the originally recorded height. Can anyone think of a simpler/more elegant solution?
15 replies
FFlow
Created by Giovanni S on 11/17/2022 in #developer-forums
Downcasting from AnyResource?
Trying to figure out a way to allow for generic attachments to NFTs. Does anyone know if we can downcast from &ref as auth &AnyResource ? Something like
let myType: @MyType <- createMyType()
let attachments: @{Type: AnyResource} <- {Type<@MyType>(): myType}

let ref = (&attachments[tag] as auth &AnyResource?)!
let castedRef = ref as &MyType
let myType: @MyType <- createMyType()
let attachments: @{Type: AnyResource} <- {Type<@MyType>(): myType}

let ref = (&attachments[tag] as auth &AnyResource?)!
let castedRef = ref as &MyType
3 replies