forked from gagliardetto/solana-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Create_test.go
76 lines (60 loc) · 1.88 KB
/
Create_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
package associatedtokenaccount
import (
"testing"
bin "github.com/gagliardetto/binary"
solana "github.com/gagliardetto/solana-go"
"github.com/test-go/testify/assert"
"github.com/test-go/testify/require"
)
func TestCreateNonIdempotentData(t *testing.T) {
t.Parallel()
wallet, err := solana.NewRandomPrivateKey()
require.NoError(t, err)
c := NewCreateInstruction(
wallet.PublicKey(),
wallet.PublicKey(),
solana.MPK("G8iheDY9bGix5qCXEitCExLcgZzZrEemngk9cbTR3CQs"),
solana.MustPublicKeyFromBase58("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"),
false,
)
data, err := c.Build().Data()
require.NoError(t, err)
assert.Len(t, data, 0)
}
func TestCreateIdempotentData(t *testing.T) {
t.Parallel()
wallet, err := solana.NewRandomPrivateKey()
require.NoError(t, err)
c := NewCreateInstruction(
wallet.PublicKey(),
wallet.PublicKey(),
solana.MPK("G8iheDY9bGix5qCXEitCExLcgZzZrEemngk9cbTR3CQs"),
solana.MustPublicKeyFromBase58("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"),
true,
)
data, err := c.Build().Data()
require.NoError(t, err)
assert.Equal(t, []byte{1}, data)
}
func TestEncodeRoundtrip(t *testing.T) {
t.Parallel()
wallet, err := solana.NewRandomPrivateKey()
require.NoError(t, err)
c := NewCreateInstruction(
wallet.PublicKey(),
wallet.PublicKey(),
solana.MPK("G8iheDY9bGix5qCXEitCExLcgZzZrEemngk9cbTR3CQs"),
solana.MustPublicKeyFromBase58("TokenzQdBNbLqP5VEhdkAS6EPFLC1PHnBqCXEpPxuEb"),
true,
).Build()
blockhash := solana.MustHashFromBase58("AnL7vVGidfdxZBFqkxLvVwXgW9pDSZC5kK6d5kyRaXEa")
tx, err := solana.NewTransaction([]solana.Instruction{c}, blockhash)
require.NoError(t, err)
data, err := tx.MarshalBinary()
require.NoError(t, err)
decoded := &solana.Transaction{}
err = decoded.UnmarshalWithDecoder(bin.NewCompactU16Decoder(data))
require.NoError(t, err)
err = tx.Message.AssertEquivalent(decoded.Message)
require.NoError(t, err)
}