@project-serum/anchor#Idl TypeScript Examples
The following examples show how to use
@project-serum/anchor#Idl.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: generateIndex.ts From solana-program-registry with Apache License 2.0 | 6 votes |
fetchAndWriteIDL = async ({
indexDir,
release,
isLatest,
}: {
indexDir: string;
release: VerifiableProgramRelease;
isLatest: boolean;
}) => {
try {
const { data: idl } = await axios.get<Idl>(
buildURL({
slug: release.build.build.slug,
file: `idl/${release.program.name}.json`,
})
);
await fs.writeFile(
`${indexDir}releases/by-name/${release.id}.idl.json`,
JSON.stringify(idl)
);
if (isLatest) {
await fs.writeFile(
`${indexDir}idls/${release.program.address}.json`,
JSON.stringify(idl)
);
}
} catch (e) {
if ((e as AxiosError).response?.status !== 404) {
throw e;
}
console.warn(
`Could not find idl for ${release.build.build.repoName} ${release.build.build.tag}`
);
}
}
Example #2
Source File: get-metadata.ts From candy-machine-v2 with MIT License | 6 votes |
getMetadata = async (
program: Program<Idl>,
candyMachineId: web3.PublicKey
) => {
const state: any = await program.account.candyMachine.fetch(candyMachineId);
const itemsAvailable = state.data.itemsAvailable.toNumber();
const itemsRedeemed = state.itemsRedeemed.toNumber();
const itemsRemaining = itemsAvailable - itemsRedeemed;
return {
id: candyMachineId,
program,
state: {
itemsAvailable,
itemsRedeemed,
itemsRemaining,
isSoldOut: itemsRemaining === 0,
isActive:
state.data.goLiveDate.toNumber() < new Date().getTime() / 1000 &&
(state.endSettings
? state.endSettings.endSettingType.date
? state.endSettings.number.toNumber() > new Date().getTime() / 1000
: itemsRedeemed < state.endSettings.number.toNumber()
: true),
goLiveDate: state.data.goLiveDate,
treasury: state.wallet,
tokenMint: state.tokenMint,
gatekeeper: state.data.gatekeeper,
endSettings: state.data.endSettings,
whitelistMintSettings: state.data.whitelistMintSettings,
hiddenSettings: state.data.hiddenSettings,
price: state.data.price,
},
};
}
Example #3
Source File: clearingHouse.ts From protocol-v1 with Apache License 2.0 | 6 votes |
/**
* Update the wallet to use for clearing house transactions and linked user account
* @param newWallet
*/
public updateWallet(newWallet: IWallet): void {
const newProvider = new AnchorProvider(
this.connection,
newWallet,
this.opts
);
const newProgram = new Program(
clearingHouseIDL as Idl,
this.program.programId,
newProvider
);
// Update provider for txSender with new wallet details
this.txSender.provider = newProvider;
this.wallet = newWallet;
this.provider = newProvider;
this.program = newProgram;
this.userAccountPublicKey = undefined;
this.userAccount = undefined;
this.userOrdersAccountPublicKey = undefined;
this.userOrdersExist = undefined;
}
Example #4
Source File: mockUSDCFaucet.ts From protocol-v1 with Apache License 2.0 | 6 votes |
public constructor(
connection: Connection,
wallet: IWallet,
programId: PublicKey,
opts?: ConfirmOptions
) {
this.connection = connection;
this.wallet = wallet;
this.opts = opts || AnchorProvider.defaultOptions();
const provider = new AnchorProvider(connection, wallet, this.opts);
this.provider = provider;
this.program = new Program(mockUSDCFaucetIDL as Idl, programId, provider);
}
Example #5
Source File: switchboardClient.ts From protocol-v1 with Apache License 2.0 | 6 votes |
async function getSwitchboardProgram(
env: DriftEnv,
connection: Connection
): Promise<Program> {
const DEFAULT_KEYPAIR = Keypair.fromSeed(new Uint8Array(32).fill(1));
const programId = getSwitchboardPid(env);
const wallet = new Wallet(DEFAULT_KEYPAIR);
const provider = new AnchorProvider(connection, wallet, {});
return new Program(switchboardV2Idl as Idl, programId, provider);
}
Example #6
Source File: index.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
* Create a new client for interacting with the Jet staking program.
* @param {Provider} provider The provider with wallet/network access that can be used to send transactions.
* @param {PublicKey} programId
* @returns {Promise<Program<Idl>>} The client
* @memberof JetClient
*/
export async function connect<T extends Idl>(programId: Address, provider: Provider): Promise<Program<T>> {
const idl = await Program.fetchIdl<T>(programId, provider)
if (!idl) {
throw new Error("Program lacks an IDL account.")
}
return new Program(idl, programId, provider)
}
Example #7
Source File: index.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
* Fetches multiple idls from the blockchain.
*
* In order to use this method, an IDL must have been previously initialized
* via the anchor CLI's `anchor idl init` command.
*
* @param programIds The on-chain addresses of the programs.
* @param provider The network and wallet context.
*/
export async function fetchMultipleIdls<Idls extends Idl[] = Idl[]>(
provider: Provider,
programIds: Address[]
): Promise<Idls> {
const idlAddresses: PublicKey[] = []
for (const programId of programIds) {
const programAddress = translateAddress(programId)
const idlAddr = await idlAddress(programAddress)
idlAddresses.push(idlAddr)
}
const accountInfos = await provider.connection.getMultipleAccountsInfo(idlAddresses)
const idls: Idl[] = []
for (const accountInfo of accountInfos) {
if (!accountInfo) {
throw new Error("Idl does not exists")
}
const idlAccount = decodeIdlAccount(accountInfo.data.slice(8))
const inflatedIdl = inflate(idlAccount.data)
const idl = JSON.parse(utf8.decode(inflatedIdl))
idls.push(idl)
}
return idls as Idls
}
Example #8
Source File: events.ts From zo-client with Apache License 2.0 | 5 votes |
export function decodeDexEvent(msg: string) {
try {
const coder = new Coder(DEX_IDL as Idl);
return decodeMsg(coder, msg);
} catch (_) {
return null;
}
}
Example #9
Source File: events.ts From zo-client with Apache License 2.0 | 5 votes |
export function decodeZoEvent(msg: string) {
try {
const coder = new Coder(IDL as Idl);
return decodeMsg(coder, msg);
} catch (_) {
return null;
}
}
Example #10
Source File: hello-world.ts From protocol with Apache License 2.0 | 5 votes |
program = new anchor.Program(
dialectIdl as Idl,
new PublicKey(programs[NETWORK_NAME].programAddress),
new Provider(local, wallet, anchor.Provider.defaultOptions()),
)
Example #11
Source File: clearingHouse.ts From protocol-v1 with Apache License 2.0 | 5 votes |
export function getClearingHouse(config: ClearingHouseConfig): ClearingHouse {
const provider = new AnchorProvider(
config.connection,
config.wallet,
config.opts
);
const program = new Program(
clearingHouseIDL as Idl,
config.programID,
provider
);
let accountSubscriber: ClearingHouseAccountSubscriber;
if (config.type === 'websocket') {
accountSubscriber = new WebSocketClearingHouseAccountSubscriber(program);
} else if (config.type === 'polling') {
accountSubscriber = new PollingClearingHouseAccountSubscriber(
program,
(config as PollingClearingHouseConfiguration).accountLoader
);
}
let txSender: TxSender;
if (config.txSenderConfig?.type === 'retry') {
const txSenderConfig = config.txSenderConfig as RetryTxSenderConfig;
txSender = new RetryTxSender(
provider,
txSenderConfig.timeout,
txSenderConfig.retrySleep,
txSenderConfig.additionalConnections
);
} else {
txSender = new DefaultTxSender(provider);
}
return new ClearingHouse(
config.connection,
config.wallet,
program,
accountSubscriber,
txSender,
config.opts
);
}
Example #12
Source File: clearingHouse.ts From protocol-v1 with Apache License 2.0 | 5 votes |
export function getAdmin(config: ClearingHouseConfig): Admin {
const provider = new AnchorProvider(
config.connection,
config.wallet,
config.opts
);
const program = new Program(
clearingHouseIDL as Idl,
config.programID,
provider
);
let accountSubscriber: ClearingHouseAccountSubscriber;
if (config.type === 'websocket') {
accountSubscriber = new WebSocketClearingHouseAccountSubscriber(program);
} else if (config.type === 'polling') {
accountSubscriber = new PollingClearingHouseAccountSubscriber(
program,
(config as PollingClearingHouseConfiguration).accountLoader
);
}
let txSender: TxSender;
if (config.txSenderConfig?.type === 'retry') {
const txSenderConfig = config.txSenderConfig as RetryTxSenderConfig;
txSender = new RetryTxSender(
provider,
txSenderConfig.timeout,
txSenderConfig.retrySleep,
txSenderConfig.additionalConnections
);
} else {
txSender = new DefaultTxSender(provider);
}
return new Admin(
config.connection,
config.wallet,
program,
accountSubscriber,
txSender,
config.opts
);
}
Example #13
Source File: idl.ts From serum-ts with Apache License 2.0 | 4 votes |
IDL: Idl = {
version: '0.0.0',
name: 'swap',
instructions: [
{
name: 'swap',
accounts: [
{
name: 'market',
accounts: [
{
name: 'market',
isMut: true,
isSigner: false,
},
{
name: 'openOrders',
isMut: true,
isSigner: false,
},
{
name: 'requestQueue',
isMut: true,
isSigner: false,
},
{
name: 'eventQueue',
isMut: true,
isSigner: false,
},
{
name: 'bids',
isMut: true,
isSigner: false,
},
{
name: 'asks',
isMut: true,
isSigner: false,
},
{
name: 'orderPayerTokenAccount',
isMut: true,
isSigner: false,
},
{
name: 'coinVault',
isMut: true,
isSigner: false,
},
{
name: 'pcVault',
isMut: true,
isSigner: false,
},
{
name: 'vaultSigner',
isMut: false,
isSigner: false,
},
{
name: 'coinWallet',
isMut: true,
isSigner: false,
},
],
},
{
name: 'authority',
isMut: false,
isSigner: true,
},
{
name: 'pcWallet',
isMut: true,
isSigner: false,
},
{
name: 'dexProgram',
isMut: false,
isSigner: false,
},
{
name: 'tokenProgram',
isMut: false,
isSigner: false,
},
{
name: 'rent',
isMut: false,
isSigner: false,
},
],
args: [
{
name: 'side',
type: {
defined: 'Side',
},
},
{
name: 'amount',
type: 'u64',
},
{
name: 'minExpectedSwapAmount',
type: 'u64',
},
],
},
{
name: 'swapTransitive',
accounts: [
{
name: 'from',
accounts: [
{
name: 'market',
isMut: true,
isSigner: false,
},
{
name: 'openOrders',
isMut: true,
isSigner: false,
},
{
name: 'requestQueue',
isMut: true,
isSigner: false,
},
{
name: 'eventQueue',
isMut: true,
isSigner: false,
},
{
name: 'bids',
isMut: true,
isSigner: false,
},
{
name: 'asks',
isMut: true,
isSigner: false,
},
{
name: 'orderPayerTokenAccount',
isMut: true,
isSigner: false,
},
{
name: 'coinVault',
isMut: true,
isSigner: false,
},
{
name: 'pcVault',
isMut: true,
isSigner: false,
},
{
name: 'vaultSigner',
isMut: false,
isSigner: false,
},
{
name: 'coinWallet',
isMut: true,
isSigner: false,
},
],
},
{
name: 'to',
accounts: [
{
name: 'market',
isMut: true,
isSigner: false,
},
{
name: 'openOrders',
isMut: true,
isSigner: false,
},
{
name: 'requestQueue',
isMut: true,
isSigner: false,
},
{
name: 'eventQueue',
isMut: true,
isSigner: false,
},
{
name: 'bids',
isMut: true,
isSigner: false,
},
{
name: 'asks',
isMut: true,
isSigner: false,
},
{
name: 'orderPayerTokenAccount',
isMut: true,
isSigner: false,
},
{
name: 'coinVault',
isMut: true,
isSigner: false,
},
{
name: 'pcVault',
isMut: true,
isSigner: false,
},
{
name: 'vaultSigner',
isMut: false,
isSigner: false,
},
{
name: 'coinWallet',
isMut: true,
isSigner: false,
},
],
},
{
name: 'authority',
isMut: false,
isSigner: true,
},
{
name: 'pcWallet',
isMut: true,
isSigner: false,
},
{
name: 'dexProgram',
isMut: false,
isSigner: false,
},
{
name: 'tokenProgram',
isMut: false,
isSigner: false,
},
{
name: 'rent',
isMut: false,
isSigner: false,
},
],
args: [
{
name: 'amount',
type: 'u64',
},
{
name: 'minExpectedSwapAmount',
type: 'u64',
},
],
},
],
types: [
{
name: 'Side',
type: {
kind: 'enum',
variants: [
{
name: 'Bid',
},
{
name: 'Ask',
},
],
},
},
],
events: [
{
name: 'DidSwap',
fields: [
{
name: 'given_amount',
type: 'u64',
index: false,
},
{
name: 'min_expected_swap_amount',
type: 'u64',
index: false,
},
{
name: 'from_amount',
type: 'u64',
index: false,
},
{
name: 'to_amount',
type: 'u64',
index: false,
},
{
name: 'spill_amount',
type: 'u64',
index: false,
},
{
name: 'from_mint',
type: 'publicKey',
index: false,
},
{
name: 'to_mint',
type: 'publicKey',
index: false,
},
{
name: 'quote_mint',
type: 'publicKey',
index: false,
},
{
name: 'authority',
type: 'publicKey',
index: false,
},
],
},
],
errors: [
{
code: 100,
name: 'SwapTokensCannotMatch',
msg: 'The tokens being swapped must have different mints',
},
{
code: 101,
name: 'SlippageExceeded',
msg: 'Slippage tolerance exceeded',
},
],
}