From a993fe8217e5c45f8463f0f469e8f8a0147d9dde Mon Sep 17 00:00:00 2001 From: mikera Date: Thu, 5 Sep 2024 10:17:48 +0100 Subject: [PATCH] Asset handling updates --- .../src/main/cvx/convex/asset/fungible.cvx | 26 ++++++--- .../cvx/convex/torus/genesis-currencies.cvx | 4 +- .../src/main/java/convex/core/init/Init.java | 13 ++++- .../src/main/java/convex/core/lang/RT.java | 12 ++++ .../convex/gui/components/BalanceLabel.java | 55 ++++++++++--------- .../convex/gui/wallet/TokenInfoPanel.java | 2 +- 6 files changed, 73 insertions(+), 39 deletions(-) diff --git a/convex-core/src/main/cvx/convex/asset/fungible.cvx b/convex-core/src/main/cvx/convex/asset/fungible.cvx index b40df26ad..880e81b42 100644 --- a/convex-core/src/main/cvx/convex/asset/fungible.cvx +++ b/convex-core/src/main/cvx/convex/asset/fungible.cvx @@ -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 @@ -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." @@ -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)))) diff --git a/convex-core/src/main/cvx/convex/torus/genesis-currencies.cvx b/convex-core/src/main/cvx/convex/torus/genesis-currencies.cvx index 102d9cfb3..dbd96d27b 100644 --- a/convex-core/src/main/cvx/convex/torus/genesis-currencies.cvx +++ b/convex-core/src/main/cvx/convex/torus/genesis-currencies.cvx @@ -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] ] \ No newline at end of file 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 cc8951cb9..b8a8fece0 100644 --- a/convex-core/src/main/java/convex/core/init/Init.java +++ b/convex-core/src/main/java/convex/core/init/Init.java @@ -383,6 +383,8 @@ private static State doActorDeploy(State s, String resource) { private static State doCurrencyDeploy(State s, AVector 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 @@ -397,11 +399,18 @@ private static State doCurrencyDeploy(State s, AVector 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) + "))")); diff --git a/convex-core/src/main/java/convex/core/lang/RT.java b/convex-core/src/main/java/convex/core/lang/RT.java index d3573fcdb..8c7144f8b 100644 --- a/convex-core/src/main/java/convex/core/lang/RT.java +++ b/convex-core/src/main/java/convex/core/lang/RT.java @@ -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. @@ -1865,4 +1875,6 @@ public static ATransaction ensureTransaction(ACell maybeTx) { if (maybeTx instanceof ATransaction) return (ATransaction)maybeTx; return null; } + + } diff --git a/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java b/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java index e53006d49..4cf8bec06 100644 --- a/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java +++ b/convex-gui/src/main/java/convex/gui/components/BalanceLabel.java @@ -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