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 06c9217
Show file tree
Hide file tree
Showing 29 changed files with 85 additions and 114 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
4 changes: 2 additions & 2 deletions transactions/dkg/create_participant.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ import FlowDKG from 0xDKGADDRESS
transaction(address: Address, nodeID: String) {

prepare(signer: auth(SaveValue) &Account) {
let admin = getAccount(address).capabilities.get<&FlowDKG.Admin>(/public/dkgAdmin)!
.borrow() ?? panic("Could not borrow admin reference")
let admin = getAccount(address).capabilities.borrow<&FlowDKG.Admin>(/public/dkgAdmin)
?? panic("Could not borrow admin reference")

let dkgParticipant <- admin.createParticipant(nodeID: nodeID)

Expand Down
3 changes: 1 addition & 2 deletions transactions/flowToken/mint_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ transaction(recipient: Address, amount: UFix64) {
?? panic("Signer is not the token admin")

self.tokenReceiver = getAccount(recipient)
.capabilities.get<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)!
.borrow()
.capabilities.borrow<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
?? panic("Unable to borrow receiver reference")
}

Expand Down
3 changes: 1 addition & 2 deletions transactions/flowToken/scripts/get_balance.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ import FlowToken from "FlowToken"
access(all) fun main(account: Address): UFix64 {

let vaultRef = getAccount(account)
.capabilities.get<&FlowToken.Vault{FungibleToken.Balance}>(/public/flowTokenBalance)!
.borrow()
.capabilities.borrow<&FlowToken.Vault{FungibleToken.Balance}>(/public/flowTokenBalance)
?? panic("Could not borrow Balance reference to the Vault")

return vaultRef.balance
Expand Down
3 changes: 1 addition & 2 deletions transactions/flowToken/transfer_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ transaction(amount: UFix64, to: Address) {

// Get a reference to the recipient's Receiver
let receiverRef = getAccount(to)
.capabilities.get<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)!
.borrow()
.capabilities.borrow<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
?? panic("Could not borrow receiver reference to the recipient's Vault")

// Deposit the withdrawn tokens in the recipient's receiver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import FlowIDTableStaking from "FlowIDTableStaking"
access(all) fun main(address: Address): FlowIDTableStaking.DelegatorInfo {

let delegator = getAccount(address)
.capabilities.get<&{FlowIDTableStaking.NodeDelegatorPublic}>(/public/flowStakingDelegator)!
.borrow()
.capabilities.borrow<&{FlowIDTableStaking.NodeDelegatorPublic}>(/public/flowStakingDelegator)
?? panic("Could not borrow reference to delegator object")

return FlowIDTableStaking.DelegatorInfo(nodeID: delegator.nodeID, delegatorID: delegator.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import FlowIDTableStaking from "FlowIDTableStaking"
access(all) fun main(address: Address): FlowIDTableStaking.NodeInfo {

let nodeStaker = getAccount(address)
.capabilities.get<&{FlowIDTableStaking.NodeStakerPublic}>(FlowIDTableStaking.NodeStakerPublicPath)!
.borrow()
.capabilities.borrow<&{FlowIDTableStaking.NodeStakerPublic}>(FlowIDTableStaking.NodeStakerPublicPath)
?? panic("Could not borrow reference to node staker object")

return FlowIDTableStaking.NodeInfo(nodeID: nodeStaker.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ transaction(custodyProviderAddress: Address) {
prepare(admin: auth(BorrowValue) &Account) {

let capabilityReceiver = getAccount(custodyProviderAddress)
.capabilities.get<&LockedTokens.LockedAccountCreator>(
.capabilities.borrow<&LockedTokens.LockedAccountCreator>(
LockedTokens.LockedAccountCreatorPublicPath
)!
.borrow()
)
?? panic("Could not borrow capability receiver reference")

let tokenAdminCollection = admin.capabilities.get<&LockedTokens.TokenAdminCollection>(
Expand Down
3 changes: 1 addition & 2 deletions transactions/lockedTokens/admin/check_main_registration.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ transaction(mainAccount: Address) {
?? panic("Could not borrow a reference to the locked token admin collection")

let lockedAccountInfoRef = getAccount(mainAccount)
.capabilities.get<&LockedTokens.TokenHolder>(LockedTokens.LockedAccountInfoPublicPath)!
.borrow()
.capabilities.borrow<&LockedTokens.TokenHolder>(LockedTokens.LockedAccountInfoPublicPath)
?? panic("Could not borrow a reference to public LockedAccountInfo")

let lockedAccount = lockedAccountInfoRef.getLockedAccountAddress()
Expand Down
5 changes: 2 additions & 3 deletions transactions/lockedTokens/admin/deposit_locked_tokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,9 @@ transaction(to: Address, amount: UFix64) {

// Get a reference to the recipient's Receiver
let receiverRef = recipient
.capabilities.get<&{FungibleToken.Receiver}>(
.capabilities.borrow<&{FungibleToken.Receiver}>(
/public/lockedFlowTokenReceiver
)!
.borrow()
)
?? panic("Could not borrow receiver reference to the recipient's locked Vault")

// Deposit the withdrawn tokens in the recipient's receiver
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ access(all) fun main(tokenAdmin: Address): {Address: UFix64} {

let account = getAccount(tokenAdmin)

let dictionaryReference = account.capabilities.get<&{Address: UFix64}>(/public/unlockingBadAccounts)!.borrow()
let dictionaryReference = account.capabilities.borrow<&{Address: UFix64}>(/public/unlockingBadAccounts)
?? panic("Could not get bad accounts dictionary")

for address in dictionaryReference.keys {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ transaction(unlockInfo: {Address: UFix64}) {
// revert the entire transaction if it fails
// to get the information for a single address
if let lockedAccountInfoRef = getAccount(unlockedAddress)
.capabilities.get<&LockedTokens.TokenHolder>(LockedTokens.LockedAccountInfoPublicPath)!
.borrow() {
.capabilities.borrow<&LockedTokens.TokenHolder>(LockedTokens.LockedAccountInfoPublicPath) {

let lockedAccountAddress = lockedAccountInfoRef.getLockedAccountAddress()

Expand Down
5 changes: 2 additions & 3 deletions transactions/lockedTokens/delegator/get_delegator_id.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import LockedTokens from 0xLOCKEDTOKENADDRESS
access(all) fun main(account: Address): UInt32 {

let lockedAccountInfoRef = getAccount(account)
.capabilities.get<&LockedTokens.TokenHolder>(
.capabilities.borrow<&LockedTokens.TokenHolder>(
LockedTokens.LockedAccountInfoPublicPath
)!
.borrow()
)
?? panic("Could not borrow a reference to public LockedAccountInfo")

return lockedAccountInfoRef.getDelegatorID()!
Expand Down
16 changes: 8 additions & 8 deletions transactions/lockedTokens/delegator/get_delegator_info.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,25 @@ access(all) fun main(account: Address): [FlowIDTableStaking.DelegatorInfo] {

let pubAccount = getAccount(account)

let delegatorCap = pubAccount
.capabilities.get<&{FlowIDTableStaking.NodeDelegatorPublic}>(
let optionalDelegatorRef = pubAccount
.capabilities.borrow<&{FlowIDTableStaking.NodeDelegatorPublic}>(
/public/flowStakingDelegator
)!
)

if let delegatorRef = delegatorCap.borrow() {
if let delegatorRef = optionalDelegatorRef {
let info = FlowIDTableStaking.DelegatorInfo(
nodeID: delegatorRef.nodeID,
delegatorID: delegatorRef.id
)
delegatorInfoArray.append(info)
}

let lockedAccountInfoCap = pubAccount
.capabilities.get<&LockedTokens.TokenHolder>(
let optionalLockedAccountInfoRef = pubAccount
.capabilities.borrow<&LockedTokens.TokenHolder>(
LockedTokens.LockedAccountInfoPublicPath
)!
)

if let lockedAccountInfoRef = lockedAccountInfoCap.borrow() {
if let lockedAccountInfoRef = optionalLockedAccountInfoRef {
let nodeID = lockedAccountInfoRef.getDelegatorNodeID()
let delegatorID = lockedAccountInfoRef.getDelegatorID()

Expand Down
5 changes: 2 additions & 3 deletions transactions/lockedTokens/delegator/get_delegator_node_id.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import LockedTokens from 0xLOCKEDTOKENADDRESS
access(all) fun main(account: Address): String {

let lockedAccountInfoRef = getAccount(account)
.capabilities.get<&LockedTokens.TokenHolder>(
.capabilities.borrow<&LockedTokens.TokenHolder>(
LockedTokens.LockedAccountInfoPublicPath
)!
.borrow()
)
?? panic("Could not borrow a reference to public LockedAccountInfo")

return lockedAccountInfoRef.getDelegatorNodeID()!
Expand Down
5 changes: 2 additions & 3 deletions transactions/lockedTokens/staker/get_node_id.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import LockedTokens from 0xLOCKEDTOKENADDRESS
access(all) fun main(account: Address): String {

let lockedAccountInfoRef = getAccount(account)
.capabilities.get<&LockedTokens.TokenHolder>(
.capabilities.borrow<&LockedTokens.TokenHolder>(
LockedTokens.LockedAccountInfoPublicPath
)!
.borrow()
)
?? panic("Could not borrow a reference to public LockedAccountInfo")

return lockedAccountInfoRef.getNodeID()!
Expand Down
16 changes: 8 additions & 8 deletions transactions/lockedTokens/staker/get_staker_info.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,22 @@ access(all) fun main(account: Address): [FlowIDTableStaking.NodeInfo] {

let pubAccount = getAccount(account)

let nodeStakerCap = pubAccount
.capabilities.get<&{FlowIDTableStaking.NodeStakerPublic}>(
let optionalNodeStakerRef = pubAccount
.capabilities.borrow<&{FlowIDTableStaking.NodeStakerPublic}>(
FlowIDTableStaking.NodeStakerPublicPath
)!
)

if let nodeStakerRef = nodeStakerCap.borrow() {
if let nodeStakerRef = optionalNodeStakerRef {
let info = FlowIDTableStaking.NodeInfo(nodeID: nodeStakerRef.id)
nodeInfoArray.append(info)
}

let lockedAccountInfoCap = pubAccount
.capabilities.get<&LockedTokens.TokenHolder>(
let optionalLockedAccountInfoRef = pubAccount
.capabilities.borrow<&LockedTokens.TokenHolder>(
LockedTokens.LockedAccountInfoPublicPath
)!
)

if let lockedAccountInfoRef = lockedAccountInfoCap.borrow() {
if let lockedAccountInfoRef = optionalLockedAccountInfoRef {
if let nodeID = lockedAccountInfoRef.getNodeID() {
let info = FlowIDTableStaking.NodeInfo(nodeID: nodeID)
nodeInfoArray.append(info)
Expand Down
5 changes: 2 additions & 3 deletions transactions/lockedTokens/user/get_locked_account_address.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import LockedTokens from 0xLOCKEDTOKENADDRESS
access(all) fun main(account: Address): Address {

let lockedAccountInfoRef = getAccount(account)
.capabilities.get<&LockedTokens.TokenHolder>(
.capabilities.borrow<&LockedTokens.TokenHolder>(
LockedTokens.LockedAccountInfoPublicPath
)!
.borrow()
)
?? panic("Could not borrow a reference to public LockedAccountInfo")

return lockedAccountInfoRef.getLockedAccountAddress()
Expand Down
5 changes: 2 additions & 3 deletions transactions/lockedTokens/user/get_locked_account_balance.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import LockedTokens from 0xLOCKEDTOKENADDRESS
access(all) fun main(account: Address): UFix64 {

let lockedAccountInfoRef = getAccount(account)
.capabilities.get<&LockedTokens.TokenHolder>(
.capabilities.borrow<&LockedTokens.TokenHolder>(
LockedTokens.LockedAccountInfoPublicPath
)!
.borrow()
)
?? panic("Could not borrow a reference to public LockedAccountInfo")

return lockedAccountInfoRef.getLockedAccountBalance()
Expand Down
5 changes: 2 additions & 3 deletions transactions/lockedTokens/user/get_multiple_unlock_limits.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ access(all) fun main(accounts: [Address]): [UFix64] {

for account in accounts {
let lockedAccountInfoRef = getAccount(account)
.capabilities.get<&LockedTokens.TokenHolder>(
.capabilities.borrow<&LockedTokens.TokenHolder>(
LockedTokens.LockedAccountInfoPublicPath
)!
.borrow()
)
?? panic("Could not borrow a reference to public LockedAccountInfo")

limits.append(lockedAccountInfoRef.getUnlockLimit())
Expand Down
Loading

0 comments on commit 06c9217

Please sign in to comment.