Skip to content

Commit

Permalink
refactored ccip example
Browse files Browse the repository at this point in the history
  • Loading branch information
silaslenihan committed Nov 26, 2024
1 parent 183e7e2 commit ed1270d
Showing 1 changed file with 61 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package chainwriter_test
package chainwriter

import (
"fmt"
"testing"

commoncodec "github.com/smartcontractkit/chainlink-common/pkg/codec"
"github.com/smartcontractkit/chainlink-solana/pkg/solana/chainwriter"
)

func TestGetAddresses(t *testing.T) {
func TestConfig() {
// Fake constant addresses for the purpose of this example.
registryAddress := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6A"
routerProgramAddress := "4Nn9dsYBcSTzRbK9hg9kzCUdrCSkMZq1UR6Vw1Tkaf6B"
Expand All @@ -23,7 +21,7 @@ func TestGetAddresses(t *testing.T) {

executionReportSingleChainIDL := `{"name":"ExecutionReportSingleChain","type":{"kind":"struct","fields":[{"name":"source_chain_selector","type":"u64"},{"name":"message","type":{"defined":"Any2SolanaRampMessage"}},{"name":"root","type":{"array":["u8",32]}},{"name":"proofs","type":{"vec":{"array":["u8",32]}}}]}},{"name":"Any2SolanaRampMessage","type":{"kind":"struct","fields":[{"name":"header","type":{"defined":"RampMessageHeader"}},{"name":"sender","type":{"vec":"u8"}},{"name":"data","type":{"vec":"u8"}},{"name":"receiver","type":{"array":["u8",32]}},{"name":"extra_args","type":{"defined":"SolanaExtraArgs"}}]}},{"name":"RampMessageHeader","type":{"kind":"struct","fields":[{"name":"message_id","type":{"array":["u8",32]}},{"name":"source_chain_selector","type":"u64"},{"name":"dest_chain_selector","type":"u64"},{"name":"sequence_number","type":"u64"},{"name":"nonce","type":"u64"}]}},{"name":"SolanaExtraArgs","type":{"kind":"struct","fields":[{"name":"compute_units","type":"u32"},{"name":"allow_out_of_order_execution","type":"bool"}]}}`

executeConfig := chainwriter.MethodConfig{
executeConfig := MethodConfig{
FromAddress: userAddress,
InputModifications: commoncodec.ModifiersConfig{
// remove merkle root since it isn't a part of the on-chain type
Expand All @@ -35,29 +33,29 @@ func TestGetAddresses(t *testing.T) {
// LookupTables are on-chain stores of accounts. They can be used in two ways:
// 1. As a way to store a list of accounts that are all associated together (i.e. Token State registry)
// 2. To compress the transactions in a TX and reduce the size of the TX. (The traditional way)
LookupTables: chainwriter.LookupTables{
LookupTables: LookupTables{
// DerivedLookupTables are useful in both the ways described above.
// a. The user can configure any type of look up to get a list of lookupTables to read from.
// b. The ChainWriter reads from this lookup table and store the internal addresses in memory
// c. Later, in the []Accounts the user can specify which accounts to include in the TX with an AccountsFromLookupTable lookup.
// d. Lastly, the lookup table is used to compress the size of the transaction.
DerivedLookupTables: []chainwriter.DerivedLookupTable{
DerivedLookupTables: []DerivedLookupTable{
{
Name: "RegistryTokenState",
// In this case, the user configured the lookup table accounts to use a PDALookup, which
// generates a list of one of more PDA accounts based on the input parameters. Specifically,
// there will be multple PDA accounts if there are multiple addresses in the message, otherwise,
// there will only be one PDA account to read from. The PDA account corresponds to the lookup table.
Accounts: chainwriter.PDALookups{
Accounts: PDALookups{
Name: "RegistryTokenState",
PublicKey: chainwriter.AccountConstant{
PublicKey: AccountConstant{
Address: registryAddress,
IsSigner: false,
IsWritable: false,
},
// Seeds would be used if the user needed to look up addresses to use as seeds, which isn't the case here.
Seeds: []chainwriter.Lookup{
chainwriter.AccountLookup{Location: "Message.TokenAmounts.DestTokenAddress"},
Seeds: []Lookup{
AccountLookup{Location: "Message.TokenAmounts.DestTokenAddress"},
},
IsSigner: false,
IsWritable: false,
Expand All @@ -74,7 +72,7 @@ func TestGetAddresses(t *testing.T) {
},
// The Accounts field is where the user specifies which accounts to include in the transaction. Each Lookup
// resolves to one or more on-chain addresses.
Accounts: []chainwriter.Lookup{
Accounts: []Lookup{
// The accounts can be of any of the following types:
// 1. Account constant
// 2. Account Lookup - Based on data from input parameters
Expand All @@ -86,51 +84,51 @@ func TestGetAddresses(t *testing.T) {
// PDALookups can resolve to multiple addresses if:
// A) The PublicKey lookup resolves to multiple addresses (i.e. multiple token addresses)
// B) The Seeds or ValueSeeds resolve to multiple values
chainwriter.PDALookups{
PDALookups{
Name: "PerChainConfig",
// PublicKey is a constant account in this case, not a lookup.
PublicKey: chainwriter.AccountConstant{
PublicKey: AccountConstant{
Address: registryAddress,
IsSigner: false,
IsWritable: false,
},
// Similar to the RegistryTokenState above, the user is looking up PDA accounts based on the dest tokens.
Seeds: []chainwriter.Lookup{
chainwriter.AccountLookup{Location: "Message.TokenAmounts.DestTokenAddress"},
chainwriter.AccountLookup{Location: "Message.Header.DestChainSelector"},
Seeds: []Lookup{
AccountLookup{Location: "Message.TokenAmounts.DestTokenAddress"},
AccountLookup{Location: "Message.Header.DestChainSelector"},
},
IsSigner: false,
IsWritable: false,
},
// Lookup Table content - Get the accounts from the derived lookup table above
chainwriter.AccountsFromLookupTable{
AccountsFromLookupTable{
LookupTablesName: "RegistryTokenState",
IncludeIndexes: []int{}, // If left empty, all addresses will be included. Otherwise, only the specified indexes will be included.
},
// Account Lookup - Based on data from input parameters
// In this case, the user wants to add the destination token addresses to the transaction.
// Once again, this can be one or multiple addresses.
chainwriter.AccountLookup{
AccountLookup{
Name: "TokenAccount",
Location: "Message.TokenAmounts.DestTokenAddress",
IsSigner: false,
IsWritable: false,
},
// PDA Account Lookup - Based on an account lookup and an address lookup
chainwriter.PDALookups{
PDALookups{
// In this case, the token address is the public key, and the receiver is the seed.
// Again, there could be multiple token addresses, in which case this would resolve to
// multiple PDA accounts.
Name: "ReceiverAssociatedTokenAccount",
PublicKey: chainwriter.AccountLookup{
PublicKey: AccountLookup{
Name: "TokenAccount",
Location: "Message.TokenAmounts.DestTokenAddress",
IsSigner: false,
IsWritable: false,
},
// The seed is the receiver address.
Seeds: []chainwriter.Lookup{
chainwriter.AccountLookup{
Seeds: []Lookup{
AccountLookup{
Name: "Receiver",
Location: "Message.Receiver",
IsSigner: false,
Expand All @@ -139,69 +137,69 @@ func TestGetAddresses(t *testing.T) {
},
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "Registry",
Address: registryAddress,
IsSigner: false,
IsWritable: false,
},
// PDA Lookup for the RegistryTokenConfig.
chainwriter.PDALookups{
PDALookups{
Name: "RegistryTokenConfig",
// constant public key
PublicKey: chainwriter.AccountConstant{
PublicKey: AccountConstant{
Address: registryAddress,
IsSigner: false,
IsWritable: false,
},
// The seed, once again, is the destination token address.
Seeds: []chainwriter.Lookup{
chainwriter.AccountLookup{Location: "Message.TokenAmounts.DestTokenAddress"},
Seeds: []Lookup{
AccountLookup{Location: "Message.TokenAmounts.DestTokenAddress"},
},
IsSigner: false,
IsWritable: false,
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "RouterProgram",
Address: routerProgramAddress,
IsSigner: false,
IsWritable: false,
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "RouterAccountConfig",
Address: routerAccountConfigAddress,
IsSigner: false,
IsWritable: false,
},
// PDA lookup to get the Router Chain Config
chainwriter.PDALookups{
PDALookups{
Name: "RouterChainConfig",
// The public key is a constant Router address.
PublicKey: chainwriter.AccountConstant{
PublicKey: AccountConstant{
Address: routerProgramAddress,
IsSigner: false,
IsWritable: false,
},
Seeds: []chainwriter.Lookup{
chainwriter.AccountLookup{Location: "Message.Header.DestChainSelector"},
chainwriter.AccountLookup{Location: "Message.Header.SourceChainSelector"},
Seeds: []Lookup{
AccountLookup{Location: "Message.Header.DestChainSelector"},
AccountLookup{Location: "Message.Header.SourceChainSelector"},
},
IsSigner: false,
IsWritable: false,
},
// PDA lookup to get the Router Report Accounts.
chainwriter.PDALookups{
PDALookups{
Name: "RouterReportAccount",
// The public key is a constant Router address.
PublicKey: chainwriter.AccountConstant{
PublicKey: AccountConstant{
Address: routerProgramAddress,
IsSigner: false,
IsWritable: false,
},
Seeds: []chainwriter.Lookup{
chainwriter.AccountLookup{
Seeds: []Lookup{
AccountLookup{
// The seed is the merkle root of the report, as passed into the input params.
Location: "args.MerkleRoot",
},
Expand All @@ -210,44 +208,44 @@ func TestGetAddresses(t *testing.T) {
IsWritable: false,
},
// PDA lookup to get UserNoncePerChain
chainwriter.PDALookups{
PDALookups{
Name: "UserNoncePerChain",
// The public key is a constant Router address.
PublicKey: chainwriter.AccountConstant{
PublicKey: AccountConstant{
Address: routerProgramAddress,
IsSigner: false,
IsWritable: false,
},
// In this case, the user configured multiple seeds. These will be used in conjunction
// with the public key to generate one or multiple PDA accounts.
Seeds: []chainwriter.Lookup{
chainwriter.AccountLookup{Location: "Message.Receiver"},
chainwriter.AccountLookup{Location: "Message.Header.DestChainSelector"},
Seeds: []Lookup{
AccountLookup{Location: "Message.Receiver"},
AccountLookup{Location: "Message.Header.DestChainSelector"},
},
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "CPISigner",
Address: cpiSignerAddress,
IsSigner: true,
IsWritable: false,
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "SystemProgram",
Address: systemProgramAddress,
IsSigner: true,
IsWritable: false,
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "ComputeBudgetProgram",
Address: computeBudgetProgramAddress,
IsSigner: true,
IsWritable: false,
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "SysvarProgram",
Address: sysvarProgramAddress,
IsSigner: true,
Expand All @@ -259,42 +257,42 @@ func TestGetAddresses(t *testing.T) {
DebugIDLocation: "Message.MessageID",
}

commitConfig := chainwriter.MethodConfig{
commitConfig := MethodConfig{
FromAddress: userAddress,
InputModifications: nil,
ChainSpecificName: "commit",
LookupTables: chainwriter.LookupTables{
LookupTables: LookupTables{
StaticLookupTables: []string{
commonAddressesLookupTable,
routerLookupTable,
},
},
Accounts: []chainwriter.Lookup{
Accounts: []Lookup{
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "RouterProgram",
Address: routerProgramAddress,
IsSigner: false,
IsWritable: false,
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "RouterAccountConfig",
Address: routerAccountConfigAddress,
IsSigner: false,
IsWritable: false,
},
// PDA lookup to get the Router Report Accounts.
chainwriter.PDALookups{
PDALookups{
Name: "RouterReportAccount",
// The public key is a constant Router address.
PublicKey: chainwriter.AccountConstant{
PublicKey: AccountConstant{
Address: routerProgramAddress,
IsSigner: false,
IsWritable: false,
},
Seeds: []chainwriter.Lookup{
chainwriter.AccountLookup{
Seeds: []Lookup{
AccountLookup{
// The seed is the merkle root of the report, as passed into the input params.
Location: "args.MerkleRoots",
},
Expand All @@ -303,21 +301,21 @@ func TestGetAddresses(t *testing.T) {
IsWritable: false,
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "SystemProgram",
Address: systemProgramAddress,
IsSigner: true,
IsWritable: false,
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "ComputeBudgetProgram",
Address: computeBudgetProgramAddress,
IsSigner: true,
IsWritable: false,
},
// Account constant
chainwriter.AccountConstant{
AccountConstant{
Name: "SysvarProgram",
Address: sysvarProgramAddress,
IsSigner: true,
Expand All @@ -327,10 +325,10 @@ func TestGetAddresses(t *testing.T) {
DebugIDLocation: "",
}

chainWriterConfig := chainwriter.ChainWriterConfig{
Programs: map[string]chainwriter.ProgramConfig{
chainWriterConfig := ChainWriterConfig{
Programs: map[string]ProgramConfig{
"ccip-router": {
Methods: map[string]chainwriter.MethodConfig{
Methods: map[string]MethodConfig{
"execute": executeConfig,
"commit": commitConfig,
},
Expand Down

0 comments on commit ed1270d

Please sign in to comment.