Skip to content

Commit

Permalink
Enabled toggling ATA support as optional
Browse files Browse the repository at this point in the history
  • Loading branch information
amit-momin committed Feb 22, 2025
1 parent c2466cd commit 4efa76d
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 3 deletions.
80 changes: 80 additions & 0 deletions integration-tests/relayinterface/lookups_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -704,6 +704,61 @@ func TestLookupTables(t *testing.T) {
require.Equal(t, lookupKeys[i], address.PublicKey)
}
})

t.Run("Resolving optional derived lookup table does not return error", func(t *testing.T) {
// Deployed contract_reader_interface contract
programID := solana.MustPublicKeyFromBase58("6AfuXF6HapDUhQfE4nQG9C1SGtA1YjP3icaJyRfU4RyE")

args := map[string]interface{}{
"seed1": []byte("lookup"),
}

lookupConfig := chainwriter.LookupTables{
DerivedLookupTables: []chainwriter.DerivedLookupTable{
{
Name: "DerivedTable",
Accounts: chainwriter.Lookup{PDALookups: &chainwriter.PDALookups{
Name: "DataAccountPDA",
PublicKey: chainwriter.Lookup{AccountConstant: &chainwriter.AccountConstant{Name: "WriteTest", Address: programID.String()}},
Seeds: []chainwriter.Seed{
{Dynamic: chainwriter.Lookup{AccountLookup: &chainwriter.AccountLookup{Name: "missing_seed", Location: "missing_seed"}}},
},
IsSigner: false,
IsWritable: false,
InternalField: chainwriter.InternalField{
TypeName: "LookupTableDataAccount",
Location: "LookupTable",
IDL: testContractIDL,
}},
},
Optional: true,
},
},
}

derivedTableMap, _, err := cw.ResolveLookupTables(ctx, args, lookupConfig)
require.NoError(t, err)

pdaWithAccountLookupSeed := chainwriter.Lookup{
PDALookups: &chainwriter.PDALookups{
PublicKey: chainwriter.Lookup{AccountConstant: &chainwriter.AccountConstant{Address: chainwriter.GetRandomPubKey(t).String()}},
Seeds: []chainwriter.Seed{
{
Dynamic: chainwriter.Lookup{
AccountsFromLookupTable: &chainwriter.AccountsFromLookupTable{
LookupTableName: "DerivedTable",
IncludeIndexes: []int{},
},
},
},
},
},
Optional: true,
}

_, err = chainwriter.GetAddresses(ctx, nil, []chainwriter.Lookup{pdaWithAccountLookupSeed}, derivedTableMap, multiClient)
require.NoError(t, err)
})
}

func TestCreateATAs(t *testing.T) {
Expand Down Expand Up @@ -936,6 +991,31 @@ func TestCreateATAs(t *testing.T) {
require.NoError(t, err)
require.Empty(t, ataInstructions, "No new instructions should be returned if ATAs already exist")
})

t.Run("optional ATA creation does not return error if lookups fail", func(t *testing.T) {
lookups := []chainwriter.ATALookup{
{
Location: "Inner.Address",
WalletAddress: chainwriter.Lookup{AccountConstant: &chainwriter.AccountConstant{
Address: feePayer.String(),
}},
TokenProgram: chainwriter.Lookup{AccountConstant: &chainwriter.AccountConstant{
Address: chainwriter.GetRandomPubKey(t).String(),
}},
MintAddress: chainwriter.Lookup{AccountLookup: &chainwriter.AccountLookup{
Location: "Inner.BadLocation",
}},
Optional: true,
},
}
args := chainwriter.TestArgs{
Inner: []chainwriter.InnerArgs{{Address: chainwriter.GetRandomPubKey(t).Bytes()}},
}

ataInstructions, err := chainwriter.CreateATAs(ctx, args, lookups, nil, multiClient, testContractIDL, feePayer, logger.Test(t))
require.NoError(t, err)
require.Len(t, ataInstructions, 0)
})
}

func checkIfATAExists(t *testing.T, rpcClient *rpc.Client, ataAddress solana.PublicKey) bool {
Expand Down
12 changes: 9 additions & 3 deletions pkg/solana/chainwriter/chain_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,9 @@ func CreateATAs(ctx context.Context, args any, lookups []ATALookup, derivedTable
}
}
walletAddresses, err := GetAddresses(ctx, args, []Lookup{lookup.WalletAddress}, derivedTableMap, client)
if err != nil {
if lookup.Optional && isIgnorableError(err) {
continue
} else if err != nil {
return nil, fmt.Errorf("error resolving wallet address: %w", err)
}
if len(walletAddresses) != 1 {
Expand All @@ -262,12 +264,16 @@ func CreateATAs(ctx context.Context, args any, lookups []ATALookup, derivedTable
wallet := walletAddresses[0].PublicKey

tokenPrograms, err := GetAddresses(ctx, args, []Lookup{lookup.TokenProgram}, derivedTableMap, client)
if err != nil {
if lookup.Optional && isIgnorableError(err) {
continue
} else if err != nil {
return nil, fmt.Errorf("error resolving token program address: %w", err)
}

mints, err := GetAddresses(ctx, args, []Lookup{lookup.MintAddress}, derivedTableMap, client)
if err != nil {
if lookup.Optional && isIgnorableError(err) {
continue
} else if err != nil {
return nil, fmt.Errorf("error resolving mint address: %w", err)
}
if len(tokenPrograms) != len(mints) {
Expand Down
1 change: 1 addition & 0 deletions pkg/solana/chainwriter/lookups.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ type ATALookup struct {
WalletAddress Lookup
TokenProgram Lookup
MintAddress Lookup
Optional bool
}

func (l Lookup) validate() error {
Expand Down

0 comments on commit 4efa76d

Please sign in to comment.