'use strict';
var sendTxCtrl = function($scope, $sce, walletService) {
$scope.ajaxReq = ajaxReq;
$scope.unitReadable = ajaxReq.type;
$scope.sendTxModal = new Modal(document.getElementById('sendTransaction'));
walletService.wallet = null;
walletService.password = '';
$scope.showAdvance = $scope.showRaw = false;
$scope.dropdownEnabled = true;
$scope.Validator = Validator;
$scope.gasLimitChanged = false;
// Tokens
$scope.tokenVisibility = 'hidden';
$scope.tokenTx = {
to: '',
value: 0,
id: -1
};
$scope.customGasMsg = '';
// For token sale holders:
// 1. Add the address users are sending to
// 2. Add the gas limit users should use to send successfully (this avoids OOG errors)
// 3. Add any data if applicable
// 4. Add a message if you want.
$scope.tx = {
// if there is no gasLimit or gas key in the URI, use the default value. Otherwise use value of gas or gasLimit. gasLimit wins over gas if both present
gasLimit: globalFuncs.urlGet('gaslimit') != null ||
globalFuncs.urlGet('gas') != null
? globalFuncs.urlGet('gaslimit') != null
? globalFuncs.urlGet('gaslimit')
: globalFuncs.urlGet('gas')
: globalFuncs.defaultTxGasLimit,
data: globalFuncs.urlGet('data') == null ? '' : globalFuncs.urlGet('data'),
to: globalFuncs.urlGet('to') == null ? '' : globalFuncs.urlGet('to'),
unit: 'ether',
value: globalFuncs.urlGet('value') == null
? ''
: globalFuncs.urlGet('value'),
nonce: null,
gasPrice: null,
donate: false,
tokenSymbol: globalFuncs.urlGet('tokenSymbol') == null
? false
: globalFuncs.urlGet('tokenSymbol'),
readOnly: globalFuncs.urlGet('readOnly') == null ? false : true
};
$scope.setSendMode = function(sendMode, tokenId = '', tokenSymbol = '') {
$scope.tx.sendMode = sendMode;
$scope.unitReadable = '';
if (sendMode == 'ether') {
$scope.unitReadable = ajaxReq.type;
} else {
$scope.unitReadable = tokenSymbol;
$scope.tokenTx.id = tokenId;
}
$scope.dropdownAmount = false;
};
$scope.setTokenSendMode = function() {
if ($scope.tx.sendMode == 'token' && !$scope.tx.tokenSymbol) {
$scope.tx.tokenSymbol = $scope.wallet.tokenObjs[0].symbol;
$scope.wallet.tokenObjs[0].type = 'custom';
$scope.setSendMode($scope.tx.sendMode, 0, $scope.tx.tokenSymbol);
} else if ($scope.tx.tokenSymbol) {
for (var i = 0; i < $scope.wallet.tokenObjs.length; i++) {
if (
$scope.wallet.tokenObjs[i].symbol
.toLowerCase()
.indexOf($scope.tx.tokenSymbol.toLowerCase()) !== -1
) {
$scope.wallet.tokenObjs[i].type = 'custom';
$scope.setSendMode('token', i, $scope.wallet.tokenObjs[i].symbol);
break;
} else $scope.tokenTx.id = -1;
}
}
if ($scope.tx.sendMode != 'token') $scope.tokenTx.id = -1;
};
var applyScope = function() {
if (!$scope.$$phase) $scope.$apply();
};
var defaultInit = function() {
globalFuncs.urlGet('sendMode') == null
? $scope.setSendMode('ether')
: $scope.setSendMode(globalFuncs.urlGet('sendMode'));
$scope.showAdvance =
globalFuncs.urlGet('gaslimit') != null ||
globalFuncs.urlGet('gas') != null ||
globalFuncs.urlGet('data') != null;
if (
globalFuncs.urlGet('data') ||
globalFuncs.urlGet('value') ||
globalFuncs.urlGet('to') ||
globalFuncs.urlGet('gaslimit') ||
globalFuncs.urlGet('sendMode') ||
globalFuncs.urlGet('gas') ||
globalFuncs.urlGet('tokenSymbol')
)
$scope.hasQueryString = true; // if there is a query string, show an warning at top of page
};
$scope.$watch(
function() {
if (walletService.wallet == null) return null;
return walletService.wallet.getAddressString();
},
function() {
if (walletService.wallet == null) return;
$scope.wallet = walletService.wallet;
$scope.wd = true;
$scope.wallet.setBalance(applyScope);
$scope.wallet.setTokens();
if ($scope.parentTxConfig) {
var setTxObj = function() {
$scope.tx.to = $scope.parentTxConfig.to;
$scope.tx.value = $scope.parentTxConfig.value;
$scope.tx.sendMode = $scope.parentTxConfig.sendMode
? $scope.parentTxConfig.sendMode
: 'ether';
$scope.tx.tokenSymbol = $scope.parentTxConfig.tokenSymbol
? $scope.parentTxConfig.tokenSymbol
: '';
$scope.tx.readOnly = $scope.parentTxConfig.readOnly
? $scope.parentTxConfig.readOnly
: false;
};
$scope.$watch(
'parentTxConfig',
function() {
setTxObj();
},
true
);
}
$scope.setTokenSendMode();
defaultInit();
}
);
$scope.$watch('ajaxReq.key', function() {
if ($scope.wallet) {
$scope.setSendMode('ether');
$scope.wallet.setBalance(applyScope);
$scope.wallet.setTokens();
}
});
$scope.$watch(
'tokenTx',
function() {
if (
$scope.wallet &&
$scope.wallet.tokenObjs !== undefined &&
$scope.wallet.tokenObjs[$scope.tokenTx.id] !== undefined &&
$scope.Validator.isValidAddress($scope.tokenTx.to) &&
$scope.Validator.isPositiveNumber($scope.tokenTx.value)
) {
if ($scope.estimateTimer) clearTimeout($scope.estimateTimer);
$scope.estimateTimer = setTimeout(function() {
$scope.estimateGasLimit();
}, 500);
}
},
true
);
$scope.$watch(
'tx',
function(newValue, oldValue) {
$scope.showRaw = false;
if (
oldValue.sendMode != newValue.sendMode &&
newValue.sendMode == 'ether'
) {
$scope.tx.data = '';
$scope.tx.gasLimit = globalFuncs.defaultTxGasLimit;
}
if (
newValue.gasLimit == oldValue.gasLimit &&
$scope.wallet &&
$scope.Validator.isValidAddress($scope.tx.to) &&
$scope.Validator.isPositiveNumber($scope.tx.value) &&
$scope.Validator.isValidHex($scope.tx.data) &&
$scope.tx.sendMode != 'token'
) {
if ($scope.estimateTimer) clearTimeout($scope.estimateTimer);
$scope.estimateTimer = setTimeout(function() {
$scope.estimateGasLimit();
}, 500);
}
if ($scope.tx.sendMode == 'token') {
$scope.tokenTx.to = $scope.tx.to;
$scope.tokenTx.value = $scope.tx.value;
}
},
true
);
$scope.estimateGasLimit = function() {
$scope.customGasMsg = '';
if ($scope.gasLimitChanged) return;
for (var i in $scope.customGas) {
if ($scope.tx.to.toLowerCase() == $scope.customGas[i].to.toLowerCase()) {
$scope.showAdvance = $scope.customGas[i].data != '' ? true : false;
$scope.tx.gasLimit = $scope.customGas[i].gasLimit;
$scope.tx.data = $scope.customGas[i].data;
$scope.customGasMsg = $scope.customGas[i].msg != ''
? $scope.customGas[i].msg
: '';
return;
}
}
if (globalFuncs.lightMode) {
$scope.tx.gasLimit = globalFuncs.defaultTokenGasLimit;
return;
}
var estObj = {
to: $scope.tx.to,
from: $scope.wallet.getAddressString(),
value: ethFuncs.sanitizeHex(
ethFuncs.decimalToHex(etherUnits.toWei($scope.tx.value, $scope.tx.unit))
)
};
if ($scope.tx.data != '')
estObj.data = ethFuncs.sanitizeHex($scope.tx.data);
if ($scope.tx.sendMode == 'token') {
estObj.to = $scope.wallet.tokenObjs[
$scope.tokenTx.id
].getContractAddress();
estObj.data = $scope.wallet.tokenObjs[$scope.tokenTx.id].getData(
$scope.tokenTx.to,
$scope.tokenTx.value
).data;
estObj.value = '0x00';
}
ethFuncs.estimateGas(estObj, function(data) {
uiFuncs.notifier.close();
if (!data.error) {
if (data.data == '-1')
$scope.notifier.danger(globalFuncs.errorMsgs[21]);
$scope.tx.gasLimit = data.data;
} else $scope.notifier.danger(data.msg);
});
};
var isEnough = function(valA, valB) {
return new BigNumber(valA).lte(new BigNumber(valB));
};
$scope.hasEnoughBalance = function() {
if ($scope.wallet.balance == 'loading') return false;
return isEnough($scope.tx.value, $scope.wallet.balance);
};
$scope.generateTx = function() {
if (!$scope.Validator.isValidAddress($scope.tx.to)) {
$scope.notifier.danger(globalFuncs.errorMsgs[5]);
return;
}
var txData = uiFuncs.getTxData($scope);
if ($scope.tx.sendMode == 'token') {
// if the amount of tokens you are trying to send > tokens you have, throw error
if (
!isEnough(
$scope.tx.value,
$scope.wallet.tokenObjs[$scope.tokenTx.id].balance
)
) {
$scope.notifier.danger(globalFuncs.errorMsgs[0]);
return;
}
txData.to = $scope.wallet.tokenObjs[
$scope.tokenTx.id
].getContractAddress();
txData.data = $scope.wallet.tokenObjs[$scope.tokenTx.id].getData(
$scope.tokenTx.to,
$scope.tokenTx.value
).data;
txData.value = '0x00';
}
uiFuncs.generateTx(txData, function(rawTx) {
if (!rawTx.isError) {
$scope.rawTx = rawTx.rawTx;
$scope.signedTx = rawTx.signedTx;
$scope.showRaw = true;
} else {
$scope.showRaw = false;
$scope.notifier.danger(rawTx.error);
}
if (!$scope.$$phase) $scope.$apply();
});
};
$scope.sendTx = function() {
$scope.sendTxModal.close();
uiFuncs.sendTx($scope.signedTx, function(resp) {
if (!resp.isError) {
var bExStr = $scope.ajaxReq.type != nodes.nodeTypes.Custom
? "View TX
"
: '';
var emailLink =
'Confused? Email Us.';
$scope.notifier.success(
globalFuncs.successMsgs[2] +
resp.data +
'
' + bExStr + '
' + emailLink + '
' ); $scope.wallet.setBalance(applyScope); if ($scope.tx.sendMode == 'token') $scope.wallet.tokenObjs[$scope.tokenTx.id].setBalance(); } else { $scope.notifier.danger(resp.error); } }); }; $scope.transferAllBalance = function() { if ($scope.tx.sendMode != 'token') { uiFuncs.transferAllBalance( $scope.wallet.getAddressString(), $scope.tx.gasLimit, function(resp) { if (!resp.isError) { $scope.tx.unit = resp.unit; $scope.tx.value = resp.value; } else { $scope.showRaw = false; $scope.notifier.danger(resp.error); } } ); } else { $scope.tx.value = $scope.wallet.tokenObjs[$scope.tokenTx.id].getBalance(); } }; }; module.exports = sendTxCtrl;