Skip to content

Latest commit

 

History

History
128 lines (94 loc) · 4.07 KB

File metadata and controls

128 lines (94 loc) · 4.07 KB
description Handle the records of your domain

📚 Records

A domain record has the following fields:

  • class: The class of the record, chosen from a predefined list of possibilities.
  • data: The data associated with the record, which can be a string or a buffer, depending on the record class.

Available record classes

RecordClassEnum exposes the following classes:

Class Value
Bio 0
Discord 1
Twitter 2
Uri 3
Wallet 4
Avatar 5
Email 6
Price 7
Main 8

The web app offers Bio, Email, Twitter, Discord, Wallet, Price and Uri. Avatar and Main exist on chain but are not exposed there.

Getting a record repository

Records are managed through the repository attached to a domain:

import { RecordClassEnum } from '@metanames/sdk'

const domain = await metaNamesSdk.domainRepository.find('supercool.mpc')
if (!domain) throw new Error('Domain not found')

const recordRepository = domain.getRecordRepository(metaNamesSdk)

Creating, updating and deleting records are write operations, so a signing strategy must be set first — see getting-started.md.

Creating a domain record

Provide the class and data fields for the new record:

const { transactionHash, fetchResult } = await recordRepository.create({
  class: RecordClassEnum.Wallet,
  data: 'data',
})
console.log(`Transaction hash: ${transactionHash}`)
const result = await fetchResult
console.log(`Domain record creation submitted: ${result}`)

Replace RecordClassEnum.Wallet with the desired record class and 'data' with the appropriate data for the record. To create several records in a single transaction, pass an array to createBatch.

Updating a domain record

const { transactionHash, fetchResult } = await recordRepository.update({
  class: RecordClassEnum.Wallet,
  data: 'newData',
})
console.log(`Transaction hash: ${transactionHash}`)
const result = await fetchResult
console.log(`Domain record update submitted: ${result}`)

Replace RecordClassEnum.Wallet with the class of the record you want to update and 'newData' with the updated data.

Deleting a domain record

delete takes the record class directly, not an object:

const { transactionHash, fetchResult } = await recordRepository.delete(RecordClassEnum.Wallet)
console.log(`Transaction hash: ${transactionHash}`)
const result = await fetchResult
console.log(`Domain record deletion submitted: ${result}`)

Reading a domain record

find takes the record class and reads from the domain data already loaded, returning null when the domain has no record of that class:

const record = await recordRepository.find(RecordClassEnum.Uri)
console.log(`Record: ${JSON.stringify(record)}`)

Example usage

Here's an example of how you can use these functionalities together:

import { RecordClassEnum } from '@metanames/sdk'

const domain = await metaNamesSdk.domainRepository.find('supercool.mpc')
if (!domain) throw new Error('Domain not found')
const recordRepository = domain.getRecordRepository(metaNamesSdk)

// Create a new domain record
const createIntent = await recordRepository.create({
  class: RecordClassEnum.Uri,
  data: 'https://example.com',
})
console.log(`Transaction hash: ${createIntent.transactionHash}`)
console.log(`Domain record creation submitted: ${await createIntent.fetchResult}`)

// Update the existing domain record
const updateIntent = await recordRepository.update({
  class: RecordClassEnum.Uri,
  data: 'https://new-example.com',
})
console.log(`Transaction hash: ${updateIntent.transactionHash}`)
console.log(`Domain record update submitted: ${await updateIntent.fetchResult}`)

// Delete the existing domain record
const deleteIntent = await recordRepository.delete(RecordClassEnum.Uri)
console.log(`Transaction hash: ${deleteIntent.transactionHash}`)
console.log(`Domain record deletion submitted: ${await deleteIntent.fetchResult}`)

Replace RecordClassEnum.Uri and the URLs with your actual values.