System Contract

操作實例

NPM version

dmc_client 對象是 dmc.js 的一個實例,它提供了一系列的方法,用於與區塊鏈進行交互。
以下代碼中的 dmc_client 均由下面的代碼創建,不再贅述。

1
2
3
4
5
6
7
8
9
10
var DMC = require('dmc.js');
var dmc_client = DMC({
chainId: 'c102a8115bef9e4a4e751559aac2cdc2859417e6476f8cb6054cd3f7dffe1ce4',
keyProvider: 'creator_priKey',
httpEndpoint: 'http://testnode.dmctech.io:8801',
logger: {
log: null,
error: null
}
});

newaccount

創建賬戶

參數

name type description
creator string 創建者的賬戶名
name string 被創建者的賬戶名
owner string 被創建者賬戶 owner 權限公鑰
active string 被創建者 active 權限公鑰

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'newaccount',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
creator: account,
name: 'newaccount',
owner: {
threshold: 1,
keys: [{
key: 'DMC6MRy..',
weight: 1
}],
accounts: [],
waits: []
},
active: {
threshold: 1,
keys: [{
key: 'DMC6MRy..',
weight: 1
}],
accounts: [],
waits: []
},
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

setcode

部署 wasm 合約

參數

name type description
account string 合約所屬賬戶
vmtype uint8 合約引擎類型
vmversion uint8 合約引擎版本
code string 合約代碼

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'setcode',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
account: account,
vmtype: '0',
vmversion: '0',
code: Buffer.from(fs.readFile(${wasm_file_path})).hex()
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

setabi

部署 abi 文件

參數

name type description
account string 合約所屬賬戶
abi json abi 文件代碼

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let r = dmc_client.transact_sync({
actions: [{
account: 'dmc',
name: 'setabi',
authorization: [
{
actor: account,
permission: 'active'
},
],
data: {
account: account,
abi: encoding.hex.encode(dmc.jsonToRawAbi(abi))
},
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

updateauth

增加或修改用戶權限

參數

name type description
account string 賬戶名稱
permission string 已存在的權限或者新增的權限名稱
parent string 創建該權限的父權限
auth json 該權限的組成

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'updateauth',
authorization: [{
actor: account,
permission: 'owner',
}],
data: {
account: account,
permission: "PERMISSION_NAME",
parent: "PARENT_PERMISSION_NAME",
auth: {
threshold: 1,
keys: [{
key: new_pub_key,
weight: 1
}],
accounts: [],
waits: []
},
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

deleteauth

刪除權限

參數

name type description
account string 賬戶名稱
permission string 需刪除的權限

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'deleteauth',
authorization: [{
actor: account,
permission: 'owner',
}],
data: {
account: account,
permission: "PERMISSION_NAME",
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});

linkauth

給權限指定 action

參數

name type description
account string 賬戶名稱
code string 合約名稱
type string action 名稱
requirement string 權限名稱

示例

以綁定 dmc.token 合約的 transfer action 為例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'linkauth',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
account: account,
code: 'dmc.token',
type: 'transfer',
requirement: 'PERMISSION_NAME',
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});

unlinkauth

刪除權限可執行的 action

參數

name type description
account string 賬戶名稱
code string 合約名稱
type string action 名稱

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'unlinkauth',
authorization: [{
actor: account,
permission: 'owner',
}],
data: {
account: account,
code: 'dmc.token',
type: 'transfer',
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});

buyrambytes

購買存儲資源

參數

name type description
payer string 創建者的賬戶名
receiver string 被創建者的賬戶名
bytes uint32 購買的內存大小(字節)

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'buyrambytes',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
payer: account,
receiver: account,
bytes: bytes,
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

buyram

購買存儲資源,區別是買特定數量的通證還是買特定大小的內容

參數

name type description
payer string 購買存儲資源的賬號
receiver string 接受存儲資源的賬號
quant string 購買存儲資源所用的通證數量

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'buyram',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
payer: account,
receiver: account,
quant: '0.1273 DMC',
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});

sellram

出售不需要的存儲資源

參數

name type description
account string 出售資源通證的接受賬號, 和出售者是同一個賬號
bytes uint64 出售多少空間的存儲資源

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'sellram',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
account: account,
bytes: bytes,
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

delegatebw

抵押通證獲取 cpu 和帶寬資源

參數

name type description
from string 資源抵押者的賬戶名
receiver string 資源接受者的賬戶名
stake_net_quantity string 抵押者為接受者抵押 DMC 獲取 NET
stake_cpu_quantity string 抵押者為接受者抵押 DMC 獲取 CPU
transfer bool 代表抵押資源同時是否將對應通證轉賬給接受者, 1 為轉賬,0 為不轉賬

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'delegatebw',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
from: account,
receiver: account,
stake_net_quantity: '1.0000 DMC',
stake_cpu_quantity: '1.0000 DMC',
transfer: 0
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

undelegatebw

用來解除抵押,釋放資源,收回通證

參數

name type description
from string 解除用哪個賬號所抵押的通證
receiver string 解除作用在哪個賬號上的抵押通證
unstake_net_quantity string 解除用來獲取帶寬資源的通證數量
unstake_cpu_quantity string 解除用來獲取計算資源的通證數量

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'undelegatebw',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
from: account,
receiver: account,
unstake_net_quantity: '1.0000 DMC',
unstake_cpu_quantity: '1.0000 DMC',
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

refund

在 undelegatebw 函數解除抵押後調用,作用是把抵押的通證退回賬戶

參數

name type description
owner string 退回賬戶名稱

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'refund',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
owner: account,
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

regproducer

註冊成為區塊生產者

參數

name type description
producer string 區塊生產者賬戶名
producer_key string 區塊生產者公鑰
url string 區塊生產者宣傳網站
location uint16 區塊生產者服務器位置地區代碼,遵循 ISO 3166-1 標準

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'regproducer',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
producer: account,
producer_key: publicKey,
url: url,
location: location,
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

unregprod

取消註冊成為區塊生產者

參數

name type description
producer string 賬戶名稱

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'unregprod',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
producer: account,
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);

claimrewards

區塊生產者領取工資

參數

name type description
owner string 區塊生產者名稱

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
let r = dmc_client.transact_sync({
actions: [
{
account: 'dmc',
name: 'claimrewards',
authorization: [{
actor: account,
permission: 'active',
}],
data: {
owner: account,
}
}]
}, {
blocksBehind: 3,
expireSeconds: 30,
});
console.log(r);