web3.js/lib/web3/transfer.js

93 lines
2.6 KiB
JavaScript
Raw Normal View History

/*
2015-10-08 15:34:07 +08:00
This file is part of web3.js.
2015-10-08 15:34:07 +08:00
web3.js is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
2015-10-08 15:34:07 +08:00
web3.js is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
2015-10-08 15:34:07 +08:00
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
*/
/**
* @file transfer.js
* @author Marek Kotewicz <marek@ethdev.com>
* @date 2015
*/
2015-08-06 17:11:15 +02:00
var Iban = require('./iban');
var exchangeAbi = require('../contracts/SmartExchange.json');
2015-05-18 15:22:58 +02:00
/**
2015-08-06 17:11:15 +02:00
* Should be used to make Iban transfer
2015-05-18 15:22:58 +02:00
*
* @method transfer
2015-08-05 16:02:12 +02:00
* @param {String} from
* @param {String} to iban
2015-05-18 15:22:58 +02:00
* @param {Value} value to be tranfered
* @param {Function} callback, callback
*/
var transfer = function (eth, from, to, value, callback) {
2015-08-06 17:11:15 +02:00
var iban = new Iban(to);
if (!iban.isValid()) {
2015-05-23 16:03:01 +02:00
throw new Error('invalid iban address');
}
2015-08-06 17:11:15 +02:00
if (iban.isDirect()) {
return transferToAddress(eth, from, iban.address(), value, callback);
}
if (!callback) {
var address = eth.icapNamereg().addr(iban.institution());
return deposit(eth, from, address, value, iban.client());
}
eth.icapNamereg().addr(iban.institution(), function (err, address) {
return deposit(eth, from, address, value, iban.client(), callback);
});
};
2015-05-18 15:22:58 +02:00
/**
* Should be used to transfer funds to certain address
*
* @method transferToAddress
2015-08-05 16:02:12 +02:00
* @param {String} from
* @param {String} to
2015-05-18 15:22:58 +02:00
* @param {Value} value to be tranfered
* @param {Function} callback, callback
*/
var transferToAddress = function (eth, from, to, value, callback) {
return eth.sendTransaction({
2015-08-05 16:02:12 +02:00
address: to,
from: from,
2015-05-23 16:03:01 +02:00
value: value
}, callback);
};
2015-05-18 15:22:58 +02:00
/**
* Should be used to deposit funds to generic Exchange contract (must implement deposit(bytes32) method!)
2015-05-18 15:22:58 +02:00
*
* @method deposit
2015-08-05 16:02:12 +02:00
* @param {String} from
* @param {String} to
* @param {Value} value to be transfered
2015-05-18 15:22:58 +02:00
* @param {String} client unique identifier
* @param {Function} callback, callback
*/
var deposit = function (eth, from, to, value, client, callback) {
var abi = exchangeAbi;
return eth.contract(abi).at(to).deposit(client, {
2015-05-18 15:22:58 +02:00
from: from,
value: value
}, callback);
};
module.exports = transfer;