mirror of
https://github.com/status-im/web3.js.git
synced 2025-02-23 11:38:12 +00:00
96 lines
2.7 KiB
JavaScript
96 lines
2.7 KiB
JavaScript
/*
|
|
This file is part of web3.js.
|
|
|
|
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.
|
|
|
|
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
|
|
along with web3.js. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
/**
|
|
* @file transfer.js
|
|
* @author Marek Kotewicz <marek@ethdev.com>
|
|
* @date 2015
|
|
*/
|
|
|
|
var web3 = require('../web3');
|
|
var Iban = require('./iban');
|
|
var namereg = require('./namereg').ibanNamereg;
|
|
var contract = require('./contract');
|
|
var exchangeAbi = require('../contracts/SmartExchange.json');
|
|
|
|
/**
|
|
* Should be used to make Iban transfer
|
|
*
|
|
* @method transfer
|
|
* @param {String} from
|
|
* @param {String} to iban
|
|
* @param {Value} value to be tranfered
|
|
* @param {Function} callback, callback
|
|
*/
|
|
var transfer = function (from, to, value, callback) {
|
|
var iban = new Iban(to);
|
|
if (!iban.isValid()) {
|
|
throw new Error('invalid iban address');
|
|
}
|
|
|
|
if (iban.isDirect()) {
|
|
return transferToAddress(from, iban.address(), value, callback);
|
|
}
|
|
|
|
if (!callback) {
|
|
var address = namereg.addr(iban.institution());
|
|
return deposit(from, address, value, iban.client());
|
|
}
|
|
|
|
namereg.addr(iban.institution(), function (err, address) {
|
|
return deposit(from, address, value, iban.client(), callback);
|
|
});
|
|
|
|
};
|
|
|
|
/**
|
|
* Should be used to transfer funds to certain address
|
|
*
|
|
* @method transferToAddress
|
|
* @param {String} from
|
|
* @param {String} to
|
|
* @param {Value} value to be tranfered
|
|
* @param {Function} callback, callback
|
|
*/
|
|
var transferToAddress = function (from, to, value, callback) {
|
|
return web3.eth.sendTransaction({
|
|
address: to,
|
|
from: from,
|
|
value: value
|
|
}, callback);
|
|
};
|
|
|
|
/**
|
|
* Should be used to deposit funds to generic Exchange contract (must implement deposit(bytes32) method!)
|
|
*
|
|
* @method deposit
|
|
* @param {String} from
|
|
* @param {String} to
|
|
* @param {Value} value to be transfered
|
|
* @param {String} client unique identifier
|
|
* @param {Function} callback, callback
|
|
*/
|
|
var deposit = function (from, to, value, client, callback) {
|
|
var abi = exchangeAbi;
|
|
return contract(abi).at(to).deposit(client, {
|
|
from: from,
|
|
value: value
|
|
}, callback);
|
|
};
|
|
|
|
module.exports = transfer;
|
|
|