Skip to content

Commit

Permalink
Enabled automatic ATA creation in CW
Browse files Browse the repository at this point in the history
  • Loading branch information
silaslenihan committed Jan 30, 2025
1 parent 4bd3449 commit 68b1468
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pkg/solana/chainwriter/chain_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ type MethodConfig struct {
FromAddress string
InputModifications commoncodec.ModifiersConfig
ChainSpecificName string
ATAs []ATALookup
LookupTables LookupTables
Accounts []Lookup
// Location in the args where the debug ID is stored
Expand Down Expand Up @@ -214,6 +215,22 @@ func (s *SolanaChainWriterService) FilterLookupTableAddresses(
return filteredLookupTables
}

// CreateATAs first checks if a specified location exists, then checks if the accounts derived from the
// ATALookups in the ChainWriter's configuration exist on-chain and creates them if they do not.
func CreateATAs(ctx context.Context, args any, lookups []ATALookup, derivedTableMap map[string]map[string][]*solana.AccountMeta, reader client.Reader, idl string) error {
for _, lookup := range lookups {
if lookup.Location != "" {
// TODO refactor GetValuesAtLocation to not return an error if the field doesn't exist
_, err := GetValuesAtLocation(args, lookup.Location)
if err != nil {
continue
}
}
addresses, err := GetAddresses(ctx, args, []Lookup{lookup.Accounts}, derivedTableMap, reader, idl)

Check failure on line 229 in pkg/solana/chainwriter/chain_writer.go

View workflow job for this annotation

GitHub Actions / Relay Run Unit Tests

declared and not used: addresses

Check failure on line 229 in pkg/solana/chainwriter/chain_writer.go

View workflow job for this annotation

GitHub Actions / Relay Run Unit Tests

declared and not used: err

Check failure on line 229 in pkg/solana/chainwriter/chain_writer.go

View workflow job for this annotation

GitHub Actions / Relay Run Unit Tests

lookup.Accounts undefined (type ATALookup has no field or method Accounts)
}
return nil
}

// SubmitTransaction builds, encodes, and enqueues a transaction using the provided program
// configuration and method details. It relies on the configured IDL, account lookups, and
// lookup tables to gather the necessary accounts and data. The function retrieves the latest
Expand Down Expand Up @@ -269,6 +286,11 @@ func (s *SolanaChainWriterService) SubmitTransaction(ctx context.Context, contra
return errorWithDebugID(fmt.Errorf("error resolving account addresses: %w", err), debugID)
}

err = CreateATAs(ctx, args, methodConfig.ATAs, derivedTableMap, s.reader, programConfig.IDL)
if err != nil {
return errorWithDebugID(fmt.Errorf("error resolving account addresses: %w", err), debugID)
}

feePayer, err := solana.PublicKeyFromBase58(methodConfig.FromAddress)
if err != nil {
return errorWithDebugID(fmt.Errorf("error parsing fee payer address: %w", err), debugID)
Expand Down
9 changes: 9 additions & 0 deletions pkg/solana/chainwriter/lookups.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,15 @@ type AccountsFromLookupTable struct {
IncludeIndexes []int
}

type ATALookup struct {
// Field that determines whether the ATA lookup is necessary. Basically
// just need to check this field exists. Dot separated location.
Location string
// If the field exists, initialize a new account for every address from
// the following lookup resolution.
ATA Lookup
}

func (ac AccountConstant) Resolve(_ context.Context, _ any, _ map[string]map[string][]*solana.AccountMeta, _ client.Reader, _ string) ([]*solana.AccountMeta, error) {
address, err := solana.PublicKeyFromBase58(ac.Address)
if err != nil {
Expand Down

0 comments on commit 68b1468

Please sign in to comment.