sns-app-contract-api
v1.1.32
Published
Most functions in this library are async functions and therefore return promises which can be awaited or chained with `.then`. ## Contracts
Downloads
7
Readme
Reusable functions and components for the SNS apps
Most functions in this library are async functions and therefore return promises which can be awaited or chained with .then
.
Contracts
Installation
# Install the npm package in your project
npm install sns-app-contract-api
or
yarn add sns-app-contract-api
// Import 'sns-app-contract-api' in your project
import { setupSNS } from 'sns-app-contract-api'
Start
setup
It can optionally provide a customProvider and an SNS address. In general, you don't need this unless you are running ganache.
It will return an object containing a registrar, sns object, network, provider object, and tithe contract object. sns object will handle name resolution, reverse logging, and handle the registry. The registrar object has the ability to interact with permanent registrars, traditional auction registrars and test registrars (just on the test network).
import { setupSNS } from 'sns-app-contract-api'
window.addEventListener('load', async () => {
const { registrar, sns } = await setupSNS()
const owner = await sns.getOwner('linkkeydao.key')
// will instantiate with window.web3/window.ethereum if found, read-only if not.
// Once setup has finished you can now call functions off the library
})
async function setupSNS(options):void
setupSNS must be called before anything other function in this library. We recommend calling it in a window.load event to make sure that your web3 object has loaded. You can provide a custom provider yourself, but by default it will look for window.web3
or window.ethereum
if you do not give it a provider. We use the custom provider when we need to run automated tests with ganache. You can also it pass it the registry address, but by default it will derive the network you are on and instantiate SNS using that network's registry. You only need to provider it with an sns address if you are on a private network.
Arguments
options (object): {
customProvider (object): Provider object from web3 (optional)
snsAddress (String): Address of the SNS registry (optional)
}
Example
import { setupSNS } from 'sns-app-contract-api'
window.addEventListener('load', async () => {
const { sns, snsWithdraw, snsResolver, provider,network,providerObject } = await setupSNS()
})
Registry API
registry()
transfer()
getNameOfOwner()
getResolverAddress()
getResolverOwner()
getTokenIdOfName()
recordExists()
getDomainDetails()
getRegisteredPrice()
async function registry(name)
This function is for a new domain name registration
Arguments
name:(string):Domain name to be registered
Returns
transactionMsg(object): Transcation response object
Example
const name = 'sns.key'
const transactionObj = await sns.registry(name)
async function transfer(address, name)
This function is used for transfers or transactions
Arguments
address(string):Eth address of the owner on the registry
name(string):An SNS name
Returns
transactionMsg(object): Transcation response object
Example
const address = '0x123abc...'
const name = 'sns.key'
const transactionObj = await sns.transfer(address, name)
async function getNameOfOwner(address)
Get the name of the owner
Arguments
address(string):Eth address of the owner on the registry
Returns
name(string):An SNS name
Example
const name = await sns.getNameOfOwner('0x123abc...')
// sns.key
async function getResolverAddress(name)
Get the address of the parser
Arguments
name(string):An SNS name
Returns
address(string):The resolver address
Example
const name = 'sns.key'
const address = await sns.getResolverAddress(name)
// 0x123abc...
async function getResolverOwner
Get the address of the owner of this resolver
Arguments
name(string):An SNS name
Returns
address(string):An owner's resolver address
Example
const name = 'sns.key'
const address = await getResolverOwner(name)
// 0x123abc...
async function getTokenIdOfName
Get the token ID for this name
Arguments
name(string):An SNS name
Returns
tokenId(number):The token of the name
Example
const name = 'sns.key'
const tokenId = await sns.getTokenIdOfName(name)
// 1
async function recordExists
Get whether the name record exists
Arguments
name(string):An SNS name
Returns
isExists(boolean):Whether the name record exists
Example
const name = 'sns.key'
const isExists = await sns.recordExists(name)
// false/true
async function getDomainDetails
Get some information about this domain
Arguments
name(string):An SNS name
Returns
domainDetailsObj:{
name: "test1.key", // An SNS name
label: "test1", // An SNS name(remove suffix)
labelhash: "0xc318..", // A hash value of an SNS name
owner: "0x9cc..", // Address of owner
resolver: "0xB20..", // The resolver address
addr: null, // custom address
content: null // custom content
}
Example
const name = 'sns.key'
const detailsObj = await sns.getDomainDetails(name)
async function getRegisteredPrice
Get Registration Price
Returns
price(number):Registration Fee
Example
const price = await sns.getRegisteredPrice()
// 10
Resolvers API
getAllProperties()
setAllProperties()
async function getAllProperties
Arguments
name(string):An SNS name
Returns
properties(string):Strings spliced with '+' (e.g:+xxx+yyy+zz+++aaa+b+)
Example
const name = 'sns.key'
const record = await snsResolver.getAllProperties(name)
// +xxx+yyy+zzz+++sss+aaaa
async function setAllProperties
Arguments
name(string):An SNS name
records:(string):Strings spliced with '+' (e.g:+xxx+yyy+zz+++aaa+b+)
Returns
transactionMsg(object): Transcation response object
Example
const name = 'sns.key'
const recordStr = '+xxx+yyy+zzz+++sss+aaaa'
const transactionTx = await snsResolver.setAllProperties(name, recordStr)
Withdraw API
getFeeValue()
withdraw()
async function getFeeValue
Returns
fee(number):User can withdraw balance
Example
const fee = snsWithdraw.getFeeValue()
async function withdraw
Returns
transactionMsg(object): Transcation response object
Example
const transcationTx = await snsWithdraw.withdraw()
Transaction Response
The transaction response object gets returned by the promise of all state modifying functions of the library. The most important properties is the wait
function which can be called by the initial response, before the transaction has been mined. You can await this promise and it will give you the transaction receipt. The transaction receipt, is the same as the transaction response object, except is has a blockHash
,blockNumber
and timestamp
of the block the transaction has been included in.
{
// Only available for unmined transactions
wait: function(){}, //this function is to wait for the transaction to be mined
// Only available for mined transactions
blockHash: "0x7f20ef60e9f91896b7ebb0962a18b8defb5e9074e62e1b6cde992648fe78794b",
blockNumber: 3346463,
// Exactly one of these will be present (send vs. deploy contract)
// They will always be a properly formatted checksum address
creates: null,
to: "0xc149Be1bcDFa69a94384b46A1F91350E5f81c1AB",
// The transaction hash
hash: "0xf517872f3c466c2e1520e35ad943d833fdca5a6739cfea9e686c4c1b3ab1022e",
// See above "Transaction Requests" for details
data: "0x",
from: "0xEA674fdDe714fd979de3EdF0F56AA9716B898ec8",
gasLimit: utils.bigNumberify("90000"),
gasPrice: utils.bigNumberify("21488430592"),
value: utils.parseEther(1.0017071732629267),
// The chain ID; 0 indicates replay-attack vulnerable
// (eg. 1 = Homestead mainnet, 3 = Ropsten testnet)
chainId: 1,
// The signature of the transaction (TestRPC may fail to include these)
r: "0x5b13ef45ce3faf69d1f40f9d15b0070cc9e2c92f3df79ad46d5b3226d7f3d1e8",
s: "0x535236e497c59e3fba93b78e124305c7c9b20db0f8531b015066725e4bb31de6",
v: 37,
// The raw transaction (TestRPC may be missing this)
raw: "0xf87083154262850500cf6e0083015f9094c149be1bcdfa69a94384b46a1f913" +
"50e5f81c1ab880de6c75de74c236c8025a05b13ef45ce3faf69d1f40f9d15b0" +
"070cc9e2c92f3df79ad46d5b3226d7f3d1e8a0535236e497c59e3fba93b78e1" +
"24305c7c9b20db0f8531b015066725e4bb31de6",
}