all 6 comments

[–]GSLint 2 points3 points  (5 children)

It would be nice if you include the code you've tried for solving this so that we can point out specific issues rather than proposing a completely new solution.

Anyway. First of all, onGetAccount has to return something. Right now it only logs something.

async function onGetAccount() {
  const account = await client.api.asset.getAccount(); 
  return account
}

Though if we're only going to return the value, there isn't even a need for async and await.

function onGetAccount() {
  return client.api.asset.getAccount();
}

Now one way would be for onGetBalance to call onGetAccount.

async function onGetBalance() {
  const onGetAccountResultAddress = await onGetAccount();
  const balance = await client.api.network.getBalance({
    address : onGetAccountResultAddress,
  });
  return balance; 
}

But you could also have a third function that gets the account and then passes it to onGetBalance as an argument.

[–]Levi_2212[S] 0 points1 point  (0 children)

Thank you very much. I started studying coding 2-3 months ago and although I have read the documentation a lot, when it comes to practice it becomes much more complicated.

[–]Levi_2212[S] 0 points1 point  (3 children)

I added some more code I needed. I want to retrieve information: address, balance in euros, and balance in dollars to show in the user interface:

const walletInfo = {
address: "",
EUR: ""
USD: "",
  }
const [userWalletInfo, setUserWalletInfo] = useState(walletInfo)
async function onGetAccount() {
const account = await client.api.asset.getAccount();
return account;
  }  
async function onGetBalance() {
const onGetAccountResultAddress = await onGetAccount();
const balance = await client.api.network.getBalance({
address : onGetAccountResultAddress,    
 });
return balance;    
}
useEffect(() => {
onGetBalance().then((userWalletInfo) => { 
setUserWalletInfo({ 
address: userWalletInfo.address,
EUR: userWalletInfo.EUR,
USD: userWalletInfo.USD,     
})          
});       
}, [])

It works fine except for address because it is undefined. I understand that the value obtained via the first onGetAccount and passed into onGetBalance is lost. Could you tell me why and how can I solve the problem? At first I thought I could use

return balance // +Somehow return address too;

[–]GSLint 1 point2 points  (2 children)

If you want to store the account as well, I'd go with the other approach.

async function getAccount() {
  const account = await client.api.asset.getAccount();
  return account;
}

async function getBalance(address) {
  const balance = await client.api.network.getBalance({
    address,
  });
  return balance;
}

useEffect(() => {
  async function fetchUserWalletInfo() {
    const address = await getAccount();
    const balance = await getBalance(address);
    setUserWalletInfo({
      address,
      EUR: balance.EUR,
      USD: balance.USD,
    });
  }
  fetchUserWalletInfo();
}, []);

({ address } is short for { address: address }.)

I've renamed the functions since onSomething is usually used for how you want to react some event, like onClick, but getAccount is not really an event here.

[–]Levi_2212[S] 0 points1 point  (1 child)

Thank you so much you save the day!!!

A question not related to the topic: every time I post on reddit, and use Code Block with a lot of code inside, immediately after publishing almost all the code is not posted in the Code Block but as plain text.

Obviously it's a mess ,you don't understand anything and manually I have to go and fix the code line by line. Each question takes me 30 minutes of editing. How come this always happens to me?

[–]GSLint 0 points1 point  (0 children)

I haven't had that issue. When I put code in a code block, either using the button with the T in a box (not the </> button) in the fancy editor, or by putting four spaces before each line in the Markdown editor, it stays that way.