Skip to content

Commit

Permalink
feat: use dep group
Browse files Browse the repository at this point in the history
  • Loading branch information
shaojunda committed Aug 21, 2019
1 parent 90c46af commit 32429fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
19 changes: 18 additions & 1 deletion lib/ckb/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module CKB
class API
attr_reader :rpc
attr_reader :system_script_out_point
attr_reader :system_script_group_out_point
attr_reader :system_script_code_hash
attr_reader :dao_out_point
attr_reader :dao_code_hash
Expand All @@ -18,10 +19,12 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
@rpc = CKB::RPC.new(host: host)
if mode == MODE::TESTNET
# Testnet system script code_hash
expected_code_hash = "0xa76801d09a0eabbfa545f1577084b6f3bafb0b6250e7f5c89efcfd4e3499fb55"
expected_code_hash = "0x664b03c55410dd7d694a73779fa86edd7f069ee17fd79b0fef4c4fec0377b9a6"
# For testnet chain, we can assume the second cell of the first transaction
# in the genesis block contains default lock script we can use here.
system_cell_transaction = genesis_block.transactions.first

system_scipt_group_transaction = genesis_block.transactions[1]
out_point = Types::OutPoint.new(
tx_hash: system_cell_transaction.hash,
index: "1"
Expand All @@ -32,6 +35,11 @@ def initialize(host: CKB::RPC::DEFAULT_URL, mode: MODE::TESTNET)
raise "System script code_hash error!" unless code_hash == expected_code_hash

set_system_script_cell(out_point, code_hash)
group_out_point = Types::OutPoint.new(
tx_hash: system_scipt_group_transaction.hash,
index: "0"
)
set_system_script_group_cell(group_out_point)

dao_out_point = Types::OutPoint.new(
tx_hash: system_cell_transaction.hash,
Expand All @@ -51,6 +59,11 @@ def set_system_script_cell(out_point, code_hash)
@system_script_code_hash = code_hash
end

# @param out_point [CKB::Types::OutPoint]
def set_system_script_group_cell(out_point)
@system_script_group_out_point = out_point
end

def set_dao_cell(out_point, code_hash)
@dao_out_point = out_point
@dao_code_hash = code_hash
Expand Down Expand Up @@ -140,6 +153,10 @@ def compute_transaction_hash(transaction)
rpc.compute_transaction_hash(transaction.to_h)
end

def compute_script_hash(script_h)
rpc.compute_script_hash(script_h)
end

# @return [CKB::Type::Peer]
def local_node_info
Types::Peer.from_h(
Expand Down
9 changes: 5 additions & 4 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def generate_tx(target_address, capacity, data = "0x", key: nil, fee: 0)
tx = Types::Transaction.new(
version: 0,
cell_deps: [
Types::CellDep.new(out_point: api.system_script_out_point)
Types::CellDep.new(out_point: api.system_script_group_out_point, is_dep_group: true)
],
inputs: i.inputs,
outputs: outputs,
Expand Down Expand Up @@ -154,14 +154,15 @@ def deposit_to_dao(capacity, key: nil)
tx = Types::Transaction.new(
version: 0,
cell_deps: [
Types::CellDep.new(out_point: api.system_script_out_point),
Types::CellDep.new(out_point: api.system_script_group_out_point, is_dep_group: true),
Types::CellDep.new(out_point: api.dao_out_point)
],
inputs: i.inputs,
outputs: outputs,
outputs_data: outputs.map(&:data),
witnesses: i.witnesses
)

tx_hash = api.compute_transaction_hash(tx)
send_transaction(tx.sign(key, tx_hash))

Expand Down Expand Up @@ -211,7 +212,7 @@ def generate_withdraw_from_dao_transaction(out_point, key: nil)
version: 0,
cell_deps: [
Types::CellDep.new(out_point: api.dao_out_point),
Types::CellDep.new(out_point: api.system_script_out_point)
Types::CellDep.new(out_point: api.system_script_group_out_point, is_dep_group: true)
],
header_deps: [
current_block.hash,
Expand Down Expand Up @@ -246,7 +247,7 @@ def block_assembler_config
end

def lock_hash
@lock_hash ||= lock.to_hash
@lock_hash ||= lock.to_hash(api)
end

private
Expand Down

0 comments on commit 32429fa

Please sign in to comment.