Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#7 name resolution #12

Merged
merged 4 commits into from
May 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions app.config.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
declare global {
namespace NodeJS {
interface ProcessEnv {
JSONRPC_URL: string,
KAP_ADDRESS: string,
NICKNAMES_ADDRESS: string
}
}
}

export type Config = {
jsonRPC: string
systemContracts: Record<string, string>
contracts: Record<string, string>
}

export const config: Config = {
jsonRPC: 'http://localhost:8080/',
systemContracts: {
koin: '15DJN4a8SgrbGhhGksSBASiSYjGnMU8dGL',
vhp: '18tWNU7E4yuQzz7hMVpceb9ixmaWLVyQsr',
pob: '159myq5YUhhoVWu3wsHKHiJYKPKGUrGiyv',
claim: '18zw3ZokdfHtudzaWAUnU4tUvKzKiJeN76',
governance: '19qj51eTbSFJYU7ZagudkpxPgNSzPMfdPX',
nameservice: '19WxDJ9Kcvx4VqQFkpwVmwVEy1hMuwXtQE',
resources: '1HGN9h47CzoFwU2bQZwe6BYoX4TM6pXc4b'
},
jsonRPC: process.env.JSONRPC_URL || 'http://localhost:8080/',
contracts: {
kap: '13tmzDmfqCsbYT26C4CmKxq86d33senqH3',
nicknames:'1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz'
kap: process.env.KAP_ADDRESS || '13tmzDmfqCsbYT26C4CmKxq86d33senqH3',
nicknames: process.env.NICKNAMES_ADDRESS || '1KD9Es7LBBjA1FY3ViCgQJ7e6WH1ipKbhz'
}
}
2 changes: 1 addition & 1 deletion app/v1/account/[account]/nonce/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { decode } from 'punycode'

/**
* @swagger
* /api/account/{account}/nonce:
* /v1/account/{account}/nonce:
* get:
* tags: [Accounts]
* description: Returns the account's nonce
Expand Down
2 changes: 1 addition & 1 deletion app/v1/block/[block_id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { decodeOperations } from '@/utils/operations'

/**
* @swagger
* /api/block/{block_id}:
* /v1/block/{block_id}:
* get:
* tags: [Blocks]
* description: Input a block id or number. Return data about that block and its receipt.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/chain/fork_heads/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getProvider } from '@/utils/providers'

/**
* @swagger
* /api/chain/fork_heads:
* /v1/chain/fork_heads:
* get:
* tags: [Chain]
* description: Returns the chain's fork heads
Expand Down
2 changes: 1 addition & 1 deletion app/v1/chain/head_info/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getProvider } from '@/utils/providers'

/**
* @swagger
* /api/chain/head_info:
* /v1/chain/head_info:
* get:
* tags: [Chain]
* description: Returns the chain's head info
Expand Down
2 changes: 1 addition & 1 deletion app/v1/chain/id/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getProvider } from '@/utils/providers'

/**
* @swagger
* /api/chain/id:
* /v1/chain/id:
* get:
* tags: [Chain]
* description: Returns the chain id
Expand Down
2 changes: 1 addition & 1 deletion app/v1/chain/resource_limits/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { getProvider } from '@/utils/providers'

/**
* @swagger
* /api/chain/resource_limits:
* /v1/chain/resource_limits:
* get:
* tags: [Chain]
* description: Returns the chain's resource limits
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { AppError, handleError, getErrorMessage } from '@/utils/errors'

/**
* @swagger
* /api/contract/{contract_id}/read/{method}:
* /v1/contract/{contract_id}/{method}:
* get:
* tags: [Contracts]
* description: Read the contract contract using the method and arguments provided
Expand Down Expand Up @@ -89,7 +89,7 @@ export async function GET(

/**
* @swagger
* /api/contract/{contract_id}/read/{method}:
* /v1/contract/{contract_id}/{method}:
* post:
* tags: [Contracts]
* description: Read the contract using the method and arguments provided
Expand All @@ -108,7 +108,6 @@ export async function GET(
* description: Method of the contract to call
* required: true
* example: balance_of
* - $ref: '#/components/parameters/X-JSON-RPC-URL'
* requestBody:
* description: Arguments for the method call
* required: true
Expand Down
2 changes: 1 addition & 1 deletion app/v1/contract/[contract_id]/abi/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import protobufjs from 'protobufjs'

/**
* @swagger
* /api/contract/{contract_id}/abi:
* /v1/contract/{contract_id}/abi:
* get:
* tags: [Contracts]
* description: Returns the contract's ABI
Expand Down
2 changes: 1 addition & 1 deletion app/v1/decode/events/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { decodeEvents } from '@/utils/events'

/**
* @swagger
* /api/decode/events:
* /v1/decode/events:
* post:
* tags: [Decode]
* description: This endpoint takes an array of "encoded" events and returns an array of "decoded" events. Feel free to test the example request body and response below before testing out your own data.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/decode/operations/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { decodeOperations } from '@/utils/operations'

/**
* @swagger
* /api/decode/operations:
* /v1/decode/operations:
* post:
* tags: [Decode]
* description: This endpoint takes an array of 'encoded' operations and returns an array of 'decoded' operations. Feel free to test the example request body and response below before testing out your own data.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/nft/[contract_id]/balance/[account]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getNFTContract } from '@/utils/tokens'

/**
* @swagger
* /api/nft/{contract_id}/balance/{account}:
* /v1/nft/{contract_id}/balance/{account}:
* get:
* tags: [Non Fungible Tokens]
* description: Returns the non fungible token's account balance.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/nft/[contract_id]/info/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { utils } from 'koilib'

/**
* @swagger
* /api/nft/{contract_id}/info:
* /v1/nft/{contract_id}/info:
* get:
* tags: [Non Fungible Tokens]
* description: Returns information about the non fungible token, such as its name, symbol, total supply, and URI.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/nft/[contract_id]/name/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getNFTContract } from '@/utils/tokens'

/**
* @swagger
* /api/nft/{contract_id}/name:
* /v1/nft/{contract_id}/name:
* get:
* tags: [Non Fungible Tokens]
* description: Returns the name of the non fungible token.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/nft/[contract_id]/symbol/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getNFTContract } from '@/utils/tokens'

/**
* @swagger
* /api/nft/{contract_id}/symbol:
* /v1/nft/{contract_id}/symbol:
* get:
* tags: [Non Fungible Tokens]
* description: Returns the symbol of the non fungible token.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/nft/[contract_id]/total_supply/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getNFTContract } from '@/utils/tokens'

/**
* @swagger
* /api/nft/{contract_id}/total_supply:
* /v1/nft/{contract_id}/total_supply:
* get:
* tags: [Non Fungible Tokens]
* description: Returns the total supply of the non fungible token.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/nft/[contract_id]/uri/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getNFTContract } from '@/utils/tokens'

/**
* @swagger
* /api/nft/{contract_id}/uri:
* /v1/nft/{contract_id}/uri:
* get:
* tags: [Non Fungible Tokens]
* description: Returns the URI associated with the non fungible token contract.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/token/[contract_id]/balance/[account]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { utils } from 'koilib'

/**
* @swagger
* /api/token/{contract_id}/balance/{account}:
* /v1/token/{contract_id}/balance/{account}:
* get:
* tags: [Fungible Tokens]
* description: Returns the fungible token balance for a specific account
Expand Down
2 changes: 1 addition & 1 deletion app/v1/token/[contract_id]/decimals/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getFTContract } from '@/utils/tokens'

/**
* @swagger
* /api/token/{contract_id}/decimals:
* /v1/token/{contract_id}/decimals:
* get:
* tags: [Fungible Tokens]
* description: Returns the number of decimals for a fungible token
Expand Down
2 changes: 1 addition & 1 deletion app/v1/token/[contract_id]/info/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { utils } from 'koilib'

/**
* @swagger
* /api/token/{contract_id}/info:
* /v1/token/{contract_id}/info:
* get:
* tags: [Fungible Tokens]
* description: Returns the fungible token's information such as name, symbol, decimals, and total supply
Expand Down
2 changes: 1 addition & 1 deletion app/v1/token/[contract_id]/name/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getFTContract } from '@/utils/tokens'

/**
* @swagger
* /api/token/{contract_id}/name:
* /v1/token/{contract_id}/name:
* get:
* tags: [Fungible Tokens]
* description: Returns the name of the fungible token
Expand Down
2 changes: 1 addition & 1 deletion app/v1/token/[contract_id]/symbol/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { getFTContract } from '@/utils/tokens'

/**
* @swagger
* /api/token/{contract_id}/symbol:
* /v1/token/{contract_id}/symbol:
* get:
* tags: [Fungible Tokens]
* description: Returns the symbol of the fungible token
Expand Down
2 changes: 1 addition & 1 deletion app/v1/token/[contract_id]/total_supply/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { utils } from 'koilib'

/**
* @swagger
* /api/token/{contract_id}/total_supply:
* /v1/token/{contract_id}/total_supply:
* get:
* tags: [Fungible Tokens]
* description: Returns the total supply of the fungible token
Expand Down
2 changes: 1 addition & 1 deletion app/v1/transaction/[transaction_id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { interfaces } from 'koilib'

/**
* @swagger
* /api/transaction/{transaction_id}:
* /v1/transaction/{transaction_id}:
* get:
* tags: [Transactions]
* description: Returns detailed information about a specific transaction.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/transaction/prepare/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NextRequest, NextResponse } from 'next/server'

/**
* @swagger
* /api/transaction/prepare:
* /v1/transaction/prepare:
* post:
* tags: [Transactions]
* description: This endpoint takes a transaction and an optional provider and/or payer, then returns a prepared transaction object.
Expand Down
2 changes: 1 addition & 1 deletion app/v1/transaction/submit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { revalidatePath } from 'next/cache'

/**
* @swagger
* /api/transaction/submit:
* /v1/transaction/submit:
* post:
* tags: [Transactions]
* description: This endpoint takes a transaction and submits it to the JSON RPC node.
Expand Down
74 changes: 69 additions & 5 deletions utils/addresses.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,30 @@
import { config } from '@/app.config'
import { getKAPName } from '@/services/kap'
import { utils } from 'koilib'
import { Serializer, utils } from 'koilib'
import { AppError } from './errors'
import { getProvider } from './providers'
import { getNicknameOwner } from '@/services/nicknames'

let contractAddresses: Record<string, string> = {};

export async function getAddress(str: string) {
if (contractAddresses[str]) {
return contractAddresses[str];
}

// system contracts
if (config.systemContracts[str]) {
return config.systemContracts[str]
const contract = await getSystemAddress(str);

if (contract) {
contractAddresses[str] = contract;
return contract;
}

// kap names
else if (str.endsWith('.koin')) {
if (str.endsWith('.koin')) {
const kapName = await getKAPName(str)

if (kapName) {
contractAddresses[str] = kapName.owner;
return kapName.owner
}
}
Expand All @@ -22,6 +33,7 @@ export async function getAddress(str: string) {
else if (str.startsWith('@')) {
const owner = await getNicknameOwner(str)
if (owner) {
contractAddresses[str] = owner;
return owner
}
}
Expand All @@ -48,3 +60,55 @@ export async function getAccountAddress(str: string) {

return address
}

export async function getSystemAddress(str: string): Promise<string | undefined> {
const provider = getProvider()

const serializer = new Serializer(
{
nested: {
get_address_arguments: {
fields: {
name: {
type: "string",
id: 1,
},
},
},
get_address_result: {
fields: {
value: {
type: "address_record",
id: 1,
},
},
},
address_record: {
fields: {
name: {
type: "bytes",
id: 1,
options: {
"(koinos.btype)": "ADDRESS",
},
},
},
},
},
},
{
argumentsTypeName: "get_address_arguments",
returnTypeName: "get_address_result",
}
);

try {
let res = await provider.invokeSystemCall(serializer, 10001, {"name": str});

if ((res["value"] as any)["name"]) {
return String((res["value"] as any)["name"]);
}
} catch (error) {}

return
}
2 changes: 0 additions & 2 deletions utils/contracts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,6 @@ let CONTRACTS_CACHE: Record<string, Contract> | undefined
function getContractsCache(contractId: string): Contract | undefined {
if (!CONTRACTS_CACHE) {
CONTRACTS_CACHE = {
[config.systemContracts.koin]: getFTContract(config.systemContracts.koin),
[config.systemContracts.vhp]: getFTContract(config.systemContracts.vhp),
[config.contracts.kap]: new Contract({
id: config.contracts.kap,
// @ts-ignore abi is compatible
Expand Down
Loading