-
Notifications
You must be signed in to change notification settings - Fork 116
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[x/programs] Add balance handling (#1086)
* add balance host functions
- Loading branch information
1 parent
82fa13c
commit 07b3ad2
Showing
23 changed files
with
368 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package cmd | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestPrefix(t *testing.T) { | ||
require := require.New(t) | ||
stateKey := accountDataKey([]byte{0}, []byte{1, 2, 3}) | ||
require.Equal([]byte{accountPrefix, 0, accountDataPrefix, 1, 2, 3}, stateKey) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// Copyright (C) 2024, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package runtime | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/ava-labs/hypersdk/codec" | ||
) | ||
|
||
const ( | ||
sendBalanceCost = 10000 | ||
getBalanceCost = 10000 | ||
) | ||
|
||
type transferBalanceInput struct { | ||
To codec.Address | ||
Amount uint64 | ||
} | ||
|
||
func NewBalanceModule() *ImportModule { | ||
return &ImportModule{ | ||
Name: "balance", | ||
HostFunctions: map[string]HostFunction{ | ||
"get": {FuelCost: getBalanceCost, Function: Function[codec.Address, uint64](func(callInfo *CallInfo, address codec.Address) (uint64, error) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
return callInfo.State.GetBalance(ctx, address) | ||
})}, | ||
"send": {FuelCost: sendBalanceCost, Function: Function[transferBalanceInput, Result[Unit, ProgramCallErrorCode]](func(callInfo *CallInfo, input transferBalanceInput) (Result[Unit, ProgramCallErrorCode], error) { | ||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
err := callInfo.State.TransferBalance(ctx, callInfo.Program, input.To, input.Amount) | ||
if err != nil { | ||
if extractedError, ok := ExtractProgramCallErrorCode(err); ok { | ||
return Err[Unit, ProgramCallErrorCode](extractedError), nil | ||
} | ||
return Err[Unit, ProgramCallErrorCode](ExecutionFailure), err | ||
} | ||
return Ok[Unit, ProgramCallErrorCode](Unit{}), nil | ||
})}, | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright (C) 2023, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package runtime | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
|
||
"github.com/ava-labs/avalanchego/ids" | ||
"github.com/stretchr/testify/require" | ||
|
||
"github.com/ava-labs/hypersdk/codec" | ||
"github.com/ava-labs/hypersdk/x/programs/test" | ||
) | ||
|
||
func TestImportBalanceGetBalance(t *testing.T) { | ||
require := require.New(t) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
actor := codec.CreateAddress(0, ids.GenerateTestID()) | ||
program := newTestProgram(ctx, "balance") | ||
program.Runtime.StateManager.(test.StateManager).Balances[actor] = 3 | ||
result, err := program.WithActor(actor).Call("balance") | ||
require.NoError(err) | ||
require.Equal(uint64(3), into[uint64](result)) | ||
} | ||
|
||
func TestImportBalanceSend(t *testing.T) { | ||
require := require.New(t) | ||
|
||
ctx, cancel := context.WithCancel(context.Background()) | ||
defer cancel() | ||
actor := codec.CreateAddress(0, ids.GenerateTestID()) | ||
program := newTestProgram(ctx, "balance") | ||
program.Runtime.StateManager.(test.StateManager).Balances[program.Address] = 3 | ||
result, err := program.Call("send_balance", actor) | ||
require.NoError(err) | ||
require.True(into[bool](result)) | ||
|
||
result, err = program.WithActor(actor).Call("balance") | ||
require.NoError(err) | ||
require.Equal(uint64(1), into[uint64](result)) | ||
|
||
result, err = program.WithActor(program.Address).Call("balance") | ||
require.NoError(err) | ||
require.Equal(uint64(2), into[uint64](result)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -206,3 +206,5 @@ func (o Option[T]) Some() (T, bool) { | |
func (o Option[T]) None() bool { | ||
return o.isNone | ||
} | ||
|
||
type Unit struct{} |
Oops, something went wrong.