Skip to content

Commit

Permalink
Add decimals support in wallet token info
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed May 5, 2024
1 parent 935ab67 commit ee26c39
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 7 deletions.
2 changes: 1 addition & 1 deletion convex-core/src/main/java/convex/core/init/Init.java
Original file line number Diff line number Diff line change
Expand Up @@ -380,7 +380,7 @@ private static State doCurrencyDeploy(State s, AVector<ACell> 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) + "))"));
Expand Down
20 changes: 20 additions & 0 deletions convex-gui/src/main/java/convex/gui/wallet/AccountOverview.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();

Expand Down Expand Up @@ -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();
}
}
}
}
29 changes: 27 additions & 2 deletions convex-gui/src/main/java/convex/gui/wallet/TokenInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -27,7 +31,7 @@ public String symbol() {
}

public int decimals() {
return (id==null)?9:2;
return decimals;
}

public static TokenInfo forID(ACell tokenID) {
Expand Down Expand Up @@ -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;
}
}
11 changes: 7 additions & 4 deletions convex-gui/src/main/java/convex/gui/wallet/WalletPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand All @@ -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);
Expand All @@ -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()) {
Expand Down

0 comments on commit ee26c39

Please sign in to comment.