This transaction involves several steps to prepare and execute the bet placement.
Within the prepare block of the transaction, the code performs the following steps:
It checks if the user's profile copy exists in storage. If not, a new UserSwitchBoard resource is created for the user and saved in the account's storage. A private link is established to access the storage path of the UserSwitchBoard resource.
If the profile copy already exists, it is extracted and saved back to the storage.
The transaction then proceeds to extract tokens from the user's vault to cover the bet amount.
The UserSwitchBoard resource is loaded from storage, and the child bet reference is retrieved from the admin account. This reference is used to create a new UserBet associated with the selected option index and the withdrawn tokens from the vault.
The newly created UserBet is added to the user's profile, and the updated profile is saved back to storage.
By executing this transaction, users can place bets on specific options within a child bet in the FlowBetPalace application. The code ensures the proper creation and storage of the UserBet, as well as the association of the bet with the user's profile.
import FlowBetPalace from 0xd19f554fdb83f838
import FlowToken from 0x7e60df042a9c0868
transaction(amount: UFix64,uuid: String,optionIndex:UInt64) {
prepare(acct: AuthAccount) {
log("start")
// start switchboard if its not started
let profilecopy <- acct.load<@FlowBetPalace.UserSwitchboard>(from: FlowBetPalace.userSwitchBoardStoragePath)
// if there is not any resrource of the profile create one else save the extracted one
if(profilecopy == nil){
//get the user address as required field for the function
let address = acct.address.toString()
//create a new UserSwitchBoard resource
let userSwitchBoardResource <-FlowBetPalace.createUserSwitchBoard(address: address)
//save the resource in account storage
acct.save(<- userSwitchBoardResource,to:FlowBetPalace.userSwitchBoardStoragePath)
//create a private link to the storage path
acct.link<&AnyResource{FlowBetPalace.UserSwitchboardPublicInterface}>(FlowBetPalace.userSwitchBoardPublicPath,target:FlowBetPalace.userSwitchBoardStoragePath)
log("account switchboard created")
// destroy the resource as its null
destroy profilecopy
}else{
// save the extracted resource
// We use the force-unwrap operator to get the value
// out of the optional. It aborts if the optional is nil
acct.save(<-profilecopy!,to:FlowBetPalace.userSwitchBoardStoragePath)
log("account switchboard was already created")
}
//extract money to pay the bet
//
// Get a reference to the signer's stored vault
let vaultRef = acct.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Could not borrow reference to the owner's Vault!")
// Withdraw tokens from the signer's stored vault
let vault <- vaultRef.withdraw(amount: amount)
// extract Profile resource of the account
let profile <- acct.load<@FlowBetPalace.UserSwitchboard>(from: FlowBetPalace.userSwitchBoardStoragePath) ?? panic("user have not started his account")
// get admin account that stores resourced
let accountFlowBetPalace = getAccount(0xd19f554fdb83f838)
log("getting ref")
// get reference of the childBet resource
let childBetRef = accountFlowBetPalace.getCapability<&AnyResource{FlowBetPalace.ChildBetPublicInterface}>(PublicPath(identifier:"betchild".concat(uuid))!)
.borrow() ?? panic("invalid childBet uuid")
log("ref getted")
log(childBetRef)
//create the UserBet
let newUserBet <- childBetRef.newBet(optionIndex: optionIndex, vault: <-vault)
//add bet to the switchboard
profile.addBet(newBet: <-newUserBet)
// save the extracted resource
// We use the force-unwrap operator to get the value
// out of the optional. It aborts if the optional is nil
acct.save(<-profile,to:FlowBetPalace.userSwitchBoardStoragePath)
}
execute {
}