mirror of
https://github.com/hanzo-labs/arcadia.git
synced 2026-10-30 18:55:41 +00:00
245 lines
12 KiB
JavaScript
245 lines
12 KiB
JavaScript
'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 = ''
|
|
|
|
$scope.customGas = CustomGasMessages
|
|
|
|
$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.addressDrtv.ensAddressField = $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.addressDrtv.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;
|
|
}
|
|
if (newValue.to !== oldValue.to) {
|
|
for (var i in $scope.customGas) {
|
|
if ($scope.tx.to.toLowerCase() == $scope.customGas[i].to.toLowerCase()) {
|
|
$scope.customGasMsg = $scope.customGas[i].msg != '' ? $scope.customGas[i].msg : ''
|
|
return;
|
|
}
|
|
}
|
|
|
|
$scope.customGasMsg = ''
|
|
}
|
|
}, 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) {
|
|
|
|
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.onDonateClick = function() {
|
|
$scope.addressDrtv.ensAddressField = globalFuncs.donateAddress;
|
|
$scope.tx.value = "1";
|
|
$scope.tx.donate = true;
|
|
}
|
|
$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 txHashLink = $scope.ajaxReq.blockExplorerTX.replace("[[txHash]]", resp.data)
|
|
var bExStr = $scope.ajaxReq.type != nodes.nodeTypes.Custom ? "<a class='btn btn-xs btn-info' href='" + txHashLink + "' class='strong' target='_blank' rel='noopener'>Verify Transaction</a>" : '';
|
|
var emailLink = '<a class="btn btn-xs btn-info" href="mailto:[email protected]?Subject=Issue%20regarding%20my%20TX%20&Body=%0A%0AI%20was%20trying%20to..............%0A%0A%0A%0ABut%20I%27m%20confused%20because...............%0A%0A%0A%0A%0A' + "%0ATo%20Address%3A%20https%3A%2F%2Fetherscan.io%2Faddress%2F" + $scope.tx.to + "%0A%0AFrom%20Address%3A%20https%3A%2F%2Fetherscan.io%2Faddress%2F" + $scope.wallet.getAddressString() + "%0A%0ATX%20Hash%3A%20https%3A%2F%2Fetherscan.io%2Ftx%2F" + resp.data + "%0A%0AAmount%3A%20" + $scope.tx.value + "%20" + $scope.unitReadable + "%0ANode%3A%20" + $scope.ajaxReq.type + "%0AToken%20To%20Addr%3A%20" + $scope.tokenTx.to + "%0AToken%20Amount%3A%20" + $scope.tokenTx.value + "%20" + $scope.unitReadable + "%0AData%3A%20" + $scope.tx.data + "%0AGas%20Limit%3A%20" + $scope.tx.gasLimit + "%0AGas%20Price%3A%20" + $scope.tx.gasPrice + '" target="_blank" rel="noopener">Confused? Email Us.</a>';
|
|
$scope.notifier.success("<p>" + globalFuncs.successMsgs[2] + '<strong>' + resp.data + "</strong></p><p>" + bExStr + " " + emailLink + "</p>");
|
|
$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;
|