Skip to content

Commit

Permalink
Add query functions for ERC-1155 support
Browse files Browse the repository at this point in the history
  • Loading branch information
SpaceManiac committed Apr 17, 2024
1 parent 6ac5903 commit c92b9ee
Show file tree
Hide file tree
Showing 4 changed files with 208 additions and 2 deletions.
97 changes: 97 additions & 0 deletions packages/node-sdk/paima-db/src/sql/cde-erc1155.queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,3 +114,100 @@ const cdeErc1155BurnIR: any = {"usedParamSet":{"cde_id":true,"token_id":true,"wa
export const cdeErc1155Burn = new PreparedQuery<ICdeErc1155BurnParams,ICdeErc1155BurnResult>(cdeErc1155BurnIR);


/** 'CdeErc1155GetTotalBalanceAllTokens' parameters type */
export interface ICdeErc1155GetTotalBalanceAllTokensParams {
cde_id: number;
wallet_address: string;
}

/** 'CdeErc1155GetTotalBalanceAllTokens' return type */
export interface ICdeErc1155GetTotalBalanceAllTokensResult {
total: string | null;
}

/** 'CdeErc1155GetTotalBalanceAllTokens' query type */
export interface ICdeErc1155GetTotalBalanceAllTokensQuery {
params: ICdeErc1155GetTotalBalanceAllTokensParams;
result: ICdeErc1155GetTotalBalanceAllTokensResult;
}

const cdeErc1155GetTotalBalanceAllTokensIR: any = {"usedParamSet":{"cde_id":true,"wallet_address":true},"params":[{"name":"cde_id","required":true,"transform":{"type":"scalar"},"locs":[{"a":83,"b":90}]},{"name":"wallet_address","required":true,"transform":{"type":"scalar"},"locs":[{"a":113,"b":128}]}],"statement":"SELECT sum(CAST(balance AS NUMERIC)) as total from cde_erc1155_data\nWHERE cde_id = :cde_id!\nAND wallet_address = :wallet_address!"};

/**
* Query generated from SQL:
* ```
* SELECT sum(CAST(balance AS NUMERIC)) as total from cde_erc1155_data
* WHERE cde_id = :cde_id!
* AND wallet_address = :wallet_address!
* ```
*/
export const cdeErc1155GetTotalBalanceAllTokens = new PreparedQuery<ICdeErc1155GetTotalBalanceAllTokensParams,ICdeErc1155GetTotalBalanceAllTokensResult>(cdeErc1155GetTotalBalanceAllTokensIR);


/** 'CdeErc1155GetAllTokens' parameters type */
export interface ICdeErc1155GetAllTokensParams {
cde_id: number;
wallet_address: string;
}

/** 'CdeErc1155GetAllTokens' return type */
export interface ICdeErc1155GetAllTokensResult {
balance: string;
cde_id: number;
token_id: string;
wallet_address: string;
}

/** 'CdeErc1155GetAllTokens' query type */
export interface ICdeErc1155GetAllTokensQuery {
params: ICdeErc1155GetAllTokensParams;
result: ICdeErc1155GetAllTokensResult;
}

const cdeErc1155GetAllTokensIR: any = {"usedParamSet":{"cde_id":true,"wallet_address":true},"params":[{"name":"cde_id","required":true,"transform":{"type":"scalar"},"locs":[{"a":46,"b":53}]},{"name":"wallet_address","required":true,"transform":{"type":"scalar"},"locs":[{"a":76,"b":91}]}],"statement":"SELECT * from cde_erc1155_data\nWHERE cde_id = :cde_id!\nAND wallet_address = :wallet_address!\nAND CAST(balance AS NUMERIC) > 0"};

/**
* Query generated from SQL:
* ```
* SELECT * from cde_erc1155_data
* WHERE cde_id = :cde_id!
* AND wallet_address = :wallet_address!
* AND CAST(balance AS NUMERIC) > 0
* ```
*/
export const cdeErc1155GetAllTokens = new PreparedQuery<ICdeErc1155GetAllTokensParams,ICdeErc1155GetAllTokensResult>(cdeErc1155GetAllTokensIR);


/** 'CdeErc1155GetByTokenId' parameters type */
export interface ICdeErc1155GetByTokenIdParams {
cde_id: number;
token_id: string;
wallet_address: string;
}

/** 'CdeErc1155GetByTokenId' return type */
export interface ICdeErc1155GetByTokenIdResult {
balance: string;
cde_id: number;
token_id: string;
wallet_address: string;
}

/** 'CdeErc1155GetByTokenId' query type */
export interface ICdeErc1155GetByTokenIdQuery {
params: ICdeErc1155GetByTokenIdParams;
result: ICdeErc1155GetByTokenIdResult;
}

const cdeErc1155GetByTokenIdIR: any = {"usedParamSet":{"cde_id":true,"wallet_address":true,"token_id":true},"params":[{"name":"cde_id","required":true,"transform":{"type":"scalar"},"locs":[{"a":46,"b":53}]},{"name":"wallet_address","required":true,"transform":{"type":"scalar"},"locs":[{"a":76,"b":91}]},{"name":"token_id","required":true,"transform":{"type":"scalar"},"locs":[{"a":108,"b":117}]}],"statement":"SELECT * from cde_erc1155_data\nWHERE cde_id = :cde_id!\nAND wallet_address = :wallet_address!\nAND token_id = :token_id!"};

/**
* Query generated from SQL:
* ```
* SELECT * from cde_erc1155_data
* WHERE cde_id = :cde_id!
* AND wallet_address = :wallet_address!
* AND token_id = :token_id!
* ```
*/
export const cdeErc1155GetByTokenId = new PreparedQuery<ICdeErc1155GetByTokenIdParams,ICdeErc1155GetByTokenIdResult>(cdeErc1155GetByTokenIdIR);
20 changes: 20 additions & 0 deletions packages/node-sdk/paima-db/src/sql/cde-erc1155.sql
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,23 @@ VALUES (
)
ON CONFLICT (cde_id, token_id, wallet_address)
DO UPDATE SET balance = CAST(cde_erc1155_burn.balance AS NUMERIC) + CAST(EXCLUDED.balance AS NUMERIC);

/* @name cdeErc1155GetTotalBalanceAllTokens */
SELECT sum(CAST(balance AS NUMERIC)) as total from cde_erc1155_data
WHERE cde_id = :cde_id!
AND wallet_address = :wallet_address!
;

/* @name cdeErc1155GetAllTokens */
SELECT * from cde_erc1155_data
WHERE cde_id = :cde_id!
AND wallet_address = :wallet_address!
AND CAST(balance AS NUMERIC) > 0
;

/* @name cdeErc1155GetByTokenId */
SELECT * from cde_erc1155_data
WHERE cde_id = :cde_id!
AND wallet_address = :wallet_address!
AND token_id = :token_id!
;
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,21 @@ import {
type ICdeCardanoGetProjectedNftResult,
getCardanoEpoch,
cdeCardanoAssetUtxosByAddress,
cdeErc1155GetTotalBalanceAllTokens,
cdeErc1155GetAllTokens,
cdeErc1155GetByTokenId,
} from '@paima/db';
import type {
OwnedNftsResponse,
GenericCdeDataUnit,
TokenIdPair,
CardanoAssetUtxo,
} from './types.js';
import type { ICdeCardanoAssetUtxosByAddressParams } from '@paima/db';
import type {
ICdeCardanoAssetUtxosByAddressParams,
ICdeErc1155GetAllTokensResult,
ICdeErc1155GetByTokenIdResult,
} from '@paima/db';

/* Functions to retrieve CDE ID: */

Expand Down Expand Up @@ -158,6 +165,40 @@ export async function internalGetGenericDataBlockheightRange(
}));
}

export async function internalGetErc1155TotalBalanceAllTokens(
readonlyDBConn: Pool,
cde_id: number,
wallet_address: string
): Promise<bigint> {
const results = await cdeErc1155GetTotalBalanceAllTokens.run(
{ cde_id, wallet_address },
readonlyDBConn
);
return BigInt(results[0].total ?? '0');
}

export async function internalGetErc1155AllTokens(
readonlyDBConn: Pool,
cde_id: number,
wallet_address: string
): Promise<ICdeErc1155GetAllTokensResult[]> {
return await cdeErc1155GetAllTokens.run({ cde_id, wallet_address }, readonlyDBConn);
}

export async function internalGetErc1155ByTokenId(
readonlyDBConn: Pool,
cde_id: number,
wallet_address: string,
token_id: bigint
): Promise<ICdeErc1155GetByTokenIdResult | null> {
return (
await cdeErc1155GetByTokenId.run(
{ cde_id, wallet_address, token_id: String(token_id) },
readonlyDBConn
)
)[0];
}

export async function internalGetErc6551AccountOwner(
readonlyDBConn: Pool,
cdeId: number,
Expand Down
50 changes: 49 additions & 1 deletion packages/node-sdk/paima-utils-backend/src/cde-access.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,16 @@ import {
internalGetCardanoAddressDelegation,
internalGetCardanoProjectedNft,
internalGetCardanoAssetUtxos,
internalGetErc1155TotalBalanceAllTokens,
internalGetErc1155AllTokens,
internalGetErc1155ByTokenId,
} from './cde-access-internals.js';
import type { ICdeCardanoGetProjectedNftResult } from '@paima/db/src';
import type {
ICdeCardanoGetProjectedNftResult,
ICdeErc1155GetAllTokensResult,
ICdeErc1155GetByTokenIdResult,
} from '@paima/db/src';
export type { ICdeErc1155GetAllTokensResult };
import type {
OwnedNftsResponse,
GenericCdeDataUnit,
Expand Down Expand Up @@ -140,6 +148,46 @@ export async function getGenericDataBlockheightRange(
return await internalGetGenericDataBlockheightRange(readonlyDBConn, cdeId, fromBlock, toBlock);
}

/**
* Get the sum of the `value` of all tokens owned by a wallet within a single ERC-1155 contract.
*/
export async function getErc1155TotalBalanceAllTokens(
readonlyDBConn: Pool,
cdeName: string,
wallet: string
): Promise<bigint> {
const cdeId = await getCdeIdByName(readonlyDBConn, cdeName);
if (cdeId === null) return 0n;
return await internalGetErc1155TotalBalanceAllTokens(readonlyDBConn, cdeId, wallet);
}

/**
* Get a listing of all tokens owned by a wallet within a single ERC-1155 contract.
*/
export async function getErc1155AllTokens(
readonlyDBConn: Pool,
cdeName: string,
wallet: string
): Promise<ICdeErc1155GetAllTokensResult[]> {
const cdeId = await getCdeIdByName(readonlyDBConn, cdeName);
if (cdeId === null) return [];
return await internalGetErc1155AllTokens(readonlyDBConn, cdeId, wallet);
}

/**
* Get info on a specific token owned by a wallet within a single ERC-1155 contract.
*/
export async function getErc1155ByTokenId(
readonlyDBConn: Pool,
cdeName: string,
wallet: string,
tokenId: bigint
): Promise<ICdeErc1155GetByTokenIdResult | null> {
const cdeId = await getCdeIdByName(readonlyDBConn, cdeName);
if (cdeId === null) return null;
return await internalGetErc1155ByTokenId(readonlyDBConn, cdeId, wallet, tokenId);
}

/**
* Fetch the NFT that owns this account
*/
Expand Down

0 comments on commit c92b9ee

Please sign in to comment.