4. 智能合约
4.1 使用方法
使用步骤:
- 编写 lua 脚本定义智能合约功能。
- 使用在线工具将lua脚本转为16进制。
- 采用交易的方式将智能合约存储到某一账号上。
- 执行智能合约,传入智能合约的参数。
1. 智能合约脚本模版
local cjson = require "cjson";
str = init(); //初始化合约执行环境,并可返回输入参数
local json = cjson.decode(str); //输入参数格式为json,可使用cjson模块解码成json对象
s = json.Secret; //获取输入参数中Secret值
list = { TransactionType = cjson.TransactionType, Account = json.Account, Amount = json.Amount, Destination = json.Destination };
tx = json.encode(list); //将list编码成json对象
result = scTransfer(s,tx); //调用智能合约接口函数
finish(result); //释放相关资源,返回合约结果
2. json转十六进制
使用在线转换工具:http://www.bejson.com/convert/ox2str/
上述脚本转换后为:
6C6F63616C20636A736F6E203D20726571756972652022636A736F6E223B737472203D20696E697428293B7072696E7428227374723A222C737472293B6C6F63616C206A736F6E203D20636A736F6E2E6465636F646528737472293B73203D206A736F6E2E5365637265743B6C697374203D207B205472616E73616374696F6E54797065203D206A736F6E2E5472616E73616374696F6E547970652C204163636F756E74203D206A736F6E2E4163636F756E742C20416D6F756E74203D206A736F6E2E416D6F756E742C2044657374696E6174696F6E203D206A736F6E2E44657374696E6174696F6E207D3B7478203D206A736F6E2E656E636F6465286C697374293B726573756C74203D2073635472616E7366657228732C7478293B66696E69736828726573756C74293B
3. 存储智能合约脚本
利用转账操作,存储智能合约脚本到账号6nUy2SHT6B9DubsPmkJZUXTf5FcNDG6YEA。
sdchaind -q submit snoPB********************SUTb '{"TransactionType":"Payment","Account":"6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh","Amount":"200000000","Destination":"6nUy2SHT6B9DubsPmkJZUXTf5FcNDG6YEA","Memos":[{"Memo":{"MemoType":"536D617274436F6E7472616374","MemoData":"6C6F63616C20636A736F6E203D20726571756972652022636A736F6E223B737472203D20696E697428293B7072696E7428227374723A222C737472293B6C6F63616C206A736F6E203D20636A736F6E2E6465636F646528737472293B73203D206A736F6E2E5365637265743B6C697374203D207B205472616E73616374696F6E54797065203D206A736F6E2E5472616E73616374696F6E547970652C204163636F756E74203D206A736F6E2E4163636F756E742C20416D6F756E74203D206A736F6E2E416D6F756E742C2044657374696E6174696F6E203D206A736F6E2E44657374696E6174696F6E207D3B7478203D206A736F6E2E656E636F6465286C697374293B726573756C74203D2073635472616E7366657228732C7478293B66696E69736828726573756C74293B"}}]}'
执行完成,生成智能合约哈希:
73D6E5E6A93404FC488BE57113DF8ADB23EB8F4477F71BBA073B80DE785E691F
4. 执行智能合约
使用智能合约哈希及对应接口参数,执行智能合约功能。
./sdchaind sc 73D6E5E6A93404FC488BE57113DF8ADB23EB8F4477F71BBA073B80DE785E691F snoPB********************SUTb '{"Secret":"snoPB********************SUTb","TransactionType":"Payment","Account":"6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh","Amount":"100000000","Destination":"6nUy2SHT6B9DubsPmkJZUXTf5FcNDG6YEA"}'
4.2 接口函数
1. scWalletPropose
函数功能:创建 SDChain 账号和私钥。
使用示例:
local cjson = require "cjson";
result = scWalletPropose();
finish(result);
Response:
{
"result": {
"account_id": "6f5y7WDNEUXUoWk6YpnKWaJP87jr16PUJM",
"key_type": "secp256k1",
"master_key": "ONE FORT HEED COON HOFF LINE SOFT AMOS BATH BAT KONG NAVE",
"master_seed": "shYP9********************1Beh",
"master_seed_hex": "84851A834F91A9E15F95693558AA902F",
"public_key": "aBPujdTMcFqRvXpN1ciwyDfnYZWtPMgjesDiiannNLt34T88FLu4",
"public_key_hex": "03011D22B97A61BD5562B7F949AB2B3EAFAA1BE0860D491AE7BF4DCA7CDCD8CDA7",
"status": "success"
}
}
字段名称 | 类型 | 描述 |
---|---|---|
account_id | string | 账号地址 |
key_type | string | 生成账号的加密算法,有secp256k1和edd25519两种算法可选。默认secp256k1。 |
master_key | string | 助记词 |
master_seed | string | 私钥 |
master_seed_hex | string | Hex编码私钥 |
publick_key | string | 公钥 |
publick_key_hex | string | Hex编码公钥 |
status | string | 响应状态 |
2. scTransfer
函数功能:支付。
参数名 | 含义 | 示例 |
---|---|---|
Secret | 账号密钥 | snoPB********************SUTb |
TransactionType | 交易类型 | Payment |
Account | 源账号地址 | 6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh |
Amount | 交易量 | 100000000 |
Destination | 目标账号地址 | 6nUy2SHT6B9DubsPmkJZUXTf5FcNDG6YEA |
使用示例:
local cjson = require "cjson";
str = init();
local json = cjson.decode(str);
s = json.Secret;
list = { TransactionType = json.TransactionType, Account = json.Account, Amount = json.Amount, Destination = json.Destination };
tx = cjson.encode(list);
result = scTransfer(s,tx);
finish(result);
Response:
{
"result": {
"engine_result": "tesSUCCESS",
"engine_result_code": 0,
"engine_result_message": "The transaction was applied. Only final in a validated ledger.",
"status": "success",
"tx_blob": "12000022800000002400000001614000000000000064684000000000000064732103011D22B97A61BD5562B7F949AB2B3EAFAA1BE0860D491AE7BF4DCA7CDCD8CDA77446304402206D68065BFDBAE8FFC01B330E2A4D544DAE99340B00ED7869748E746661DCE865022017FDEAB137354143C8C715F201A53E92D9F3AFF3FD4F552B76BF9BD990A4A391811449C0F3909061BCEC7363A2CF6CAC4484821F5F6B8314B5F762798A53D543A014CAF8B297CFF8F2F937E8",
"tx_json": {
"Account": "6f5y7WDNEUXUoWk6YpnKWaJP87jr16PUJM",
"Amount": "100",
"Destination": "6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh",
"Fee": "100",
"Flags": 2147483648,
"Sequence": 1,
"SigningPubKey": "03011D22B97A61BD5562B7F949AB2B3EAFAA1BE0860D491AE7BF4DCA7CDCD8CDA7",
"TransactionType": "Payment",
"TxnSignature": "304402206D68065BFDBAE8FFC01B330E2A4D544DAE99340B00ED7869748E746661DCE865022017FDEAB137354143C8C715F201A53E92D9F3AFF3FD4F552B76BF9BD990A4A391",
"hash": "F4E38224481B65A523934EB77ED0E33D3CED7BCC611F6851AF177B7F05886521"
}
}
}
字段名称 | 类型 | 描述 |
---|---|---|
engine_result | string | 支付执行结果 |
engine_result_code | int | 支付结果代码 |
engine_result_message | string | 支付结果信息 |
tx_blob | string | 签名结果,二进制数据格式 |
tx_json | object | 交易要素,json格式 |
Fee | string | 交易燃料费,单位为滴 |
status | string | 响应状态 |
3. scAccountInfo
函数功能:查询账号信息,获取账号的余额。
参数名 | 含义 | 示例 |
---|---|---|
Account | 源账号地址 | 6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh |
使用示例:
local cjson = require "cjson";
str = init();
local json = cjson.decode(str);
account = json.Account;
result = scAccountInfo(account);
finish(result);
Response:
{
"result": {
"account_data": {
"Account": "6f5y7WDNEUXUoWk6YpnKWaJP87jr16PUJM",
"Balance": "200000000",
"Flags": 0,
"LedgerEntryType": "AccountRoot",
"OwnerCount": 0,
"PreviousTxnID": "E9A0A2992FB3CC8703DCCBDBAA0B3C543C9AE1E8B683D08CBEA3B5BE44CA608F",
"PreviousTxnLgrSeq": 79342,
"Sequence": 1,
"index": "CB748D32BFAEF53EAAD5644940C25F75BAF91467E77C105E2E30F25D26AADC4D"
},
"ledger_current_index": 79356,
"queue_data": {
"txn_count": 0
},
"status": "success",
"validated": false
}
}
字段名称 | 类型 | 描述 |
---|---|---|
Account | string | 账号地址 |
Balance | string | 账号SDA余额,单位为滴。1 SDA=1000000 Drops,即一百万滴。 |
Flags | unsigned int | 标志 |
PreviousTxnID | string | 上一次交易的HashID |
PreviousTxnLgrSeq | unsigned int | 上一次交易的账本高度 |
Sequence | unsigned int | 交易流水号 |
index | string | 账号对象的HashID |
status | string | 响应状态 |
4. scAccountTx
函数功能:查询账号交易信息。
参数名 | 含义 | 示例 |
---|---|---|
Account | 源账号地址 | 6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh |
ledger_index_min | 最小账本高度 | 100 |
ledger_index_max | 最高账本高度 | 900 |
使用示例:
local cjson = require "cjson";
str = init();
local json = cjson.decode(str);
account = json.Account;
ledger_index_min = json.ledger_index_min;
ledger_index_max = json.ledger_index_max;
result = scAccountTx(account,ledger_index_min,ledger_index_max);
finish(result);
Response:
{
"id" : 1,
"result" : {
"account" : "6PrXdWVoXzmGCmi56wTFTC3e6xezLWh2dc",
"ledger_index_max" : 116672,
"ledger_index_min" : 116000,
"status" : "success",
"transactions" : [
{
"meta" : {
"AffectedNodes" : [
{
"ModifiedNode" : {
"FinalFields" : {
"Account" : "6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh",
"Balance" : "20997720",
"Flags" : 0,
"OwnerCount" : 0,
"Sequence" : 28
},
"LedgerEntryType" : "AccountRoot",
"LedgerIndex" : "2B6AC232AA4C4BE41BF49D2459FA4A0347E1B543A4C92FCEE0821C0201E2E9A8",
"PreviousFields" : {
"Balance" : "20998730",
"Sequence" : 27
},
"PreviousTxnID" : "F8B2D9DBFD8CB1A3DEB864822305DE574EDAB60C355DD108B663C63F7535E476",
"PreviousTxnLgrSeq" : 116592
}
},
{
"ModifiedNode" : {
"FinalFields" : {
"Account" : "6PrXdWVoXzmGCmi56wTFTC3e6xezLWh2dc",
"Balance" : "82001000",
"Flags" : 0,
"OwnerCount" : 0,
"Sequence" : 1
},
"LedgerEntryType" : "AccountRoot",
"LedgerIndex" : "35C97BB2890349B177A9DAF1DA5B5631C0F07B6F25059BB0E5136C38606A6395",
"PreviousFields" : {
"Balance" : "82000000"
},
"PreviousTxnID" : "B973619E24F6B016AC5259E6ED0C43D352F3F6BC2471FBEF722B4998194B1527",
"PreviousTxnLgrSeq" : 92532
}
}
],
"TransactionIndex" : 0,
"TransactionResult" : "tesSUCCESS",
"delivered_amount" : "1000"
},
"tx" : {
"Account" : "6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh",
"Amount" : "1000",
"Destination" : "6PrXdWVoXzmGCmi56wTFTC3e6xezLWh2dc",
"Fee" : "10",
"Flags" : 2147483648,
"Sequence" : 27,
"SigningPubKey" : "0330E7FC9D56BB25D6893BA3F317AE5BCF33B3291BD63DB32654A313222F7FD020",
"TransactionType" : "Payment",
"TxnSignature" : "30440220399965A5C60B9E38D7CB235CAA729A67BAD0C853FFCE8E2E6C82E76F2082844402203D7ECC9ECFDA3A45DFD9109C97BCBD8F67B73990EEC5C3C73D0259455D1E72CF",
"date" : 573729581,
"hash" : "E5684BFCEDE95B0539839E1B757401D5E27478E2567502C86C0972567A67933F",
"inLedger" : 116596,
"ledger_index" : 116596
},
"validated" : true
}
]
}
}
字段名称 | 类型 | 描述 |
---|---|---|
marker | object | 分页标志 |
meta object | 交易记录原始信息 | |
AffectedNodes | object | 影响的单元 |
ModifiedNode | object | 修改的单元 |
FinalFields | object | 最终字段 |
TransactionIndex | unsigned int | 交易序号 |
TransactionResult | string | 交易结果 |
delivered_amount | string | 交易金额 |
validated | bool | 验证状态 |
5. scLedgerClosed
函数功能:查询最新关闭的区块信息。
使用示例:
local cjson = require "cjson";
str = init();
result = scLedgerClosed();
finish(result);
Response:
{
"id" : 1,
"result" : {
"ledger_hash" : "B43F7B4F16F686CEE9A93152B43A8B8CFE78466DFB2A42A592FFD3A84FDFD915",
"ledger_index" : 1241981,
"status" : "success"
}
}
字段名称 | 类型 | 描述 |
---|---|---|
ledger_hash | string | 账本哈希 |
ledger_index | Int | 账本高度 |
status | string | 状态 |
6. scAccountCurrencies
函数功能:查询账号二级资产类型信息。
参数名 | 含义 | 示例 |
---|---|---|
Account | 源账号地址 | 6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh |
使用示例:
local cjson = require "cjson";
str = init();
local json = cjson.decode(str);
account = json.Account;
result = scAccountCurrencies(account);
finish(result);
Response:
{
"id" : 1,
"result" : {
"ledger_current_index" : 1242529,
"receive_currencies" : [ "AAA", "BBB", "DSA", "DUO", "OII", "DUDO", "BBCCB" ],
"send_currencies" : [ "DSA", "DUO", "OII", "DUDO", "BBBBB" ],
"status" : "success",
"validated" : false
}
}
字段名称 | 类型 | 描述 |
---|---|---|
ledger_current_index | Int | 当前账本高度 |
receive_currencies | Object | 接收的币种 |
send_currencies | Object | 发送的币种 |
status | String | 状态 |
validated | Bool | 验证状态 |
7. scAccountObjects
函数功能:查询账号交易、授信等信息。
参数名 | 含义 | 示例 |
---|---|---|
Account | 源账号地址 | 6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh |
使用示例:
local cjson = require "cjson";
str = init();
local json = cjson.decode(str);
account = json.Account;
result = scAccountObjects(account);
finish(result);
Response:
{
"id" : 1,
"result" : {
"account" : "6Hb9CJAWyB46j91VRWn9rDkukG4bwdtyTh",
"account_objects" : [{
"Balance" : {
"currency" : "CNY",
"issuer" : "66666666666666666666BZbvji",
"value" : "-981.1730416207281"
},
"Flags" : 131072,
"HighLimit" : {
"currency" : "CNY",
"issuer" : "6GiohV7XVhabbtV1DfKjWEyikeyimiDSNx",
"value" : "10000"
},
"HighNode" : "0000000000000000",
"LedgerEntryType" : "SDChainState",
"LowLimit" : {
"currency" : "CNY",
"issuer" : "6UPd52jHtu1d88nc3S3WeroACFQpKfybhU",
"value" : "0"
},
"LowNode" : "0000000000000000",
"PreviousTxnID" : "85A043F0B2BA02DC90906E75114936F3BB11EC25590D2FD10C0366103D989F60",
"PreviousTxnLgrSeq" : 1096782,
"index" : "0F17770ED3ED7C394010A2E6E7D5960C5541E4454B9BA7096B93127DEC912921"
},
{
"Balance" : {
"currency" : "DUO",
"issuer" : "66666666666666666666BZbvji",
"value" : "995.292066872863"
},
"Flags" : 65536,
"HighLimit" : {
"currency" : "DUO",
"issuer" : "6J6MmEM2zV1DqVnvB55an2eYNtEzVSKZu7",
"value" : "0"
},
"HighNode" : "0000000000000002",
"LedgerEntryType" : "SDChainState",
"LowLimit" : {
"currency" : "DUO",
"issuer" : "6GiohV7XVhabbtV1DfKjWEyikeyimiDSNx",
"value" : "1100000"
},
"LowNode" : "0000000000000000",
"PreviousTxnID" : "74539E63D4AD4A18D1C9C0D7BE8082C12511A5FCF1EA0EE4A6C7E5A3866CACAC",
"PreviousTxnLgrSeq" : 702127,
"index" : "01E0EAEE99B884388EE6116ABBDDB2DB368172EBC6F4791FC21575FDD8CE7480"
}
],
"ledger_current_index" : 1242016,
"status" : "success",
"validated" : false
}
}
字段名称 | 类型 | 描述 |
---|---|---|
account | String | 账号地址 |
account_objects | Object | 账号的交易对象类,包括授信、转账等。 |
ledger_current_index | Int | 当前账本高度 |
status | String | 状态 |
validated | Bool | 验证状态 |
8. scTx
函数功能:指定交易哈希值,获取对应哈希详情。
参数名 | 含义 | 示例 |
---|---|---|
hash | 交易记录号 | 71752B0AEB897476A24483C66AD47C9F439EA4943DD88A0C6D12D827BD4451C8 |
使用示例:
local cjson = require "cjson";
str = init();
local json = cjson.decode(str);
hash= json.hash;
result = scTx(hash);
finish(result);
Response:
{
"id" : 1,
"result" : {
"Account" : "6nUy2SHT6B9DubsPmkJZUXTf5FcNDG6YEA",
"Amount" : "1000000",
"Destination" : "6pzEQxifBLM6N5hi4cnmXL4QpKBc3ZUQKE",
"Fee" : "100",
"Flags" : 2147483648,
"Sequence" : 36,
"SigningPubKey" : "03EAC169AF572EB52038D1C51F7E5B9C94CE34A7FBB714860BE001BDE25A4B03C8",
"TransactionType" : "Payment",
"TxnSignature" : "3045022100CF41EA1C5E49470583EC607F4F84B644015AB3B427BA464480F85FBA253F85A30220393B24BF34284663A35A213A273133D604D6F8EB3285D6174D3666A759047A6D",
"date" : 595406530,
"hash" : "71752B0AEB897476A24483C66AD47C9F439EA4943DD88A0C6D12D827BD4451C8",
"inLedger" : 1466161,
"ledger_index" : 1466161,
"meta" : {
"AffectedNodes" : [
{
"ModifiedNode" : {
"FinalFields" : {
"Account" : "6pzEQxifBLM6N5hi4cnmXL4QpKBc3ZUQKE",
"Balance" : "43000000",
"Flags" : 0,
"OwnerCount" : 0,
"Sequence" : 1
},
"LedgerEntryType" : "AccountRoot",
"LedgerIndex" : "511883401303E1BEF48D9FBA884F255FDC7D6E537969F5C4DE25696620617654",
"PreviousFields" : {
"Balance" : "42000000"
},
"PreviousTxnID" : "13E0661A0944922D56E835E38726FCFE322BD9028EB5B7EB22A4777E6A3895C3",
"PreviousTxnLgrSeq" : 1466119
}
},
{
"ModifiedNode" : {
"FinalFields" : {
"Account" : "6nUy2SHT6B9DubsPmkJZUXTf5FcNDG6YEA",
"Balance" : "2005205996422",
"Flags" : 0,
"OwnerCount" : 3,
"Sequence" : 37
},
"LedgerEntryType" : "AccountRoot",
"LedgerIndex" : "AA22EEB13821659B8316218C1D78606FD9C57C3192C2104EB7B0920AA30FF6C6",
"PreviousFields" : {
"Balance" : "2005206996522",
"Sequence" : 36
},
"PreviousTxnID" : "AD80F7EEEF06F84C4BA8B01F333B5318C7C610D02CBBBE5CE152812C619C769A",
"PreviousTxnLgrSeq" : 1466144
}
}
],
"TransactionIndex" : 0,
"TransactionResult" : "tesSUCCESS",
"delivered_amount" : "1000000"
},
"status" : "success",
"validated" : true
}
}
字段名称 | 类型 | 描述 |
---|---|---|
meta | object | 交易记录原始信息 |
AffectedNodes | object | 影响的单元 |
ModifiedNode | object | 修改的单元 |
FinalFields | object | 最终字段 |
TransactionIndex | unsigned int | 交易序号 |
TransactionResult | string | 交易结果 |
delivered_amount | string | 交易金额 |
validated | bool | 验证状态 |
9. os.sleep
函数功能:指定睡眠等待时间。
参数名 | 含义 | 示例 |
---|---|---|
time | 时长,单位毫秒 | 5000 |
使用示例:
print("start sleep",os.time());
local time = 5000;
os.sleep(time);
print("sleep over",os.time());
10. str2hex
函数功能:字符串转十六进制。
参数名 | 含义 | 示例 |
---|---|---|
str | 字符串 | "hello sdchain" |
local str = "hello sdchain"
print("str :",str);
print("str to hex:",str2hex(str));
11. hex2str
函数功能:十六进制转字符串。
参数名 | 含义 | 示例 |
---|---|---|
str | 字符串 | "68656C6C6F207364636861696E" |
local hex = "68656C6C6F207364636861696E";
print("hex is ",hex);
print("hex to str ",hex2str(hex));
12. getledgerseq
函数功能:指定起始时间,获取对应时间的账本高度。
参数名 | 含义 | 示例 |
---|---|---|
startTime | 起始时间(UTC时间戳,从2000年1月1日0时0分0秒计算。) | 1541001600 |
endTime | 结束时间(UTC时间戳,从2000年1月1日0时0分0秒计算。) | 1541088000 |
local startTime = 1541001600;
local endTime = 1541088000;
local ledgerStart;
local ledgerEnd;
ledgerStart,ledgerEnd = getledgerseq(startTime,endTime);
print(ledgerStart,ledgerEnd);