Skip to content

Commit

Permalink
Merge pull request #146 from cardinal-labs/attr
Browse files Browse the repository at this point in the history
Mainnet secondary and att expiration
  • Loading branch information
jpbogle authored Sep 20, 2022
2 parents 6c56d94 + 225f176 commit 3580715
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 88 deletions.
12 changes: 8 additions & 4 deletions api/common/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,16 @@ import { Connection } from "@solana/web3.js";
const networkURLs: { [key: string]: { primary: string; secondary?: string } } =
{
["mainnet-beta"]: {
primary: process.env.MAINNET_PRIMARY || "https://ssc-dao.genesysgo.net/",
secondary: "https://ssc-dao.genesysgo.net/",
primary:
process.env.MAINNET_PRIMARY || "https://solana-api.projectserum.com",
secondary:
process.env.MAINNET_SECONDARY || "https://solana-api.projectserum.com",
},
mainnet: {
primary: process.env.MAINNET_PRIMARY || "https://ssc-dao.genesysgo.net/",
secondary: "https://ssc-dao.genesysgo.net/",
primary:
process.env.MAINNET_PRIMARY || "https://solana-api.projectserum.com",
secondary:
process.env.MAINNET_SECONDARY || "https://solana-api.projectserum.com",
},
devnet: { primary: "https://api.devnet.solana.com/" },
testnet: { primary: "https://api.testnet.solana.com/" },
Expand Down
107 changes: 107 additions & 0 deletions api/metadata-generator/attributes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
import {
InvalidationType,
TokenManagerState,
} from "@cardinal/token-manager/dist/cjs/programs/tokenManager";

import type { TokenData } from "../common/tokenData";

export const stateAttributes = (tokenData: TokenData) => {
if (tokenData?.tokenManagerData?.parsed.state) {
return [
{
trait_type: "state",
value:
tokenData?.tokenManagerData?.parsed.state ===
TokenManagerState.Invalidated
? "INVALIDATED"
: "VALID",
display_type: "State",
},
];
}
return [];
};

export const typeAttributes = (tokenData: TokenData) => {
if (
tokenData.tokenManagerData?.parsed.invalidationType ===
InvalidationType.Return
) {
return [
{
trait_type: "type",
value: "Rental",
display_type: "Type",
},
];
} else if (
tokenData.tokenManagerData?.parsed.invalidationType ===
InvalidationType.Release
) {
return [
{
trait_type: "type",
value: "Release",
display_type: "Type",
},
];
}
return [];
};

export const usageAttributes = (tokenData: TokenData) => {
if (tokenData.useInvalidatorData?.parsed.usages) {
return [
{
trait_type: "used",
value: `(${tokenData.useInvalidatorData?.parsed.usages.toNumber()}${
tokenData.useInvalidatorData?.parsed.maxUsages
? `/${tokenData.useInvalidatorData?.parsed.maxUsages.toNumber()}`
: ""
})`,
display_type: "Used",
},
];
}
return [];
};

export const expirationAttributes = (tokenData: TokenData) => {
const expiration =
tokenData.timeInvalidatorData?.parsed.maxExpiration ||
tokenData.timeInvalidatorData?.parsed.expiration;
if (expiration) {
return [
{
trait_type: "expiration",
value: `${new Date(expiration.toNumber() * 1000).toLocaleTimeString(
"en-US",
{
month: "numeric",
day: "numeric",
year: "numeric",
hour: "numeric",
minute: "numeric",
timeZone: "America/New_York",
timeZoneName: "short",
}
)}`,
display_type: "Expiration",
},
];
}
return [];
};
export const durationAttributes = (tokenData: TokenData) => {
const duration = tokenData.timeInvalidatorData?.parsed?.durationSeconds;
if (duration) {
return [
{
trait_type: "duration",
value: `${duration.toNumber()}`,
display_type: "Duration",
},
];
}
return [];
};
110 changes: 26 additions & 84 deletions api/metadata-generator/generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
/* eslint-disable @typescript-eslint/no-non-null-asserted-optional-chain, @typescript-eslint/no-non-null-assertion, @typescript-eslint/no-unsafe-assignment*/
import * as namespaces from "@cardinal/namespaces";
import {
InvalidationType,
TokenManagerState,
} from "@cardinal/token-manager/dist/cjs/programs/tokenManager";
import type { Idl } from "@project-serum/anchor";
import { BorshAccountsCoder } from "@project-serum/anchor";
import * as web3 from "@solana/web3.js";
Expand All @@ -18,6 +14,13 @@ import type { TokenData } from "../common/tokenData";
import { getTokenData } from "../common/tokenData";
import { getOwner } from "../common/utils";
import { getTicketImageURL } from "../img-generator/event-ticket-image";
import {
durationAttributes,
expirationAttributes,
stateAttributes,
typeAttributes,
usageAttributes,
} from "./attributes";
import { getDefaultMetadata } from "./default";
import { getTicketMetadataLink } from "./event-ticket-metadata";
import { getExpiredMetadata } from "./expired";
Expand Down Expand Up @@ -199,6 +202,14 @@ export async function getMetadata(
name: eventData?.eventName,
family: eventData?.eventName,
};

metadata.attributes = [
...(metadata.attributes || []),
...typeAttributes(tokenData),
...usageAttributes(tokenData),
...expirationAttributes(tokenData),
];

if (eventData) {
const verifyUrl = `https://events.cardinal.so/default/${eventData?.shortLink}/verify`;
metadata.external_url = `https://phantom.app/ul/browse/${encodeURIComponent(
Expand Down Expand Up @@ -280,86 +291,17 @@ export async function getMetadata(
}
}

if (tokenData?.tokenManagerData?.parsed.state) {
response = {
...response,
attributes: [
...(response.attributes || []),
{
trait_type: "state",
value:
tokenData?.tokenManagerData?.parsed.state ===
TokenManagerState.Invalidated
? "INVALIDATED"
: "VALID",
display_type: "State",
},
],
};
}

if (
tokenData.tokenManagerData?.parsed.invalidationType ===
InvalidationType.Return
) {
response = {
...response,
attributes: [
...(response.attributes || []),
{
trait_type: "type",
value: "RENTAL",
display_type: "Type",
},
],
};
}

if (tokenData.useInvalidatorData?.parsed.usages) {
response = {
...response,
attributes: [
...(response.attributes || []),
{
trait_type: "used",
value: `(${tokenData.useInvalidatorData?.parsed.usages.toNumber()}${
tokenData.useInvalidatorData?.parsed.maxUsages
? `/${tokenData.useInvalidatorData?.parsed.maxUsages.toNumber()}`
: ""
})`,
display_type: "Used",
},
],
};
}

if (tokenData.timeInvalidatorData?.parsed.expiration) {
response = {
...response,
attributes: [
...(response.attributes || []),
{
trait_type: "expiration",
value: `${tokenData.timeInvalidatorData?.parsed.expiration.toNumber()}`,
display_type: "Expiration",
},
],
};
}

if (tokenData.timeInvalidatorData?.parsed?.durationSeconds) {
response = {
...response,
attributes: [
...(response.attributes || []),
{
trait_type: "duration",
value: `${tokenData.timeInvalidatorData?.parsed?.durationSeconds.toNumber()}`,
display_type: "Duration",
},
],
};
}
response = {
...response,
attributes: [
...(response.attributes || []),
...stateAttributes(tokenData),
...typeAttributes(tokenData),
...usageAttributes(tokenData),
...expirationAttributes(tokenData),
...durationAttributes(tokenData),
],
};

// collection
if (tokenData?.metaplexData?.parsed.data.symbol === "$JAMB") {
Expand Down
1 change: 1 addition & 0 deletions api/serverless.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ provider:
lambdaHashingVersion: "20201221"
environment:
MAINNET_PRIMARY: ${param:MAINNET_PRIMARY}
MAINNET_SECONDARY: ${param:MAINNET_SECONDARY}
http:
cors: true
apiGateway:
Expand Down

0 comments on commit 3580715

Please sign in to comment.