iost_sdk 0.1.3 → 0.1.4

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: 47f23025639c7a13c134e39e88e3e91696a5a04459528fc515b31289f5fdde6b
4
- data.tar.gz: 34ae6bafab7ac64c5329d5c80e83f257e446e61c7ca05e03ab263a8096c212d0
3
+ metadata.gz: 6ad731fd51e432cb3e208830e9bac05b6791929a5b10b3160292e2c9dabbf32d
4
+ data.tar.gz: 2acb03fbf50d62203bbe8727c66ee17a038b0c909355b643ce2ba5e1092485b0
5
5
  SHA512:
6
- metadata.gz: cff87f7f815c940755e552bf99534c79ad93d7ebad82cab690d122339319661147309b7623b151006044dd9675bb9c6b6dd96fb48bc79ad7259e2d532c2b4a37
7
- data.tar.gz: ea48f173fe2d6d9c30acdd37c085ed609350bdacef3a9e01f75821aca69b4bd2e6068e3dd6bc105c9f8c20d815ce3f8cfc0331028a40f2bcbd9f261c9f0af3fd
6
+ metadata.gz: 3282a86197eb10e5f0aeedbfea442200e0504ff619f031e66f4d6f99dcbdf56b873a41b64406c5064157226be9483b12990958a56846ed642d95f64f0f49daf1
7
+ data.tar.gz: a5ae133dc8048a86c2cf0ff0a52fec861c1ad876eb9a5a7469f0ad4167f08f7ec7e731684514fd7a0572ac4c1846f09df4754cb06175b64b024dbfb55d13a37e
@@ -6,5 +6,11 @@ module IOSTSdk
6
6
  super(@message)
7
7
  end
8
8
  end
9
+
10
+ class InvalidTransactionError < StandardError
11
+ def initialize(message)
12
+ super(message)
13
+ end
14
+ end
9
15
  end
10
16
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
+ require 'iost_sdk/errors'
4
5
  require 'iost_sdk/models'
5
6
  require 'iost_sdk/models/amount_limit'
6
7
  require 'iost_sdk/models/action'
@@ -12,6 +13,10 @@ module IOSTSdk
12
13
  class Transaction
13
14
  include Models
14
15
 
16
+ GAS_LIMIT_RANGE = [6000, 4_000_000].freeze
17
+ GAS_RATIO_RANGE = [1, 100].freeze
18
+ NUMERIC_REGEX = /^\d+(\.\d+)?$/.freeze
19
+
15
20
  attr_accessor :chain_id
16
21
 
17
22
  def self.attr_names
@@ -62,6 +67,9 @@ module IOSTSdk
62
67
  # @param token [String] name of the token
63
68
  # @param amount [Integer|String] amount of the token or 'unlimited'
64
69
  def add_approve(token:, amount:)
70
+ raise IOSTSdk::Errors::InvalidTransactionError.new('approve token should not be *') if token == '*'
71
+ raise IOSTSdk::Errors::InvalidTransactionError.new('approve amount should be numeric') unless amount.is_a?(Numeric)
72
+
65
73
  @amount_limit << IOSTSdk::Models::AmountLimit.new.populate(
66
74
  model_data: {
67
75
  'token' => token.to_s,
@@ -69,6 +77,18 @@ module IOSTSdk
69
77
  }
70
78
  )
71
79
  end
80
+
81
+ # Verify if the transaction object is valid
82
+ def is_valid?
83
+ [
84
+ # check gas limit
85
+ gas_limit.is_a?(Numeric) && gas_limit.between?(GAS_LIMIT_RANGE.first, GAS_LIMIT_RANGE.last),
86
+ # check gas ratio
87
+ gas_ratio.is_a?(Numeric) && gas_ratio.between?(GAS_RATIO_RANGE.first, GAS_RATIO_RANGE.last),
88
+ # check approve token and amount
89
+ amount_limit.all? { |al| al.token != '*' || /^\d+(\.\d+)?$/ =~ al.value }
90
+ ].all?
91
+ end
72
92
  end
73
93
  end
74
94
  end
@@ -1,3 +1,3 @@
1
1
  module IOSTSdk
2
- VERSION = ENV['IOST_SDK_VERSION'] || '0.1.0'
2
+ VERSION = ENV['IOST_SDK_VERSION'] || '0.1.4'
3
3
  end
data/lib/iost_sdk.rb CHANGED
@@ -46,7 +46,7 @@ module IOSTSdk
46
46
  def_delegators :@client, *IOSTSdk::Http::Client.read_apis.keys
47
47
 
48
48
  def sign_and_send(account_name:, key_pair:)
49
- if @transaction
49
+ if @transaction && @transaction.is_valid?
50
50
  resp = @client.send_tx(
51
51
  transaction: @transaction,
52
52
  account_name: account_name,
@@ -91,7 +91,6 @@ module IOSTSdk
91
91
  def call_abi(contract_id:, abi_name:, abi_args:)
92
92
  transaction = init_transaction
93
93
  transaction.add_action(contract_id: contract_id, action_name: abi_name, action_data: abi_args)
94
- transaction.add_approve(token: '*', amount: :unlimited)
95
94
  transaction.set_time_params(expiration: expiration, delay: delay)
96
95
 
97
96
  @transaction = transaction
@@ -110,7 +109,7 @@ module IOSTSdk
110
109
  call_abi(
111
110
  contract_id: 'token.iost',
112
111
  abi_name: :transfer,
113
- abi_args: [token, from, to, amount, memo]
112
+ abi_args: [token, from, to, amount.to_s, memo]
114
113
  )
115
114
  @transaction.add_approve(token: :iost, amount: amount)
116
115
  @transaction.set_time_params(expiration: expiration, delay: delay)
@@ -151,8 +150,6 @@ module IOSTSdk
151
150
  end
152
151
 
153
152
  transaction.set_time_params(expiration: expiration, delay: delay)
154
- transaction.add_approve(token: '*', amount: approval_limit_amount)
155
-
156
153
  @transaction = transaction
157
154
  self
158
155
  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.3
4
+ version: 0.1.4
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-03-26 00:00:00.000000000 Z
11
+ date: 2019-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler