Skip to content

Commit

Permalink
fix: add fee to dao
Browse files Browse the repository at this point in the history
  • Loading branch information
classicalliu committed Oct 21, 2019
1 parent 22037c8 commit c5a8ee4
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
15 changes: 15 additions & 0 deletions lib/ckb/types/transaction.rb
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,21 @@ def serialized_size_in_block
Utils.hex_to_bin(transaction_serializer.serialize).bytesize + serialized_tx_offset_bytesize
end

# @param tx_size [Integer] in Bytes
# @param fee_rate [Integer] shannons/KB
def self.fee(tx_serialized_size_in_block, fee_rate)
base = tx_serialized_size_in_block * fee_rate
result = base / 1000
return result + 1 if base % 1000 > 0

result
end

# @param fee_rate [Integer]
def fee(fee_rate)
self.class.fee(self.serialized_size_in_block, fee_rate)
end

def self.from_h(hash)
return if hash.nil?

Expand Down
14 changes: 8 additions & 6 deletions lib/ckb/wallet.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,10 @@ def send_capacity(target_address, capacity, data = "0x", key: nil, fee: 0)

# @param capacity [Integer]
# @param key [CKB::Key | String] Key or private key hex string
# @param fee [Integer]
#
# @return [CKB::Type::OutPoint]
def deposit_to_dao(capacity, key: nil)
def deposit_to_dao(capacity, key: nil, fee: 0)
key = get_key(key)

output = Types::Output.new(
Expand All @@ -162,13 +163,13 @@ def deposit_to_dao(capacity, key: nil)
capacity,
output.calculate_min_capacity(output_data),
change_output.calculate_min_capacity(change_output_data),
0
fee
)
input_capacities = i.capacities

outputs = [output]
outputs_data = [output_data]
change_output.capacity = input_capacities - capacity
change_output.capacity = input_capacities - (capacity + fee)
if change_output.capacity.to_i > 0
outputs << change_output
outputs_data << change_output_data
Expand All @@ -194,9 +195,10 @@ def deposit_to_dao(capacity, key: nil)

# @param out_point [CKB::Type::OutPoint]
# @param key [CKB::Key | String] Key or private key hex string
# @param fee [Integer]
#
# @return [CKB::Type::Transaction]
def generate_withdraw_from_dao_transaction(out_point, key: nil)
def generate_withdraw_from_dao_transaction(out_point, key: nil, fee: 0)
key = get_key(key)

cell_status = api.get_live_cell(out_point)
Expand Down Expand Up @@ -230,7 +232,7 @@ def generate_withdraw_from_dao_transaction(out_point, key: nil)
minimal_since = self.class.epoch_since(minimal_since_epoch_length, minimal_since_epoch_index, minimal_since_epoch_number)

# a hex string
output_capacity = api.calculate_dao_maximum_withdraw(out_point, current_block.hash)
output_capacity = api.calculate_dao_maximum_withdraw(out_point, current_block.hash).hex

dup_out_point = out_point.dup
new_out_point = Types::OutPoint.new(
Expand All @@ -239,7 +241,7 @@ def generate_withdraw_from_dao_transaction(out_point, key: nil)
)

outputs = [
Types::Output.new(capacity: output_capacity, lock: lock)
Types::Output.new(capacity: output_capacity - fee, lock: lock)
]
outputs_data = ["0x"]
tx = Types::Transaction.new(
Expand Down
17 changes: 17 additions & 0 deletions spec/ckb/types/transaction_size_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,21 @@
it "compute size" do
expect(transaction.serialized_size_in_block).to eq 536
end

context "self.fee" do
it "carry" do
fee = CKB::Types::Transaction.fee(1035, 900)
expect(fee).to eq 932
end

it "no carry" do
fee = CKB::Types::Transaction.fee(1035, 1000)
expect(fee).to eq 1035
end
end

it "fee" do
fee = transaction.fee(1000)
expect(fee).to eq 536
end
end

0 comments on commit c5a8ee4

Please sign in to comment.