Skip to content

Commit

Permalink
#22: Update NFT paths for KCS-5
Browse files Browse the repository at this point in the history
  • Loading branch information
mvandeberg committed Jan 31, 2025
1 parent 7f621fc commit e2aa466
Show file tree
Hide file tree
Showing 25 changed files with 1,481 additions and 559 deletions.
655 changes: 120 additions & 535 deletions abis/nft.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions app/v1/contract/[contract_id]/[method]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { AppError, handleError, getErrorMessage } from '@/utils/errors'
* get:
* tags: [Contracts]
* summary: Call the contract contract using the method and arguments provided.
* description: Executes a specified method on the given contract.
* description: Executes a specified method on the given contract.
* If the method is a read call, the result is returned, without making any state changes.
* If the method is a write call, the associated operation is returned for inclusion in a transaction and no state changes are made.
* parameters:
Expand Down Expand Up @@ -102,7 +102,7 @@ export async function GET(
* post:
* tags: [Contracts]
* summary: Call the contract contract using the method and arguments provided.
* description: Executes a specified method on the given contract.
* description: Executes a specified method on the given contract.
* If the method is a read call, the result is returned, without making any state changes.
* If the method is a write call, the associated operation is returned for inclusion in a transaction and no state changes are made.
* parameters:
Expand Down
104 changes: 104 additions & 0 deletions app/v1/nft/[contract_id]/[token_id]/approve/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'

/**
* @swagger
* /v1/nft/{contract_id}/{token_id}/approve:
* get:
* tags: [Non Fungible Tokens]
* description: Approves a user for a token, returning the operation.
* summary: Approves a user for a token, returning the operation.
* parameters:
* - name: contract_id
* in: path
* schema:
* type: string
* description: Koinos address of the contract
* required: true
* example: "@koinos.fun"
* - name: token_id
* in: path
* schema:
* type: string
* description: The token ID
* required: true
* example: "0x35303437"
* - name: operator
* in: query
* schema:
* type: string
* description: Koinos address of the account operating the token
* required: true
* example: 1EnaBDVTA5hqXokC2dDzt2JT5eHv1y7ff1
* - name: approve
* in: query
* schema:
* type: boolean
* description: Whether to approve the operator or not.
* required: false
* example: true
* - name: memo
* in: query
* schema:
* type: string
* description: Optional memo
* required: false
* responses:
* 200:
* description: Operation
* content:
* application/json:
* schema:
* type: object
* properties:
* call_contract:
* type: object
* properties:
* contract_id:
* type: string
* entry_point:
* type: integer
* args:
* type: string
* example:
* call_contract:
* contract_id: 1EnaBDVTA5hqXokC2dDzt2JT5eHv1y7ff1
* entry_point: 1960973952
* args: "ChkAY8ED6InNJ-LSQhg38spEhfSAWi8UN6HnEhkAlzgMKVPolWBD5lWUM7yqKcudeRB3V_SkGgQ1MDQ3"
*/
export async function GET(
request: Request,
{ params }: { params: { contract_id: string, token_id: string } }
) {
try {
const contract_id = await getContractId(params.contract_id)
const contract = await getNFTContract(contract_id)

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

try {
const { result: ownerRes } = await contract.functions.owner_of({
token_id: params.token_id
});

return Response.json(await contract.encodeOperation({
name: 'approve',
args: {
owner: ownerRes?.value,
operator: operator,
token_id: params.token_id,
approved: approved ? approved == 'true' : undefined,
memo
}
}));
} catch (error) {
throw new AppError(getErrorMessage(error as Error))
}
} catch (error) {
return handleError(error as Error)
}
}
98 changes: 98 additions & 0 deletions app/v1/nft/[contract_id]/[token_id]/approved/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'

/**
* @swagger
* /v1/nft/{contract_id}/{token_id}/approved:
* get:
* tags: [Non Fungible Tokens]
* description: Returns the approved operators for the token
* summary: Returns the approved operators for the token.
* parameters:
* - name: contract_id
* in: path
* schema:
* type: string
* description: The Koinos address of the NFT contract.
* required: true
* example: "@koinos.fun"
* - name: token_id
* in: path
* schema:
* type: string
* description: The token ID
* required: true
* example: 0x3937
* - name: start
* in: query
* schema:
* type: string
* example: "\"0x00\""
* description: Token ID to start with
* required: true
* - name: limit
* in: query
* schema:
* type: integer
* example: 5
* description: Number of tokens to return
* required: true
* - name: descending
* in: query
* schema:
* type: boolean
* example:
* description: "Flag to return tokens in descending order (default: false)"
* required: false
* responses:
* 200:
* description: The operator's approval status for the token
* content:
* application/json:
* schema:
* type: object
* properties:
* value:
* type: array
* items:
* type: string
* example:
* value: []
*/

export async function GET(request: Request, { params }: { params: { contract_id: string, token_id: string } }) {
try {
const contract_id = await getContractId(params.contract_id)

const contract = getNFTContract(contract_id)

const { searchParams } = new URL(request.url)
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,
limit,
descending: descending ? descending == 'true' : undefined
})

if (approvedRes?.values) {
return Response.json({value: approvedRes?.values})
}

if (approvedRes?.value){
return Response.json({value: [approvedRes?.value]})
}

return Response.json({value: []})
} catch (error) {
throw new AppError(getErrorMessage(error as Error))
}
} catch (error) {
return handleError(error as Error)
}
}
81 changes: 81 additions & 0 deletions app/v1/nft/[contract_id]/[token_id]/burn/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'

/**
* @swagger
* /v1/nft/{contract_id}/{token_id}/burn:
* get:
* tags: [Non Fungible Tokens]
* description: Burns the token, returning the operation.
* summary: Burns the token, returning the operation.
* parameters:
* - name: contract_id
* in: path
* schema:
* type: string
* description: Koinos address of the contract
* required: true
* example: "@koinos.fun"
* - name: token_id
* in: path
* schema:
* type: string
* description: The token ID
* required: true
* example: "0x3937"
* - name: memo
* in: query
* schema:
* type: string
* description: Optional memo
* required: false
* responses:
* 200:
* description: Operation
* content:
* application/json:
* schema:
* type: object
* properties:
* call_contract:
* type: object
* properties:
* contract_id:
* type: string
* entry_point:
* type: integer
* args:
* type: string
* example:
* call_contract:
* contract_id: 1EnaBDVTA5hqXokC2dDzt2JT5eHv1y7ff1
* entry_point: 2241834181
* args: "CgI5Nw=="
*/
export async function GET(
request: Request,
{ params }: { params: { contract_id: string; token_id: string } }
) {
try {
const contract_id = await getContractId(params.contract_id)
const contract = await getNFTContract(contract_id)

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

try {
return Response.json(await contract.encodeOperation({
name: 'burn',
args: {
token_id: params.token_id,
memo
}
}))
} catch (error) {
throw new AppError(getErrorMessage(error as Error))
}
} catch (error) {
return handleError(error as Error)
}
}
60 changes: 60 additions & 0 deletions app/v1/nft/[contract_id]/[token_id]/metadata/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import { getContractId } from '@/utils/contracts'
import { AppError, getErrorMessage, handleError } from '@/utils/errors'
import { getNFTContract } from '@/utils/tokens'

/**
* @swagger
* /v1/nft/{contract_id}/{token_id}/metadata:
* get:
* tags: [Non Fungible Tokens]
* description: Returns the metadata of the token.
* summary: Returns the metadata of the token.
* parameters:
* - name: contract_id
* in: path
* schema:
* type: string
* description: The Koinos address of the NFT contract.
* required: true
* example: "@koinos.fun"
* - name: token_id
* in: path
* schema:
* type: string
* description: The token ID
* required: true
* example: "0x35303437"
* responses:
* 200:
* description: The metadata of the token
* content:
* application/json:
* schema:
* type: object
* properties:
* value:
* type: string
* example:
* value: false
*/

export async function GET(request: Request, { params }: { params: { contract_id: string, token_id: string } }) {
try {
const contract_id = await getContractId(params.contract_id)
const contract = getNFTContract(contract_id)

try {
const { result } = await contract.functions.metadata_of({
token_id: params.token_id
});

return result?.value ?
Response.json(JSON.parse(result?.value)) :
Response.json({});
} catch (error) {
throw new AppError(getErrorMessage(error as Error))
}
} catch (error) {
return handleError(error as Error)
}
}
Loading

0 comments on commit e2aa466

Please sign in to comment.