Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extension field to get ahead of potential unupgradable change #41

Merged
merged 1 commit into from
Nov 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion locked-nft/contracts/NFTLocker.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub contract NFTLocker {
pub let lockedUntil: UInt64
pub let duration: UInt64
pub let nftType: Type
pub let extension: {String: AnyStruct}

init (id: UInt64, owner: Address, duration: UInt64, nftType: Type) {
if let lockedToken = (NFTLocker.lockedTokens[nftType]!)[id] {
Expand All @@ -56,19 +57,21 @@ pub contract NFTLocker {
self.lockedUntil = lockedToken.lockedUntil
self.duration = lockedToken.duration
self.nftType = lockedToken.nftType
self.extension = lockedToken.extension
} else {
self.id = id
self.owner = owner
self.lockedAt = UInt64(getCurrentBlock().timestamp)
self.lockedUntil = self.lockedAt + duration
self.duration = duration
self.nftType = nftType
self.extension = {}
}
}
}

pub fun getNFTLockerDetails(id: UInt64, nftType: Type): NFTLocker.LockedData? {
return (NFTLocker.lockedTokens[nftType]!)[id]!
return (NFTLocker.lockedTokens[nftType]!)[id]
}

/// Determine if NFT can be unlocked
Expand Down
7 changes: 7 additions & 0 deletions locked-nft/lib/go/test/lockednft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func testLockNFT(
)

assert.Equal(t, lockedAt+duration, lockedUntil)
lockedData := getLockedTokenData(
t,
b,
contracts,
exampleNftID,
)
assert.Equal(t, lockedData.LockedUntil, lockedUntil)
}

func TestReLockNFT(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions locked-nft/lib/go/test/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const (
// MetadataViews
MetadataViewsContractsBaseURL = "https://raw.githubusercontent.com/onflow/flow-nft/master/contracts/"
MetadataViewsInterfaceFile = "MetadataViews.cdc"
MetadataFTReplaceAddress = `"./utility/FungibleToken.cdc"`
MetadataNFTReplaceAddress = `"./NonFungibleToken.cdc"`
MetadataFTReplaceAddress = `"FungibleToken"`
MetadataNFTReplaceAddress = `"NonFungibleToken"`

// NFTLocker
GetLockedTokenByIDScriptPath = ScriptsRootPath + "/get_locked_token.cdc"
Expand Down
11 changes: 7 additions & 4 deletions locked-nft/lib/go/test/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,21 @@ import (
)

type LockedData struct {
ID uint64
Owner string
LockedAt uint64
LockedUntil uint64
nftType string
}

func parseLockedData(value cadence.Value) LockedData {
fields := value.(cadence.Struct).Fields
fields := value.(cadence.Optional).Value.(cadence.Struct).Fields

return LockedData{
fields[0].ToGoValue().(string),
fields[1].ToGoValue().(uint64),
fields[0].ToGoValue().(uint64),
fields[1].(cadence.Address).String(),
fields[2].ToGoValue().(uint64),
fields[3].ToGoValue().(string),
fields[3].ToGoValue().(uint64),
fields[5].(cadence.TypeValue).StaticType.ID(),
}
}
5 changes: 3 additions & 2 deletions locked-nft/scripts/get_locked_token.cdc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import NFTLocker from "../contracts/NFTLocker.cdc"
import ExampleNFT from 0xEXAMPLENFTADDRESS

pub fun main(id: UInt64, nftType: Type): NFTLocker.LockedData? {
return NFTLocker.getNFTLockerDetails(id: id, nftType: nftType)
pub fun main(id: UInt64): NFTLocker.LockedData? {
return NFTLocker.getNFTLockerDetails(id: id, nftType: Type<@ExampleNFT.NFT>())
}