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 user ability to unlock with authorized deposit #82

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
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
17 changes: 17 additions & 0 deletions escrow/contracts/Escrow.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*/

import NonFungibleToken from "NonFungibleToken"
import NFTLocker from "NFTLocker"

access(all) contract Escrow {
// Event emitted when a new leaderboard is created.
Expand Down Expand Up @@ -231,6 +232,22 @@ access(all) contract Escrow {
}
}

// Handler for depositing NFTs to the Escrow Collection, used by the NFTLocker contract.
access(all) struct DepositHandler: NFTLocker.IAuthorizedDepositHandler {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deposit handler definition to unlock a locked NFT and add into escrow leaderboard

access(all) fun deposit(nft: @{NonFungibleToken.NFT}, ownerAddress: Address, passThruParams: {String: AnyStruct}) {
// Get leaderboard name from pass-thru parameters
let leaderboardName = passThruParams["leaderboardName"] as! String?
?? panic("Missing or invalid 'leaderboardName' entry in pass-thru parameters map")

// Get the Escrow Collection public reference
let escrowCollectionPublic = Escrow.account.capabilities.borrow<&Escrow.Collection>(Escrow.CollectionPublicPath)
?? panic("Could not borrow a reference to the public leaderboard collection")

// Add the NFT to the escrow leaderboard
escrowCollectionPublic.addEntryToLeaderboard(nft: <-nft, leaderboardName: leaderboardName, ownerAddress: ownerAddress)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

}
}

// Escrow contract initializer.
init() {
// Initialize paths.
Expand Down
16 changes: 15 additions & 1 deletion escrow/lib/go/test/templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ const (
AllDayAddressPlaceholder = "\"AllDay\""
royaltyAddressPlaceholder = "0xALLDAYROYALTYADDRESS"
escrowAddressPlaceholder = "\"Escrow\""
nftLockerAddressPlaceholder = "\"NFTLocker\""
escrowAddressPlaceholderBis = "0xf8d6e0586b0a20c7"
)

const (
EscrowPath = "../../../contracts/Escrow.cdc"
AllDayPath = "../../../contracts/AllDay.cdc"
NFTLockerPath = "../../../../locked-nft/contracts/NFTLocker.cdc"
EscrowTransactionsRootPath = "../../../transactions"
EscrowScriptsRootPath = "../../../scripts"

Expand Down Expand Up @@ -111,12 +113,24 @@ func LoadAllDay(nftAddress, metaAddress, viewResolverAddress, royaltyAddress flo
return code
}

func LoadEscrow(nftAddress flow.Address) []byte {
func LoadEscrow(nftAddress, nftLockerAddress flow.Address) []byte {
code := readFile(EscrowPath)

nftRe := regexp.MustCompile(nftAddressPlaceholder)
code = nftRe.ReplaceAll(code, []byte("0x"+nftAddress.String()))

nftLockerRe := regexp.MustCompile(nftLockerAddressPlaceholder)
code = nftLockerRe.ReplaceAll(code, []byte("0x"+nftLockerAddress.String()))

return code
}

func LoadNFTLockerContract(nftAddress flow.Address) []byte {
code := readFile(NFTLockerPath)

nftRe := regexp.MustCompile(nftAddressPlaceholder)
code = nftRe.ReplaceAll(code, []byte("0x"+nftAddress.String()))

return code
}

Expand Down
34 changes: 30 additions & 4 deletions escrow/lib/go/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,25 +115,51 @@ func EscrowContracts(t *testing.T, b *emulator.Blockchain) Contracts {
_, err = b.CommitBlock()
require.NoError(t, err)

EscrowCode := LoadEscrow(nftAddress)
NFTLockerCode := LoadNFTLockerContract(nftAddress)

tx1 = sdktemplates.AddAccountContract(
tx2 := sdktemplates.AddAccountContract(
AllDayAddress,
sdktemplates.Contract{
Name: "NFTLocker",
Source: string(NFTLockerCode),
},
)

tx2.
SetComputeLimit(100).
SetProposalKey(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().SequenceNumber).
SetPayer(b.ServiceKey().Address)

require.NoError(t, err)
signAndSubmit(
t, b, tx2,
[]flow.Address{b.ServiceKey().Address, AllDayAddress},
[]crypto.Signer{signer, AllDaySigner},
false,
)

_, err = b.CommitBlock()
require.NoError(t, err)

EscrowCode := LoadEscrow(nftAddress, AllDayAddress)

tx3 := sdktemplates.AddAccountContract(
AllDayAddress,
sdktemplates.Contract{
Name: "Escrow",
Source: string(EscrowCode),
},
)

tx1.
tx3.
SetComputeLimit(100).
SetProposalKey(b.ServiceKey().Address, b.ServiceKey().Index, b.ServiceKey().SequenceNumber).
SetPayer(b.ServiceKey().Address)

signer, err = b.ServiceKey().Signer()
require.NoError(t, err)
signAndSubmit(
t, b, tx1,
t, b, tx3,
[]flow.Address{b.ServiceKey().Address, AllDayAddress},
[]crypto.Signer{signer, AllDaySigner},
false,
Expand Down
Loading
Loading