Class: Neb::Transaction
- Inherits:
-
Object
- Object
- Neb::Transaction
- Defined in:
- lib/neb/transaction.rb
Constant Summary collapse
- MAX_GAS_PRICE =
1_000_000_000_000
- MAX_GAS =
50_000_000_000
- GAS_PRICE =
1_000_000
- GAS_LIMIT =
20_000
- PAYLOAD_BINARY_TYPE =
"binary".freeze
- PAYLOAD_DEPLOY_TYPE =
"deploy".freeze
- PAYLOAD_CALL_TYPE =
"call".freeze
- CHAIN_ID_LIST =
{ 1 => { name: "Mainnet", url: "https://mainnet.nebulas.io" }, 1001 => { name: "Testnet", url: "https://testnet.nebulas.io" }, 100 => { name: "Local Nodes", url: "http://127.0.0.1:8685" } }.freeze
Instance Attribute Summary collapse
-
#chain_id ⇒ Object
readonly
Returns the value of attribute chain_id.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#from_account ⇒ Object
readonly
Returns the value of attribute from_account.
-
#gas_limit ⇒ Object
readonly
Returns the value of attribute gas_limit.
-
#gas_price ⇒ Object
readonly
Returns the value of attribute gas_price.
-
#hash ⇒ Object
readonly
Returns the value of attribute hash.
-
#nonce ⇒ Object
readonly
Returns the value of attribute nonce.
-
#sign ⇒ Object
readonly
Returns the value of attribute sign.
-
#timestamp ⇒ Object
readonly
Returns the value of attribute timestamp.
-
#to_address ⇒ Object
readonly
Returns the value of attribute to_address.
-
#value ⇒ Object
readonly
Returns the value of attribute value.
Instance Method Summary collapse
- #calculate_hash ⇒ Object
-
#initialize(chain_id:, from_account:, to_address:, value:, nonce:, gas_price: GAS_PRICE, gas_limit: GAS_LIMIT, contract: {}) ⇒ Transaction
constructor
A new instance of Transaction.
- #parse_contract(contract) ⇒ Object
- #sign_hash ⇒ Object
- #to_proto ⇒ Object
- #to_proto_str ⇒ Object
Constructor Details
#initialize(chain_id:, from_account:, to_address:, value:, nonce:, gas_price: GAS_PRICE, gas_limit: GAS_LIMIT, contract: {}) ⇒ Transaction
Returns a new instance of Transaction.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/neb/transaction.rb', line 26 def initialize(chain_id:, from_account:, to_address:, value:, nonce:, gas_price: GAS_PRICE, gas_limit: GAS_LIMIT, contract: {}) @chain_id = chain_id @from_account = from_account @to_address = Address.new(to_address) @value = value @nonce = nonce @gas_price = gas_price @gas_limit = gas_limit @data = parse_contract(contract) @timestamp = Time.now.to_i end |
Instance Attribute Details
#chain_id ⇒ Object (readonly)
Returns the value of attribute chain_id.
22 23 24 |
# File 'lib/neb/transaction.rb', line 22 def chain_id @chain_id end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
22 23 24 |
# File 'lib/neb/transaction.rb', line 22 def data @data end |
#from_account ⇒ Object (readonly)
Returns the value of attribute from_account.
22 23 24 |
# File 'lib/neb/transaction.rb', line 22 def from_account @from_account end |
#gas_limit ⇒ Object (readonly)
Returns the value of attribute gas_limit.
22 23 24 |
# File 'lib/neb/transaction.rb', line 22 def gas_limit @gas_limit end |
#gas_price ⇒ Object (readonly)
Returns the value of attribute gas_price.
22 23 24 |
# File 'lib/neb/transaction.rb', line 22 def gas_price @gas_price end |
#hash ⇒ Object (readonly)
Returns the value of attribute hash.
24 25 26 |
# File 'lib/neb/transaction.rb', line 24 def hash @hash end |
#nonce ⇒ Object (readonly)
Returns the value of attribute nonce.
22 23 24 |
# File 'lib/neb/transaction.rb', line 22 def nonce @nonce end |
#sign ⇒ Object (readonly)
Returns the value of attribute sign.
24 25 26 |
# File 'lib/neb/transaction.rb', line 24 def sign @sign end |
#timestamp ⇒ Object (readonly)
Returns the value of attribute timestamp.
22 23 24 |
# File 'lib/neb/transaction.rb', line 22 def @timestamp end |
#to_address ⇒ Object (readonly)
Returns the value of attribute to_address.
22 23 24 |
# File 'lib/neb/transaction.rb', line 22 def to_address @to_address end |
#value ⇒ Object (readonly)
Returns the value of attribute value.
22 23 24 |
# File 'lib/neb/transaction.rb', line 22 def value @value end |
Instance Method Details
#calculate_hash ⇒ Object
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/neb/transaction.rb', line 98 def calculate_hash buffer = [ from_account.address_obj.encode(:bin_extended), to_address.encode(:bin_extended), Utils.zpad(Utils.int_to_big_endian(value), 16), Utils.zpad(Utils.int_to_big_endian(nonce), 8), Utils.zpad(Utils.int_to_big_endian(), 8), Corepb::Data.new(data).to_proto, Utils.zpad(Utils.int_to_big_endian(chain_id), 4), Utils.zpad(Utils.int_to_big_endian(gas_price), 16), Utils.zpad(Utils.int_to_big_endian(gas_limit), 16) ].join Utils.keccak256(buffer) end |
#parse_contract(contract) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/neb/transaction.rb', line 39 def parse_contract(contract) payload_type, payload = nil, nil contract.deep_symbolize_keys! if contract[:source].present? payload_type = PAYLOAD_DEPLOY_TYPE payload = { source_type: contract[:source_type], source: contract[:source], args: contract[:args] } elsif contract[:function].present? payload_type = PAYLOAD_CALL_TYPE payload = { function: contract[:function], args: contract[:args] } else payload_type = PAYLOAD_BINARY_TYPE if contract.present? payload = { data: BaseConvert.decode(contract[:binary], 256) } end end if payload.present? payload = String.new(JSON.dump(payload.deep_camelize_keys(:upper)).html_safe) { type: payload_type, payload: payload } else { type: payload_type } end end |
#sign_hash ⇒ Object
114 115 116 117 |
# File 'lib/neb/transaction.rb', line 114 def sign_hash @hash = calculate_hash @sign = Secp256k1.sign(@hash, from_account.private_key) end |
#to_proto ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/neb/transaction.rb', line 73 def to_proto raise UnsignError.new("Must sign_hash first") if sign.blank? tx = Corepb::Transaction.new( hash: hash, from: from_account.address_obj.encode(:bin_extended), to: to_address.encode(:bin_extended), value: Utils.zpad(Utils.int_to_big_endian(value), 16), nonce: nonce, timestamp: , data: Corepb::Data.new(data), chain_id: chain_id, gas_price: Utils.zpad(Utils.int_to_big_endian(gas_price), 16), gas_limit: Utils.zpad(Utils.int_to_big_endian(gas_limit), 16), alg: Secp256k1::SECP256K1, sign: sign ) tx.to_proto end |