API
Network
Currently, there are 3 networks compatible with dio-wallet
:
Decimal | Network |
1 | dioxide main chain network |
2 | dioxide test chain network |
3 | dioxide development chain network |
RPC
dioxide.request (args:RequestArguments)
Use the request
function to submit an RPC request to the wallet and obtain an asynchronous response.
interface RequestArguments {
method: string;
params?: unknown[] | {[key: string]: any};
}
dioxide.request(args: RequestArguments): Promise<unknown>;
The ProviderRpcError
will be returned when an RPC call encounters an exception. Please utilize it to retrieve error details.
dioxide.request({
method: "...",
params: {...},
})
.then(response => {
// The result varies by RPC method.
return response
})
.catch(ex => console.error("ProviderRpcError error",ex))
WalletConnect
WalletConnect is an open-source protocol for connecting DApps to wallets. When invoking this API, it prompts the appearance of a popup window in the wallet, you are required to finish the connection manually within the wallet.
method:
method | params | mandatory | response |
request_connect_address | params | optional | string[] |
params
name | type | mandatory | description |
symbol | string | Yes | only symbol of DApp |
Params are optional when the current DApp is an official Dioxide DApp, such as CoreDApp and Testnet Gadgets.
However, if you're an unofficial DApp connecting to the wallet, you must include
params.symbol
.Dio-wallet
will validate the DApp's authenticity on the chain based on the provided symbol.When
Dio-Wallet
is in the developer mode, we don't verify the validity ofparams.symbol
. Check here to open developer mode inDio-Wallet
.
Sample:
dioxide.request({
method: "request_connect_address",
// optional
params: {
symbol: "your_dapp_symbol"
}
})
// complete address connection
.then((accounts: string[]) => {
return accounts[0]
})
// cancel address connection(eg: close the connection window)
.catch (ex: ProviderRpcError => {
console.error(ex.code, ex.message, ex.data)
})
Get the wallet address in use within the DApp
When successfully connected to the DApp, a wallet address will be returned. Otherwise, an empty array will be returned.
name | param | response |
connected_address | - | string[] |
Sample:
const accounts: string[] = await dioxide.request({
method: "connected_address",
})
console.log(accounts[0])
Get the blockchain network the DApp is connected to
name | param | response |
network | - | NetworkItem |
Sample:
interface NetworkItem {
name: string;
id: number
}
const network: NetworkItem = await dioxide.request({
method: "network",
})
console.log(network.id, network.name)
Get the balance of the address in use
Please ensure the address is in use in the DApp. Otherwise, calling this API will result in an Unauthorized (code: 4100)
error.
name | param | response |
balance | - | string |
The decimal precision of DIO is 8. The precision of results obtained through RPC requires manual processing. For instance, "100000000:DIO" represents 1 DIO. We recommend using a library like bignumber.js for accurate floating-point conversion or high-precision calculations when working with balances.
Sample:
const balance:string = await dioxide.request({
method: "balance",
})
console.log(balance) // output xxxx:DIO
Get the profile details of the address in use (profile)
name | param | response |
profile | - | Profile |
interface Profile {
address: string
avatar: string
alias: string
}
const profile: Profile = await dioxide.request({
method: "profile",
})
console.log(profile)
Get the lock screen status of the current wallet
name | param | response |
is_locked | - | true(locked) / false(unlocked) |
Sample:
const locked: boolean = await dioxide.request({
method: "is_locked",
})
console.log(locked) // output: true or false
Sign and send transactions
name | param | mandatory | response |
send_transaction | params | Y | string |
params
name | type | mandatory | description |
func | string | Y | contract function name |
args | json | N | contract parameter. The specific value varies with the function |
limitArgs | string[] | N | custom args limit show |
tokens | <{symbole:amount}>[] | N | fca tokens |
ttl | string | N | time to live |
delegatee | string | N | proxy address |
DApp built-in wallet sending DIO
params.args:
name | type | mandatory | description |
Amount | string | Y | transfer amount(1 is 1DIO) |
To | string | Y | target address (receiver) |
try {
const txHash: string = await dioxide.request({
method: 'send_transaction',
params: {
func: 'core.coin.transfer',
args: {
Amount: “100”, // transfer 100 DIO
To:'was7fdnfrqh42c7f68pv9s8efmf8y0hv4y6m3h0sgj3ypdfyv09379wh2g:ed25519'
},
}
})
}
catch(ex) {
if (ex.code === 4001) {
console.error("User Reject")
return
}
console.error("unexpected error:",ex)
}
Create a token
To create a token, access ‘Tokens' in Core Dapps to facilitate this process.
You can also create a token using the API by configuring the parameters below to define the token's characteristics:
params.args:
name | type | mandatory | description |
Symbol | string | Y | Token name(3-8 capital letters - #) |
Flags | uint | Y | "1" represents an immutable minter, "0" represents a mutable minter |
InitSupply | uint | Y | Initial quantity (default to 0 during creation) |
Minter | uint | Y | The cid (contractid) for the token contract can be initialized with a value of 1. |
Deposit | string | Y | pre-deposited DIO during initialization (the precision of DIO must be included. If the precision of DIO decimal is 8 digits, the pre stored 1 DIO must be represented as 100000000) |
Decimals | uint | Y | Token precision |
For example:
const txHash: string = await dioxide.request({
method: 'send_transaction',
params: {
func: 'core.delegation.create_token',
args:{
"Flags":1,
"Minter": 1,
// pre store 2345 DIO,expression is String(2345 * 10**8)
"Deposit":"234500000000",
// 5-digit decimal precision,Initial circulation is 100, Initial expression is String(100 * 10**5)
"InitSupply": 0,
"Symbol":"TOKEN1"
"Decimals": 8
},
}
})
Set Token/DApp metadata information
Access Core DApps to configure the metadata information.
params.delegatee:
name | type | mandatory | description |
delegatee | string | Y | The address of the Token/NFT/DApp for which metadata will be configured |
params.args:
name | type | mandatory | description |
Metadata | string | Y | metadata information. |
const txHash: string = await dioxide.request({
method: 'send_transaction',
params: {
func: 'core.profile.set_metadata',
args: {"Metadata":"{\\"IconUrl\\":\\"https://png.pngtree.com/png-clipart/20220921/ourmid/pngtree-halloween-gnome-pumpkin-bat-illustration-png-image_6209020.png\\",\\"Description\\":\\"The description of token1\\",\\"Social\\":{}}"},
//The token address to be edited is c0jkjfzyzebdgyt2zcdp7125h8hpmvkv3rbwcsvkh0eea75jy00fkz02dr:token"
delegatee: "TOKEN1:token"
},
})
Faucet
const txHash: string = await dioxide.request({
method: 'send_transaction',
params: {
func: 'core.coin.faucet',
}
})
Create a DApp
To create a token, access ‘DApps' to facilitate this process.
You can also create a DApp using the API by configuring the parameters below to define the DApp's characteristics:
params.args:
name | type | mandatory | description |
Name | string | Y | DApp name,4-8 alphanumeric digits and _-#@ |
Type | uint | Y | Must be 10,meaning creating a DApp |
Deposit | string | Y | The amount of token deposited to the DApp, no less than 10000000 |
const txHash: string = await dioxide.request({
method: 'send_transaction',
params: {
func: 'core.delegation.create',
args: {
"Name":"MYDAPP",
"Type": 10,
// 32 DIO
'Deposit': "3200000000"
},
},
})
Increase token supply
params.args:
name | type | mandatory | description |
amount | string | Y | The amount of the increased token supply, including the decimal precision of token (For example, with a decimal precision of 5, issuing an additional 1 token means 100,000) |
token_id | string | Y | The Contract ID of the token contract |
const txHash: string = await dioxide.request({
method: 'send_transaction',
params: {
func: 'Asset.TokenManager.mint',
// If Symbol is ATH and decimal precision is 5, then 3 ATH will be issued additionally, which is expressed as Amount: 3 * 10 * * 5
args: {
"amount":"300000",
"token_id": "token_id"
}
})
Transfer DIO between any addresses
params.delegatee:
name | type | mandatory | description |
delegatee | string | If the address belongs to a Token/DApp, this parameter is required; otherwise, it is optional | DIO target address |
params.args:
name | type | mandatory | description |
To | string | Y | The recipient’s address |
Amount | string | Y | Transfer amount of DIO(1 is 1 DIO) |
const hash: string = await dioxide.request({
method: "send_transaction",
params: {
func: "core.coin.transfer",
args: {
// It can be any chain address such as token/app/user
To: "c0jkjfzyzebdgyt2zcdp7125h8hpmvkv3rbwcsvkh0eea75jy00fkz02dr:token",
// transfer 200 DIO
Amount: '200'
},
// If the target address is Token/DApp, delegatee is a required parameter and the value is the current address to be transferred out
// the connection address of the current wallet must have the management permission of the current address to be transferred out
delegatee: “TOKEN:token”,
},
});
console.log("tranasction hash", hash);
Get Estimated Transfer Gasfee
params
name | type | mandatory | description |
func | string | Y | contract function name |
args | json | N | contract parameter. The specific value varies with the function |
const gasFee = await dioxide.request({
method: 'gasfee',
params: {
func: 'core.coin.transfer',
args: {
Amount: 150,
To: 'm04adrp625hw66pg98xtzcwvyth7qd39rst7k96crcj5phmte7b26jj7bc:ed25519'
},
}
})
console.log("tranasction gasFee", gasFee);
Event
JS-API provides a set of built-in event processes, from which you can monitor if the address in use changes.
connected_address_changed
The address in use has changed, triggered by the event of switching the active address in
Dio-wallet
.
dioxide.on('connected_address_changed', (accounts: string[]) => {
// Handle the new accounts, or lack thereof.
// "accounts" will always be an array,
});
network_changed
The blockchain network the DApp is connected to has changed. For example, switching the test network to the development network or the main network in Dio-wallet
.
interface NetworkItem {
name: string;
id: number
}
dioxide.on('network_changed', (chain: ChainItem) => {
// Handle the new chain.
// Correctly handling chain changes can be complicated.
// We recommend reloading the page
// unless you have good reason not to.
window.location.reload();
});
disconnect
DApp is disconnected from the Network. For example, lost connection between wallet and remote service.
dioxide.on('disconnect', handler: (error: ProviderRpcError) => void);
lock_status_changed
The status of Lock screen has changed. For example, the wallet locks the screen.
dioxide.on("lock_status_changed", (locked: boolean) => {});
Errors
The error messages returned by an RPC request:
interface ProviderRpcError extends Error {
message: string;
code: number;
data?: unknown;
}
for example:
dioxide.request({
method: "request_connect_address",
params: {
logo: "https://scan.dioxide.network/img/logo.png",
},
})
.then((accounts: string[]) => {
return accounts
})
.catch (ex: ProviderRpcError => {
console.error(ex.code, ex.message, ex.data)
})
part of build-in error code:
export enum StandardCode {
UserReject = 4001,
Unauthorized = 4100,
Unsupported_Method = 4200,
Unsupported_Event = 4300,
Absent_method_handler = 4400,
Disconnected = 4900, // The Provider is disconnected from all chains.
Chain_Disconnected = 4901, // The Provider is not connected to the requested chain.
Invalid_Request = 32600,
Method_Not_Found = 32601,
Invalid_Params = 32602,
Internal_Error = 32603,
Dioxide_Scan_Service_Error = 32604,
Operation_Timeout = 32605,
Parse_Error = 32700,
}
const StandardMessage: KeyValue<string> = {
4001: 'User Reject',
4100: 'unauthorized',
4200: 'unsupported method',
4300: 'unsupported event',
4400: 'absent method handler',
4900: 'all chains disconnected',
4901: 'the chain disconnected',
32600: 'invalid request',
32601: 'method not found',
32602: 'invalid params',
32603: 'internal error',
32604: 'dioxide scan service networking error',
32605: 'operation timeout',
32700: 'parse error',
}