Skip to content

Commit

Permalink
feat: implement SyncNative for tokenprog
Browse files Browse the repository at this point in the history
  • Loading branch information
yihau committed Aug 28, 2021
1 parent 63a9627 commit 392ff05
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
21 changes: 21 additions & 0 deletions program/tokenprog/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
InstructionMintToChecked
InstructionBurnChecked
InstructionInitializeAccount2
InstructionSyncNative
)

// InitializeMint init a mint, if you don't need to freeze, pass the empty pubKey common.PublicKey{}
Expand Down Expand Up @@ -517,3 +518,23 @@ func InitializeAccount2(accountPubkey, mintPubkey, ownerPubkey common.PublicKey)
Data: data,
}
}

// SyncNative will update your wrapped SOL balance
func SyncNative(accountPubkey common.PublicKey) types.Instruction {
data, err := bincode.SerializeData(struct {
Instruction Instruction
}{
Instruction: InstructionSyncNative,
})
if err != nil {
panic(err)
}

return types.Instruction{
ProgramID: common.TokenProgramID,
Accounts: []types.AccountMeta{
{PubKey: accountPubkey, IsSigner: false, IsWritable: true},
},
Data: data,
}
}
31 changes: 31 additions & 0 deletions program/tokenprog/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -580,3 +580,34 @@ func TestInitializeMultisig(t *testing.T) {
})
}
}

func TestSyncNative(t *testing.T) {
type args struct {
accountPubkey common.PublicKey
}
tests := []struct {
name string
args args
want types.Instruction
}{
{
args: args{
accountPubkey: common.PublicKeyFromString("FtvD2ymcAFh59DGGmJkANyJzEpLDR1GLgqDrUxfe2dPm"),
},
want: types.Instruction{
ProgramID: common.TokenProgramID,
Accounts: []types.AccountMeta{
{PubKey: common.PublicKeyFromString("FtvD2ymcAFh59DGGmJkANyJzEpLDR1GLgqDrUxfe2dPm"), IsSigner: false, IsWritable: true},
},
Data: []byte{17},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := SyncNative(tt.args.accountPubkey); !reflect.DeepEqual(got, tt.want) {
t.Errorf("SyncNative() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit 392ff05

Please sign in to comment.