Skip to content

Commit

Permalink
#22, #39: Resolve account names in searchParams and check existence o…
Browse files Browse the repository at this point in the history
…f required params
  • Loading branch information
mvandeberg committed Jan 31, 2025
1 parent c9d230f commit b4ca766
Show file tree
Hide file tree
Showing 19 changed files with 97 additions and 35 deletions.
3 changes: 3 additions & 0 deletions app/v1/account/[account]/history/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { decodeEvents } from '@/utils/events'
import { decodeOperations } from '@/utils/operations'
import { getProvider } from '@/utils/providers'
import { requireParameters } from '@/utils/validation'
import { BlockHeaderJson, EventData, TransactionJson, TransactionReceipt } from 'koilib'
import { NextRequest, NextResponse } from 'next/server'

Expand Down Expand Up @@ -173,6 +174,8 @@ export async function GET(request: NextRequest, { params }: { params: { account:
try {
const provider = getProvider()
const { searchParams } = new URL(request.url)
requireParameters(searchParams, 'limit');

const limit = searchParams.get('limit')
const seqNum = searchParams.get('sequence_number')
const ascending = searchParams.get('ascending') !== 'false'
Expand Down
3 changes: 3 additions & 0 deletions app/v1/blocks/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { AppError, handleError } from '@/utils/errors'
import { getProvider } from '@/utils/providers'
import { decodeEvents } from '@/utils/events'
import { decodeOperations } from '@/utils/operations'
import { requireParameters } from '@/utils/validation'

/**
* @swagger
Expand Down Expand Up @@ -141,6 +142,8 @@ export async function GET(request: Request) {
const provider = getProvider()

const { searchParams } = new URL(request.url)
requireParameters(searchParams, 'limit');

const start = searchParams.get('start')
const limit = searchParams.get('limit')
const return_block = searchParams.get('return_block') !== 'false'
Expand Down
6 changes: 5 additions & 1 deletion app/v1/nft/[contract_id]/[token_id]/approve/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getAccountAddress } from '@/utils/addresses'
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation'

/**
* @swagger
Expand Down Expand Up @@ -76,7 +78,9 @@ export async function GET(
const contract = await getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
const operator = searchParams.get('operator')
requireParameters(searchParams, 'operator')

const operator = await getAccountAddress(searchParams.get('operator')!)
const approved = searchParams.get('approved')
const memo = searchParams.get('memo')

Expand Down
11 changes: 7 additions & 4 deletions app/v1/nft/[contract_id]/[token_id]/approved/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation'

/**
* @swagger
Expand Down Expand Up @@ -28,9 +29,9 @@ import { getNFTContract } from '@/utils/tokens'
* in: query
* schema:
* type: string
* example: "\"0x00\""
* description: Token ID to start with
* required: true
* example: "0x00"
* description: Token ID to start with. If blank, will be "0x00"
* required: false
* - name: limit
* in: query
* schema:
Expand Down Expand Up @@ -68,14 +69,16 @@ export async function GET(request: Request, { params }: { params: { contract_id:
const contract = getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
requireParameters(searchParams, 'limit')

const start = searchParams.get('start')
const limit = searchParams.get('limit')
const descending = searchParams.get('descending')

try {
const { result: approvedRes } = await contract.functions.get_approved({
token_id: params.token_id,
start,
start: start ? start : '0x00',
limit,
descending: descending ? descending == 'true' : undefined
})
Expand Down
2 changes: 1 addition & 1 deletion app/v1/nft/[contract_id]/[token_id]/burn/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export async function GET(
const contract = await getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
const memo = searchParams.get('owner')
const memo = searchParams.get('memo')

try {
return Response.json(await contract.encodeOperation({
Expand Down
6 changes: 5 additions & 1 deletion app/v1/nft/[contract_id]/[token_id]/mint/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getAccountAddress } from '@/utils/addresses';
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation';

/**
* @swagger
Expand Down Expand Up @@ -69,7 +71,9 @@ export async function GET(
const contract = await getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
const to = searchParams.get('to')
requireParameters(searchParams, 'to')

const to = await getAccountAddress(searchParams.get('to')!)
const memo = searchParams.get('memo')

try {
Expand Down
8 changes: 6 additions & 2 deletions app/v1/nft/[contract_id]/[token_id]/transfer/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getAccountAddress } from '@/utils/addresses';
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation';

/**
* @swagger
Expand Down Expand Up @@ -29,7 +31,7 @@ import { getNFTContract } from '@/utils/tokens'
* schema:
* type: string
* description: Koinos address to receive the NFT
* required: false
* required: true
* example: 1LDDWoGgQ1CEa8B1d9GuziQ4fgbxcqawC3
* - name: memo
* in: query
Expand Down Expand Up @@ -69,7 +71,9 @@ export async function GET(
const contract = await getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
const to = searchParams.get('to')
requireParameters(searchParams, 'to')

const to =getAccountAddress(searchParams.get('to')!)
const memo = searchParams.get('memo')

try {
Expand Down
16 changes: 10 additions & 6 deletions app/v1/nft/[contract_id]/approve_for_all/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getAccountAddress } from '@/utils/addresses';
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation';

/**
* @swagger
Expand Down Expand Up @@ -35,8 +37,8 @@ import { getNFTContract } from '@/utils/tokens'
* in: query
* schema:
* type: boolean
* description: If the operator is approved
* required: true
* description: If the operator is approved. (Defaults to true)
* required: false
* example: true
* - name: memo
* in: query
Expand Down Expand Up @@ -65,7 +67,7 @@ import { getNFTContract } from '@/utils/tokens'
* call_contract:
* contract_id: 1EnaBDVTA5hqXokC2dDzt2JT5eHv1y7ff1
* entry_point: 541336086
* args: "ChkAY8ED6InNJ-LSQhg38spEhfSAWi8UN6HnEhkA7-Mh3yERswBXFp2UPvegxIiGAauR1O_zGAEiIjFBNlQ3dm1md3lHeDJMRDExUlJFd3Rjb1hyTHhHNnEycno="
* args: "ChkAY8ED6InNJ-LSQhg38spEhfSAWi8UN6HnEhkA7-Mh3yERswBXFp2UPvegxIiGAauR1O_zGAE="
*/
export async function GET(
request: Request,
Expand All @@ -76,10 +78,12 @@ export async function GET(
const contract = await getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
const owner = searchParams.get('owner')
const operator = searchParams.get('operator')
requireParameters(searchParams, 'owner', 'operator')

const owner = await getAccountAddress(searchParams.get('owner')!)
const operator = await getAccountAddress(searchParams.get('operator')!)
const approved = searchParams.get('approved') === 'true'
const memo = searchParams.get('owner')
const memo = searchParams.get('memo')

try {
return Response.json(await contract.encodeOperation({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getAccountAddress } from '@/utils/addresses'
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
Expand Down Expand Up @@ -50,10 +51,13 @@ export async function GET(request: Request, { params }: { params: { contract_id:
const contract_id = await getContractId(params.contract_id)
const contract = getNFTContract(contract_id)

const owner = await getAccountAddress(params.owner);
const operator = await getAccountAddress(params.operator);

try {
const { result } = await contract.functions.is_approved_for_all({
owner: params.owner,
operator: params.operator,
owner,
operator
});

if (result)
Expand Down
5 changes: 3 additions & 2 deletions app/v1/nft/[contract_id]/balance/[account]/route.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { getAccountAddress } from '@/utils/addresses';
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
Expand Down Expand Up @@ -46,11 +47,11 @@ export async function GET(
const contract_id = await getContractId(params.contract_id)
const contract = getNFTContract(contract_id)

const account = await getContractId(params.account)
const owner = await getAccountAddress(params.account)

try {
const { result: balanceRes } = await contract.functions.balance_of({
owner: account
owner
})

if (balanceRes)
Expand Down
14 changes: 9 additions & 5 deletions app/v1/nft/[contract_id]/owner/[account]/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getAccountAddress } from '@/utils/addresses'
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation'

/**
* @swagger
Expand Down Expand Up @@ -28,9 +30,9 @@ import { getNFTContract } from '@/utils/tokens'
* in: query
* schema:
* type: string
* example: "\"0x00\""
* description: Token ID to start with
* required: true
* example: "0x00"
* description: Token ID to start with. Defaults to 0.
* required: false
* - name: limit
* in: query
* schema:
Expand Down Expand Up @@ -69,18 +71,20 @@ import { getNFTContract } from '@/utils/tokens'
export async function GET(request: Request, { params }: { params: { contract_id: string, account: string } }) {
try {
const contract_id = await getContractId(params.contract_id)
const owner = await getContractId(params.account)
const owner = await getAccountAddress(params.account)
const contract = getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
requireParameters(searchParams, 'limit')

const start = searchParams.get('start')
const limit = searchParams.get('limit')
const descending = searchParams.get('descending')

try {
const { result } = await contract.functions.get_tokens_by_owner({
owner,
start,
start: start ? start : '0x00',
limit,
descending: descending ? descending !== 'false' : false
})
Expand Down
11 changes: 7 additions & 4 deletions app/v1/nft/[contract_id]/tokens/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation'

/**
* @swagger
Expand All @@ -21,9 +22,9 @@ import { getNFTContract } from '@/utils/tokens'
* in: query
* schema:
* type: string
* example: "\"0x00\""
* description: Token ID to start with
* required: true
* example: "0x00"
* description: Token ID to start with. Defaults to "0x00".
* required: false
* - name: limit
* in: query
* schema:
Expand Down Expand Up @@ -65,13 +66,15 @@ export async function GET(request: Request, { params }: { params: { contract_id:
const contract = getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
requireParameters(searchParams, 'limit')

const start = searchParams.get('start')
const limit = searchParams.get('limit')
const descending = searchParams.get('descending')

try {
const { result } = await contract.functions.get_tokens({
start,
start: start ? start : '0x00',
limit,
descending: descending ? descending !== 'false' : false
})
Expand Down
5 changes: 4 additions & 1 deletion app/v1/nft/[contract_id]/transfer/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getAccountAddress } from '@/utils/addresses';
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation';

/**
* @swagger
Expand Down Expand Up @@ -56,7 +58,8 @@ export async function GET(
const contract = await getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
const to = searchParams.get('to')
requireParameters(searchParams, 'to')
const to = await getAccountAddress(searchParams.get('to')!)

try {
return Response.json(await contract.encodeOperation({
Expand Down
2 changes: 2 additions & 0 deletions app/v1/token/[contract_id]/allowances/[account]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getAccountAddress } from '@/utils/addresses'
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getTokenContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation'
import { utils } from 'koilib'

/**
Expand Down Expand Up @@ -77,6 +78,7 @@ export async function GET(
const account = await getAccountAddress(params.account)

const { searchParams } = new URL(request.url)
requireParameters(searchParams, 'limit');

const start = searchParams.get('start')
const limit = searchParams.get('limit')
Expand Down
7 changes: 5 additions & 2 deletions app/v1/token/[contract_id]/approve/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getAccountAddress } from '@/utils/addresses';
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getTokenContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation';
import { utils } from 'koilib'

/**
Expand Down Expand Up @@ -68,9 +70,10 @@ export async function GET(
const contract = await getTokenContract(contract_id)

const { searchParams } = new URL(request.url)
requireParameters(searchParams, 'owner', 'spender', 'value')

const owner = searchParams.get('owner')
const spender = searchParams.get('spender')
const owner = await getAccountAddress(searchParams.get('owner')!)
const spender = await getAccountAddress(searchParams.get('spender')!)
const value = searchParams.get('value')
const memo = searchParams.get('memo')

Expand Down
5 changes: 4 additions & 1 deletion app/v1/token/[contract_id]/burn/route.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { getAccountAddress } from '@/utils/addresses';
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getTokenContract } from '@/utils/tokens'
import { requireParameters } from '@/utils/validation';
import { utils } from 'koilib'

/**
Expand Down Expand Up @@ -61,8 +63,9 @@ export async function GET(
const contract = await getTokenContract(contract_id)

const { searchParams } = new URL(request.url)
requireParameters(searchParams, 'from', 'value')

const from = searchParams.get('from')
const from = await getAccountAddress(searchParams.get('from')!)
const value = searchParams.get('value')
const memo = searchParams.get('memo')

Expand Down
Loading

0 comments on commit b4ca766

Please sign in to comment.