iost_sdk 0.1.4 → 0.1.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6ad731fd51e432cb3e208830e9bac05b6791929a5b10b3160292e2c9dabbf32d
4
- data.tar.gz: 2acb03fbf50d62203bbe8727c66ee17a038b0c909355b643ce2ba5e1092485b0
3
+ metadata.gz: 3c7627fde4fe416cfe76cdcb120424fb3b13ec46e2faa5d473be118aaa143486
4
+ data.tar.gz: 6901a1b985095abf199ab7156acb3b5337ac7a037f579b1a2973ffdb1cc89044
5
5
  SHA512:
6
- metadata.gz: 3282a86197eb10e5f0aeedbfea442200e0504ff619f031e66f4d6f99dcbdf56b873a41b64406c5064157226be9483b12990958a56846ed642d95f64f0f49daf1
7
- data.tar.gz: a5ae133dc8048a86c2cf0ff0a52fec861c1ad876eb9a5a7469f0ad4167f08f7ec7e731684514fd7a0572ac4c1846f09df4754cb06175b64b024dbfb55d13a37e
6
+ metadata.gz: b63944945a76f2971f9286b0189a46dde39e89933cdadf2ddada0d8261edbde950809cbbb7b38c83f4379a3594c5fa21218b1b90e12cd4078f9c58d93f8c15e8
7
+ data.tar.gz: 42d605532bd9445e9d2fab3643230dcf74893390b9d1f6f84394033e1b8d52d13ba7afca68a9d8b189b27e233049e5b0450edfa0bb141311677b64d3810c04d7
@@ -32,6 +32,8 @@ module IOSTSdk
32
32
  failed: 'failed'
33
33
  }.freeze
34
34
 
35
+ SERVER_TIME_DIFF_THRESHOLD = (30 * 1_000_000_000).freeze
36
+
35
37
  #
36
38
  # @param endpoint [String] a URL of the JSON RPC endpoint of IOST
37
39
  def initialize(endpoint:)
@@ -53,10 +55,11 @@ module IOSTSdk
53
55
  key_pair: key_pair
54
56
  )
55
57
 
56
- if resp['pre_tx_receipt']['status_code'] != TXN_STATUS[:success].upcase
58
+ if !resp['pre_tx_receipt'] || (resp['pre_tx_receipt'] && resp['pre_tx_receipt']['status_code'] != TXN_STATUS[:success].upcase)
57
59
  {
58
60
  status: TXN_STATUS[:failed],
59
- txn_hash: resp['hash']
61
+ txn_hash: resp['hash'],
62
+ message: resp['pre_tx_receipt'] ? resp['pre_tx_receipt']['error'] : ''
60
63
  }
61
64
  else
62
65
  txn_hash = resp['hash']
@@ -76,7 +79,8 @@ module IOSTSdk
76
79
 
77
80
  {
78
81
  status: txn_status,
79
- txn_hash: txn_receipt.tx_hash
82
+ txn_hash: txn_receipt.tx_hash,
83
+ message: ''
80
84
  }
81
85
  end
82
86
  end
@@ -91,8 +95,11 @@ module IOSTSdk
91
95
  def call_abi(contract_id:, abi_name:, abi_args:)
92
96
  transaction = init_transaction
93
97
  transaction.add_action(contract_id: contract_id, action_name: abi_name, action_data: abi_args)
94
- transaction.set_time_params(expiration: expiration, delay: delay)
95
-
98
+ transaction.set_time_params(
99
+ expiration: expiration,
100
+ delay: delay,
101
+ server_time_diff: server_time_diff
102
+ )
96
103
  @transaction = transaction
97
104
  self
98
105
  end
@@ -112,7 +119,6 @@ module IOSTSdk
112
119
  abi_args: [token, from, to, amount.to_s, memo]
113
120
  )
114
121
  @transaction.add_approve(token: :iost, amount: amount)
115
- @transaction.set_time_params(expiration: expiration, delay: delay)
116
122
 
117
123
  self
118
124
  end
@@ -148,9 +154,14 @@ module IOSTSdk
148
154
  action_data: [creator, name, initial_gas_pledge.to_s]
149
155
  )
150
156
  end
157
+ transaction.set_time_params(
158
+ expiration: expiration,
159
+ delay: delay,
160
+ server_time_diff: server_time_diff
161
+ )
151
162
 
152
- transaction.set_time_params(expiration: expiration, delay: delay)
153
163
  @transaction = transaction
164
+
154
165
  self
155
166
  end
156
167
 
@@ -174,5 +185,17 @@ module IOSTSdk
174
185
  )
175
186
  transaction
176
187
  end
188
+
189
+ def server_time_diff
190
+ request_start_time = Time.now.utc.to_i * 1_000_000_000
191
+ node_server_time = @client.get_node_info.server_time.to_i
192
+ request_end_time = Time.now.utc.to_i * 1_000_000_000
193
+
194
+ if request_end_time - request_start_time < SERVER_TIME_DIFF_THRESHOLD
195
+ node_server_time - request_end_time
196
+ else
197
+ 0
198
+ end
199
+ end
177
200
  end
178
201
  end
@@ -39,10 +39,11 @@ module IOSTSdk
39
39
  #
40
40
  # @param expiration [Integer] number of seconds, since creation, the transaction will expire in
41
41
  # @param delay [Integer] the delay
42
- def set_time_params(expiration:, delay:)
42
+ # @param server_time_diff [Integer] diff between client time and IOST node server time
43
+ def set_time_params(expiration:, delay:, server_time_diff:)
43
44
  time_now = (Time.now.utc.to_f * 1000).to_i * 1_000_000
44
45
 
45
- @time = time_now
46
+ @time = time_now + (server_time_diff || 0)
46
47
  @expiration = @time + expiration * 1_000_000_000
47
48
  @delay = delay
48
49
  end
@@ -67,7 +68,6 @@ module IOSTSdk
67
68
  # @param token [String] name of the token
68
69
  # @param amount [Integer|String] amount of the token or 'unlimited'
69
70
  def add_approve(token:, amount:)
70
- raise IOSTSdk::Errors::InvalidTransactionError.new('approve token should not be *') if token == '*'
71
71
  raise IOSTSdk::Errors::InvalidTransactionError.new('approve amount should be numeric') unless amount.is_a?(Numeric)
72
72
 
73
73
  @amount_limit << IOSTSdk::Models::AmountLimit.new.populate(
@@ -1,3 +1,3 @@
1
1
  module IOSTSdk
2
- VERSION = ENV['IOST_SDK_VERSION'] || '0.1.4'
2
+ VERSION = ENV['IOST_SDK_VERSION'] || '0.1.5'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: iost_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Han Wang
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-18 00:00:00.000000000 Z
11
+ date: 2019-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler