Skip to content

Commit

Permalink
Merge pull request #12 from koinos/7-name-resolution
Browse files Browse the repository at this point in the history
#7 name resolution
  • Loading branch information
sgerbino authored May 9, 2024
2 parents b282c0c + c2bc447 commit fcf3149
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 31 deletions.
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'
}
}
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
12 changes: 1 addition & 11 deletions utils/providers.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,6 @@
import { config } from '@/app.config'
import { Provider } from 'koilib'

declare global {
namespace NodeJS {
interface ProcessEnv {
JSONRPC_URL: string;
}
}
}

export function getProvider() {
const xJsonRpc = process.env.JSONRPC_URL || config.jsonRPC

return new Provider(xJsonRpc)
return new Provider(config.jsonRPC)
}

0 comments on commit fcf3149

Please sign in to comment.