Skip to content

Commit

Permalink
Merge pull request #79 from packagelabs/blind-nft-view
Browse files Browse the repository at this point in the history
Add metadata view for blind NFTs
  • Loading branch information
psiemens authored Aug 29, 2022
2 parents cc8f44e + 48405a7 commit d0df079
Show file tree
Hide file tree
Showing 9 changed files with 166 additions and 25 deletions.
35 changes: 25 additions & 10 deletions cadence/blind-edition-nft/BlindEditionNFT.template.cdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import NonFungibleToken from {{{ imports.NonFungibleToken }}}
import MetadataViews from {{{ imports.MetadataViews }}}
import FungibleToken from {{{ imports.FungibleToken }}}
import FreshmintMetadataViews from {{{ imports.FreshmintMetadataViews }}}

pub contract {{ contractName }}: NonFungibleToken {

Expand Down Expand Up @@ -134,15 +135,22 @@ pub contract {{ contractName }}: NonFungibleToken {
}

pub fun getViews(): [Type] {
{{#if views }}
if self.getEdition() != nil {
{{#if views }}
return [
{{#each views}}
{{{ this.cadenceTypeString }}}{{#unless @last}},{{/unless}}
{{/each}}
]
{{ else }}
return []
{{/if}}
}

return [
{{#each views}}
{{{ this.cadenceTypeString }}}{{#unless @last}},{{/unless}}
{{/each}}
Type<MetadataViews.Display>(),
Type<FreshmintMetadataViews.BlindNFT>()
]
{{ else }}
return []
{{/if}}
}

pub fun resolveView(_ view: Type): AnyStruct? {
Expand All @@ -158,7 +166,15 @@ pub contract {{ contractName }}: NonFungibleToken {
{{/with}}
{{/each}}
}

return nil
}
{{ else }}

if self.getEdition() != nil {
return []
}
{{/if}}

switch view {
case Type<MetadataViews.Display>():
Expand All @@ -170,12 +186,11 @@ pub contract {{ contractName }}: NonFungibleToken {
path: nil
)
)
case Type<FreshmintMetadataViews.BlindNFT>():
return FreshmintMetadataViews.BlindNFT(metadataHash: self.editionHash)
}

return nil
{{ else }}
return nil
{{/if}}
}

destroy() {
Expand Down
34 changes: 24 additions & 10 deletions cadence/blind-nft/BlindNFT.template.cdc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import NonFungibleToken from {{{ imports.NonFungibleToken }}}
import MetadataViews from {{{ imports.MetadataViews }}}
import FungibleToken from {{{ imports.FungibleToken }}}
import FreshmintMetadataViews from {{{ imports.FreshmintMetadataViews }}}

pub contract {{ contractName }}: NonFungibleToken {

Expand Down Expand Up @@ -102,15 +103,22 @@ pub contract {{ contractName }}: NonFungibleToken {
}

pub fun getViews(): [Type] {
{{#if views }}
if self.getMetadata() != nil {
{{#if views }}
return [
{{#each views}}
{{{ this.cadenceTypeString }}}{{#unless @last}},{{/unless}}
{{/each}}
]
{{ else }}
return []
{{/if}}
}

return [
{{#each views}}
{{{ this.cadenceTypeString }}}{{#unless @last}},{{/unless}}
{{/each}}
Type<MetadataViews.Display>(),
Type<FreshmintMetadataViews.BlindNFT>()
]
{{ else }}
return []
{{/if}}
}

pub fun resolveView(_ view: Type): AnyStruct? {
Expand All @@ -124,7 +132,14 @@ pub contract {{ contractName }}: NonFungibleToken {
{{/with}}
{{/each}}
}

return nil
}
{{ else }}
if self.getMetadata() != nil {
return []
}
{{/if}}

switch view {
case Type<MetadataViews.Display>():
Expand All @@ -136,12 +151,11 @@ pub contract {{ contractName }}: NonFungibleToken {
path: nil
)
)
case Type<FreshmintMetadataViews.BlindNFT>():
return FreshmintMetadataViews.BlindNFT(metadataHash: self.metadataHash)
}

return nil
{{ else }}
return nil
{{/if}}
}

destroy() {
Expand Down
45 changes: 45 additions & 0 deletions cadence/common-nft/scripts/get_nft.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import {{ contractName }} from {{{ contractAddress }}}
import NonFungibleToken from {{{ imports.NonFungibleToken }}}
import MetadataViews from {{{ imports.MetadataViews }}}
import FreshmintMetadataViews from {{{ imports.FreshmintMetadataViews }}}

pub struct NFT {
pub let id: UInt64

pub let display: MetadataViews.Display
pub let metadataHash: String?

init(
id: UInt64,
display: MetadataViews.Display,
metadataHash: String?
) {
self.id = id
self.display = display
self.metadataHash = metadataHash
}
}

pub fun main(address: Address, id: UInt64): NFT? {
if let col = getAccount(address).getCapability<&{{ contractName }}.Collection{NonFungibleToken.CollectionPublic, {{ contractName }}.{{ contractName }}CollectionPublic}>({{ contractName }}.CollectionPublicPath).borrow() {
if let nft = col.borrow{{ contractName }}(id: id) {

let display = nft.resolveView(Type<MetadataViews.Display>())! as! MetadataViews.Display

var metadataHash: String? = nil

if let blindNFTView = nft.resolveView(Type<FreshmintMetadataViews.BlindNFT>()) {
let blindNFT = blindNFTView as! FreshmintMetadataViews.BlindNFT
metadataHash = String.encodeHex(blindNFT.metadataHash)
}

return NFT(
id: id,
display: display,
metadataHash: metadataHash
)
}
}

return nil
}
13 changes: 13 additions & 0 deletions cadence/metadata-views/FreshmintMetadataViews.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
pub contract FreshmintMetadataViews {

// BlindNFT returns a representation of
// a hidden or "blind" NFT, which at this point
// is a secure hash of the NFT's metadata values.
pub struct BlindNFT {
pub let metadataHash: [UInt8]

init(metadataHash: [UInt8]) {
self.metadataHash = metadataHash
}
}
}
33 changes: 29 additions & 4 deletions src/cli/generateProject.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import * as fs from 'fs-extra';
import * as path from 'path';
import * as Handlebars from 'handlebars';
import { ContractImports, StandardNFTGenerator, EditionNFTGenerator, NFTAirDropGenerator } from '../lib';
import {
ContractImports,
StandardNFTGenerator,
EditionNFTGenerator,
NFTAirDropGenerator,
FreshmintMetadataViewsGenerator,
CommonNFTGenerator,
ClaimSaleGenerator,
} from '../lib';
import { ContractConfig, ContractType } from './config';

export async function generateProject(dir: string, contract: ContractConfig, nftDataPath: string) {
Expand All @@ -20,6 +28,7 @@ export async function generateProjectCadence(dir: string, contract: ContractConf
const imports = {
NonFungibleToken: `"./NonFungibleToken.cdc"`,
MetadataViews: `"./MetadataViews.cdc"`,
FreshmintMetadataViews: `"./FreshmintMetadataViews.cdc"`,
FungibleToken: `"./FungibleToken.cdc"`,
FlowToken: `"./FlowToken.cdc"`,
NFTAirDrop: `"./NFTAirDrop.cdc"`,
Expand All @@ -35,8 +44,12 @@ export async function generateProjectCadence(dir: string, contract: ContractConf
}

await writeFile(path.resolve(dir, `cadence/contracts/NFTAirDrop.cdc`), NFTAirDropGenerator.contract({ imports }));
await writeFile(path.resolve(dir, `cadence/contracts/NFTClaimSale.cdc`), ClaimSaleGenerator.contract({ imports }));

await createGetNFTScript(dir, contract.name);
await writeFile(
path.resolve(dir, `cadence/contracts/FreshmintMetadataViews.cdc`),
FreshmintMetadataViewsGenerator.contract(),
);
}

async function generateStandardProject(
Expand All @@ -62,6 +75,8 @@ async function generateStandardProject(
// Find a better solution.
NonFungibleToken: `"../contracts/NonFungibleToken.cdc"`,
NFTAirDrop: `"../contracts/NFTAirDrop.cdc"`,
MetadataViews: `"../contracts/MetadataViews.cdc"`,
FreshmintMetadataViews: `"../contracts/FreshmintMetadataViews.cdc"`,
};

const mintTransaction = StandardNFTGenerator.mint({
Expand All @@ -85,6 +100,11 @@ async function generateStandardProject(
if (includeCSVFile) {
await createNFTsCSVFile(dir, contract.name, { fields: contract.schema.fields });
}

await writeFile(
path.resolve(dir, `cadence/scripts/get_nft.cdc`),
CommonNFTGenerator.getNFT({ imports: adjustedImports, contractName: contract.name, contractAddress }),
);
}

async function generateEditionProject(
Expand All @@ -110,6 +130,8 @@ async function generateEditionProject(
// Find a better solution.
NonFungibleToken: `"../contracts/NonFungibleToken.cdc"`,
NFTAirDrop: `"../contracts/NFTAirDrop.cdc"`,
MetadataViews: `"../contracts/MetadataViews.cdc"`,
FreshmintMetadataViews: `"../contracts/FreshmintMetadataViews.cdc"`,
};

const createEditionsTransaction = EditionNFTGenerator.createEditions({
Expand Down Expand Up @@ -140,6 +162,11 @@ async function generateEditionProject(
if (includeCSVFile) {
await createEditionsCSVFile(dir, contract.name, { fields: contract.schema.fields });
}

await writeFile(
path.resolve(dir, `cadence/scripts/get_nft.cdc`),
CommonNFTGenerator.getNFT({ imports: adjustedImports, contractName: contract.name, contractAddress }),
);
}

async function createScaffold(dir: string) {
Expand Down Expand Up @@ -170,8 +197,6 @@ async function createScaffold(dir: string) {
await fs.copy(path.resolve(__dirname, 'templates/gitignore'), path.resolve(dir, '.gitignore'));
}

const createGetNFTScript = template('templates/cadence/scripts/get_nft.cdc', 'cadence/scripts/get_nft.cdc');

const createNFTsCSVFile = template('templates/nfts.csv', 'nfts.csv');
const createEditionsCSVFile = template('templates/editions.csv', 'editions.csv');

Expand Down
6 changes: 5 additions & 1 deletion src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@ export namespace FreshmintConfig {
NonFungibleToken: '0xf8d6e0586b0a20c7',
FlowToken: '0x0ae53cb6e3f42a79',
MetadataViews: '0xf8d6e0586b0a20c7',
FreshmintMetadataViews: '0xf8d6e0586b0a20c7',
NFTClaimSale: '0xf8d6e0586b0a20c7',
NFTAirDrop: '0xf8d6e0586b0a20c7'
},
};

Expand All @@ -28,7 +30,9 @@ export namespace FreshmintConfig {
FungibleToken: '0x9a0766d93b6608b7',
NonFungibleToken: '0x631e88ae7f1d7c20',
MetadataViews: '0x631e88ae7f1d7c20',
NFTClaimSale: '0xc7cfd3d1b4951113',
FreshmintMetadataViews: '0x3c67d33388b03a69',
NFTClaimSale: '0x3c67d33388b03a69',
NFTAirDrop: '0x3c67d33388b03a69'
},
};

Expand Down
16 changes: 16 additions & 0 deletions src/lib/generators/CommonNFTGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { ContractImports } from '../config';
import TemplateGenerator from './TemplateGenerator';

export class CommonNFTGenerator extends TemplateGenerator {
static getNFT({
imports,
contractName,
contractAddress,
}: {
imports: ContractImports;
contractName: string;
contractAddress: string;
}): string {
return this.generate('../../../cadence/common-nft/scripts/get_nft.cdc', { imports, contractName, contractAddress });
}
}
7 changes: 7 additions & 0 deletions src/lib/generators/FreshmintMetadataViewsGenerator.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import TemplateGenerator from './TemplateGenerator';

export class FreshmintMetadataViewsGenerator extends TemplateGenerator {
static contract(): string {
return this.generate('../../../cadence/metadata-views/FreshmintMetadataViews.cdc', {});
}
}
2 changes: 2 additions & 0 deletions src/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export { ClaimSaleContract } from './contracts/ClaimSaleContract';
export { ClaimSaleGenerator } from './generators/ClaimSaleGenerator';

export { NFTAirDropGenerator } from './generators/NFTAirDropGenerator';
export { CommonNFTGenerator } from './generators/CommonNFTGenerator';
export { FreshmintMetadataViewsGenerator } from './generators/FreshmintMetadataViewsGenerator';

export { OnChainCollection } from './collections/OnChainCollection';
export { OnChainBlindCollection } from './collections/OnChainBlindCollection';
Expand Down

0 comments on commit d0df079

Please sign in to comment.