Class: SafeEthRuby::Protocol

Inherits:
Object
  • Object
show all
Defined in:
lib/safe_eth_ruby/protocol.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signer:, chain_id:, safe_address:, rpc:) ⇒ Protocol

Returns a new instance of Protocol.



9
10
11
12
13
14
15
16
# File 'lib/safe_eth_ruby/protocol.rb', line 9

def initialize(signer:, chain_id:, safe_address:, rpc:)
  puts SafeEthRuby.inspect
  @signer = signer
  @chain_id = chain_id
  @safe_address = safe_address
  @safe_api = TransactionServiceApi.new(chain_id:, safe_address:)
  @contract = Contract.new(safe_address:, rpc:)
end

Instance Attribute Details

#chain_idObject (readonly)

Returns the value of attribute chain_id.



7
8
9
# File 'lib/safe_eth_ruby/protocol.rb', line 7

def chain_id
  @chain_id
end

#safe_addressObject (readonly)

Returns the value of attribute safe_address.



7
8
9
# File 'lib/safe_eth_ruby/protocol.rb', line 7

def safe_address
  @safe_address
end

#safe_apiObject (readonly)

Returns the value of attribute safe_api.



7
8
9
# File 'lib/safe_eth_ruby/protocol.rb', line 7

def safe_api
  @safe_api
end

#signerObject (readonly)

Returns the value of attribute signer.



7
8
9
# File 'lib/safe_eth_ruby/protocol.rb', line 7

def signer
  @signer
end

Instance Method Details

#build_transaction(encoded_data) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/safe_eth_ruby/protocol.rb', line 46

def build_transaction(encoded_data)
  {
    to: "0x998739BFdAAdde7C933B942a68053933098f9EDa",
    value: 0,
    data: encoded_data,
    operation: 1,
    baseGas: 0,
    gasPrice: 0,
    gasToken: "0x0000000000000000000000000000000000000000",
    refundReceiver: "0x0000000000000000000000000000000000000000",
    nonce: @contract.nonce,
    safeTxGas: 0,
  }
end

#create_transaction(txs) ⇒ Object

Creates a consolidated transaction for all given individual transactions



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/safe_eth_ruby/protocol.rb', line 19

def create_transaction(txs)
  data = Util.encode_multi_send_data(txs)
  encoded_data = Util.encode_function_data(function_name: "multiSend", abi: "bytes", data: data)
  transaction = build_transaction(encoded_data)

  tx_hash = transaction_hash(transaction)
  signature = sign_hash(tx_hash)

  result = @safe_api.multisig_transaction(
    to: transaction[:to],
    value: transaction[:value],
    data: transaction[:data],
    operation: transaction[:operation],
    nonce: transaction[:nonce],
    gasToken: transaction[:gasToken],
    baseGas: transaction[:baseGas],
    gasPrice: transaction[:gasPrice],
    safeTxGas: transaction[:safeTxGas],
    refundReceiver: transaction[:refundReceiver],
    contractTransactionHash: "0x#{Eth::Util.bin_to_hex(tx_hash)}",
    sender: @signer.address.to_s,
    signature: "0x#{signature}",
  )

  result.merge({ tx_hash: "0x#{Eth::Util.bin_to_hex(tx_hash)}" })
end

#sign_hash(tx_hash) ⇒ Object



65
66
67
# File 'lib/safe_eth_ruby/protocol.rb', line 65

def sign_hash(tx_hash)
  Util.adjust_v_in_signature(@signer.personal_sign(tx_hash))
end

#transaction_hash(transaction) ⇒ Object



61
62
63
# File 'lib/safe_eth_ruby/protocol.rb', line 61

def transaction_hash(transaction)
  Eip712.build(transaction, @chain_id, @safe_address)
end