Skip to content

Commit a4f666c

Browse files
committed
Add setIDL
1 parent 77f9639 commit a4f666c

1 file changed

Lines changed: 47 additions & 0 deletions

File tree

chain/solana/solana_chain.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,53 @@ func (c Chain) DeployProgram(logger logger.Logger, programInfo ProgramInfo, isUp
161161
return parseProgramID(output, prefix)
162162
}
163163

164+
func (c Chain) SetIDL(logger logger.Logger, programID string, programName string, isUpgrade bool) error {
165+
if _, err := exec.LookPath("npx"); err != nil {
166+
logger.Infow("Skipping setIDL, npx not installed", "program", programName)
167+
}
168+
169+
idlFile := filepath.Join(c.ProgramsPath, "..", "idl", programName+".json")
170+
if _, err := os.Stat(idlFile); err != nil {
171+
return fmt.Errorf("IDL file not found: %w", err)
172+
}
173+
174+
// TODO: if isUpgrade
175+
// cliCommand = upgrade
176+
177+
baseArgs := []string{
178+
"@solana-program/program-metadata@latest",
179+
"write",
180+
"idl",
181+
programID,
182+
idlFile,
183+
"--keypair", c.KeypairPath,
184+
"--rpc", c.URL,
185+
}
186+
187+
cmd := exec.CommandContext(context.TODO(), "npx", baseArgs...) // #nosec G204
188+
logger.Infof("Setting IDL with command: %s", cmd.String())
189+
190+
// Capture the command output
191+
var stdout, stderr bytes.Buffer
192+
cmd.Stdout = &stdout
193+
cmd.Stderr = &stderr
194+
195+
// Run the command
196+
if err := cmd.Run(); err != nil {
197+
logger.Errorw("Error setting IDL",
198+
"error", err,
199+
"stdout", stdout.String(),
200+
"stderr", stderr.String())
201+
202+
return err
203+
}
204+
logger.Infow("Set IDL",
205+
"stdout", stdout.String(),
206+
"stderr", stderr.String())
207+
208+
return nil
209+
}
210+
164211
func (c Chain) GetAccountDataBorshInto(ctx context.Context, pubkey sollib.PublicKey, accountState interface{}) error {
165212
err := solCommonUtil.GetAccountDataBorshInto(ctx, c.Client, pubkey, SolDefaultCommitment, accountState)
166213
if err != nil {

0 commit comments

Comments
 (0)