Skip to content

Commit

Permalink
Add account[addr].assetBalance() test
Browse files Browse the repository at this point in the history
  • Loading branch information
algorandskiy authored and pzbitskiy committed Apr 8, 2022
1 parent 3ee12d1 commit aa49f98
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 34 deletions.
7 changes: 7 additions & 0 deletions GUIDE.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,13 @@ There are 9 builtin objects:
Starting with TEAL v5 assets and apps can be specified by both index and absolute value (but must appear in `ForeignApps`/ `ForeignAssets`).
The same rule is applicable to account addresses - it can be either an index or an address (but must appear in `Accounts`).
For example, to obtain an _application account_ balance (of asset balance), one can write
```
let asset = 100;
let appAddr = global.CurrentApplicationAddress
let balance, ok = accounts[appAddr].acctBalance()
let amount, exist = accounts[appAddr].assetBalance(asset);
```
#### Transaction fields
Expand Down
6 changes: 6 additions & 0 deletions compiler/codegen_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -927,6 +927,7 @@ function approval() {
let asset = 100;
let acc = 1;
let amount, exist = accounts[acc].assetBalance(asset);
amount, exist = accounts[global.CurrentApplicationAddress].assetBalance(asset);
return exist;
}
`
Expand All @@ -946,6 +947,11 @@ load 0
asset_holding_get AssetBalance
store 2
store 3
global CurrentApplicationAddress
load 0
asset_holding_get AssetBalance
store 2
store 3
load 2
return
end_main:
Expand Down
16 changes: 16 additions & 0 deletions test/misc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package test

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestByteArith(t *testing.T) {
Expand Down Expand Up @@ -72,3 +74,17 @@ function logic() {
`
performTest(t, source)
}

func TestAppAddress(t *testing.T) {
source := `
function approval() {
let asset = 100;
let acc = 1;
let amount, exist = accounts[acc].assetBalance(asset);
amount, exist = accounts[global.CurrentApplicationAddress].assetBalance(asset);
return exist;
}
`
ops := compileTest(t, source)
require.NotEmpty(t, ops.Program)
}
34 changes: 0 additions & 34 deletions test/tuple_test.go
Original file line number Diff line number Diff line change
@@ -1,43 +1,9 @@
package test

import (
"fmt"
"strings"
"testing"

"github.com/algorand/go-algorand/data/transactions/logic"
"github.com/stretchr/testify/require"

"github.com/pzbitskiy/tealang/compiler"
"github.com/pzbitskiy/tealang/dryrun"
)

func compileTest(t *testing.T, source string) *logic.OpStream {
t.Helper()
a := require.New(t)
result, errors := compiler.Parse(source)
a.NotEmpty(result, errors)
a.Empty(errors)

teal := compiler.Codegen(result)
op, err := logic.AssembleString(teal)
a.NoError(err)
return op
}

func performTest(t *testing.T, source string) {
t.Helper()
a := require.New(t)
op := compileTest(t, source)

sb := strings.Builder{}
pass, err := dryrun.Run(op.Program, "", &sb)
fmt.Printf("trace:\n%s\n", sb.String())

a.NoError(err)
a.True(pass)
}

func TestAddw(t *testing.T) {
source := `
function logic() {
Expand Down
38 changes: 38 additions & 0 deletions test/util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package test

import (
"fmt"
"strings"
"testing"

"github.com/algorand/go-algorand/data/transactions/logic"
"github.com/pzbitskiy/tealang/compiler"
"github.com/pzbitskiy/tealang/dryrun"
"github.com/stretchr/testify/require"
)

func compileTest(t *testing.T, source string) *logic.OpStream {
t.Helper()
a := require.New(t)
result, errors := compiler.Parse(source)
a.NotEmpty(result, errors)
a.Empty(errors)

teal := compiler.Codegen(result)
op, err := logic.AssembleString(teal)
a.NoError(err)
return op
}

func performTest(t *testing.T, source string) {
t.Helper()
a := require.New(t)
op := compileTest(t, source)

sb := strings.Builder{}
pass, err := dryrun.Run(op.Program, "", &sb)
fmt.Printf("trace:\n%s\n", sb.String())

a.NoError(err)
a.True(pass)
}

0 comments on commit aa49f98

Please sign in to comment.