-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: read program id from deployed addresses or pass it as arg
Signed-off-by: Pablo Maldonado <[email protected]>
- Loading branch information
Showing
4 changed files
with
59 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,61 @@ | ||
import { Idl, Program, Provider } from "@coral-xyz/anchor"; | ||
import { Idl, Program, AnchorProvider } from "@coral-xyz/anchor"; | ||
import { getSolanaChainId, isSolanaDevnet } from "../../scripts/svm/utils/helpers"; | ||
import { getDeployedAddress } from "../DeploymentUtils"; | ||
import { SupportedNetworks } from "../types/svm"; | ||
import { | ||
SvmSpokeIdl, | ||
SvmSpokeAnchor, | ||
MessageTransmitterAnchor, | ||
MessageTransmitterIdl, | ||
TokenMessengerMinterAnchor, | ||
TokenMessengerMinterIdl, | ||
MulticallHandlerAnchor, | ||
MulticallHandlerIdl, | ||
SvmSpokeAnchor, | ||
SvmSpokeIdl, | ||
TokenMessengerMinterAnchor, | ||
TokenMessengerMinterIdl, | ||
} from "./assets"; | ||
|
||
export function getConnectedProgram<P extends Idl>(idl: P, provider: Provider) { | ||
type ProgramOptions = { network?: SupportedNetworks; programId?: string }; | ||
|
||
export function getConnectedProgram<P extends Idl>(idl: P, provider: AnchorProvider, programId: string) { | ||
idl.address = programId; | ||
return new Program<P>(idl, provider); | ||
} | ||
|
||
export function getSpokePoolProgram(provider: Provider) { | ||
return getConnectedProgram<SvmSpokeAnchor>(SvmSpokeIdl, provider); | ||
// Resolves the program ID from options or defaults to the deployed address. Prioritizes programId, falls back to | ||
// network, and if network is not defined, determines the network from the provider's RPC URL. Throws an error if | ||
// the program ID cannot be resolved. | ||
function resolveProgramId(programName: string, provider: AnchorProvider, options?: ProgramOptions): string { | ||
const { network, programId } = options ?? {}; | ||
|
||
if (programId) { | ||
return programId; // Prioritize explicitly provided programId | ||
} | ||
|
||
const resolvedNetwork = network ?? (isSolanaDevnet(provider) ? "devnet" : "mainnet"); | ||
const deployedAddress = getDeployedAddress(programName, getSolanaChainId(resolvedNetwork).toString()); | ||
|
||
if (!deployedAddress) { | ||
throw new Error(`${programName} Program ID not found for ${resolvedNetwork}`); | ||
} | ||
|
||
return deployedAddress; | ||
} | ||
|
||
export function getSpokePoolProgram(provider: AnchorProvider, options?: ProgramOptions) { | ||
const id = resolveProgramId("SvmSpoke", provider, options); | ||
return getConnectedProgram<SvmSpokeAnchor>(SvmSpokeIdl, provider, id); | ||
} | ||
|
||
export function getMessageTransmitterProgram(provider: Provider) { | ||
return getConnectedProgram<MessageTransmitterAnchor>(MessageTransmitterIdl, provider); | ||
export function getMessageTransmitterProgram(provider: AnchorProvider, options?: ProgramOptions) { | ||
const id = resolveProgramId("MessageTransmitter", provider, options); | ||
return getConnectedProgram<MessageTransmitterAnchor>(MessageTransmitterIdl, provider, id); | ||
} | ||
|
||
export function getTokenMessengerMinterProgram(provider: Provider) { | ||
return getConnectedProgram<TokenMessengerMinterAnchor>(TokenMessengerMinterIdl, provider); | ||
export function getTokenMessengerMinterProgram(provider: AnchorProvider, options?: ProgramOptions) { | ||
const id = resolveProgramId("TokenMessengerMinter", provider, options); | ||
return getConnectedProgram<TokenMessengerMinterAnchor>(TokenMessengerMinterIdl, provider, id); | ||
} | ||
|
||
export function getMulticallHandlerProgram(provider: Provider) { | ||
return getConnectedProgram<MulticallHandlerAnchor>(MulticallHandlerIdl, provider); | ||
export function getMulticallHandlerProgram(provider: AnchorProvider, options?: ProgramOptions) { | ||
const id = resolveProgramId("MulticallHandler", provider, options); | ||
return getConnectedProgram<MulticallHandlerAnchor>(MulticallHandlerIdl, provider, id); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters