Skip to content

Commit

Permalink
integrate ft and nft stable cadence changes, use access, view, and en…
Browse files Browse the repository at this point in the history
…titlements
  • Loading branch information
joshuahannan committed Jul 18, 2023
1 parent a9e9e5d commit 253f985
Show file tree
Hide file tree
Showing 10 changed files with 1,869 additions and 53 deletions.
32 changes: 18 additions & 14 deletions contracts/FlowIDTableStaking.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,8 @@ access(all) contract FlowIDTableStaking {
access(all) let id: String
}

access(all) entitlement NodeOperator

/// Resource that the node operator controls for staking
access(all) resource NodeStaker: NodeStakerPublic {

Expand All @@ -438,7 +440,7 @@ access(all) contract FlowIDTableStaking {
}

/// Change the node's networking address to a new one
access(all) fun updateNetworkingAddress(_ newAddress: String) {
access(NodeOperator) fun updateNetworkingAddress(_ newAddress: String) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot update networking address if the staking auction isn't in progress"
newAddress.length > 0 && newAddress.length <= 510: "The networkingAddress must be less than 510 characters"
Expand All @@ -458,7 +460,7 @@ access(all) contract FlowIDTableStaking {
}

/// Add new tokens to the system to stake during the next epoch
access(all) fun stakeNewTokens(_ tokens: @FungibleToken.Vault) {
access(NodeOperator) fun stakeNewTokens(_ tokens: @FungibleToken.Vault) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot stake if the staking auction isn't in progress"
}
Expand All @@ -481,7 +483,7 @@ access(all) contract FlowIDTableStaking {
}

/// Stake tokens that are in the tokensUnstaked bucket
access(all) fun stakeUnstakedTokens(amount: UFix64) {
access(NodeOperator) fun stakeUnstakedTokens(amount: UFix64) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot stake if the staking auction isn't in progress"
}
Expand Down Expand Up @@ -515,7 +517,7 @@ access(all) contract FlowIDTableStaking {
}

/// Stake tokens that are in the tokensRewarded bucket
access(all) fun stakeRewardedTokens(amount: UFix64) {
access(NodeOperator) fun stakeRewardedTokens(amount: UFix64) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot stake if the staking auction isn't in progress"
}
Expand All @@ -536,7 +538,7 @@ access(all) contract FlowIDTableStaking {
}

/// Request amount tokens to be removed from staking at the end of the next epoch
access(all) fun requestUnstaking(amount: UFix64) {
access(NodeOperator) fun requestUnstaking(amount: UFix64) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot unstake if the staking auction isn't in progress"
}
Expand Down Expand Up @@ -594,7 +596,7 @@ access(all) contract FlowIDTableStaking {

/// Requests to unstake all of the node operators staked and committed tokens
/// as well as all the staked and committed tokens of all of their delegators
access(all) fun unstakeAll() {
access(NodeOperator) fun unstakeAll() {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot unstake if the staking auction isn't in progress"
}
Expand Down Expand Up @@ -624,7 +626,7 @@ access(all) contract FlowIDTableStaking {
}

/// Withdraw tokens from the unstaked bucket
access(all) fun withdrawUnstakedTokens(amount: UFix64): @FungibleToken.Vault {
access(NodeOperator) fun withdrawUnstakedTokens(amount: UFix64): @FungibleToken.Vault {
let nodeRecord = FlowIDTableStaking.borrowNodeRecord(self.id)

emit UnstakedTokensWithdrawn(nodeID: nodeRecord.id, amount: amount)
Expand All @@ -633,7 +635,7 @@ access(all) contract FlowIDTableStaking {
}

/// Withdraw tokens from the rewarded bucket
access(all) fun withdrawRewardedTokens(amount: UFix64): @FungibleToken.Vault {
access(NodeOperator) fun withdrawRewardedTokens(amount: UFix64): @FungibleToken.Vault {
let nodeRecord = FlowIDTableStaking.borrowNodeRecord(self.id)

emit RewardTokensWithdrawn(nodeID: nodeRecord.id, amount: amount)
Expand All @@ -649,6 +651,8 @@ access(all) contract FlowIDTableStaking {
access(all) let nodeID: String
}

access(all) entitlement DelegatorOwner

/// Resource object that the delegator stores in their account to perform staking actions
access(all) resource NodeDelegator: NodeDelegatorPublic {

Expand All @@ -661,7 +665,7 @@ access(all) contract FlowIDTableStaking {
}

/// Delegate new tokens to the node operator
access(all) fun delegateNewTokens(from: @FungibleToken.Vault) {
access(DelegatorOwner) fun delegateNewTokens(from: @FungibleToken.Vault) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot delegate if the staking auction isn't in progress"
}
Expand All @@ -679,7 +683,7 @@ access(all) contract FlowIDTableStaking {
}

/// Delegate tokens from the unstaked bucket to the node operator
access(all) fun delegateUnstakedTokens(amount: UFix64) {
access(DelegatorOwner) fun delegateUnstakedTokens(amount: UFix64) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot delegate if the staking auction isn't in progress"
}
Expand Down Expand Up @@ -708,7 +712,7 @@ access(all) contract FlowIDTableStaking {
}

/// Delegate tokens from the rewards bucket to the node operator
access(all) fun delegateRewardedTokens(amount: UFix64) {
access(DelegatorOwner) fun delegateRewardedTokens(amount: UFix64) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot delegate if the staking auction isn't in progress"
}
Expand All @@ -724,7 +728,7 @@ access(all) contract FlowIDTableStaking {
}

/// Request to unstake delegated tokens during the next epoch
access(all) fun requestUnstaking(amount: UFix64) {
access(DelegatorOwner) fun requestUnstaking(amount: UFix64) {
pre {
FlowIDTableStaking.stakingEnabled(): "Cannot request unstaking if the staking auction isn't in progress"
}
Expand Down Expand Up @@ -766,7 +770,7 @@ access(all) contract FlowIDTableStaking {
}

/// Withdraw tokens from the unstaked bucket
access(all) fun withdrawUnstakedTokens(amount: UFix64): @FungibleToken.Vault {
access(DelegatorOwner) fun withdrawUnstakedTokens(amount: UFix64): @FungibleToken.Vault {
let nodeRecord = FlowIDTableStaking.borrowNodeRecord(self.nodeID)
let delRecord = nodeRecord.borrowDelegatorRecord(self.id)

Expand All @@ -776,7 +780,7 @@ access(all) contract FlowIDTableStaking {
}

/// Withdraw tokens from the rewarded bucket
access(all) fun withdrawRewardedTokens(amount: UFix64): @FungibleToken.Vault {
access(DelegatorOwner) fun withdrawRewardedTokens(amount: UFix64): @FungibleToken.Vault {
let nodeRecord = FlowIDTableStaking.borrowNodeRecord(self.nodeID)
let delRecord = nodeRecord.borrowDelegatorRecord(self.id)

Expand Down
32 changes: 17 additions & 15 deletions contracts/FlowStakingCollection.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,8 @@ access(all) contract FlowStakingCollection {
access(all) fun getMachineAccounts(): {String: MachineAccountInfo}
}

access(all) entitlement CollectionOwner

/// The resource that stakers store in their accounts to store
/// all their staking objects and capability to the locked account object
/// Keeps track of how many locked and unlocked tokens are staked
Expand Down Expand Up @@ -327,7 +329,7 @@ access(all) contract FlowStakingCollection {
/// If the user has used any locked tokens, removing NodeStaker objects is not allowed.
/// We do not clear the machine account field for this node here
/// because the operator may want to keep it the same
access(all) fun removeNode(nodeID: String): @FlowIDTableStaking.NodeStaker? {
access(CollectionOwner) fun removeNode(nodeID: String): @FlowIDTableStaking.NodeStaker? {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil): "Specified node does not exist in this collection"
self.lockedTokensUsed == UFix64(0.0): "Cannot remove node if locked tokens are used"
Expand Down Expand Up @@ -357,7 +359,7 @@ access(all) contract FlowStakingCollection {

/// Function to remove an existing NodeDelegator object.
/// If the user has used any locked tokens, removing NodeDelegator objects is not allowed.
access(all) fun removeDelegator(nodeID: String, delegatorID: UInt32): @FlowIDTableStaking.NodeDelegator? {
access(CollectionOwner) fun removeDelegator(nodeID: String, delegatorID: UInt32): @FlowIDTableStaking.NodeDelegator? {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID): "Specified delegator does not exist in this collection"
self.lockedTokensUsed == UFix64(0.0): "Cannot remove delegator if locked tokens are used"
Expand Down Expand Up @@ -390,7 +392,7 @@ access(all) contract FlowStakingCollection {
/// Operations to register new staking objects
/// Function to register a new Staking Record to the Staking Collection
access(all) fun registerNode(id: String, role: UInt8, networkingAddress: String, networkingKey: String, stakingKey: String, amount: UFix64, payer: AuthAccount): AuthAccount? {
access(CollectionOwner) fun registerNode(id: String, role: UInt8, networkingAddress: String, networkingKey: String, stakingKey: String, amount: UFix64, payer: AuthAccount): AuthAccount? {

let tokens <- self.getTokens(amount: amount)

Expand Down Expand Up @@ -508,7 +510,7 @@ access(all) contract FlowStakingCollection {

/// If a user has created a node before epochs were enabled, they'll need to use this function
/// to create their machine account with their node
access(all) fun createMachineAccountForExistingNode(nodeID: String, payer: AuthAccount): AuthAccount? {
access(CollectionOwner) fun createMachineAccountForExistingNode(nodeID: String, payer: AuthAccount): AuthAccount? {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil)
}
Expand All @@ -535,7 +537,7 @@ access(all) contract FlowStakingCollection {
}

/// Allows the owner to withdraw any available FLOW from their machine account
access(all) fun withdrawFromMachineAccount(nodeID: String, amount: UFix64) {
access(CollectionOwner) fun withdrawFromMachineAccount(nodeID: String, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil): "Specified stake does not exist in this collection"
}
Expand All @@ -554,7 +556,7 @@ access(all) contract FlowStakingCollection {
}

/// Function to register a new Delegator Record to the Staking Collection
access(all) fun registerDelegator(nodeID: String, amount: UFix64) {
access(CollectionOwner) fun registerDelegator(nodeID: String, amount: UFix64) {
let delegatorIDs = self.getDelegatorIDs()
for idInfo in delegatorIDs {
if idInfo.delegatorNodeID == nodeID {
Expand Down Expand Up @@ -598,7 +600,7 @@ access(all) contract FlowStakingCollection {
// and their delegator ID to specify that it is for their delegator object
/// Updates the stored networking address for the specified node
access(all) fun updateNetworkingAddress(nodeID: String, newAddress: String) {
access(CollectionOwner) fun updateNetworkingAddress(nodeID: String, newAddress: String) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil): "Specified stake does not exist in this collection"
}
Expand All @@ -614,7 +616,7 @@ access(all) contract FlowStakingCollection {
}

/// Function to stake new tokens for an existing Stake or Delegation record in the StakingCollection
access(all) fun stakeNewTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
access(CollectionOwner) fun stakeNewTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID): "Specified stake does not exist in this collection"
}
Expand Down Expand Up @@ -657,7 +659,7 @@ access(all) contract FlowStakingCollection {
}

/// Function to stake unstaked tokens for an existing Stake or Delegation record in the StakingCollection
access(all) fun stakeUnstakedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
access(CollectionOwner) fun stakeUnstakedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID): "Specified stake does not exist in this collection"
}
Expand All @@ -679,7 +681,7 @@ access(all) contract FlowStakingCollection {
}

/// Function to stake rewarded tokens for an existing Stake or Delegation record in the StakingCollection
access(all) fun stakeRewardedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
access(CollectionOwner) fun stakeRewardedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID): "Specified stake does not exist in this collection"
}
Expand All @@ -706,7 +708,7 @@ access(all) contract FlowStakingCollection {
}

/// Function to request tokens to be unstaked for an existing Stake or Delegation record in the StakingCollection
access(all) fun requestUnstaking(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
access(CollectionOwner) fun requestUnstaking(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID): "Specified stake does not exist in this collection"
}
Expand All @@ -729,7 +731,7 @@ access(all) contract FlowStakingCollection {

/// Function to unstake all tokens for an existing node staking record in the StakingCollection
/// Only available for node operators
access(all) fun unstakeAll(nodeID: String) {
access(CollectionOwner) fun unstakeAll(nodeID: String) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: nil): "Specified stake does not exist in this collection"
}
Expand All @@ -743,7 +745,7 @@ access(all) contract FlowStakingCollection {
}

/// Function to withdraw unstaked tokens for an existing Stake or Delegation record in the StakingCollection
access(all) fun withdrawUnstakedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
access(CollectionOwner) fun withdrawUnstakedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID): "Specified stake does not exist in this collection"
}
Expand All @@ -766,7 +768,7 @@ access(all) contract FlowStakingCollection {
}

/// Function to withdraw rewarded tokens for an existing Stake or Delegation record in the StakingCollection
access(all) fun withdrawRewardedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
access(CollectionOwner) fun withdrawRewardedTokens(nodeID: String, delegatorID: UInt32?, amount: UFix64) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID): "Specified stake does not exist in this collection"
}
Expand Down Expand Up @@ -810,7 +812,7 @@ access(all) contract FlowStakingCollection {
/// Closes an existing stake or delegation, moving all withdrawable tokens back to the users account and removing the stake
/// or delegator object from the StakingCollection.
access(all) fun closeStake(nodeID: String, delegatorID: UInt32?) {
access(CollectionOwner) fun closeStake(nodeID: String, delegatorID: UInt32?) {
pre {
self.doesStakeExist(nodeID: nodeID, delegatorID: delegatorID): "Specified stake does not exist in this collection"
}
Expand Down
12 changes: 7 additions & 5 deletions contracts/LockedTokens.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ access(all) contract LockedTokens {
access(all) view fun getDelegatorNodeID(): String?
}

access(all) entitlement TokenOperations

/// Stored in Holder unlocked account
access(all) resource TokenHolder: FungibleToken.Receiver, FungibleToken.Provider, LockedAccountInfo {

Expand Down Expand Up @@ -329,13 +331,13 @@ access(all) contract LockedTokens {
/// Withdraws tokens from the locked vault. This will only succeed
/// if the withdraw amount is less than or equal to the limit
access(all) fun withdraw(amount: UFix64): @FungibleToken.Vault {
access(TokenOperations) fun withdraw(amount: UFix64): @FungibleToken.Vault {
return <- self.borrowTokenManager().withdraw(amount: amount)
}

/// The user calls this function if they want to register as a node operator
/// They have to provide all the info for their node
access(all) fun createNodeStaker(nodeInfo: StakingProxy.NodeInfo, amount: UFix64) {
access(TokenOperations) fun createNodeStaker(nodeInfo: StakingProxy.NodeInfo, amount: UFix64) {

self.borrowTokenManager().registerNode(nodeInfo: nodeInfo, amount: amount)

Expand All @@ -345,7 +347,7 @@ access(all) contract LockedTokens {

/// The user calls this function if they want to register as a node operator
/// They have to provide the node ID for the node they want to delegate to
access(all) fun createNodeDelegator(nodeID: String) {
access(TokenOperations) fun createNodeDelegator(nodeID: String) {

self.borrowTokenManager().registerDelegator(nodeID: nodeID, amount: FlowIDTableStaking.getDelegatorMinimumStakeRequirement())

Expand All @@ -355,7 +357,7 @@ access(all) contract LockedTokens {

/// Borrow a "reference" to the staking object which allows the caller
/// to perform all staking actions with locked tokens.
access(all) fun borrowStaker(): LockedNodeStakerProxy {
access(TokenOperations) fun borrowStaker(): LockedNodeStakerProxy {
pre {
self.nodeStakerProxy != nil:
"The NodeStakerProxy doesn't exist!"
Expand All @@ -371,7 +373,7 @@ access(all) contract LockedTokens {

/// Borrow a "reference" to the delegating object which allows the caller
/// to perform all delegating actions with locked tokens.
access(all) fun borrowDelegator(): LockedNodeDelegatorProxy {
access(TokenOperations) fun borrowDelegator(): LockedNodeDelegatorProxy {
pre {
self.nodeDelegatorProxy != nil:
"The NodeDelegatorProxy doesn't exist!"
Expand Down
8 changes: 4 additions & 4 deletions lib/go/contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,8 @@ func FungibleToken() []byte {
}

// FungibleTokenMetadataViews returns the FungibleTokenMetadataViews contract interface.
func FungibleTokenMetadataViews(fungibleTokenAddr, metadataViewsAddr string) []byte {
return ftcontracts.FungibleTokenMetadataViews(fungibleTokenAddr, metadataViewsAddr)
func FungibleTokenMetadataViews(fungibleTokenAddr, metadataViewsAddr, viewResolverAddress string) []byte {
return ftcontracts.FungibleTokenMetadataViews(fungibleTokenAddr, metadataViewsAddr, viewResolverAddress)
}

func NonFungibleToken() []byte {
Expand All @@ -95,8 +95,8 @@ func ViewResolver() []byte {
}

// MetadataViews returns the MetadataViews contract interface.
func MetadataViews(fungibleTokenAddr, nonFungibleTokenAddr string) []byte {
return nftcontracts.MetadataViews(flow.HexToAddress(fungibleTokenAddr), flow.HexToAddress(nonFungibleTokenAddr))
func MetadataViews(fungibleTokenAddr, nonFungibleTokenAddr, viewResolverAddr string) []byte {
return nftcontracts.MetadataViews(flow.HexToAddress(fungibleTokenAddr), flow.HexToAddress(nonFungibleTokenAddr), flow.HexToAddress(viewResolverAddr))
}

// FlowToken returns the FlowToken contract.
Expand Down
Loading

0 comments on commit 253f985

Please sign in to comment.