-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtransaction_test.go
80 lines (65 loc) · 2.7 KB
/
transaction_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package transaction
import (
"testing"
"github.com/mr-tron/base58/base58"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/code-payments/code-server/pkg/solana"
"github.com/code-payments/code-server/pkg/solana/system"
"github.com/code-payments/code-server/pkg/solana/token"
"github.com/code-payments/code-server/pkg/testutil"
"github.com/code-payments/code-server/pkg/code/common"
code_data "github.com/code-payments/code-server/pkg/code/data"
)
func TestTransaction_MakeNoncedTransaction_HappyPath(t *testing.T) {
subsidizer := testutil.SetupRandomSubsidizer(t, code_data.NewTestDataProvider())
nonceAccount, err := common.NewAccountFromPublicKeyString("non9MZDuwcTzNYfWFu18XT4MLi3Pf6vscuuMuKTbrTx")
require.NoError(t, err)
untypedBlockhash, err := base58.Decode("9eRZTogvYM4WC8PRrw27fpzcZTvEvQuaREQyRETyw46d")
require.NoError(t, err)
var typedBlockhash solana.Blockhash
copy(typedBlockhash[:], untypedBlockhash)
ixns := []solana.Instruction{
token.Transfer(
testutil.NewRandomAccount(t).PublicKey().ToBytes(),
testutil.NewRandomAccount(t).PublicKey().ToBytes(),
testutil.NewRandomAccount(t).PublicKey().ToBytes(),
1,
), token.Transfer(
testutil.NewRandomAccount(t).PublicKey().ToBytes(),
testutil.NewRandomAccount(t).PublicKey().ToBytes(),
testutil.NewRandomAccount(t).PublicKey().ToBytes(),
2,
),
token.Transfer(
testutil.NewRandomAccount(t).PublicKey().ToBytes(),
testutil.NewRandomAccount(t).PublicKey().ToBytes(),
testutil.NewRandomAccount(t).PublicKey().ToBytes(),
3,
),
}
txn, err := MakeNoncedTransaction(nonceAccount, typedBlockhash, ixns...)
require.NoError(t, err)
assert.Equal(t, typedBlockhash, txn.Message.RecentBlockhash)
assert.EqualValues(t, txn.Message.Accounts[0], subsidizer.PublicKey().ToBytes())
require.Len(t, txn.Message.Instructions, 4)
actual, err := system.DecompileAdvanceNonce(txn.Message, 0)
require.NoError(t, err)
assert.EqualValues(t, nonceAccount.PublicKey().ToBytes(), actual.Nonce)
assert.EqualValues(t, subsidizer.PublicKey().ToBytes(), actual.Authority)
for i := range ixns {
actual, err := token.DecompileTransfer(txn.Message, i+1)
require.NoError(t, err)
assert.EqualValues(t, i+1, actual.Amount)
}
}
func TestTransaction_MakeNoncedTransaction_NoInstructions(t *testing.T) {
nonceAccount, err := common.NewAccountFromPublicKeyString("non9MZDuwcTzNYfWFu18XT4MLi3Pf6vscuuMuKTbrTx")
require.NoError(t, err)
untypedBlockhash, err := base58.Decode("9eRZTogvYM4WC8PRrw27fpzcZTvEvQuaREQyRETyw46d")
require.NoError(t, err)
var typedBlockhash solana.Blockhash
copy(typedBlockhash[:], untypedBlockhash)
_, err = MakeNoncedTransaction(nonceAccount, typedBlockhash)
assert.Error(t, err)
}