Skip to content

Commit

Permalink
Merge branch 'judez/added-embed-pds' into loic/fix-pack-nft-entitlement
Browse files Browse the repository at this point in the history
  • Loading branch information
loic1 authored Jun 12, 2024
2 parents 83c5462 + 5197c2b commit d2a3b36
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 29 deletions.
53 changes: 53 additions & 0 deletions common/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package common

import (
"fmt"
"reflect"
"regexp"
)

var (
placeholderNonFungibleToken = regexp.MustCompile(`"[^"\s]?.*NonFungibleToken(\.cdc)?"`)
placeholderExampleNFT = regexp.MustCompile(`"[^"\s].*ExampleNFT.cdc"`)
placeholderMetadataViews = regexp.MustCompile(`"[^"\s].*MetadataViews.cdc"`)
placeholderFungibleToken = regexp.MustCompile(`"[^"\s].*FungibleToken.cdc"`)
)

type Replacer map[AddressName]*regexp.Regexp

type AddressName string

const (
NonFungibleTokenAddr AddressName = "NonFungibleToken"
)

func NewReplacer() Replacer {
xxx := map[AddressName]*regexp.Regexp{
NonFungibleTokenAddr: placeholderNonFungibleToken,
}
return xxx
}

func (r Replacer) ReplaceAddresses(code string, data interface{}) (string, error) {

val := reflect.ValueOf(data)
if val.Kind() != reflect.Struct {
return "", fmt.Errorf("data must be a struct")
}

var code1 = code
for i := 0; i < val.NumField(); i++ {
field := val.Type().Field(i)
fieldName := field.Name
fieldValue := val.Field(i).String()

placeholder := AddressName(fieldName)
if r[placeholder] == nil {
return "", fmt.Errorf("no placeholder found for %s", placeholder)
}

code1 = r[placeholder].ReplaceAllString(code1, "0x"+fieldValue)
fmt.Printf(`"%s %s"\n`, fieldName, fieldValue)
}
return code1, nil
}
32 changes: 32 additions & 0 deletions common/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package common

import (
"github.com/dapperlabs/studio-platform-smart-contracts/pds"
"testing"
)

func TestGetUserOwnedEditionToMomentMap_Transaction(t *testing.T) {
script, err := pds.Transaction.ReadFile("transactions/deploy/deploy-packNFT-with-auth.cdc")
if err != nil {
t.Fatal(err)
}
t.Log(string(script))
}

func TestGetUserOwnedEditionToMomentMap_Script(t *testing.T) {
script, err := pds.Scripts.ReadFile("scripts/packNFT/balance_packNFT.cdc")
if err != nil {
t.Fatal(err)
}
t.Log(string(script))
dddd := NewReplacer()
aaa, err := dddd.ReplaceAddresses(string(script), struct {
NonFungibleToken string
}{
"0x1",
})
if err != nil {
t.Fatal(err)
}
t.Log(aaa)
}
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module github.com/dapperlabs/studio-platform-smart-contracts

go 1.20
Empty file added go.sum
Empty file.
14 changes: 14 additions & 0 deletions pds/embed.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package pds

import (
"embed"
)

//go:embed transactions/*
var Transactions embed.FS

//go:embed scripts/*
var Scripts embed.FS

//go:embed contracts/*
var Contracts embed.FS
12 changes: 7 additions & 5 deletions pds/transactions/dapperSport/link_providerCap_dapperSport.cdc
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import NonFungibleToken from 0x{{.NonFungibleToken}}
import {{.DapperSportContract}} from 0x{{.DapperSportAddress}}
import NonFungibleToken from "NonFungibleToken"
import DapperSport from "DapperSport"

transaction(NFTProviderPath: PrivatePath) {

prepare(signer: AuthAccount) {
if signer.getCapability<&{NonFungibleToken.Provider}>(NFTProviderPath).check() {
prepare(signer: auth(Capabilities) &Account) {
if signer.capabilities.get<&{NonFungibleToken.Provider}>(at: NFTProviderPath).check() {
return
}
let cap = signer.capabilities.storage.issue<&{NonFungibleToken.Provider}>(target: DapperSport.CollectionStoragePath)

// This needs to be used to allow for PDS to withdraw
signer.link<&{NonFungibleToken.Provider}>( NFTProviderPath, target: {{.DapperSportContract}}.CollectionStoragePath)
signer.capabilities.publish(cap, at: NFTProviderPath)
}

}
24 changes: 14 additions & 10 deletions pds/transactions/flowTokens/transfer_flow_tokens_emulator.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -9,19 +9,24 @@
// This is required because the newly created account requires
// balance for the deployment of the FiatToken contract.
import FungibleToken from 0xee82856bf20e2aa6
import FlowToken from 0x0ae53cb6e3f42a79
import FungibleToken from "FungibleToken"
import FlowToken from "FlowToken"
import FungibleTokenMetadataViews from "FungibleTokenMetadataViews"

transaction(amount: UFix64, to: Address) {

// The Vault resource that holds the tokens that are being transferred
let sentVault: @FungibleToken.Vault
/// FTVaultData metadata view for the token being used
let vaultData: FungibleTokenMetadataViews.FTVaultData
let sentVault: @{FungibleToken.Vault}

prepare(signer: AuthAccount) {
prepare(signer: auth(BorrowValue) &Account) {
self.vaultData = FlowToken.resolveContractView(resourceType: nil, viewType: Type<FungibleTokenMetadataViews.FTVaultData>()) as! FungibleTokenMetadataViews.FTVaultData?
?? panic("ViewResolver does not resolve FTVaultData view")

// Get a reference to he signer's stored vault
let vaultRef = signer.borrow<&FlowToken.Vault>(from: /storage/flowTokenVault)
?? panic("Could not borrow reference to the owner's Vault!")
let vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: self.vaultData.storagePath)
?? panic("Could not borrow reference to the owner's Vault!")

// Withdraw tokens from the signer's stored vault
self.sentVault <- vaultRef.withdraw(amount: amount)
Expand All @@ -30,11 +35,10 @@ transaction(amount: UFix64, to: Address) {
execute {
// Get a reference to the recipient's Receiver
let receiverRef = getAccount(to)
.getCapability(/public/flowTokenReceiver)
.borrow<&{FungibleToken.Receiver}>()
?? panic("Could not borrow receiver reference to the recipient's Vault")
.capabilities.borrow<&{FungibleToken.Receiver}>(self.vaultData.receiverPath)
?? panic("Could not borrow receiver reference to the recipient's Vault")

// Deposit the withdrawn tokens in the recipient's receiver
receiverRef.deposit(from: <-self.sentVault)
}
}
}
15 changes: 9 additions & 6 deletions pds/transactions/packNFT/create_new_packNFT_collection.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@ import PackNFT from "PackNFT"
import NonFungibleToken from "NonFungibleToken"

transaction() {
prepare (issuer: AuthAccount) {
prepare (issuer: auth(Storage, Capabilities) &Account) {

// Check if account already have a PackIssuer resource, if so destroy it
if issuer.borrow<&PackNFT.Collection>(from: PackNFT.CollectionStoragePath) == nil {
issuer.save(<- PackNFT.createEmptyCollection(), to: PackNFT.CollectionStoragePath);
issuer.link<&{NonFungibleToken.CollectionPublic}>(PackNFT.CollectionPublicPath, target: PackNFT.CollectionStoragePath)
?? panic("Could not link Collection Pub Path");
}
if issuer.storage.borrow<&PackNFT.Collection>(from: PackNFT.CollectionStoragePath) == nil {
let collection <- PackNFT.createEmptyCollection(nftType: Type<@PackNFT.NFT>())

issuer.storage.save(<- collection, to: PackNFT.CollectionStoragePath);
issuer.capabilities.publish(
issuer.capabilities.storage.issue<&PackNFT.Collection>(PackNFT.CollectionStoragePath),
at: PackNFT.CollectionPublicPath
)
}
}
}

8 changes: 4 additions & 4 deletions pds/transactions/pds/create_distribution.cdc
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import PDS from "PDS"
import {{.PackNFTName}} from "PackNFT"
import PackNFT from "PackNFT"
import IPackNFT from "IPackNFT"
import NonFungibleToken from "NonFungibleToken"

transaction(title: String, metadata: {String: String}) {
transaction(NFTProviderPathIdentifier: String, title: String, metadata: {String: String}) {
prepare (issuer: auth(BorrowValue, Capabilities) &Account) {

let i = issuer.storage.borrow<auth(PDS.CreateDist) &PDS.PackIssuer>(from: PDS.PackIssuerStoragePath)
?? panic ("issuer does not have PackIssuer resource")

// issuer must have a PackNFT collection
let withdrawCap = issuer.capabilities.storage.issue<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>(StoragePath(identifier: "exampleNFTCollection")!);
let operatorCap = issuer.capabilities.storage.issue<auth(IPackNFT.Operate) &{IPackNFT.IOperator}>({{.PackNFTName}}.OperatorStoragePath);
let withdrawCap = issuer.capabilities.storage.issue<auth(NonFungibleToken.Withdraw) &{NonFungibleToken.Provider}>(StoragePath(identifier: NFTProviderPathIdentifier)!);
let operatorCap = issuer.capabilities.storage.issue<auth(IPackNFT.Operate) &{IPackNFT.IOperator}>(PackNFT.OperatorStoragePath);
assert(withdrawCap.check(), message: "cannot borrow withdraw capability")
assert(operatorCap.check(), message: "cannot borrow operator capability")

Expand Down
4 changes: 2 additions & 2 deletions pds/transactions/pds/mint_packNFT.cdc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import PDS from "PDS"
import {{.PackNFTName}} from "PackNFT"
import PackNFT from "PackNFT"
import NonFungibleToken from "NonFungibleToken"

transaction (distId: UInt64, commitHashes: [String], issuer: Address ) {
prepare(pds: auth(BorrowValue) &Account) {
let recvAcct = getAccount(issuer)
let recv = recvAcct.capabilities.borrow<&{NonFungibleToken.CollectionPublic}>({{.PackNFTName}}.CollectionPublicPath)
let recv = recvAcct.capabilities.borrow<&{NonFungibleToken.CollectionPublic}>(PackNFT.CollectionPublicPath)
?? panic("Unable to borrow Collection Public reference for recipient")
let cap = pds.storage.borrow<&PDS.DistributionManager>(from: PDS.DistManagerStoragePath)
?? panic("pds does not have Dist manager")
Expand Down
4 changes: 2 additions & 2 deletions pds/transactions/pds/settle.cdc
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import PDS from "PDS"
import ExampleNFT from "ExampleNFT"

transaction (distId: UInt64, nftIDs: [UInt64]) {
transaction (NFTProviderPath: String, distId: UInt64, nftIDs: [UInt64]) {
prepare(pds: auth(BorrowValue) &Account) {
let cap = pds.storage.borrow<&PDS.DistributionManager>(from: PDS.DistManagerStoragePath)
?? panic("pds does not have Dist manager")
cap.withdraw(
distId: distId,
nftIDs: nftIDs,
escrowCollectionPublic: PublicPath(identifier: "exampleNFTCollection")!,
escrowCollectionPublic: PublicPath(identifier: NFTProviderPath)!,
)
}
}

0 comments on commit d2a3b36

Please sign in to comment.