@macroid/midjs v0.0.6
Use midjs SDK to interact with MID contracts
MID.js
MIDjs integrates the MID contract and ENS and supports all the ENSjs APIs, you will only need one unified SDK to integrate all domains across multiple chains. MIDjs will hide all the complicated cross-chain detail from the partners, making the integration very easy.
Overview of the API
Installation
Install @macroid/midjs, alongside web3.
npm install @macroid/midjs web3
or
yarn add @macroid/midjs
yarn add web3
Getting Started
All that's needed to get started is a web3 provider instance, you should pass it and select network id when creating a new MID instance.
// Mumbai Testnet domain example
const MID = require("@macroid/midjs").default;
const RPC = require("@macroid/midjs/dist/constants/rpc");
const MIDfunctions = require("@macroid/midjs/dist/index");
const Web3 = require("web3");
let mid;
async function main(name) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
mid = new MID({ provider, getMidAddress: MIDfunctions.getMidAddress("80001") }); //chainId
const address = await mid.name(name).getAddress("60"); // coidId
console.log("name: %s, address: %s", name, address);
}
main("xuran12.meta");
// Mumbai Testnet domain example
async function mainName(address) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
mid = new MID({ provider, getResolverAddress: MIDfunctions.getResolverAddress("80001") });
const name = await mid.getName(address); // 0x123
console.log("address: %s, name: %s", address, name);
}
mainName("0xd530ec9517c20de518345a7210338dfb6279f454");
Registering Steps
Step 1
Check if the normalized and validated domain name is available for registering through the ENSRegistrarController contract.
async function getvalidne(name, from) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
mid = new MID({ provider, from, getENSControllerAddr: MIDfunctions.getENSControllerAddr("80001") });
const namevalue = await mid.getValidName(name); // 0x123
console.log("name: %s", namevalue);
}
getvalidne("xuranhhh", "0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0");
Parameter | Meaning | Value | Required |
---|---|---|---|
name | a validated domain name | xuranhhh | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | false |
Step 2
Call the makeCommitmentWithConfig method in ENSRegistrarController with a return of encoded commitment of type bytes32.
async function makeCommitment(name, randomString, from) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
mid = new MID({ provider, from, getENSControllerAddr: MIDfunctions.getENSControllerAddr("80001"), getMidAddress: MIDfunctions.getMidAddress("80001") });
const namevalue = await mid.makeCommitmentWithConfig(name, randomString); // 0x123
console.log("name: %s", namevalue);
}
makeCommitment("xuranhhh", "sdfadfasdfa1243sd", "0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0");
Parameter | Meaning | Value | Required |
---|---|---|---|
name | a validated domain name | xuranhhh | true |
randomString | random string | sdfadfasdfa1243sd | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | true |
Step 3
Call commit method in ENSRegistrarController. The input is step 3’s output.
async function commit(commitment, from) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
mid = new MID({ provider, from, getENSControllerAddr: MIDfunctions.getENSControllerAddr("80001") });
const value = await mid.makeCommit(commitment); // 0x123
console.log("name: %s", value);
}
commit("0xda3120f5993a30904214d0edd2581b27be1a630484908a186b3b5a7b986bd67b");
//send the result to the wallet and send the transaction
Parameter | Meaning | Value | Required |
---|---|---|---|
commitment | the input is step 3’s output. | 0xda3120f5993a30904214d0edd2581b27be1a630484908a186b3b5a7b986bd67b | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | true |
Step 4
Retrieve the real-time price of the domain by calling the rentPrice Method in ENSController contract.
async function rentPrice(name, number, from) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
mid = new MID({ provider, from, getENSControllerAddr: MIDfunctions.getENSControllerAddr("80001") });
const value = await mid.makeRentPrice(name, number); // 0x123
console.log("price: %s", value);
}
rentPrice("xuranhhh", 1);
Parameter | Meaning | Value | Required |
---|---|---|---|
name | a validated domain name | xuranhhh | true |
number | unit: year. | 1 | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | true |
Step 5
Wait for minCommitmentAge, retrieving from ENSController contract and call registerWithConfig method in ENSController.
async function regiseterConfirm(name, randomString, from, expiration, inviteAddr, rentPrice) {
const provider = new Web3.providers.HttpProvider(RPC.apis.mig_mainnet);
mid = new MID({ provider, from, getENSControllerAddr: MIDfunctions.getENSControllerAddr("80001"), getMidAddress: MIDfunctions.getMidAddress("80001") });
const resultValue = await mid.makeRegisterWithConfig(name, randomString, expiration, inviteAddr, rentPrice); // 0x123
console.log("name: %s", resultValue);
}
regiseterConfirm("xuranhhh", "sdfadfasdfa1243sd", "0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0", 1, "0x0000000000000000000000000000000000000000", "62500000000");
//send the result to the wallet and send the transaction
Parameter | Meaning | Value | Required |
---|---|---|---|
name | a validated domain name | xuranhhh | true |
randomString | need to be consistent with the random string in the step 2. | sdfadfasdfa1243sd | true |
from | the address of the user | 0x661699Cf19990B5ed7E27a12e9F6051A7D587Bb0 | true |
expiration | this domain validity period | 1 | true |
inviteAddr | inviter's address,please use empty address when empty | 0x0000000000000000000000000000000000000000 | false:empty address |
rentPrice | step 4’s output. | 62500000000 | true |
exports
default - MID
getMidAddress
getResolverContract
getMIDContract
namehash
labelhash
Build SDK and test on your test machine
yarn install
yarn run build
node main.js