Skip to content

Commit

Permalink
feat: add getTokenMetadata to solana api. (#1171)
Browse files Browse the repository at this point in the history
* feat: add getTokenMetadata to solana api.
* update swagger.
  • Loading branch information
b4rtaz authored Jan 23, 2024
1 parent 3fc8286 commit 444c0dd
Show file tree
Hide file tree
Showing 15 changed files with 309 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .changeset/shaggy-sheep-wonder.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@moralisweb3/common-sol-utils': patch
'@moralisweb3/sol-api': patch
'moralis': patch
---

This version adds a new `getTokenMetadata` endpoint to the Solana API.
7 changes: 7 additions & 0 deletions .changeset/tidy-bears-tan.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@moralisweb3/common-sol-utils': minor
'@moralisweb3/sol-api': minor
'moralis': minor
---

`nativePrice`, `exchangeName` and `exchangeAddress` properties in the `SolSPLTokenPrice` class are now optional.
2 changes: 1 addition & 1 deletion packages/common/solUtils/generator.config.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"operations": {
"groupRef": "#/x-tag-sdk",
"isEnabledRef": "#/x-tag-sdk",
"filterOperationIds": ["getTokenPrice"]
"filterOperationIds": ["getTokenPrice", "getTokenMetadata"]
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions packages/common/solUtils/src/generated/client/abstractClient.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { GetTokenPriceOperation, GetTokenPriceOperationRequest, GetTokenPriceOperationRequestJSON } from '../operations/GetTokenPriceOperation';
import { SolSPLTokenPrice, SolSPLTokenPriceJSON } from '../types/SolSPLTokenPrice';
import { GetTokenMetadataOperation, GetTokenMetadataOperationRequest, GetTokenMetadataOperationRequestJSON } from '../operations/GetTokenMetadataOperation';
import { SolTokenMetadata, SolTokenMetadataJSON } from '../types/SolTokenMetadata';

export interface OperationV3<Request, RequestJSON, Response, ResponseJSON, Body, BodyJSON> {
operationId: string;
Expand Down Expand Up @@ -36,5 +38,18 @@ export abstract class AbstractClient {
SolSPLTokenPrice,
SolSPLTokenPriceJSON
>(GetTokenPriceOperation),
/**
* @description Get the global token metadata for a given network and contract (mint, standard, name, symbol, metaplex).
* @param request Request with parameters.
* @param {Object} request.network The network to query
* @param {Object} request.address The address of the contract
* @returns {Object} Response for the request.
*/
getTokenMetadata: this.createEndpoint<
GetTokenMetadataOperationRequest,
GetTokenMetadataOperationRequestJSON,
SolTokenMetadata,
SolTokenMetadataJSON
>(GetTokenMetadataOperation),
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { SolNetwork, SolNetworkInput, SolNetworkJSON, SolAddress, SolAddressInput, SolAddressJSON } from '../../dataTypes';
import { SolTokenMetadata, SolTokenMetadataJSON } from '../types/SolTokenMetadata';

// request parameters:
// - network ($ref: #/paths/~1token~1{network}~1{address}~1metadata/get/parameters/0/schema)
// - address ($ref: #/paths/~1token~1{network}~1{address}~1metadata/get/parameters/1/schema)

export interface GetTokenMetadataOperationRequest {
/**
* @description The network to query
*/
readonly network: SolNetworkInput | SolNetwork;
/**
* @description The address of the contract
*/
readonly address: SolAddressInput | SolAddress;
}

export interface GetTokenMetadataOperationRequestJSON {
readonly network: SolNetworkJSON;
readonly address: SolAddressJSON;
}

export type GetTokenMetadataOperationResponse = SolTokenMetadata;
export type GetTokenMetadataOperationResponseJSON = SolTokenMetadataJSON;

export const GetTokenMetadataOperation = {
operationId: "getTokenMetadata",
groupName: "token",
httpMethod: "get",
routePattern: "/token/{network}/{address}/metadata",
parameterNames: ["network","address"],
hasResponse: true,
hasBody: false,

parseResponse(json: SolTokenMetadataJSON): SolTokenMetadata {
return SolTokenMetadata.fromJSON(json);
},

serializeRequest(request: GetTokenMetadataOperationRequest): GetTokenMetadataOperationRequestJSON {
const network = SolNetwork.create(request.network);
const address = SolAddress.create(request.address);
return {
network: network.toJSON(),
address: address.toJSON(),
};
},

}
1 change: 1 addition & 0 deletions packages/common/solUtils/src/generated/operations/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export * from './GetTokenPriceOperation';
export * from './GetTokenMetadataOperation';
export * from './operations';
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { GetTokenPriceOperation } from './GetTokenPriceOperation';
import { GetTokenMetadataOperation } from './GetTokenMetadataOperation';

export const operations = [
GetTokenPriceOperation,
GetTokenMetadataOperation,
];
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// $ref: #/paths/~1token~1{network}~1{address}~1metadata/get/parameters/0/schema
// typeName: getTokenMetadata_network_Enum

export type SolGetTokenMetadataNetworkEnumJSON = "mainnet" | "devnet";
export type SolGetTokenMetadataNetworkEnumInput = "mainnet" | "devnet";
export type SolGetTokenMetadataNetworkEnumValue = "mainnet" | "devnet";

export abstract class SolGetTokenMetadataNetworkEnum {
public static create(input: SolGetTokenMetadataNetworkEnumInput | SolGetTokenMetadataNetworkEnumValue): SolGetTokenMetadataNetworkEnumValue {
return input;
}

public static fromJSON(json: SolGetTokenMetadataNetworkEnumJSON): SolGetTokenMetadataNetworkEnumValue {
return json;
}
}
75 changes: 75 additions & 0 deletions packages/common/solUtils/src/generated/types/SolMetaplexToken.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// $ref: #/components/schemas/MetaplexToken
// type: MetaplexToken
// properties:
// - metadataUri ($ref: #/components/schemas/MetaplexToken/properties/metadataUri)
// - masterEdition ($ref: #/components/schemas/MetaplexToken/properties/masterEdition)
// - isMutable ($ref: #/components/schemas/MetaplexToken/properties/isMutable)
// - primarySaleHappened ($ref: #/components/schemas/MetaplexToken/properties/primarySaleHappened)
// - sellerFeeBasisPoints ($ref: #/components/schemas/MetaplexToken/properties/sellerFeeBasisPoints)
// - updateAuthority ($ref: #/components/schemas/MetaplexToken/properties/updateAuthority)

export interface SolMetaplexTokenJSON {
readonly metadataUri: string;
readonly masterEdition: boolean;
readonly isMutable: boolean;
readonly primarySaleHappened: number;
readonly sellerFeeBasisPoints: number;
readonly updateAuthority: string;
}

export interface SolMetaplexTokenInput {
readonly metadataUri: string;
readonly masterEdition: boolean;
readonly isMutable: boolean;
readonly primarySaleHappened: number;
readonly sellerFeeBasisPoints: number;
readonly updateAuthority: string;
}

export class SolMetaplexToken {
public static create(input: SolMetaplexTokenInput | SolMetaplexToken): SolMetaplexToken {
if (input instanceof SolMetaplexToken) {
return input;
}
return new SolMetaplexToken(input);
}

public static fromJSON(json: SolMetaplexTokenJSON): SolMetaplexToken {
const input: SolMetaplexTokenInput = {
metadataUri: json.metadataUri,
masterEdition: json.masterEdition,
isMutable: json.isMutable,
primarySaleHappened: json.primarySaleHappened,
sellerFeeBasisPoints: json.sellerFeeBasisPoints,
updateAuthority: json.updateAuthority,
};
return SolMetaplexToken.create(input);
}

public readonly metadataUri: string;
public readonly masterEdition: boolean;
public readonly isMutable: boolean;
public readonly primarySaleHappened: number;
public readonly sellerFeeBasisPoints: number;
public readonly updateAuthority: string;

private constructor(input: SolMetaplexTokenInput) {
this.metadataUri = input.metadataUri;
this.masterEdition = input.masterEdition;
this.isMutable = input.isMutable;
this.primarySaleHappened = input.primarySaleHappened;
this.sellerFeeBasisPoints = input.sellerFeeBasisPoints;
this.updateAuthority = input.updateAuthority;
}

public toJSON(): SolMetaplexTokenJSON {
return {
metadataUri: this.metadataUri,
masterEdition: this.masterEdition,
isMutable: this.isMutable,
primarySaleHappened: this.primarySaleHappened,
sellerFeeBasisPoints: this.sellerFeeBasisPoints,
updateAuthority: this.updateAuthority,
}
}
}
24 changes: 12 additions & 12 deletions packages/common/solUtils/src/generated/types/SolSPLTokenPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ import { SolAddress, SolAddressInput, SolAddressJSON } from '../../dataTypes';

export interface SolSPLTokenPriceJSON {
readonly nativePrice?: SolSPLNativePriceJSON;
readonly usdPrice: number;
readonly exchangeAddress: SolAddressJSON;
readonly exchangeName: string;
readonly usdPrice?: number;
readonly exchangeAddress?: SolAddressJSON;
readonly exchangeName?: string;
}

export interface SolSPLTokenPriceInput {
readonly nativePrice?: SolSPLNativePriceInput | SolSPLNativePrice;
readonly usdPrice: number;
readonly exchangeAddress: SolAddressInput | SolAddress;
readonly exchangeName: string;
readonly usdPrice?: number;
readonly exchangeAddress?: SolAddressInput | SolAddress;
readonly exchangeName?: string;
}

export class SolSPLTokenPrice {
Expand All @@ -35,29 +35,29 @@ export class SolSPLTokenPrice {
const input: SolSPLTokenPriceInput = {
nativePrice: json.nativePrice ? SolSPLNativePrice.fromJSON(json.nativePrice) : undefined,
usdPrice: json.usdPrice,
exchangeAddress: SolAddress.fromJSON(json.exchangeAddress),
exchangeAddress: json.exchangeAddress ? SolAddress.fromJSON(json.exchangeAddress) : undefined,
exchangeName: json.exchangeName,
};
return SolSPLTokenPrice.create(input);
}

public readonly nativePrice?: SolSPLNativePrice;
public readonly usdPrice: number;
public readonly exchangeAddress: SolAddress;
public readonly exchangeName: string;
public readonly usdPrice?: number;
public readonly exchangeAddress?: SolAddress;
public readonly exchangeName?: string;

private constructor(input: SolSPLTokenPriceInput) {
this.nativePrice = input.nativePrice ? SolSPLNativePrice.create(input.nativePrice) : undefined;
this.usdPrice = input.usdPrice;
this.exchangeAddress = SolAddress.create(input.exchangeAddress);
this.exchangeAddress = input.exchangeAddress ? SolAddress.create(input.exchangeAddress) : undefined;
this.exchangeName = input.exchangeName;
}

public toJSON(): SolSPLTokenPriceJSON {
return {
nativePrice: this.nativePrice ? this.nativePrice.toJSON() : undefined,
usdPrice: this.usdPrice,
exchangeAddress: this.exchangeAddress.toJSON(),
exchangeAddress: this.exchangeAddress ? this.exchangeAddress.toJSON() : undefined,
exchangeName: this.exchangeName,
}
}
Expand Down
70 changes: 70 additions & 0 deletions packages/common/solUtils/src/generated/types/SolTokenMetadata.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { SolMetaplexToken, SolMetaplexTokenInput, SolMetaplexTokenJSON } from '../types/SolMetaplexToken';

// $ref: #/components/schemas/TokenMetadata
// type: TokenMetadata
// properties:
// - mint ($ref: #/components/schemas/TokenMetadata/properties/mint)
// - standard ($ref: #/components/schemas/TokenMetadata/properties/standard)
// - name ($ref: #/components/schemas/TokenMetadata/properties/name)
// - symbol ($ref: #/components/schemas/TokenMetadata/properties/symbol)
// - metaplex ($ref: #/components/schemas/MetaplexToken)

export interface SolTokenMetadataJSON {
readonly mint: string;
readonly standard: string;
readonly name: string;
readonly symbol: string;
readonly metaplex: SolMetaplexTokenJSON;
}

export interface SolTokenMetadataInput {
readonly mint: string;
readonly standard: string;
readonly name: string;
readonly symbol: string;
readonly metaplex: SolMetaplexTokenInput | SolMetaplexToken;
}

export class SolTokenMetadata {
public static create(input: SolTokenMetadataInput | SolTokenMetadata): SolTokenMetadata {
if (input instanceof SolTokenMetadata) {
return input;
}
return new SolTokenMetadata(input);
}

public static fromJSON(json: SolTokenMetadataJSON): SolTokenMetadata {
const input: SolTokenMetadataInput = {
mint: json.mint,
standard: json.standard,
name: json.name,
symbol: json.symbol,
metaplex: SolMetaplexToken.fromJSON(json.metaplex),
};
return SolTokenMetadata.create(input);
}

public readonly mint: string;
public readonly standard: string;
public readonly name: string;
public readonly symbol: string;
public readonly metaplex: SolMetaplexToken;

private constructor(input: SolTokenMetadataInput) {
this.mint = input.mint;
this.standard = input.standard;
this.name = input.name;
this.symbol = input.symbol;
this.metaplex = SolMetaplexToken.create(input.metaplex);
}

public toJSON(): SolTokenMetadataJSON {
return {
mint: this.mint,
standard: this.standard,
name: this.name,
symbol: this.symbol,
metaplex: this.metaplex.toJSON(),
}
}
}
3 changes: 3 additions & 0 deletions packages/common/solUtils/src/generated/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export * from './SolGetTokenPriceNetworkEnum';
export * from './SolGetTokenMetadataNetworkEnum';
export * from './SolSPLTokenPrice';
export * from './SolTokenMetadata';
export * from './SolSPLNativePrice';
export * from './SolMetaplexToken';
Loading

1 comment on commit 444c0dd

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

Test coverage

Title Lines Statements Branches Functions
api-utils Coverage: 20%
20.6% (61/296) 20.48% (17/83) 19.04% (12/63)
auth Coverage: 89%
92.45% (98/106) 83.33% (20/24) 86.66% (26/30)
evm-api Coverage: 91%
91.83% (90/98) 66.66% (6/9) 87.5% (56/64)
common-aptos-utils Coverage: 4%
4.56% (151/3306) 4.49% (25/556) 5.53% (45/813)
common-evm-utils Coverage: 64%
65.34% (1650/2525) 25% (186/744) 44.15% (419/949)
sol-api Coverage: 97%
97.56% (40/41) 66.66% (6/9) 93.75% (15/16)
common-sol-utils Coverage: 64%
65.42% (229/350) 41.86% (18/43) 50.89% (57/112)
common-streams-utils Coverage: 90%
90.73% (1204/1327) 73.63% (363/493) 82.07% (444/541)
streams Coverage: 91%
90.54% (603/666) 72.34% (68/94) 90.97% (131/144)

Please sign in to comment.