Skip to content

Commit

Permalink
catching up all the commits up to 05-23-2019 (#9)
Browse files Browse the repository at this point in the history
* catching up all the commits up to 05-23-2019

* final polish
  • Loading branch information
DaHoopster committed May 29, 2019
1 parent 4edeaf9 commit a898c8b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 16 deletions.
4 changes: 2 additions & 2 deletions Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
iost_sdk (0.1.4)
iost_sdk (0.1.5)
base58 (~> 0.2.3)
btcruby (~> 1.8)
ed25519 (~> 1.2.4)
Expand All @@ -25,7 +25,7 @@ GEM
docile (1.3.1)
ed25519 (1.2.4)
ffi (1.10.0)
httparty (0.16.4)
httparty (0.17.0)
mime-types (~> 3.0)
multi_xml (>= 0.5.2)
json (2.1.0)
Expand Down
37 changes: 30 additions & 7 deletions lib/iost_sdk.rb
Expand Up @@ -32,6 +32,8 @@ class Main
failed: 'failed'
}.freeze

SERVER_TIME_DIFF_THRESHOLD = (30 * 1_000_000_000).freeze

#
# @param endpoint [String] a URL of the JSON RPC endpoint of IOST
def initialize(endpoint:)
Expand All @@ -53,10 +55,11 @@ def sign_and_send(account_name:, key_pair:)
key_pair: key_pair
)

if resp['pre_tx_receipt']['status_code'] != TXN_STATUS[:success].upcase
if !resp['pre_tx_receipt'] || (resp['pre_tx_receipt'] && resp['pre_tx_receipt']['status_code'] != TXN_STATUS[:success].upcase)
{
status: TXN_STATUS[:failed],
txn_hash: resp['hash']
txn_hash: resp['hash'],
message: resp['pre_tx_receipt'] ? resp['pre_tx_receipt']['error'] : ''
}
else
txn_hash = resp['hash']
Expand All @@ -76,7 +79,8 @@ def sign_and_send(account_name:, key_pair:)

{
status: txn_status,
txn_hash: txn_receipt.tx_hash
txn_hash: txn_receipt.tx_hash,
message: ''
}
end
end
Expand All @@ -91,8 +95,11 @@ def sign_and_send(account_name:, key_pair:)
def call_abi(contract_id:, abi_name:, abi_args:)
transaction = init_transaction
transaction.add_action(contract_id: contract_id, action_name: abi_name, action_data: abi_args)
transaction.set_time_params(expiration: expiration, delay: delay)

transaction.set_time_params(
expiration: expiration,
delay: delay,
server_time_diff: server_time_diff
)
@transaction = transaction
self
end
Expand All @@ -112,7 +119,6 @@ def transfer(token:, from:, to:, amount:, memo:)
abi_args: [token, from, to, amount.to_s, memo]
)
@transaction.add_approve(token: :iost, amount: amount)
@transaction.set_time_params(expiration: expiration, delay: delay)

self
end
Expand Down Expand Up @@ -148,9 +154,14 @@ def new_account(name:, creator:, owner_key:, active_key:, initial_ram:, initial_
action_data: [creator, name, initial_gas_pledge.to_s]
)
end
transaction.set_time_params(
expiration: expiration,
delay: delay,
server_time_diff: server_time_diff
)

transaction.set_time_params(expiration: expiration, delay: delay)
@transaction = transaction

self
end

Expand All @@ -174,5 +185,17 @@ def init_transaction
)
transaction
end

def server_time_diff
request_start_time = Time.now.utc.to_i * 1_000_000_000
node_server_time = @client.get_node_info.server_time.to_i
request_end_time = Time.now.utc.to_i * 1_000_000_000

if request_end_time - request_start_time < SERVER_TIME_DIFF_THRESHOLD
node_server_time - request_end_time
else
0
end
end
end
end
6 changes: 3 additions & 3 deletions lib/iost_sdk/models/query/transaction.rb
Expand Up @@ -39,10 +39,11 @@ def self.attr_names
#
# @param expiration [Integer] number of seconds, since creation, the transaction will expire in
# @param delay [Integer] the delay
def set_time_params(expiration:, delay:)
# @param server_time_diff [Integer] diff between client time and IOST node server time
def set_time_params(expiration:, delay:, server_time_diff:)
time_now = (Time.now.utc.to_f * 1000).to_i * 1_000_000

@time = time_now
@time = time_now + (server_time_diff || 0)
@expiration = @time + expiration * 1_000_000_000
@delay = delay
end
Expand All @@ -67,7 +68,6 @@ def add_action(contract_id:, action_name:, action_data:)
# @param token [String] name of the token
# @param amount [Integer|String] amount of the token or 'unlimited'
def add_approve(token:, amount:)
raise IOSTSdk::Errors::InvalidTransactionError.new('approve token should not be *') if token == '*'
raise IOSTSdk::Errors::InvalidTransactionError.new('approve amount should be numeric') unless amount.is_a?(Numeric)

@amount_limit << IOSTSdk::Models::AmountLimit.new.populate(
Expand Down
2 changes: 1 addition & 1 deletion lib/iost_sdk/version.rb
@@ -1,3 +1,3 @@
module IOSTSdk
VERSION = ENV['IOST_SDK_VERSION'] || '0.1.4'
VERSION = ENV['IOST_SDK_VERSION'] || '0.1.5'
end
3 changes: 2 additions & 1 deletion spec/iost_sdk_spec.rb
Expand Up @@ -219,9 +219,10 @@
.call_abi(
contract_id: 'token.iost',
abi_name: 'transfer',
abi_args: ['iost', 'binary_test', 'binary_test', '10.000', '']
abi_args: ['iost', 'binary_test', 'binary_test', '1.0', '']
)
iost.transaction.chain_id = 1023
iost.transaction.add_approve(token: 'iost', amount: 1.0)
resp = iost.sign_and_send(account_name: 'binary_test', key_pair: key_pair)
expect(resp[:status]).to eq('success')
expect(resp[:txn_hash]).to_not be_nil
Expand Down
4 changes: 2 additions & 2 deletions spec/models/query/transaction_spec.rb
Expand Up @@ -22,8 +22,8 @@
}

describe '.add_approve' do
it 'should raise an error if token is *' do
expect { txn.add_approve(token: '*', amount: 89.0) }.to raise_error(IOSTSdk::Errors::InvalidTransactionError)
it 'should not raise an error if token is *' do
expect { txn.add_approve(token: '*', amount: 89.0) }.not_to raise_error
end

it 'should raise an error if amount is not numeric' do
Expand Down

0 comments on commit a898c8b

Please sign in to comment.