From 42dec3690cb738005d0524ce7c709311b4d6f80a Mon Sep 17 00:00:00 2001 From: mikera Date: Fri, 15 Dec 2023 13:44:27 +0000 Subject: [PATCH] Modify get-holding to return null for non-existent accounts --- convex-core/src/main/cvx/convex/asset.cvx | 2 +- convex-core/src/main/java/convex/core/lang/Core.java | 3 ++- convex-core/src/test/java/convex/core/lang/CoreTest.java | 4 +++- convex-core/src/test/java/convex/lib/AssetTester.java | 1 + 4 files changed, 7 insertions(+), 3 deletions(-) diff --git a/convex-core/src/main/cvx/convex/asset.cvx b/convex-core/src/main/cvx/convex/asset.cvx index 763949a9d..be1db074d 100644 --- a/convex-core/src/main/cvx/convex/asset.cvx +++ b/convex-core/src/main/cvx/convex/asset.cvx @@ -192,7 +192,7 @@ (recur path *address*)) ([path owner] - (query ;; external call, so uery to avoid re-entrant secuity risks + (query ;; external call, so use query to avoid re-entrant secuity risks (call path (balance owner))))) (defn burn diff --git a/convex-core/src/main/java/convex/core/lang/Core.java b/convex-core/src/main/java/convex/core/lang/Core.java index 4f811c212..41281c7d5 100644 --- a/convex-core/src/main/java/convex/core/lang/Core.java +++ b/convex-core/src/main/java/convex/core/lang/Core.java @@ -1174,7 +1174,8 @@ public Context invoke(Context context, ACell[] args) { if (address == null) return context.withCastError(args[0], Types.ADDRESS); AccountStatus as=context.getAccountStatus(address); - if (as==null) return context.withError(ErrorCodes.NOBODY,"Account "+address+" does not exist to get holdings"); + if (as==null) return context.withResult(Juice.LOOKUP, null); + BlobMap holdings=as.getHoldings(); // we get the target accounts holdings for the currently executing account diff --git a/convex-core/src/test/java/convex/core/lang/CoreTest.java b/convex-core/src/test/java/convex/core/lang/CoreTest.java index 3eb0780a3..3ddba6380 100644 --- a/convex-core/src/test/java/convex/core/lang/CoreTest.java +++ b/convex-core/src/test/java/convex/core/lang/CoreTest.java @@ -4680,7 +4680,9 @@ public void testSpecialEdgeCases() { assertNull(eval(ctx,"(get-holding VILLAIN)")); assertCastError(step(ctx,"(get-holding :foo)")); assertCastError(step(ctx,"(get-holding nil)")); - assertNobodyError(step(ctx,"(get-holding NOONE)")); + + // NOTE: holdings are nil for non-existent accounts, better for default behaviour + assertNull(eval(ctx,"(get-holding NOONE)")); // OK to set holding for a real owner account assertEquals(100L,evalL(ctx,"(set-holding VILLAIN 100)")); diff --git a/convex-core/src/test/java/convex/lib/AssetTester.java b/convex-core/src/test/java/convex/lib/AssetTester.java index 47d4d5b3b..9e3103a7b 100644 --- a/convex-core/src/test/java/convex/lib/AssetTester.java +++ b/convex-core/src/test/java/convex/lib/AssetTester.java @@ -54,6 +54,7 @@ public static void doFungibleTests (Context ctx, ACell token, Address user) { assertEquals(0L,evalL(ctx,"(asset/balance token (deploy nil))")); assertEquals(0L,evalL(ctx,"(asset/balance token (create-account *key*))")); assertEquals(0L,evalL(ctx,"(asset/get-offer token (create-account *key*) (deploy nil))")); + assertEquals(0L,evalL(ctx,"(asset/balance token #675475647)")); } // New Address gets zero offers