I am looking to store the output of these functions in a variable so that I can reuse them later in my code
function sellPrice() {
contract.sellPrice(function(err, result) {
if(err) {
console.log(err, 'err');
} else {
document.getElementById('sellPrice').innerHTML = result/100000000;
}
});
} sellPrice();
However, I can only access the (result) inside the function and would like to use it further down in my code.
Is there a way for me to access the result of sellFunction()?
Below is my code:
const address = '0xE462CbEE0cd420f6c199B0194B1D8D93Fb5e7720';
// GLOBALS
const web3Mode = null
const walletMode = 'connect'
const currentAddress = null
const keystore = null
const dividendValue = 0
const tokenBalance = 0
//const contract = null
const abi = [{"constant":true,"inputs":[{"name":"_customerAddress","type":"address"}],"name":"dividendsOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_agiToSpend","type":"uint256"}],"name":"calculateTokensReceived","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_tokensToSell","type":"uint256"}],"name":"calculateAgiReceived","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"onlyAmbassadors","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs...
const contract = web3.eth.contract(abi).at(address);
window.addEventListener('load', async function() {
// Wait for loading completion to avoid race conditions with web3 injection timing.
var web3;
var globalState = {};
console.log(web3, 'web3');
if(window.ethereum) {
web3 = new Web3(window['ethereum']);
try {
// Request account access if needed
await window.ethereum.enable();
web3.eth.getAccounts(function (error, accounts) {
document.getElementById('account-address').innerHTML = accounts[0];
});
// Acccounts now exposed
window.ethereum.on('accountsChanged', function () {
web3.eth.getAccounts(function (error, accounts) {
document.getElementById('account-address').innerHTML = accounts[0];
});
window.ethereum.on('connect', function () {
// console.log('connect');
});
});
} catch (error) {
console.error(error);
}
} else if (window.web3) {
console.log(2);
// Legacy dapp browsers...
// Use Mist/MetaMask's provider.
const web3 = window.web3;
} else {
console.log(3);
// Fallback to localhost; use dev console port by default...
const provider = new Web3.providers.HttpProvider('https://ropsten.infura.io/v3/9bee77f147884c73bb2852e269dacece');
web3 = new Web3(provider);
}
function sellPrice() {
contract.sellPrice(function(err, result) {
if(err) {
console.log(err, 'err');
} else {
document.getElementById('sellPrice').innerHTML = result/100000000;
}
});
}
function buyPrice() {
contract.buyPrice(function(err, result) {
if(err) {
console.log(err, 'err');
} else {
document.getElementById('buyPrice').innerHTML = result/100000000;
}
});
}
buyPrice();
function totalSupply() {
contract.totalSupply(function(err, result) {
if(err) {
console.log(err, 'err');
} else {
document.getElementById('contractBalanceSnet').innerHTML = result/100000000 + ' SNET';
}
});
}
totalSupply();
function balanceOf() {
web3.eth.getAccounts(function (error, accounts) {
contract.balanceOf(accounts[0], function(err, result) {
if(err) {
console.log(err, 'err');
} else {
document.getElementById('snet-holding').innerHTML = result/100000000;
console.log(result);
}
});
});
}
balanceOf();
function totalAgiBalance() {
web3.eth.getAccounts(function (error, accounts) {
contract.totalAgiBalance( function(err, result) {
if(err) {
console.log(err, 'err');
} else {
document.getElementById('agiContractBalance').innerHTML = result/100000000 + ' AGI';
console.log(result);
}
});
});
}
totalAgiBalance();
function myDividends() {
web3.eth.getAccounts(function (error, accounts) {
contract.myDividends(accounts[0], function(err, result) {
if(err) {
console.log(err, 'err');
} else {
document.getElementById('myDividends').innerHTML = result/100000000 + ' AGI';
console.log(result);
}
});
});
}
myDividends();
}); //end of first stuff
var tokenAddress = '0xb97E9bBB6fd49865709d3F1576e8506ad640a13B';
var walletAddress = '0x62f28320f688A7A4e0021c55d7ffD1acd770A133';
function getERC20TokenBalance() {
let minABI = [
// balanceOf
{
"constant":true,
"inputs":[{"name":"_owner","type":"address"}],
"name":"balanceOf",
"outputs":[{"name":"balance","type":"uint256"}],
"type":"function"
},
// decimals
{
"constant":true,
"inputs":[],
"name":"decimals",
"outputs":[{"name":"","type":"uint8"}],
"type":"function"
}
];
let contract2 = web3.eth.contract(minABI).at(tokenAddress);
web3.eth.getAccounts(function (error, walletAddress) {
contract2.balanceOf(walletAddress, function(err, result) {
if(err) {
console.log(err, 'err');
} else {
console.log(result);
document.getElementById('agiAvailable').innerHTML = result + ' AGI';
}
});
});
}getERC20TokenBalance();
function onAddressChange(e) {
if(tokenAddress != "" && walletAddress != "") {
getERC20TokenBalance(tokenAddress, walletAddress, (balance) => {
console.log(balance.toString());
});
}
}
I hope to achieve something similar to this:
function sellPrice() {
contract.sellPrice(function(err, result) {
if(err) {
console.log(err, 'err');
} else {
return (result);
}
});
}
document.getElementById('sellPrice').innerHTML = sellPrice();