diff --git a/convex-core/src/main/java/convex/core/init/Init.java b/convex-core/src/main/java/convex/core/init/Init.java index 6e9881b7b..2e5bd23da 100644 --- a/convex-core/src/main/java/convex/core/init/Init.java +++ b/convex-core/src/main/java/convex/core/init/Init.java @@ -380,7 +380,7 @@ private static State doCurrencyDeploy(State s, AVector row) { Context ctx = Context.createFake(s, DISTRIBUTION_ADDRESS); ctx = ctx.eval(Reader - .read("(do (import convex.fungible :as fun) (deploy (fun/build-token {:supply " + supply + "})))")); + .read("(do (import convex.fungible :as fun) (deploy (fun/build-token {:supply " + supply + " :decimals "+decimals+"})))")); Address addr = ctx.getResult(); ctx = ctx.eval(Reader.read("(do (import torus.exchange :as torus) (torus/add-liquidity " + addr + " " + (supply / 2) + " " + (cvx / 2) + "))")); diff --git a/convex-gui/src/main/java/convex/gui/wallet/AccountOverview.java b/convex-gui/src/main/java/convex/gui/wallet/AccountOverview.java index 6c639cb14..cfaaf8f78 100644 --- a/convex-gui/src/main/java/convex/gui/wallet/AccountOverview.java +++ b/convex-gui/src/main/java/convex/gui/wallet/AccountOverview.java @@ -7,6 +7,7 @@ import convex.api.Convex; import convex.core.lang.RT; +import convex.core.util.ThreadUtils; import convex.gui.components.BalanceLabel; import convex.gui.utils.SymbolIcon; import net.miginfocom.swing.MigLayout; @@ -15,9 +16,11 @@ public class AccountOverview extends JPanel { final BalanceLabel balance=new BalanceLabel(); + private Convex convex; public AccountOverview(Convex convex) { + this.convex=convex; setLayout(new MigLayout("fill","","push[][]")); Font font=this.getFont(); @@ -67,5 +70,22 @@ public AccountOverview(Convex convex) { add(balance); } //add(KeyPairCombo.forConvex(convex)); + + ThreadUtils.runVirtual(this::updateLoop); + } + + private void updateLoop() { + while (true) { + try { + if (isShowing()) { + balance.setBalance(convex.getBalance()); + } + Thread.sleep(1000); + } catch (InterruptedException e) { + return; + } catch (Exception e) { + e.printStackTrace(); + } + } } } diff --git a/convex-gui/src/main/java/convex/gui/wallet/TokenInfo.java b/convex-gui/src/main/java/convex/gui/wallet/TokenInfo.java index 24d720de6..558ef9544 100644 --- a/convex-gui/src/main/java/convex/gui/wallet/TokenInfo.java +++ b/convex-gui/src/main/java/convex/gui/wallet/TokenInfo.java @@ -10,12 +10,16 @@ import convex.core.data.Cells; import convex.core.data.prim.AInteger; import convex.core.data.prim.CVMLong; +import convex.core.lang.RT; +import convex.core.util.Utils; public class TokenInfo { private ACell id; + private int decimals; - public TokenInfo(ACell tokenID) { + private TokenInfo(ACell tokenID) { this.id=tokenID; + this.decimals=(id==null)?9:2; } public ACell getID() { @@ -27,7 +31,7 @@ public String symbol() { } public int decimals() { - return (id==null)?9:2; + return decimals; } public static TokenInfo forID(ACell tokenID) { @@ -70,4 +74,25 @@ private static Address getFungibleAddress(Convex convex) throws TimeoutException fungibleAddress=convex.querySync("(import convex.fungible)").getValue(); return fungibleAddress; } + + public static TokenInfo get(Convex convex, ACell tokenID) { + TokenInfo tokenInfo=new TokenInfo(tokenID); + if (tokenID==null) return tokenInfo; // Convex coins + + // We need to get token info + try { + Result r=convex.querySync("("+getFungibleAddress(convex)+"/decimals "+tokenID+")"); + if (r.isError()) { + System.err.println("Dubious Token: "+r.toString()); + return null; + } + CVMLong decimals=RT.ensureLong(r.getValue()); + if (decimals==null) return null; + tokenInfo.decimals=Utils.checkedInt(decimals.longValue()); + return tokenInfo; + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } } \ No newline at end of file diff --git a/convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java b/convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java index 8a4beef16..eb251355e 100644 --- a/convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java +++ b/convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java @@ -36,7 +36,7 @@ public WalletPanel(Convex convex) { add(list,"dock center"); // add(new AccountOverview(convex),"dock north"); - model.addElement(new TokenInfo(null)); + model.addElement(TokenInfo.get(convex,null)); // add(new AccountChooserPanel(convex),"dock south"); @@ -46,8 +46,10 @@ public WalletPanel(Convex convex) { if (newID==null) return; try { ACell tokenID=newID.isBlank()?null:Reader.read(newID); - TokenInfo token=TokenInfo.forID(tokenID); - if (model.contains(token)) { + TokenInfo token=TokenInfo.get(convex,tokenID); + if (token==null) { + Toast.display(WalletPanel.this, "Token does not exist: "+tokenID,Color.ORANGE); + } else if (model.contains(token)) { Toast.display(WalletPanel.this, "Token already added",Color.ORANGE); } else { model.addElement(token); @@ -64,10 +66,11 @@ public WalletPanel(Convex convex) { ThreadUtils.runVirtual(this::updateLoop); } - public void updateLoop() { + private void updateLoop() { while (true) { try { if (isShowing()) { + Component[] comps=list.getListComponents(); for (Component c: comps) { if ((c instanceof TokenComponent)&&c.isShowing()) {