ethereum.rb 1.6.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +11 -0
  3. data/.rspec +2 -0
  4. data/.ruby-gemset +1 -0
  5. data/.travis.yml +26 -0
  6. data/CODE_OF_CONDUCT.md +13 -0
  7. data/Gemfile +4 -0
  8. data/LICENSE +22 -0
  9. data/LICENSE.txt +21 -0
  10. data/README.md +183 -0
  11. data/Rakefile +11 -0
  12. data/bin/console +14 -0
  13. data/bin/install_parity +29 -0
  14. data/bin/setup +7 -0
  15. data/contracts/AccountingLib.sol +112 -0
  16. data/contracts/AuditorInterface.sol +4 -0
  17. data/contracts/AuditorRegistry.sol +14 -0
  18. data/contracts/CustodianInterface.sol +27 -0
  19. data/contracts/CustodianRegistry.sol +40 -0
  20. data/contracts/DigixConfiguration.sol +68 -0
  21. data/contracts/Directory.sol +67 -0
  22. data/contracts/DoublyLinked.sol +54 -0
  23. data/contracts/GenericInterface.sol +56 -0
  24. data/contracts/GenericRegistry.sol +15 -0
  25. data/contracts/Gold.sol +105 -0
  26. data/contracts/GoldRegistry.sol +82 -0
  27. data/contracts/GoldTokenLedger.sol +3 -0
  28. data/contracts/Interface.sol +27 -0
  29. data/contracts/Minter.sol +3 -0
  30. data/contracts/Recaster.sol +3 -0
  31. data/contracts/Testing.sol +59 -0
  32. data/contracts/VendorInterface.sol +82 -0
  33. data/contracts/VendorRegistry.sol +39 -0
  34. data/contracts/classic/Digixbot.sol +106 -0
  35. data/contracts/classic/DigixbotConfiguration.sol +62 -0
  36. data/contracts/classic/DigixbotEthereum.sol +86 -0
  37. data/contracts/classic/DigixbotUsers.sol +103 -0
  38. data/contracts/classic/Gold.sol +497 -0
  39. data/contracts/classic/GoldRegistry.sol +503 -0
  40. data/contracts/classic/GoldTokenLedger.sol +560 -0
  41. data/contracts/classic/GoldTokenMinter.sol +607 -0
  42. data/contracts/classic/ParticipantRegistry.sol +94 -0
  43. data/contracts/classic/QueueSample.sol +54 -0
  44. data/ethereum.gemspec +35 -0
  45. data/lib/ethereum.rb +24 -0
  46. data/lib/ethereum/client.rb +97 -0
  47. data/lib/ethereum/contract.rb +266 -0
  48. data/lib/ethereum/contract_event.rb +25 -0
  49. data/lib/ethereum/contract_initializer.rb +54 -0
  50. data/lib/ethereum/deployment.rb +49 -0
  51. data/lib/ethereum/formatter.rb +172 -0
  52. data/lib/ethereum/function.rb +20 -0
  53. data/lib/ethereum/function_input.rb +13 -0
  54. data/lib/ethereum/function_output.rb +14 -0
  55. data/lib/ethereum/http_client.rb +38 -0
  56. data/lib/ethereum/initializer.rb +27 -0
  57. data/lib/ethereum/ipc_client.rb +46 -0
  58. data/lib/ethereum/project_initializer.rb +28 -0
  59. data/lib/ethereum/railtie.rb +10 -0
  60. data/lib/ethereum/singleton.rb +39 -0
  61. data/lib/ethereum/solidity.rb +47 -0
  62. data/lib/ethereum/transaction.rb +36 -0
  63. data/lib/ethereum/version.rb +3 -0
  64. data/lib/tasks/ethereum_contract.rake +27 -0
  65. data/lib/tasks/ethereum_node.rake +51 -0
  66. data/lib/tasks/ethereum_test.rake +32 -0
  67. data/lib/tasks/ethereum_transaction.rake +24 -0
  68. metadata +198 -0
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 847343b8aaa240f07a46b58d0423fdc1ac3e368b
4
+ data.tar.gz: 8610cf63893eaf0686cd5230156bc6e29c020d1f
5
+ SHA512:
6
+ metadata.gz: a0b703025e50dacbf4448209709563e4075ff4176d6d01129925f975b9e9f3b68c1dcf1e6febe21bee10ef885814844273a593ac39533ce4561f0baad63e480e
7
+ data.tar.gz: 149a4faa03278bbed430632dba56ed4ef8d1b5ca84440244af1caadfe2cae17ecfc3f51f4fd8a69894eebee6f10c983946dfdeddd60d67d9d32fd4ab8cc52fcf
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /.*.swp
11
+ /**/.*.swp
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1 @@
1
+ rubythereum
@@ -0,0 +1,26 @@
1
+ dist: trusty
2
+ language: ruby
3
+ rvm:
4
+ - 2.3.1
5
+ cache:
6
+ bundler: true
7
+ directories:
8
+ - ~/.parity
9
+ before_install:
10
+ - sudo bin/install_parity
11
+ - gem install bundler -v 1.11.2
12
+ before_script:
13
+ - parity --chain ~/.parity/ropsten.json --warp --password ~/.parity/pass --unlock 21e72c12878136d9349c77c1645deef121ee8926 --author 21e72c12878136d9349c77c1645deef121ee8926 daemon ~/.parity.pid --log-file ~/.parity.log
14
+ - sleep 5
15
+ - parity --chain ~/.parity/ropsten.json account list
16
+ - cat ~/.parity.log
17
+ - bundle exec rake ethereum:node:waitforsync
18
+ - bundle exec rake ethereum:test:setup
19
+ script:
20
+ - bundle exec rspec --tag ~slow
21
+ - bundle exec rspec --tag slow
22
+ before_cache:
23
+ - killall parity
24
+ - sleep 3
25
+ after_script:
26
+ - cat /tmp/ethereum_ruby_http.log
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ethereum.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 DigixGlobal Pte Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 DigixGlobal Pte Ltd
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,183 @@
1
+ # Ethereum.rb
2
+
3
+ [![Build Status](https://travis-ci.org/marekkirejczyk/ethereum.rb.svg?branch=master)](https://travis-ci.org/marekkirejczyk/ethereum.rb) [![security](https://hakiri.io/github/NullVoxPopuli/MetaHash/master.svg)](https://hakiri.io/github/NullVoxPopuli/MetaHash/master) [![Dependency Status](https://gemnasium.com/marekkirejczyk/ethereum.rb.svg)](https://gemnasium.com/marekkirejczyk/ethereum.rb)
4
+
5
+ A simple library for Ethereum.
6
+
7
+ ## Highlights
8
+
9
+ * Pure Ruby implementation
10
+ * IPC & HTTP Client with batch calls support
11
+ * Compile Solidity contracts with solc compiler
12
+ * Deploy and call contracts methods
13
+ * Contract events
14
+
15
+ ## Compatibility and requirements
16
+
17
+ * Tested with parity 1.4.8, might work with geth
18
+ * Tested with solc 0.4.8
19
+ * Ruby 2.x
20
+ * UNIX/Linux or OS X environment
21
+
22
+ ## Prerequisites
23
+
24
+ Ethereum.rb requires installation of ethereum node and solidity compiler.
25
+
26
+ ### Ethereum node
27
+
28
+ Currently the lib supports only [parity](https://ethcore.io/parity.html). It might work with [geth](https://github.com/ethereum/go-ethereum/wiki/geth) as well, but this configuration is not tested. Library assumes that you have at least one wallet configured.
29
+
30
+ ### Solidity complier
31
+
32
+ To be able to compile [solidity](https://github.com/ethereum/solidity) contracts you need to install solc compiler. Installation instructions are available [here](http://solidity.readthedocs.io/en/latest/installing-solidity.html).
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ ```ruby
39
+ gem 'ethereum'
40
+ ```
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install ethereum
49
+
50
+ ## Basic Usage
51
+
52
+ ### IPC Client Connection
53
+
54
+ To create client instance simply create Ethereum::IpcClient:
55
+
56
+ ```ruby
57
+ client = Ethereum::IpcClient.new
58
+ ```
59
+
60
+ You can also customize it with path to ipc file path and logging flag:
61
+
62
+ ```ruby
63
+ client = Ethereum::IpcClient.new("~/.parity/mycustom.ipc", false)
64
+ ```
65
+
66
+ If no ipc file path given, IpcClient looks for ipc file in default locations for parity and geth.
67
+ By default logging is on and logs are saved in "/tmp/ethereum_ruby_http.log".
68
+
69
+
70
+ ### Solidity contract compilation and deployment
71
+
72
+ You can create contract from solidity source and deploy it to the blockchain.
73
+
74
+ ```ruby
75
+ contract = Ethereum::Contract.from_file("mycontract.sol", client)
76
+ contract_instance = contract.deploy_and_wait
77
+ ```
78
+
79
+ Deployment may take up to couple minutes.
80
+ Or with more complex syntax (might be useful if you want to complie multiple contracts at once):
81
+
82
+ ```ruby
83
+ init = Ethereum::Initializer.new("mycontract.sol", client)
84
+ init.build_all
85
+ contract = MyContract.new
86
+ contract_instance = contract.deploy_and_wait(60)
87
+ ```
88
+
89
+ Note that contract variable holds the reference to contract code, while contract_instance holds refernce to contract deployed to the blockchain. You can call contract functions on contract_instance as described in sections "Transacting and Calling Solidity Functions".
90
+
91
+ All names used to name contract in solidity source will transalte to name of classes in ruby (camelized).
92
+ If class of given name exist it will be undefined first to avoid name collision.
93
+
94
+ ### Get contract from blockchain
95
+
96
+ The other way to obtain contract instance is get one form the blockchain. To do so you need a contract name, contract address and ABI definition.
97
+
98
+ ```ruby
99
+ contract_instance = Ethereum::Contract.from_blockchain("MyContract", "0x01a4d1A62F01ED966646acBfA8BB0b59960D06dd ", abi, client)
100
+
101
+ ```
102
+
103
+ Note that you need to specify contract name, that will be used to define new class in ruby, as it is not a part of ABI definition.
104
+
105
+ ### Transacting and Calling Solidity Functions
106
+
107
+ Solidity functions are exposed using the following conventions:
108
+
109
+ ```
110
+ transact_[function_name](params)
111
+ transact_and_wait_[function_name](params)
112
+ call_[function_name](params)
113
+ ```
114
+
115
+ **Example Contract in Solidity**
116
+ ```
117
+ contract SimpleNameRegistry {
118
+
119
+ mapping (address => bool) public myMapping;
120
+
121
+ function register(address _a, bytes32 _b) {
122
+ }
123
+
124
+ function getSomeValue(address _x) public constant returns(bool b, address b) {
125
+ }
126
+
127
+ }
128
+ ```
129
+
130
+ ```ruby
131
+ simple_name_registry_instance.transact_and_wait_register("0x5b6cb65d40b0e27fab87a2180abcab22174a2d45", "minter.contract.dgx")
132
+ simple_name_registry_instance.transact_register("0x385acafdb80b71ae001f1dbd0d65e62ec2fff055", "anthony@eufemio.dgx")
133
+ simple_name_registry_instance.call_get_some_value("0x385acafdb80b71ae001f1dbd0d65e62ec2fff055")
134
+ simple_name_registry_instance.call_my_mapping("0x385acafdb80b71ae001f1dbd0d65e62ec2fff055")
135
+ ```
136
+
137
+ ### Run contracts using a different address
138
+
139
+ ```ruby
140
+ simple_name_registry_instance.as("0x0c0d99d3608a2d1d38bb1b28025e970d3910b1e1")
141
+ ```
142
+
143
+ ### Point contract instance to a previously deployed contract
144
+
145
+ ```ruby
146
+ simple_name_registry_instance.at("0x734533083b5fc0cd14b7cb8c8eb6ed0c9bd184d3")
147
+ ```
148
+
149
+ ## Utils rake tasks
150
+
151
+ ```ruby
152
+ rake ethereum:contract:compile[path] # Compile a contract / Compile and deploy contract
153
+ rake ethereum:node:mine # Mine ethereum testing environment for ethereum node
154
+ rake ethereum:node:run # Run morden (production) node
155
+ rake ethereum:node:test # Run testnet node
156
+ rake ethereum:test:setup # Setup testing environment for ethereum node
157
+ rake ethereum:transaction:byhash[id] # Get info about transaction
158
+ rake ethereum:transaction:send[address,amount] # Send [amount of] ether to an account
159
+
160
+ ```
161
+
162
+ ## Debbuging
163
+ Logs from communication with node are available under following path:
164
+ ```
165
+ /tmp/ethereum_ruby_http.log
166
+ ```
167
+
168
+ ## Development
169
+
170
+ After checking out the repo, run `bin/setup` to install dependencies.
171
+ Make sure `rake ethereum:test:setup` passes before running tests.
172
+ Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
173
+
174
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
175
+
176
+ ## Ethereum ruby
177
+
178
+ This library has been forked from [ethereum-ruby](https://github.com/DigixGlobal/ethereum-ruby) by DigixGlobal Pte Ltd (https://dgx.io).
179
+
180
+ ## License
181
+
182
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
183
+
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ import "./lib/tasks/ethereum_test.rake"
5
+ import "./lib/tasks/ethereum_node.rake"
6
+ import "./lib/tasks/ethereum_contract.rake"
7
+ import "./lib/tasks/ethereum_transaction.rake"
8
+
9
+ RSpec::Core::RakeTask.new(:spec)
10
+
11
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ethereum"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
@@ -0,0 +1,29 @@
1
+ #!/bin/bash
2
+
3
+ install_parity () {
4
+ sudo add-apt-repository ppa:ethereum/ethereum -y
5
+ sudo apt-get update
6
+ sudo apt-get install solc
7
+ wget http://d1h4xl4cr1h0mo.cloudfront.net/v1.4.8/x86_64-unknown-linux-gnu/parity_1.4.8_amd64.deb
8
+ sudo dpkg -i parity_1.4.8_amd64.deb
9
+ }
10
+
11
+
12
+
13
+ echo "Installing parity..."
14
+
15
+ if [ `which parity` ]; then
16
+ echo "Parity cached, skipping"
17
+ install_parity
18
+ else
19
+ echo "Settuping parity..."
20
+ install_parity
21
+ mkdir -p ~/.parity
22
+ wget https://raw.githubusercontent.com/ethcore/parity/master/ethcore/res/ethereum/ropsten.json --output-document ~/.parity/ropsten.json
23
+ echo $pass > ~/.parity/pass
24
+ mkdir -p ~/.parity/keys
25
+ echo $wallet > ~/.parity/keys/10890ad6-5171-4945-be95-984d83394120
26
+ chown -R travis:travis ~/.parity
27
+ fi
28
+
29
+ ls -la ~/.parity
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,112 @@
1
+ // Accounting v0.1
2
+
3
+ /// @title Accounting Lib - Accounting utilities
4
+ /// @author Piper Merriam - <pipermerriam@gmail.com>
5
+ library AccountingLib {
6
+ struct Bank {
7
+ mapping (address => uint) accountBalances;
8
+ }
9
+
10
+ /// @dev Low level method for adding funds to an account. Protects against overflow.
11
+ /// @param self The Bank instance to operate on.
12
+ /// @param accountAddress The address of the account the funds should be added to.
13
+ /// @param value The amount that should be added to the account.
14
+ function addFunds(Bank storage self, address accountAddress, uint value) public {
15
+ /*
16
+ * Helper function that should be used for any addition of
17
+ * account funds. It has error checking to prevent
18
+ * overflowing the account balance.
19
+ */
20
+ if (self.accountBalances[accountAddress] + value < self.accountBalances[accountAddress]) {
21
+ // Prevent Overflow.
22
+ throw;
23
+ }
24
+ self.accountBalances[accountAddress] += value;
25
+ }
26
+
27
+ event _Deposit(address indexed _from, address indexed accountAddress, uint value);
28
+
29
+ /// @dev Function wrapper around the _Deposit event so that it can be used by contracts. Can be used to log a deposit to an account.
30
+ /// @param _from The address that deposited the funds.
31
+ /// @param accountAddress The address of the account the funds were added to.
32
+ /// @param value The amount that was added to the account.
33
+ function Deposit(address _from, address accountAddress, uint value) public {
34
+ _Deposit(_from, accountAddress, value);
35
+ }
36
+
37
+ /// @dev Safe function for depositing funds. Returns boolean for whether the deposit was successful
38
+ /// @param self The Bank instance to operate on.
39
+ /// @param accountAddress The address of the account the funds should be added to.
40
+ /// @param value The amount that should be added to the account.
41
+ function deposit(Bank storage self, address accountAddress, uint value) public returns (bool) {
42
+ /*
43
+ * Public API for depositing funds in a specified account.
44
+ */
45
+ if (self.accountBalances[accountAddress] + value < self.accountBalances[accountAddress]) {
46
+ return false;
47
+ }
48
+ addFunds(self, accountAddress, value);
49
+ return true;
50
+ }
51
+
52
+ event _Withdrawal(address indexed accountAddress, uint value);
53
+
54
+ /// @dev Function wrapper around the _Withdrawal event so that it can be used by contracts. Can be used to log a withdrawl from an account.
55
+ /// @param accountAddress The address of the account the funds were withdrawn from.
56
+ /// @param value The amount that was withdrawn to the account.
57
+ function Withdrawal(address accountAddress, uint value) public {
58
+ _Withdrawal(accountAddress, value);
59
+ }
60
+
61
+ event _InsufficientFunds(address indexed accountAddress, uint value, uint balance);
62
+
63
+ /// @dev Function wrapper around the _InsufficientFunds event so that it can be used by contracts. Can be used to log a failed withdrawl from an account.
64
+ /// @param accountAddress The address of the account the funds were to be withdrawn from.
65
+ /// @param value The amount that was attempted to be withdrawn from the account.
66
+ /// @param balance The current balance of the account.
67
+ function InsufficientFunds(address accountAddress, uint value, uint balance) public {
68
+ _InsufficientFunds(accountAddress, value, balance);
69
+ }
70
+
71
+ /// @dev Low level method for removing funds from an account. Protects against underflow.
72
+ /// @param self The Bank instance to operate on.
73
+ /// @param accountAddress The address of the account the funds should be deducted from.
74
+ /// @param value The amount that should be deducted from the account.
75
+ function deductFunds(Bank storage self, address accountAddress, uint value) public {
76
+ /*
77
+ * Helper function that should be used for any reduction of
78
+ * account funds. It has error checking to prevent
79
+ * underflowing the account balance which would be REALLY bad.
80
+ */
81
+ if (value > self.accountBalances[accountAddress]) {
82
+ // Prevent Underflow.
83
+ throw;
84
+ }
85
+ self.accountBalances[accountAddress] -= value;
86
+ }
87
+
88
+ /// @dev Safe function for withdrawing funds. Returns boolean for whether the deposit was successful as well as sending the amount in ether to the account address.
89
+ /// @param self The Bank instance to operate on.
90
+ /// @param accountAddress The address of the account the funds should be withdrawn from.
91
+ /// @param value The amount that should be withdrawn from the account.
92
+ function withdraw(Bank storage self, address accountAddress, uint value) public returns (bool) {
93
+ /*
94
+ * Public API for withdrawing funds.
95
+ */
96
+ if (self.accountBalances[accountAddress] >= value) {
97
+ deductFunds(self, accountAddress, value);
98
+ if (!accountAddress.send(value)) {
99
+ // Potentially sending money to a contract that
100
+ // has a fallback function. So instead, try
101
+ // tranferring the funds with the call api.
102
+ if (!accountAddress.call.value(value)()) {
103
+ // Revert the entire transaction. No
104
+ // need to destroy the funds.
105
+ //throw;
106
+ }
107
+ }
108
+ return true;
109
+ }
110
+ return false;
111
+ }
112
+ }