-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add simple UDT example * Add script to introduction
- Loading branch information
1 parent
d3d06ca
commit 7ad933b
Showing
10 changed files
with
250 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use ckb_js_tests::read_tx_template; | ||
|
||
pub fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
let tx = read_tx_template("templates/simple_udt.json")?; | ||
|
||
let json = serde_json::to_string_pretty(&tx).unwrap(); | ||
println!("{}", json); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
{ | ||
"mock_info": { | ||
"inputs": [ | ||
{ | ||
"output": { | ||
"capacity": "0x10000000", | ||
"lock": { | ||
"args": "0x", | ||
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"hash_type": "data1" | ||
}, | ||
"type": { | ||
"args": "0x0000{{ ref_type js-code-file }}01", | ||
"code_hash": "0x{{ ref_type ckb-js-vm }}", | ||
"hash_type": "type" | ||
} | ||
}, | ||
"data": "0x11000000000000000000000000000000" | ||
} | ||
], | ||
"cell_deps": [ | ||
{ | ||
"output": { | ||
"capacity": "0x10000000", | ||
"lock": { | ||
"args": "0x00AE9DF3447C404A645BC48BEA4B7643B95AC5C3AE", | ||
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"hash_type": "data1" | ||
}, | ||
"type": "{{ def_type ckb-js-vm }}" | ||
}, | ||
"data": "0x{{ data ../../../build/ckb-js-vm }}" | ||
}, | ||
{ | ||
"output": { | ||
"capacity": "0x10000000", | ||
"lock": { | ||
"args": "0x00AE9DF3447C404A645BC48BEA4B7643B95AC5C3AE", | ||
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"hash_type": "data1" | ||
}, | ||
"type": "{{ def_type js-code-file }}" | ||
}, | ||
"data": "0x{{ data ../test_data/simple_udt.js }}" | ||
} | ||
], | ||
"header_deps": [] | ||
}, | ||
"tx": { | ||
"outputs": [ | ||
{ | ||
"capacity": "0x0", | ||
"lock": { | ||
"args": "0x", | ||
"code_hash": "0x0000000000000000000000000000000000000000000000000000000000000000", | ||
"hash_type": "type" | ||
}, | ||
"type": { | ||
"args": "0x0000{{ ref_type js-code-file }}01", | ||
"code_hash": "0x{{ ref_type ckb-js-vm }}", | ||
"hash_type": "type" | ||
} | ||
} | ||
], | ||
"witnesses": [ | ||
"0x0001020304050607" | ||
], | ||
"outputs_data": [ | ||
"0x11000000000000000000000000000000" | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
|
||
const CKB_INDEX_OUT_OF_BOUND = 1; | ||
const ERROR_AMOUNT = -52; | ||
|
||
function assert(cond, obj1) { | ||
if (!cond) { | ||
throw Error(obj1); | ||
} | ||
} | ||
|
||
function compare_array(a, b) { | ||
if (a.byteLength != b.byteLength) { | ||
return false; | ||
} | ||
for (let i = 0; i < a.byteLength; i++) { | ||
if (a[i] != b[i]) { | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
function unpack_script(buf) { | ||
let script = new Uint32Array(buf); | ||
let raw_data = new Uint8Array(buf); | ||
|
||
let full_size = script[0]; | ||
assert(full_size == buf.byteLength, 'full_size == buf.byteLength'); | ||
let code_hash_offset = script[1]; | ||
let code_hash = buf.slice(code_hash_offset, code_hash_offset + 32); | ||
let hash_type_offset = script[2]; | ||
let hash_type = raw_data[hash_type_offset]; | ||
let args_offset = script[3]; | ||
let args = buf.slice(args_offset + 4); | ||
return {'code_hash': code_hash, 'hash_type': hash_type, 'args': args}; | ||
} | ||
|
||
function* iterate_field(source, field) { | ||
let index = 0; | ||
while (true) { | ||
try { | ||
let ret = ckb.load_cell_by_field(index, source, field); | ||
yield ret; | ||
index++; | ||
} catch (e) { | ||
if (e.error_code == CKB_INDEX_OUT_OF_BOUND) { | ||
break; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function* iterate_cell_data(source) { | ||
let index = 0; | ||
while (true) { | ||
try { | ||
let ret = ckb.load_cell_data(index, source); | ||
yield ret; | ||
index++; | ||
} catch (e) { | ||
if (e.error_code == CKB_INDEX_OUT_OF_BOUND) { | ||
break; | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
} | ||
|
||
function main() { | ||
console.log('simple UDT ...'); | ||
let buf = ckb.load_script(); | ||
let script = unpack_script(buf); | ||
let owner_mode = false; | ||
// ckb-js-vm has leading 35 bytes args | ||
let real_args = script.args.slice(35); | ||
for (let lock_hash of iterate_field(ckb.SOURCE_INPUT, ckb.CKB_CELL_FIELD_LOCK_HASH)) { | ||
if (compare_array(lock_hash, real_args)) { | ||
owner_mode = true; | ||
} | ||
} | ||
if (owner_mode) { | ||
return 0; | ||
} | ||
let input_amount = 0n; | ||
|
||
for (let data of iterate_cell_data(ckb.SOURCE_GROUP_INPUT)) { | ||
if (data.byteLength != 16) { | ||
throw `Invalid data length: ${data.byteLength}`; | ||
} | ||
let n = new BigUint64Array(data); | ||
let current_amount = n[0] | (n[1] << 64n); | ||
input_amount += current_amount; | ||
} | ||
let output_amount = 0n; | ||
for (let data of iterate_cell_data(ckb.SOURCE_GROUP_OUTPUT)) { | ||
if (data.byteLength != 16) { | ||
throw `Invalid data length: ${data.byteLength}`; | ||
} | ||
let n = new BigUint64Array(data); | ||
let current_amount = n[0] | (n[1] << 64n); | ||
output_amount += current_amount; | ||
} | ||
console.log(`verifying amount: ${input_amount} and ${output_amount}`); | ||
if (input_amount < output_amount) { | ||
return ERROR_AMOUNT; | ||
} | ||
console.log('Simple UDT quit successfully'); | ||
return 0; | ||
} | ||
|
||
ckb.exit(main()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters