-
Notifications
You must be signed in to change notification settings - Fork 1
/
Transfer.go
81 lines (70 loc) · 2.02 KB
/
Transfer.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
81
package native
import (
"encoding/binary"
"github.com/cielu/go-solana/common"
"github.com/cielu/go-solana/pkg/encodbin"
"github.com/cielu/go-solana/types/base"
)
type Transfer struct {
// Number of lamports to transfer to the new account
Lamports *uint64
// [0] = [WRITE, SIGNER] FundingAccount
// ··········· Funding account
//
// [1] = [WRITE] RecipientAccount
// ··········· Recipient account
base.AccountMetaSlice `bin:"-" borsh_skip:"true"`
Signers []*base.AccountMeta `bin:"-" borsh_skip:"true"`
}
func NewTransferInstructionBuilder() *Transfer {
nd := &Transfer{
AccountMetaSlice: make([]*base.AccountMeta, 2),
}
return nd
}
func (trans Transfer) GetAccounts() (accounts []*base.AccountMeta) {
accounts = append(accounts, trans.AccountMetaSlice...)
accounts = append(accounts, trans.Signers...)
return
}
func (trans *Transfer) SetLamports(lamports uint64) *Transfer {
trans.Lamports = &lamports
return trans
}
// Funding account
func (trans *Transfer) SetFundingAccount(fundingAccount common.Address) *Transfer {
trans.AccountMetaSlice[0] = base.Meta(fundingAccount).WRITE().SIGNER()
return trans
}
// Recipient account
func (trans *Transfer) SetRecipientAccount(recipientAccount common.Address) *Transfer {
trans.AccountMetaSlice[1] = base.Meta(recipientAccount).WRITE()
return trans
}
func (trans Transfer) Build() *Instruction {
return &Instruction{BaseVariant: encodbin.BaseVariant{
Impl: trans,
TypeID: encodbin.TypeIDFromUint32(Instruction_Transfer, binary.LittleEndian),
}}
}
func (trans Transfer) MarshalWithEncoder(encoder encodbin.Encoder) error {
// Serialize `Lamports` param:
{
err := encoder.Encode(*trans.Lamports)
if err != nil {
return err
}
}
return nil
}
func NewTransferInstruction(
// Parameters:
// Accounts:
fundingAccount common.Address,
recipientAccount common.Address,
lamports uint64) *Transfer {
return NewTransferInstructionBuilder().
SetLamports(lamports).
SetFundingAccount(fundingAccount).
SetRecipientAccount(recipientAccount)
}