Skip to content

Commit

Permalink
Replace get()+borrow() with borrow()
Browse files Browse the repository at this point in the history
  • Loading branch information
SupunS committed Sep 14, 2023
1 parent 4aea8aa commit f6aaf58
Show file tree
Hide file tree
Showing 33 changed files with 202 additions and 231 deletions.
6 changes: 2 additions & 4 deletions contracts/FlowServiceAccount.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,8 @@ access(all) contract FlowServiceAccount {
/// Returns 0 if the account has no default balance
access(all) fun defaultTokenBalance(_ acct: &Account): UFix64 {
var balance = 0.0
if let balanceCap = acct.capabilities.get<&FlowToken.Vault>(/public/flowTokenBalance) {
if let balanceRef = balanceCap.borrow() {
balance = balanceRef.getBalance()
}
if let balanceCap = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance) {
balance = balanceRef.getBalance()
}

return balance
Expand Down
16 changes: 8 additions & 8 deletions contracts/FlowStakingCollection.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ access(all) contract FlowStakingCollection {
access(all) fun doesStakeExist(address: Address, nodeID: String, delegatorID: UInt32?): Bool {
let account = getAccount(address)

let stakingCollectionRef = account.capabilities.get<&StakingCollection>(self.StakingCollectionPublicPath)!.borrow()
let stakingCollectionRef = account.capabilities.borrow<&StakingCollection>(self.StakingCollectionPublicPath)
?? panic("Could not borrow ref to StakingCollection")

return stakingCollectionRef.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID)
Expand All @@ -1049,7 +1049,7 @@ access(all) contract FlowStakingCollection {
access(all) fun getUnlockedTokensUsed(address: Address): UFix64 {
let account = getAccount(address)

let stakingCollectionRef = account.capabilities.get<&StakingCollection>(self.StakingCollectionPublicPath)!.borrow()
let stakingCollectionRef = account.capabilities.borrow<&StakingCollection>(self.StakingCollectionPublicPath)
?? panic("Could not borrow ref to StakingCollection")

return stakingCollectionRef.unlockedTokensUsed
Expand All @@ -1059,7 +1059,7 @@ access(all) contract FlowStakingCollection {
access(all) fun getLockedTokensUsed(address: Address): UFix64 {
let account = getAccount(address)

let stakingCollectionRef = account.capabilities.get<&StakingCollection>(self.StakingCollectionPublicPath)!.borrow()
let stakingCollectionRef = account.capabilities.borrow<&StakingCollection>(self.StakingCollectionPublicPath)
?? panic("Could not borrow ref to StakingCollection")

return stakingCollectionRef.lockedTokensUsed
Expand All @@ -1069,7 +1069,7 @@ access(all) contract FlowStakingCollection {
access(all) fun getNodeIDs(address: Address): [String] {
let account = getAccount(address)

let stakingCollectionRef = account.capabilities.get<&StakingCollection>(self.StakingCollectionPublicPath)!.borrow()
let stakingCollectionRef = account.capabilities.borrow<&StakingCollection>(self.StakingCollectionPublicPath)
?? panic("Could not borrow ref to StakingCollection")

return stakingCollectionRef.getNodeIDs()
Expand All @@ -1079,7 +1079,7 @@ access(all) contract FlowStakingCollection {
access(all) fun getDelegatorIDs(address: Address): [DelegatorIDs] {
let account = getAccount(address)

let stakingCollectionRef = account.capabilities.get<&StakingCollection>(self.StakingCollectionPublicPath)!.borrow()
let stakingCollectionRef = account.capabilities.borrow<&StakingCollection>(self.StakingCollectionPublicPath)
?? panic("Could not borrow ref to StakingCollection")

return stakingCollectionRef.getDelegatorIDs()
Expand All @@ -1089,7 +1089,7 @@ access(all) contract FlowStakingCollection {
access(all) fun getAllNodeInfo(address: Address): [FlowIDTableStaking.NodeInfo] {
let account = getAccount(address)

let stakingCollectionRef = account.capabilities.get<&StakingCollection>(self.StakingCollectionPublicPath)!.borrow()
let stakingCollectionRef = account.capabilities.borrow<&StakingCollection>(self.StakingCollectionPublicPath)
?? panic("Could not borrow ref to StakingCollection")

return stakingCollectionRef.getAllNodeInfo()
Expand All @@ -1099,7 +1099,7 @@ access(all) contract FlowStakingCollection {
access(all) fun getAllDelegatorInfo(address: Address): [FlowIDTableStaking.DelegatorInfo] {
let account = getAccount(address)

let stakingCollectionRef = account.capabilities.get<&StakingCollection>(self.StakingCollectionPublicPath)!.borrow()
let stakingCollectionRef = account.capabilities.borrow<&StakingCollection>(self.StakingCollectionPublicPath)
?? panic("Could not borrow ref to StakingCollection")

return stakingCollectionRef.getAllDelegatorInfo()
Expand All @@ -1109,7 +1109,7 @@ access(all) contract FlowStakingCollection {
access(all) fun getMachineAccounts(address: Address): {String: MachineAccountInfo} {
let account = getAccount(address)

let stakingCollectionRef = account.capabilities.get<&StakingCollection>(self.StakingCollectionPublicPath)!.borrow()
let stakingCollectionRef = account.capabilities.borrow<&StakingCollection>(self.StakingCollectionPublicPath)
?? panic("Could not borrow ref to StakingCollection")

return stakingCollectionRef.getMachineAccounts()
Expand Down
26 changes: 10 additions & 16 deletions contracts/FlowStorageFees.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ access(all) contract FlowStorageFees {
var balance = 0.0
let acct = getAccount(accountAddress)

if let balanceCap = acct.capabilities.get<&FlowToken.Vault>(/public/flowTokenBalance) {
if let balanceRef = balanceCap.borrow() {
balance = balanceRef.getBalance()
}
if let balanceRef = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance) {
balance = balanceRef.getBalance()
}

return self.accountBalanceToAccountStorageCapacity(balance)
Expand All @@ -96,14 +94,12 @@ access(all) contract FlowStorageFees {
var balance = 0.0
let acct = getAccount(accountAddress)

if let balanceCap = acct.capabilities.get<&FlowToken.Vault>(/public/flowTokenBalance) {
if let balanceRef = balanceCap.borrow() {
if accountAddress == payer {
// if the account is the payer, deduct the maximum possible transaction fees from the balance
balance = balanceRef.getBalance().saturatingSubtract(maxTxFees)
} else {
balance = balanceRef.getBalance()
}
if let balanceRef = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance) {
if accountAddress == payer {
// if the account is the payer, deduct the maximum possible transaction fees from the balance
balance = balanceRef.getBalance().saturatingSubtract(maxTxFees)
} else {
balance = balanceRef.getBalance()
}
}

Expand Down Expand Up @@ -160,10 +156,8 @@ access(all) contract FlowStorageFees {
let acct = getAccount(accountAddress)
var balance = 0.0

if let balanceCap = acct.capabilities.get<&FlowToken.Vault>(/public/flowTokenBalance) {
if let balanceRef = balanceCap.borrow() {
balance = balanceRef.getBalance()
}
if let balanceRef = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance) {
balance = balanceRef.getBalance()
}

// get how much should be reserved for storage
Expand Down
18 changes: 9 additions & 9 deletions lib/go/contracts/internal/assets/assets.go

Large diffs are not rendered by default.

Loading

0 comments on commit f6aaf58

Please sign in to comment.