Skip to content

Commit

Permalink
Support burning coins into account #0
Browse files Browse the repository at this point in the history
  • Loading branch information
mikera committed Oct 4, 2024
1 parent 0287ac8 commit c9e256b
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 12 deletions.
24 changes: 12 additions & 12 deletions convex-core/src/main/java/convex/core/lang/Context.java
Original file line number Diff line number Diff line change
Expand Up @@ -1470,7 +1470,8 @@ public Context transfer(Address target, long amount) {
}
AccountStatus targetAccount=accounts.get(targetIndex);

if (targetAccount.isActor()) {
// Special handling for an actor account other than #0 (burn address)
if (targetAccount.isActor()&&(!(target.longValue()==0))) {
// (call target amount (receive-coin source amount nil))
// SECURITY: actorCall must do fork to preserve this
Context actx=this.fork();
Expand All @@ -1479,19 +1480,18 @@ public Context transfer(Address target, long amount) {

long sent=currentBalance-actx.getBalance(source);
return actx.withResult(CVMLong.create(sent));
} else {
// must be a user account
long oldTargetBalance=targetAccount.getBalance();
long newTargetBalance=oldTargetBalance+amount;
AccountStatus newTargetAccount=targetAccount.withBalance(newTargetBalance);
accounts=accounts.assoc(targetIndex, newTargetAccount);

// SECURITY: new context with updated accounts
Context result=withChainState(chainState.withAccounts(accounts)).withResult(CVMLong.create(amount));
}

// must be a user account
long oldTargetBalance=targetAccount.getBalance();
long newTargetBalance=oldTargetBalance+amount;
AccountStatus newTargetAccount=targetAccount.withBalance(newTargetBalance);
accounts=accounts.assoc(targetIndex, newTargetAccount);

return result;
}
// SECURITY: new context with updated accounts
Context result=withChainState(chainState.withAccounts(accounts)).withResult(CVMLong.create(amount));

return result;
}

/**
Expand Down
11 changes: 11 additions & 0 deletions convex-core/src/test/java/convex/core/lang/CoreTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -3335,6 +3335,17 @@ public void testTransfer() {
assertArityError(step("(transfer 1)"));
assertArityError(step("(transfer 1 2 3)"));
}

@Test
public void testTransferBurn() {
Context ctx=context();
long supply=ctx.getState().computeSupply();
long AMT=1000000;

ctx=exec(ctx,"(transfer #0 "+AMT+")");

assertEquals(supply-AMT,ctx.getState().computeSupply());
}

@Test
public void testStake() {
Expand Down

0 comments on commit c9e256b

Please sign in to comment.