// Code generated by https://github.com/daog1/solana-anchor-go. DO NOT EDIT.

package pump

import (
	"errors"
	ag_binary "github.com/gagliardetto/binary"
	ag_solanago "github.com/gagliardetto/solana-go"
	ag_format "github.com/gagliardetto/solana-go/text/format"
	ag_treeout "github.com/gagliardetto/treeout"
)

// Allows Global::set_creator_authority to set the bonding curve creator from Metaplex metadata or input argument
type SetCreator struct {
	Creator *ag_solanago.PublicKey

	// [0] = [SIGNER] set_creator_authority
	//
	// [1] = [] global
	//
	// [2] = [] mint
	//
	// [3] = [] metadata
	//
	// [4] = [WRITE] bonding_curve
	//
	// [5] = [] event_authority
	//
	// [6] = [] program
	ag_solanago.AccountMetaSlice `bin:"-"`
}

// NewSetCreatorInstructionBuilder creates a new `SetCreator` instruction builder.
func NewSetCreatorInstructionBuilder() *SetCreator {
	nd := &SetCreator{
		AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 7),
	}
	return nd
}

// NewSetCreatorInstructionBuilderExt creates a new `SetCreator` instruction builder.
func NewSetCreatorInstructionBuilderExt(remainingAccounts int) *SetCreator {
	nd := &SetCreator{
		AccountMetaSlice: make(ag_solanago.AccountMetaSlice, 7+remainingAccounts),
	}
	return nd
}

// SetCreator sets the "creator" parameter.
func (inst *SetCreator) SetCreator(creator ag_solanago.PublicKey) *SetCreator {
	inst.Creator = &creator
	return inst
}

// SetSetCreatorAuthorityAccount sets the "set_creator_authority" account.
func (inst *SetCreator) SetSetCreatorAuthorityAccount(setCreatorAuthority ag_solanago.PublicKey) *SetCreator {
	inst.AccountMetaSlice[0] = ag_solanago.Meta(setCreatorAuthority).SIGNER()
	return inst
}

// GetSetCreatorAuthorityAccount gets the "set_creator_authority" account.
func (inst *SetCreator) GetSetCreatorAuthorityAccount() *ag_solanago.AccountMeta {
	return inst.AccountMetaSlice.Get(0)
}

// SetGlobalAccount sets the "global" account.
func (inst *SetCreator) SetGlobalAccount(global ag_solanago.PublicKey) *SetCreator {
	inst.AccountMetaSlice[1] = ag_solanago.Meta(global)
	return inst
}

func (inst *SetCreator) findFindGlobalAddress(knownBumpSeed uint8) (pda ag_solanago.PublicKey, bumpSeed uint8, err error) {
	var seeds [][]byte
	// const: 0x676c6f62616c
	seeds = append(seeds, []byte{byte(0x67), byte(0x6c), byte(0x6f), byte(0x62), byte(0x61), byte(0x6c)})

	if knownBumpSeed != 0 {
		seeds = append(seeds, []byte{byte(bumpSeed)})
		pda, err = ag_solanago.CreateProgramAddress(seeds, ProgramID)
	} else {
		pda, bumpSeed, err = ag_solanago.FindProgramAddress(seeds, ProgramID)
	}
	return
}

// FindGlobalAddressWithBumpSeed calculates Global account address with given seeds and a known bump seed.
func (inst *SetCreator) FindGlobalAddressWithBumpSeed(bumpSeed uint8) (pda ag_solanago.PublicKey, err error) {
	pda, _, err = inst.findFindGlobalAddress(bumpSeed)
	return
}

func (inst *SetCreator) MustFindGlobalAddressWithBumpSeed(bumpSeed uint8) (pda ag_solanago.PublicKey) {
	pda, _, err := inst.findFindGlobalAddress(bumpSeed)
	if err != nil {
		panic(err)
	}
	return
}

// FindGlobalAddress finds Global account address with given seeds.
func (inst *SetCreator) FindGlobalAddress() (pda ag_solanago.PublicKey, bumpSeed uint8, err error) {
	pda, bumpSeed, err = inst.findFindGlobalAddress(0)
	return
}

func (inst *SetCreator) MustFindGlobalAddress() (pda ag_solanago.PublicKey) {
	pda, _, err := inst.findFindGlobalAddress(0)
	if err != nil {
		panic(err)
	}
	return
}

// GetGlobalAccount gets the "global" account.
func (inst *SetCreator) GetGlobalAccount() *ag_solanago.AccountMeta {
	return inst.AccountMetaSlice.Get(1)
}

// SetMintAccount sets the "mint" account.
func (inst *SetCreator) SetMintAccount(mint ag_solanago.PublicKey) *SetCreator {
	inst.AccountMetaSlice[2] = ag_solanago.Meta(mint)
	return inst
}

// GetMintAccount gets the "mint" account.
func (inst *SetCreator) GetMintAccount() *ag_solanago.AccountMeta {
	return inst.AccountMetaSlice.Get(2)
}

// SetMetadataAccount sets the "metadata" account.
func (inst *SetCreator) SetMetadataAccount(metadata ag_solanago.PublicKey) *SetCreator {
	inst.AccountMetaSlice[3] = ag_solanago.Meta(metadata)
	return inst
}

func (inst *SetCreator) findFindMetadataAddress(mint ag_solanago.PublicKey, knownBumpSeed uint8) (pda ag_solanago.PublicKey, bumpSeed uint8, err error) {
	var seeds [][]byte
	// const: 0x6d65746164617461
	seeds = append(seeds, []byte{byte(0x6d), byte(0x65), byte(0x74), byte(0x61), byte(0x64), byte(0x61), byte(0x74), byte(0x61)})
	// const: 0x0b7065b1e3d17c45389d527f6b04c3cd58b86c731aa0fdb549b6d1bc03f82946
	seeds = append(seeds, []byte{byte(0xb), byte(0x70), byte(0x65), byte(0xb1), byte(0xe3), byte(0xd1), byte(0x7c), byte(0x45), byte(0x38), byte(0x9d), byte(0x52), byte(0x7f), byte(0x6b), byte(0x4), byte(0xc3), byte(0xcd), byte(0x58), byte(0xb8), byte(0x6c), byte(0x73), byte(0x1a), byte(0xa0), byte(0xfd), byte(0xb5), byte(0x49), byte(0xb6), byte(0xd1), byte(0xbc), byte(0x3), byte(0xf8), byte(0x29), byte(0x46)})
	// path: mint
	seeds = append(seeds, mint.Bytes())

	programID := Addresses["metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s"]

	if knownBumpSeed != 0 {
		seeds = append(seeds, []byte{byte(bumpSeed)})
		pda, err = ag_solanago.CreateProgramAddress(seeds, programID)
	} else {
		pda, bumpSeed, err = ag_solanago.FindProgramAddress(seeds, programID)
	}
	return
}

// FindMetadataAddressWithBumpSeed calculates Metadata account address with given seeds and a known bump seed.
func (inst *SetCreator) FindMetadataAddressWithBumpSeed(mint ag_solanago.PublicKey, bumpSeed uint8) (pda ag_solanago.PublicKey, err error) {
	pda, _, err = inst.findFindMetadataAddress(mint, bumpSeed)
	return
}

func (inst *SetCreator) MustFindMetadataAddressWithBumpSeed(mint ag_solanago.PublicKey, bumpSeed uint8) (pda ag_solanago.PublicKey) {
	pda, _, err := inst.findFindMetadataAddress(mint, bumpSeed)
	if err != nil {
		panic(err)
	}
	return
}

// FindMetadataAddress finds Metadata account address with given seeds.
func (inst *SetCreator) FindMetadataAddress(mint ag_solanago.PublicKey) (pda ag_solanago.PublicKey, bumpSeed uint8, err error) {
	pda, bumpSeed, err = inst.findFindMetadataAddress(mint, 0)
	return
}

func (inst *SetCreator) MustFindMetadataAddress(mint ag_solanago.PublicKey) (pda ag_solanago.PublicKey) {
	pda, _, err := inst.findFindMetadataAddress(mint, 0)
	if err != nil {
		panic(err)
	}
	return
}

// GetMetadataAccount gets the "metadata" account.
func (inst *SetCreator) GetMetadataAccount() *ag_solanago.AccountMeta {
	return inst.AccountMetaSlice.Get(3)
}

// SetBondingCurveAccount sets the "bonding_curve" account.
func (inst *SetCreator) SetBondingCurveAccount(bondingCurve ag_solanago.PublicKey) *SetCreator {
	inst.AccountMetaSlice[4] = ag_solanago.Meta(bondingCurve).WRITE()
	return inst
}

func (inst *SetCreator) findFindBondingCurveAddress(mint ag_solanago.PublicKey, knownBumpSeed uint8) (pda ag_solanago.PublicKey, bumpSeed uint8, err error) {
	var seeds [][]byte
	// const: 0x626f6e64696e672d6375727665
	seeds = append(seeds, []byte{byte(0x62), byte(0x6f), byte(0x6e), byte(0x64), byte(0x69), byte(0x6e), byte(0x67), byte(0x2d), byte(0x63), byte(0x75), byte(0x72), byte(0x76), byte(0x65)})
	// path: mint
	seeds = append(seeds, mint.Bytes())

	if knownBumpSeed != 0 {
		seeds = append(seeds, []byte{byte(bumpSeed)})
		pda, err = ag_solanago.CreateProgramAddress(seeds, ProgramID)
	} else {
		pda, bumpSeed, err = ag_solanago.FindProgramAddress(seeds, ProgramID)
	}
	return
}

// FindBondingCurveAddressWithBumpSeed calculates BondingCurve account address with given seeds and a known bump seed.
func (inst *SetCreator) FindBondingCurveAddressWithBumpSeed(mint ag_solanago.PublicKey, bumpSeed uint8) (pda ag_solanago.PublicKey, err error) {
	pda, _, err = inst.findFindBondingCurveAddress(mint, bumpSeed)
	return
}

func (inst *SetCreator) MustFindBondingCurveAddressWithBumpSeed(mint ag_solanago.PublicKey, bumpSeed uint8) (pda ag_solanago.PublicKey) {
	pda, _, err := inst.findFindBondingCurveAddress(mint, bumpSeed)
	if err != nil {
		panic(err)
	}
	return
}

// FindBondingCurveAddress finds BondingCurve account address with given seeds.
func (inst *SetCreator) FindBondingCurveAddress(mint ag_solanago.PublicKey) (pda ag_solanago.PublicKey, bumpSeed uint8, err error) {
	pda, bumpSeed, err = inst.findFindBondingCurveAddress(mint, 0)
	return
}

func (inst *SetCreator) MustFindBondingCurveAddress(mint ag_solanago.PublicKey) (pda ag_solanago.PublicKey) {
	pda, _, err := inst.findFindBondingCurveAddress(mint, 0)
	if err != nil {
		panic(err)
	}
	return
}

// GetBondingCurveAccount gets the "bonding_curve" account.
func (inst *SetCreator) GetBondingCurveAccount() *ag_solanago.AccountMeta {
	return inst.AccountMetaSlice.Get(4)
}

// SetEventAuthorityAccount sets the "event_authority" account.
func (inst *SetCreator) SetEventAuthorityAccount(eventAuthority ag_solanago.PublicKey) *SetCreator {
	inst.AccountMetaSlice[5] = ag_solanago.Meta(eventAuthority)
	return inst
}

func (inst *SetCreator) findFindEventAuthorityAddress(knownBumpSeed uint8) (pda ag_solanago.PublicKey, bumpSeed uint8, err error) {
	var seeds [][]byte
	// const: 0x5f5f6576656e745f617574686f72697479
	seeds = append(seeds, []byte{byte(0x5f), byte(0x5f), byte(0x65), byte(0x76), byte(0x65), byte(0x6e), byte(0x74), byte(0x5f), byte(0x61), byte(0x75), byte(0x74), byte(0x68), byte(0x6f), byte(0x72), byte(0x69), byte(0x74), byte(0x79)})

	if knownBumpSeed != 0 {
		seeds = append(seeds, []byte{byte(bumpSeed)})
		pda, err = ag_solanago.CreateProgramAddress(seeds, ProgramID)
	} else {
		pda, bumpSeed, err = ag_solanago.FindProgramAddress(seeds, ProgramID)
	}
	return
}

// FindEventAuthorityAddressWithBumpSeed calculates EventAuthority account address with given seeds and a known bump seed.
func (inst *SetCreator) FindEventAuthorityAddressWithBumpSeed(bumpSeed uint8) (pda ag_solanago.PublicKey, err error) {
	pda, _, err = inst.findFindEventAuthorityAddress(bumpSeed)
	return
}

func (inst *SetCreator) MustFindEventAuthorityAddressWithBumpSeed(bumpSeed uint8) (pda ag_solanago.PublicKey) {
	pda, _, err := inst.findFindEventAuthorityAddress(bumpSeed)
	if err != nil {
		panic(err)
	}
	return
}

// FindEventAuthorityAddress finds EventAuthority account address with given seeds.
func (inst *SetCreator) FindEventAuthorityAddress() (pda ag_solanago.PublicKey, bumpSeed uint8, err error) {
	pda, bumpSeed, err = inst.findFindEventAuthorityAddress(0)
	return
}

func (inst *SetCreator) MustFindEventAuthorityAddress() (pda ag_solanago.PublicKey) {
	pda, _, err := inst.findFindEventAuthorityAddress(0)
	if err != nil {
		panic(err)
	}
	return
}

// GetEventAuthorityAccount gets the "event_authority" account.
func (inst *SetCreator) GetEventAuthorityAccount() *ag_solanago.AccountMeta {
	return inst.AccountMetaSlice.Get(5)
}

// SetProgramAccount sets the "program" account.
func (inst *SetCreator) SetProgramAccount(program ag_solanago.PublicKey) *SetCreator {
	inst.AccountMetaSlice[6] = ag_solanago.Meta(program)
	return inst
}

// GetProgramAccount gets the "program" account.
func (inst *SetCreator) GetProgramAccount() *ag_solanago.AccountMeta {
	return inst.AccountMetaSlice.Get(6)
}

func (inst *SetCreator) AddRemainingAccounts(remainingAccounts []ag_solanago.PublicKey) *SetCreator {
	accounts := 7
	for i, _ := range remainingAccounts {
		index := accounts + i
		inst.AccountMetaSlice[index] = ag_solanago.Meta(remainingAccounts[i]).WRITE()
	}
	return inst
}

func (inst SetCreator) Build() *Instruction {
	return &Instruction{BaseVariant: ag_binary.BaseVariant{
		Impl:   inst,
		TypeID: Instruction_SetCreator,
	}}
}

// ValidateAndBuild validates the instruction parameters and accounts;
// if there is a validation error, it returns the error.
// Otherwise, it builds and returns the instruction.
func (inst SetCreator) ValidateAndBuild() (*Instruction, error) {
	if err := inst.Validate(); err != nil {
		return nil, err
	}
	return inst.Build(), nil
}

func (inst *SetCreator) Validate() error {
	// Check whether all (required) parameters are set:
	{
		if inst.Creator == nil {
			return errors.New("Creator parameter is not set")
		}
	}

	// Check whether all (required) accounts are set:
	{
		if inst.AccountMetaSlice[0] == nil {
			return errors.New("accounts.SetCreatorAuthority is not set")
		}
		if inst.AccountMetaSlice[1] == nil {
			return errors.New("accounts.Global is not set")
		}
		if inst.AccountMetaSlice[2] == nil {
			return errors.New("accounts.Mint is not set")
		}
		if inst.AccountMetaSlice[3] == nil {
			return errors.New("accounts.Metadata is not set")
		}
		if inst.AccountMetaSlice[4] == nil {
			return errors.New("accounts.BondingCurve is not set")
		}
		if inst.AccountMetaSlice[5] == nil {
			return errors.New("accounts.EventAuthority is not set")
		}
		if inst.AccountMetaSlice[6] == nil {
			return errors.New("accounts.Program is not set")
		}
	}
	return nil
}

func (inst *SetCreator) EncodeToTree(parent ag_treeout.Branches) {
	parent.Child(ag_format.Program(ProgramName, ProgramID)).
		//
		ParentFunc(func(programBranch ag_treeout.Branches) {
			programBranch.Child(ag_format.Instruction("SetCreator")).
				//
				ParentFunc(func(instructionBranch ag_treeout.Branches) {

					// Parameters of the instruction:
					instructionBranch.Child("Params[len=1]").ParentFunc(func(paramsBranch ag_treeout.Branches) {
						paramsBranch.Child(ag_format.Param("Creator", *inst.Creator))
					})

					// Accounts of the instruction:
					instructionBranch.Child("Accounts[len=7]").ParentFunc(func(accountsBranch ag_treeout.Branches) {
						accountsBranch.Child(ag_format.Meta("set_creator_authority", inst.AccountMetaSlice.Get(0)))
						accountsBranch.Child(ag_format.Meta("               global", inst.AccountMetaSlice.Get(1)))
						accountsBranch.Child(ag_format.Meta("                 mint", inst.AccountMetaSlice.Get(2)))
						accountsBranch.Child(ag_format.Meta("             metadata", inst.AccountMetaSlice.Get(3)))
						accountsBranch.Child(ag_format.Meta("        bonding_curve", inst.AccountMetaSlice.Get(4)))
						accountsBranch.Child(ag_format.Meta("      event_authority", inst.AccountMetaSlice.Get(5)))
						accountsBranch.Child(ag_format.Meta("              program", inst.AccountMetaSlice.Get(6)))
					})
				})
		})
}

func (obj SetCreator) MarshalWithEncoder(encoder *ag_binary.Encoder) (err error) {
	// Serialize `Creator` param:
	err = encoder.Encode(obj.Creator)
	if err != nil {
		return err
	}
	return nil
}
func (obj *SetCreator) UnmarshalWithDecoder(decoder *ag_binary.Decoder) (err error) {
	// Deserialize `Creator`:
	err = decoder.Decode(&obj.Creator)
	if err != nil {
		return err
	}
	return nil
}

// NewSetCreatorInstruction declares a new SetCreator instruction with the provided parameters and accounts.
func NewSetCreatorInstruction(
	// Parameters:
	creator ag_solanago.PublicKey,
	// Accounts:
	setCreatorAuthority ag_solanago.PublicKey,
	global ag_solanago.PublicKey,
	mint ag_solanago.PublicKey,
	metadata ag_solanago.PublicKey,
	bondingCurve ag_solanago.PublicKey,
	eventAuthority ag_solanago.PublicKey,
	program ag_solanago.PublicKey) *SetCreator {
	return NewSetCreatorInstructionBuilder().
		SetCreator(creator).
		SetSetCreatorAuthorityAccount(setCreatorAuthority).
		SetGlobalAccount(global).
		SetMintAccount(mint).
		SetMetadataAccount(metadata).
		SetBondingCurveAccount(bondingCurve).
		SetEventAuthorityAccount(eventAuthority).
		SetProgramAccount(program)
}
