Skip to content

Commit

Permalink
fix supply
Browse files Browse the repository at this point in the history
  • Loading branch information
Matlefebvre1234 committed Jul 10, 2024
1 parent b856df8 commit f4d5f09
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 53 deletions.
48 changes: 21 additions & 27 deletions src/queries.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ export function getTotalSupply(endpoint: UsageEndpoints, query_param: any, examp
const q = query_param as ValidUserParams<typeof endpoint>;

let contract = q.contract;
let symbol = q.symbol;
let name = q.name;

let additional_query_params = {};

Expand All @@ -87,15 +85,11 @@ export function getTotalSupply(endpoint: UsageEndpoints, query_param: any, examp
const where = [];

// equals
if (contract) where.push(`${table}.contract = {contract : string}`);
if (contract) where.push(`${table}.contract = {contract : String}`);

// timestamp and block filter
additional_query_params = addBlockFilter(q, additional_query_params, where);

if (symbol) where.push(`LOWER(symbol) = {symbol : string} `);
if (name) where.push(`LOWER(name) = {name : string} `);


// Join WHERE statements with AND
if (where.length) query += ` WHERE(${where.join(' AND ')})`;

Expand Down Expand Up @@ -143,9 +137,9 @@ export function getContracts(endpoint: UsageEndpoints, query_param: any, example
if (!example) {
// WHERE statements
const where = [];
if (contract) where.push(`contract = {contract : string}`);
if (symbol) where.push(`LOWER(symbol) = {symbol : string} `);
if (name) where.push(`LOWER(name) = {name : string} `);
if (contract) where.push(`contract = {contract : String}`);
if (symbol) where.push(`LOWER(symbol) = {symbol : String} `);
if (name) where.push(`LOWER(name) = {name : String} `);


// Join WHERE statements with AND
Expand Down Expand Up @@ -175,8 +169,8 @@ function getBalanceChanges_latest(q: any) {
let contract = q.contract;
let account = q.account;

let table = `{chain : string }_erc20_token.account_balances`
const contractTable = `{chain : string }_erc20_token.contracts`;
let table = `{chain : String }_erc20_token.account_balances`
const contractTable = `{chain : String }_erc20_token.contracts`;
let query = `SELECT
${table}.account,
${table}.contract,
Expand All @@ -189,9 +183,9 @@ function getBalanceChanges_latest(q: any) {
const where = [];

// equals
where.push(`account = {account : string}`)
where.push(`account = {account : String}`)
where.push(`amount != '0'`);
if (contract) where.push(`contract = {contract : string}`);
if (contract) where.push(`contract = {contract : String}`);

// Join WHERE statements with AND
if (where.length) query += ` WHERE(${where.join(' AND ')})`;
Expand Down Expand Up @@ -229,10 +223,10 @@ function getBalanceChanges_historical(q: any) {
let additional_query_params = {};

let table;
const contractTable = `{chain : string}_erc20_token.contracts`;
const contractTable = `{chain : String}_erc20_token.contracts`;
// SQL Query
if (contract) table = `{chain : string}_erc20_token.balance_changes_contract_historical_mv`;
else table = `{chain : string}_erc20_token.balance_changes_account_historical_mv`
if (contract) table = `{chain : String}_erc20_token.balance_changes_contract_historical_mv`;
else table = `{chain : String}_erc20_token.balance_changes_account_historical_mv`

let query = `SELECT
${table}.owner,
Expand All @@ -253,8 +247,8 @@ function getBalanceChanges_historical(q: any) {
else joinSelectQuery = `SELECT contract, owner, MAX(block_num) as block_num FROM (SELECT owner, block_num , contract FROM ${table} ${blockfilterQuery})`;
const joinWhereQuery: any = [];
//add where filter to joinQuery
if (contract) joinWhereQuery.push(`contract = {contract : string}`);
joinWhereQuery.push(`owner = {account : string}`);
if (contract) joinWhereQuery.push(`contract = {contract : String}`);
joinWhereQuery.push(`owner = {account : String}`);
if (joinWhereQuery.length) joinSelectQuery += ` WHERE(${joinWhereQuery.join(' AND ')})`;

//Add group by to joinQuery
Expand All @@ -268,9 +262,9 @@ function getBalanceChanges_historical(q: any) {
const where = [];

// equals
where.push(`owner = {account : string}`)
where.push(`owner = {account : String}`)
where.push(`amount != '0'`);
if (contract) where.push(`contract = {contract : string}`);
if (contract) where.push(`contract = {contract : String}`);

// Join WHERE statements with AND
if (where.length) query += ` WHERE(${where.join(' AND ')})`;
Expand Down Expand Up @@ -337,7 +331,7 @@ function getHolder_latest(q: any) {

// WHERE statements
const where: any = [];
if (contract) where.push(`contract = {contract : string}`);
if (contract) where.push(`contract = {contract : String}`);
where.push(`amount != '0'`);

// Join WHERE statements with AND
Expand Down Expand Up @@ -376,7 +370,7 @@ FROM ${table} `;
const joinWhereQuery: any = [];

//add where filter to joinQuery
joinWhereQuery.push(`contract = {contract : string}`);
joinWhereQuery.push(`contract = {contract : String}`);
if (joinWhereQuery.length) joinSelectQuery += ` WHERE(${joinWhereQuery.join(' AND ')})`;

//Add group by to joinQuery
Expand All @@ -386,7 +380,7 @@ FROM ${table} `;

// WHERE statements
const where: any = [];
if (contract) where.push(`contract = {contract : string}`);
if (contract) where.push(`contract = {contract : String}`);
where.push(`amount != '0'`);

// Join WHERE statements with AND
Expand Down Expand Up @@ -462,9 +456,9 @@ export function getTransfers(endpoint: UsageEndpoints, query_param: any) {
const where = [];

// equals
if (contract) where.push(`contract = {contract : string}`);
if (from) where.push(`from = {from : string}`);
if (to) where.push(`to = {to : string}`);
if (contract) where.push(`contract = {contract : String}`);
if (from) where.push(`from = {from : String}`);
if (to) where.push(`to = {to : String}`);

// timestamp and block filter
additional_query_params = addBlockRangeFilter(q, additional_query_params, where);
Expand Down
10 changes: 4 additions & 6 deletions src/types/zod.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,12 +217,10 @@ export const get_Usage_supply = {
path: z.literal("/{chain}/supply"),
parameters: z.object({
query: z.object({
contract: z.string().optional(),
symbol: z.string().optional(),
name: z.string().optional(),
block_num: z.number().optional(),
limit: z.number().optional(),
page: z.number().optional(),
contract: z.string(),
block_num: z.union([z.number(), z.undefined()]),
limit: z.union([z.number(), z.undefined()]),
page: z.union([z.number(), z.undefined()]),
}),
path: z.object({
chain: z.union([z.literal("eth"), z.literal("polygon")]),
Expand Down
4 changes: 1 addition & 3 deletions src/typespec/openapi3.tsp
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,7 @@ interface Usage {
@get
supply(
@path chain: SupportedChains,
@query contract?: Supply.contract,
@query symbol?: Contract.symbol,
@query name?: Contract.name,
@query contract: Supply.contract,
...BlockFilter,
...PaginationQueryParams,
): APIResponse<UsageResponse<Supply[]>>;
Expand Down
18 changes: 1 addition & 17 deletions tsp-output/@typespec/openapi3/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -442,23 +442,7 @@
{
"name": "contract",
"in": "query",
"required": false,
"schema": {
"type": "string"
}
},
{
"name": "symbol",
"in": "query",
"required": false,
"schema": {
"type": "string"
}
},
{
"name": "name",
"in": "query",
"required": false,
"required": true,
"schema": {
"type": "string"
}
Expand Down

0 comments on commit f4d5f09

Please sign in to comment.