Class: TonSdkRubySmc::HighloadWalletV2

Inherits:
Object
  • Object
show all
Extended by:
TonSdkRuby, TonSdkRubySmc
Includes:
TonSdkRuby
Defined in:
lib/ton-sdk-ruby-smc/wallets/highload_wallet_v2.rb

Constant Summary collapse

HIGHLOADWALLET_V2_CODE =
"B5EE9C724101090100E5000114FF00F4A413F4BCF2C80B010201200203020148040501EAF28308D71820D31FD33FF823AA1F5320B9F263ED44D0D31FD33FD3FFF404D153608040F40E6FA131F2605173BAF2A207F901541087F910F2A302F404D1F8007F8E16218010F4786FA5209802D307D43001FB009132E201B3E65B8325A1C840348040F4438AE63101C8CB1F13CB3FCBFFF400C9ED54080004D03002012006070017BD9CE76A26869AF98EB85FFC0041BE5F976A268698F98E99FE9FF98FA0268A91040207A0737D098C92DBFC95DD1F140034208040F4966FA56C122094305303B9DE2093333601926C21E2B39F9E545A"

Constants included from TonSdkRubySmc

PWV2_CODE, TOKEN_ATTRIBUTES_SHA256, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from TonSdkRubySmc

require_type

Constructor Details

#initialize(pubkey, workchain = 0, sub_wallet_id = 0) ⇒ HighloadWalletV2

Returns a new instance of HighloadWalletV2.



70
71
72
73
74
75
76
# File 'lib/ton-sdk-ruby-smc/wallets/highload_wallet_v2.rb', line 70

def initialize(pubkey, workchain = 0, sub_wallet_id = 0)
  @pubkey = pubkey
  @sub_wallet_id = sub_wallet_id
  @code = deserialize(hex_to_bytes(HIGHLOADWALLET_V2_CODE)).first
  @init = build_state_init
  @address = Address.new("#{workchain}:#{init.cell.hash}")
end

Instance Attribute Details

#addressObject

Returns the value of attribute address.



68
69
70
# File 'lib/ton-sdk-ruby-smc/wallets/highload_wallet_v2.rb', line 68

def address
  @address
end

#codeObject

Returns the value of attribute code.



68
69
70
# File 'lib/ton-sdk-ruby-smc/wallets/highload_wallet_v2.rb', line 68

def code
  @code
end

#initObject

Returns the value of attribute init.



68
69
70
# File 'lib/ton-sdk-ruby-smc/wallets/highload_wallet_v2.rb', line 68

def init
  @init
end

#pubkeyObject

Returns the value of attribute pubkey.



68
69
70
# File 'lib/ton-sdk-ruby-smc/wallets/highload_wallet_v2.rb', line 68

def pubkey
  @pubkey
end

#sub_wallet_idObject

Returns the value of attribute sub_wallet_id.



68
69
70
# File 'lib/ton-sdk-ruby-smc/wallets/highload_wallet_v2.rb', line 68

def sub_wallet_id
  @sub_wallet_id
end

Class Method Details

.generate_query_id(timeout, random_id = nil) ⇒ Object



78
79
80
81
82
# File 'lib/ton-sdk-ruby-smc/wallets/highload_wallet_v2.rb', line 78

def self.generate_query_id(timeout, random_id = nil)
  now = Time.now.to_i
  random = random_id || rand(0..(2**32 - 1))
  (now + timeout) << 32 | random
end

Instance Method Details

#build_transfer(transfers, private_key, is_init = false, timeout = 60, query_id = nil) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/ton-sdk-ruby-smc/wallets/highload_wallet_v2.rb', line 84

def build_transfer(transfers, private_key, is_init = false, timeout = 60, query_id = nil)
  raise "Transfers must be an [WalletV4Transfer]" unless transfers.size > 0 && transfers.first.is_a?(HighloadWalletV2Transfer)
  raise "HighloadWallet v2 can handle only 254 transfers at once" unless (transfers.size <= 254 && transfers.size > 0)

  query_id ||= HighloadWalletV2.generate_query_id(timeout)
  body = Builder.new
                .store_uint(sub_wallet_id, 32)
                .store_uint(query_id, 64)

  dict = HashmapE.new(16, {
    serializers: {
      key: ->(number) {
        Builder.new.store_int(number, 16).bits
      },
      value: ->(transfer) {
        internal_message = Message.new(
          MessageOptions.new(
            info: CommonMsgInfo.new(
              IntMsgInfo.new(
                dest: transfer.destination,
                bounce: transfer.bounce,
                value: transfer.value,
                )
            ),
            body: transfer.body,
            init: transfer.init,
            )
        )

        Builder.new
                   .store_uint(transfer.mode, 8)
                   .store_ref(internal_message.cell)
                   .cell
      }
    }
  })

  transfers.each_with_index do |transfer, index|
    dict.set(index, transfer)
  end

  body.store_dict(dict)
  signature = sign_cell(body.cell, private_key)

  message_body = Builder.new
                        .store_bytes(signature.unpack('C*'))
                        .store_slice(body.cell.parse)

  state_init = is_init ? init : nil

  Message.new(
    MessageOptions.new(
      info: CommonMsgInfo.new(
        ExtInMsgInfo.new(
          dest: address
        )
      ),
      body: message_body.cell,
      init: state_init
    )
  )
end