@project-serum/anchor#translateAddress TypeScript Examples
The following examples show how to use
@project-serum/anchor#translateAddress.
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: associatedToken.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
* Get the address for the associated token account
* @static
* @param {Address} mint Token mint account
* @param {Address} owner Owner of the new account
* @returns {Promise<PublicKey>} Public key of the associated token account
* @memberof AssociatedToken
*/
static derive(mint: Address, owner: Address): PublicKey {
const mintAddress = translateAddress(mint)
const ownerAddress = translateAddress(owner)
return findDerivedAccount(ASSOCIATED_TOKEN_PROGRAM_ID, ownerAddress, TOKEN_PROGRAM_ID, mintAddress)
}
Example #2
Source File: associatedToken.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
* TODO:
* @static
* @param {Provider} provider
* @param {Address} mint
* @param {Address} owner
* @returns {(Promise<AssociatedToken | undefined>)}
* @memberof AssociatedToken
*/
static async load(connection: Connection, mint: Address, owner: Address): Promise<AssociatedToken | undefined> {
const mintAddress = translateAddress(mint)
const ownerAddress = translateAddress(owner)
const address = this.derive(mintAddress, ownerAddress)
const token = await this.loadAux(connection, address)
if (token && !token.info.owner.equals(ownerAddress)) {
throw new Error("Unexpected owner of the associated token")
}
return token
}
Example #3
Source File: associatedToken.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
static async loadAux(connection: Connection, address: Address) {
const pubkey = translateAddress(address)
const account = await connection.getAccountInfo(pubkey)
if (!account) {
return undefined
}
const info = parseTokenAccount(account, pubkey)
return new AssociatedToken(account, info)
}
Example #4
Source File: associatedToken.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
static async loadMultipleAux(connection: Connection, addresses: Address[]): Promise<(AssociatedToken | undefined)[]> {
const pubkeys = addresses.map(translateAddress)
const accounts = await connection.getMultipleAccountsInfo(pubkeys)
return accounts.map((account, i) => {
if (!account) {
return undefined
}
const info = parseTokenAccount(account, pubkeys[i])
return new AssociatedToken(account, info)
})
}
Example #5
Source File: associatedToken.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/** TODO:
* Get mint info
* @static
* @param {Provider} connection
* @param {Address} mint
* @returns {(Promise<Mint | undefined>)}
* @memberof AssociatedToken
*/
static async loadMint(connection: Connection, mint: Address): Promise<Mint | undefined> {
const mintAddress = translateAddress(mint)
const mintInfo = await connection.getAccountInfo(mintAddress)
if (!mintInfo) {
return undefined
}
return parseMintAccount(mintInfo, mintAddress)
}
Example #6
Source File: index.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
* Derive a PDA from the argued list of seeds.
* @param {PublicKey} programId
* @param {AccountSeed[]} seeds
* @returns {Promise<PublicKey>}
* @memberof JetClient
*/
export function findDerivedAccount(programId: Address, ...seeds: AccountSeed[]): PublicKey {
const seedBytes = seeds.map(s => {
if (typeof s == "string") {
const pubkeyBytes = bs58.decodeUnsafe(s)
if (!pubkeyBytes || pubkeyBytes.length !== 32) {
return Buffer.from(s)
} else {
return translateAddress(s).toBytes()
}
} else if ("publicKey" in s) {
return s.publicKey.toBytes()
} else if ("toBytes" in s) {
return s.toBytes()
} else {
return s
}
})
const [address] = findProgramAddressSync(seedBytes, translateAddress(programId))
return address
}
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: marginAccount.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
*
* @param {Program<JetMarginIdl>} marginProgram
* @param {Address} owner
* @param {number} seed
* @returns {Promise<MarginAccount>}
*/
static async load(marginProgram: Program<JetMarginIdl>, owner: Address, seed: number): Promise<MarginAccount> {
const ownerPubkey = translateAddress(owner)
const address = this.derive(marginProgram.programId, ownerPubkey, seed)
const marginAccount = await marginProgram.account.marginAccount.fetch(address)
const positions = AccountPositionListLayout.decode(new Uint8Array(marginAccount.positions))
return new MarginAccount(marginProgram, address, marginAccount, positions)
}
Example #9
Source File: marginAccount.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
* Build instruction for Create Margin Account
*
* @static
* @param {TransactionInstruction[]} instructions
* @param {Program<JetMarginIdl>} program
* @param {Address} owner
* @param {number} seed
* @memberof MarginAccount
*/
static withCreate(
instructions: TransactionInstruction[],
program: Program<JetMarginIdl>,
owner: Address,
seed: number
): void {
const ownerAddress = translateAddress(owner)
const marginAccount = this.derive(program.programId, ownerAddress, seed)
const createInfo = {
accounts: {
owner,
payer: owner,
marginAccount: marginAccount,
systemProgram: SystemProgram.programId
},
args: {
seed: seed
}
}
instructions.push(program.instruction.createAccount(seed, createInfo))
}
Example #10
Source File: marginPool.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
* Load a Margin Pool Account
* @param {Program<JetMarginPoolIdl>} program
* @param {Address} tokenMint
* @returns {Promise<MarginPool>}
*/
static async load(programs: JetPrograms, tokenMint: Address): Promise<MarginPool> {
const poolProgram = programs.marginPool
const tokenMintAddress = translateAddress(tokenMint)
const addresses = this.derive(poolProgram.programId, tokenMintAddress)
const [marginPoolInfo, poolTokenMintInfo, vaultMintInfo, depositNoteMintInfo, loanNoteMintInfo] =
await poolProgram.provider.connection.getMultipleAccountsInfo([
addresses.marginPool,
addresses.tokenMint,
addresses.vault,
addresses.depositNoteMint,
addresses.loanNoteMint
])
if (!marginPoolInfo || !poolTokenMintInfo || !vaultMintInfo || !depositNoteMintInfo || !loanNoteMintInfo) {
throw new Error("Invalid margin pool")
}
const marginPool = poolProgram.coder.accounts.decode<MarginPoolData>("marginPool", marginPoolInfo.data)
const poolTokenMint = parseMintAccount(poolTokenMintInfo, tokenMintAddress)
const vault = parseTokenAccount(vaultMintInfo, addresses.vault)
const depositNoteMint = parseMintAccount(depositNoteMintInfo, addresses.depositNoteMint)
const loanNoteMint = parseMintAccount(loanNoteMintInfo, addresses.loanNoteMint)
return new MarginPool(poolProgram, addresses, marginPool, vault, depositNoteMint, loanNoteMint, poolTokenMint)
}
Example #11
Source File: marginPool.ts From jet-engine with GNU Affero General Public License v3.0 | 6 votes |
/**
* Derive accounts from tokenMint
* @param {Address} programId
* @param {Address} tokenMint
* @returns {PublicKey} Margin Pool Address
*/
static derive(programId: Address, tokenMint: Address): MarginPoolAddresses {
const tokenMintAddress = translateAddress(tokenMint)
const marginPool = findDerivedAccount(programId, tokenMintAddress)
const vault = findDerivedAccount(programId, tokenMint, "vault")
const depositNoteMint = findDerivedAccount(programId, tokenMint, "deposit-note-mint")
const loanNoteMint = findDerivedAccount(programId, tokenMint, "loan-note-mint")
return {
tokenMint: tokenMintAddress,
marginPool,
vault,
depositNoteMint,
loanNoteMint
}
}
Example #12
Source File: index.ts From saber-periphery with GNU Affero General Public License v3.0 | 6 votes |
async function associated(
programId: Address,
...args: Array<Address | Buffer>
): Promise<PublicKey> {
const seeds = [Buffer.from([97, 110, 99, 104, 111, 114])]; // b"anchor".
args.forEach((arg) => {
seeds.push(arg instanceof Buffer ? arg : translateAddress(arg).toBuffer());
});
const [assoc] = await PublicKey.findProgramAddress(
seeds,
translateAddress(programId)
);
return assoc;
}