Skip to content

Commit

Permalink
implement tokenprog approve checked
Browse files Browse the repository at this point in the history
  • Loading branch information
yihau committed Mar 25, 2021
1 parent 03211a2 commit df0fef1
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 2 deletions.
30 changes: 28 additions & 2 deletions tokenprog/instruction.go
Original file line number Diff line number Diff line change
Expand Up @@ -330,8 +330,34 @@ func TransferChecked(srcPubkey, destPubkey, mintPubkey, authPubkey common.Public
}
}

func ApproveChecked() types.Instruction {
panic("not implement yet")
func ApproveChecked(sourcePubkey, mintPubkey, delegatePubkey, authPubkey common.PublicKey, signerPubkeys []common.PublicKey, amount uint64, decimals uint8) types.Instruction {
data, err := common.SerializeData(struct {
Instruction Instruction
Amount uint64
Decimals uint8
}{
Instruction: InstructionApproveChecked,
Amount: amount,
Decimals: decimals,
})
if err != nil {
panic(err)
}

accounts := make([]types.AccountMeta, 0, 4+len(signerPubkeys))
accounts = append(accounts, types.AccountMeta{PubKey: sourcePubkey, IsSigner: false, IsWritable: true})
accounts = append(accounts, types.AccountMeta{PubKey: mintPubkey, IsSigner: false, IsWritable: false})
accounts = append(accounts, types.AccountMeta{PubKey: delegatePubkey, IsSigner: false, IsWritable: false})
accounts = append(accounts, types.AccountMeta{PubKey: authPubkey, IsSigner: len(signerPubkeys) == 0, IsWritable: false})
for _, signerPubkey := range signerPubkeys {
accounts = append(accounts, types.AccountMeta{PubKey: signerPubkey, IsSigner: true, IsWritable: false})
}

return types.Instruction{
ProgramID: common.TokenProgramID,
Accounts: accounts,
Data: data,
}
}

func MintToChecked(mintPubkey, destPubkey, authPubkey common.PublicKey, signerPubkeys []common.PublicKey, amount uint64, decimals uint8) types.Instruction {
Expand Down
46 changes: 46 additions & 0 deletions tokenprog/instruction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -490,3 +490,49 @@ func TestRevoke(t *testing.T) {
})
}
}

func TestApproveChecked(t *testing.T) {
type args struct {
sourcePubkey common.PublicKey
mintPubkey common.PublicKey
delegatePubkey common.PublicKey
authPubkey common.PublicKey
signerPubkeys []common.PublicKey
amount uint64
decimals uint8
}
tests := []struct {
name string
args args
want types.Instruction
}{
{
args: args{
sourcePubkey: common.PublicKeyFromString("FtvD2ymcAFh59DGGmJkANyJzEpLDR1GLgqDrUxfe2dPm"),
mintPubkey: common.PublicKeyFromString("BkXBQ9ThbQffhmG39c2TbXW94pEmVGJAvxWk6hfxRvUJ"),
delegatePubkey: common.PublicKeyFromString("DuNVVSmxNkXZvzT7fEDAWhfDvEgBYohuCGYB9AQzrctY"),
authPubkey: common.PublicKeyFromString("EvN4kgKmCmYzdbd5kL8Q8YgkUW5RoqMTpBczrfLExtx7"),
signerPubkeys: []common.PublicKey{},
amount: 99999,
decimals: 9,
},
want: types.Instruction{
ProgramID: common.TokenProgramID,
Accounts: []types.AccountMeta{
{PubKey: common.PublicKeyFromString("FtvD2ymcAFh59DGGmJkANyJzEpLDR1GLgqDrUxfe2dPm"), IsSigner: false, IsWritable: true},
{PubKey: common.PublicKeyFromString("BkXBQ9ThbQffhmG39c2TbXW94pEmVGJAvxWk6hfxRvUJ"), IsSigner: false, IsWritable: false},
{PubKey: common.PublicKeyFromString("DuNVVSmxNkXZvzT7fEDAWhfDvEgBYohuCGYB9AQzrctY"), IsSigner: false, IsWritable: false},
{PubKey: common.PublicKeyFromString("EvN4kgKmCmYzdbd5kL8Q8YgkUW5RoqMTpBczrfLExtx7"), IsSigner: true, IsWritable: false},
},
Data: []byte{13, 159, 134, 1, 0, 0, 0, 0, 0, 9},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := ApproveChecked(tt.args.sourcePubkey, tt.args.mintPubkey, tt.args.delegatePubkey, tt.args.authPubkey, tt.args.signerPubkeys, tt.args.amount, tt.args.decimals); !reflect.DeepEqual(got, tt.want) {
t.Errorf("ApproveChecked() = %v, want %v", got, tt.want)
}
})
}
}

0 comments on commit df0fef1

Please sign in to comment.