F
Flow•16mo ago
Giovanni S

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! 😃
3 Replies
Unknown User
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View
Giovanni S
Giovanni S•16mo ago
Just little snippets of code as I answer my own & others' questions? 🤔 That could be fun!
Unknown User
Unknown User•16mo ago
Message Not Public
Sign In & Join Server To View