Skip to content

Commit

Permalink
Asset handling updates
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Sep 5, 2024
1 parent 1e532e0 commit a993fe8
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 39 deletions.
26 changes: 17 additions & 9 deletions convex-core/src/main/cvx/convex/asset/fungible.cvx
Original file line number Diff line number Diff line change
Expand Up @@ -274,9 +274,9 @@
0)))

(defn total-supply
^{:doc {:description "Gets the total supply of a fungible token. i.e. the sum of all balances."
:examples [{:code "(total-supply my-token)"}]
:signature [{:params [token amount]}]}}
^{:doc {:description "Gets the total supply of a fungible token. i.e. the sum of all balances. May return nil if not supported"
:examples [{:code "(total-supply my-token)"}]
:signature [{:params [token amount]}]}}
[token]
(query
(try
Expand All @@ -288,9 +288,9 @@
:examples [{:code "(balance my-token-address)"}]
:signature [{:params [token holder]}]}}
([token]
(call token (balance *address*)))
(recur token *address*))
([token holder]
(call token (balance holder))))
(query (call token (balance holder)))))

(defn burn
^{:doc {:description "Burns an amount of tokens for the given token, if allowed by the implementation. Amount must be non-negative and no greater than the caller's balance."
Expand All @@ -313,7 +313,15 @@
:examples [{:code "(transfer my-token-address my-friend 100)"}]
:signature [{:params [token target amount]}]}}
[token target amount]
;; TODO should take account of actors as recipients, similar to convex.asset?
(call token
(direct-transfer target
amount)))
(cond
;; First check if receiver has a callable receive-asset function. If so, use this
(callable? target 'receive-asset)
(do
(offer target token amopunt) ;; Offer correct quantity
(call target
(receive-asset token amount nil)))

;; An actor without a receive-asset function is not a valid receiver?
(actor? target) (fail :STATE "Target Actor does not have receive-asset function")

(call token (direct-transfer target amount))))
4 changes: 2 additions & 2 deletions convex-core/src/main/cvx/convex/torus/genesis-currencies.cvx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
[
["USDF" "Foundation US Dollar" "Convex Foundation USD" "$" 10000 2 1.0]
["GBPF" "Foundation Pound Sterling" "Convex Foundation GBP" "£" 10000 2 1.27]
["USDF" "Foundation USD" "Convex Foundation USD pre-payment credits" "$" 10000 2 1.0]
["GBPF" "Foundation GBP" "Convex Foundation GBP pre-payment credits" "£" 10000 2 1.27]
]
13 changes: 11 additions & 2 deletions convex-core/src/main/java/convex/core/init/Init.java
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,8 @@ private static State doActorDeploy(State s, String resource) {

private static State doCurrencyDeploy(State s, AVector<ACell> row) {
String symName = row.get(0).toString();
String name = row.get(1).toString();
String desc = row.get(2).toString();
double usdPrice = RT.jvm(row.get(6)); // Value in USD for currency, e.g. USD=1.0, GBP=1.3
long decimals = RT.jvm(row.get(5)); // Decimals for lowest currency unit, e.g. USD = 2
long usdValue=(Long) RT.jvm(row.get(4)); // USD value of liquidity in currency
Expand All @@ -397,11 +399,18 @@ private static State doCurrencyDeploy(State s, AVector<ACell> row) {
// CVX price for currency
double cvxPrice = usdPrice * 1000000000; // One CVX Gold = 1 USD in genesis
double cvx = cvxPrice * supply / subDivisions;


String metaString="{:name "+RT.print(name)+ ":desc "+RT.print(desc)+"}";

Context ctx = Context.create(s, GENESIS_ADDRESS);
ctx = ctx.eval(Reader
.read("(do (import convex.fungible :as fun) (deploy (fun/build-token {:supply " + supply + " :decimals "+decimals+"})))"));
.read("(do "
+ "(import convex.fungible :as fun) "
+ "(deploy "
+ "'(call *registry* (register "+metaString+"))"
+ "(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
12 changes: 12 additions & 0 deletions convex-core/src/main/java/convex/core/lang/RT.java
Original file line number Diff line number Diff line change
Expand Up @@ -1000,6 +1000,16 @@ public static AString print(ACell a, long limit) {
public static AString print(ACell a) {
return print(a,Constants.PRINT_LIMIT);
}

/**
* Prints a value after converting to appropriate CVM type
* @param o Any value to print
* @return
*/
public static AString print(Object o) {
ACell cell=cvm(o);
return print(cell);
}
/**
* Converts a value to a CVM String representation. Required to work for all
* valid Cells.
Expand Down Expand Up @@ -1865,4 +1875,6 @@ public static ATransaction ensureTransaction(ACell maybeTx) {
if (maybeTx instanceof ATransaction) return (ATransaction)maybeTx;
return null;
}


}
55 changes: 30 additions & 25 deletions convex-gui/src/main/java/convex/gui/components/BalanceLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,40 +61,26 @@ public void setBalance(long a) {

public void setDecimals(int decimals) {
this.decimals=decimals;
// reset balance to ensure correct display
refreshBalance();
}

public void setBalanceColour(Color c) {
this.balanceColour=c;
}

@Override
public Dimension getPreferredSize() {
Dimension d=super.getPreferredSize();
FontMetrics font=getFontMetrics(getFont());
int pw=font.charWidth('0')*(10+decimals);
if (d.width<pw) d.width=pw;
return d;
}

public void setBalance(AInteger a) {
if (Utils.equals(a, balance)) return;
try {
if (a==null) {
setText("- ");
balance=null;
return;
private synchronized void refreshBalance() {
try {
if (balance==null) {
super.setText("- ");
return;
} else {
balance=a;
// Clear balance for appends
super.setText("");
}

int size=getFont().getSize();

BigInteger unit=getUnit(decimals);
BigInteger bi=a.big();
BigInteger bi=balance.big();
BigInteger change=bi.remainder(unit);
BigInteger coins=bi.divide(unit);

setText("");
AttributeSet attribs = new SimpleAttributeSet();
attribs=styleContext.addAttribute(attribs, StyleConstants.Alignment, StyleConstants.ALIGN_RIGHT);
attribs=styleContext.addAttribute(attribs, StyleConstants.FontFamily, getFont().getFamily());
Expand All @@ -121,10 +107,29 @@ public void setBalance(AInteger a) {
Toolkit.addPopupMenu(this, new BalanceMenu());
} catch (NullPointerException e) {
e.printStackTrace();
setText(e.getMessage());
super.setText(e.getMessage());
balance=null;
}
}

public void setBalanceColour(Color c) {
this.balanceColour=c;
}

@Override
public Dimension getPreferredSize() {
Dimension d=super.getPreferredSize();
FontMetrics font=getFontMetrics(getFont());
int pw=font.charWidth('0')*(10+decimals);
if (d.width<pw) d.width=pw;
return d;
}

public void setBalance(AInteger a) {
if (Utils.equals(a, balance)) return;
balance=a;
refreshBalance();
}

public AInteger getBalance() {
return balance;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ public TokenInfoPanel(Convex convex, TokenInfo token) {
supplyLabel.setDecimals(0);
} else {
AInteger decs=RT.ensureInteger(r.getValue());
decimalsLabel.setText(RT.toString(decs));
supplyLabel.setDecimals((int)decs.longValue());
decimalsLabel.setText(RT.toString(decs));
}
});
}
Expand Down

0 comments on commit a993fe8

Please sign in to comment.