Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Metaplex common #384

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
"@solana/spl-token-swap": "0.1.0",
"@solana/wallet-base": "0.0.1",
"@solana/wallet-ledger": "0.0.1",
"@solana/web3.js": "^1.22.0",
"@solana/web3.js": "^1.29.2"
"@testing-library/jest-dom": "^4.2.4",
"@testing-library/react": "^9.5.0",
"@testing-library/user-event": "^7.2.1",
Expand All @@ -42,7 +42,7 @@
"antd": "^4.6.6",
"bignumber.js": "^9.0.1",
"bn.js": "^5.1.3",
"borsh": "^0.3.1",
"borsh": "^0.4.0",
"bs58": "^4.0.1",
"buffer-layout": "^1.2.0",
"eventemitter3": "^4.0.7",
Expand Down
76 changes: 63 additions & 13 deletions packages/common/src/actions/account.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { AccountLayout, MintLayout, Token } from '@solana/spl-token';
import {
Account,
Keypair,
PublicKey,
SystemProgram,
SYSVAR_RENT_PUBKEY,
Expand All @@ -11,6 +11,7 @@ import {
TOKEN_PROGRAM_ID,
WRAPPED_SOL_MINT,
} from '../utils/ids';
import { programIds } from '../utils/programIds';
import { TokenAccount } from '../models/account';
import { cache, TokenAccountParser } from '../contexts/accounts';

Expand All @@ -20,7 +21,7 @@ export function ensureSplAccount(
toCheck: TokenAccount,
payer: PublicKey,
amount: number,
signers: Account[],
signers: Keypair[],
) {
if (!toCheck.info.isNative) {
return toCheck.pubkey;
Expand Down Expand Up @@ -60,11 +61,11 @@ export const DEFAULT_TEMP_MEM_SPACE = 65548;
export function createTempMemoryAccount(
instructions: TransactionInstruction[],
payer: PublicKey,
signers: Account[],
signers: Keypair[],
owner: PublicKey,
space = DEFAULT_TEMP_MEM_SPACE,
) {
const account = new Account();
const account = Keypair.generate();
instructions.push(
SystemProgram.createAccount({
fromPubkey: payer,
Expand All @@ -85,9 +86,9 @@ export function createUninitializedMint(
instructions: TransactionInstruction[],
payer: PublicKey,
amount: number,
signers: Account[],
signers: Keypair[],
) {
const account = new Account();
const account = Keypair.generate();
instructions.push(
SystemProgram.createAccount({
fromPubkey: payer,
Expand All @@ -107,9 +108,9 @@ export function createUninitializedAccount(
instructions: TransactionInstruction[],
payer: PublicKey,
amount: number,
signers: Account[],
signers: Keypair[],
) {
const account = new Account();
const account = Keypair.generate();
instructions.push(
SystemProgram.createAccount({
fromPubkey: payer,
Expand Down Expand Up @@ -185,7 +186,7 @@ export function createMint(
decimals: number,
owner: PublicKey,
freezeAuthority: PublicKey,
signers: Account[],
signers: Keypair[],
) {
const account = createUninitializedMint(
instructions,
Expand Down Expand Up @@ -213,7 +214,7 @@ export function createTokenAccount(
accountRentExempt: number,
mint: PublicKey,
owner: PublicKey,
signers: Account[],
signers: Keypair[],
) {
const account = createUninitializedAccount(
instructions,
Expand All @@ -229,6 +230,54 @@ export function createTokenAccount(
return account;
}

export function ensureWrappedAccount(
instructions: TransactionInstruction[],
cleanupInstructions: TransactionInstruction[],
toCheck: TokenAccount | undefined,
payer: PublicKey,
amount: number,
signers: Keypair[],
) {
if (toCheck && !toCheck.info.isNative) {
return toCheck.pubkey;
}

const TOKEN_PROGRAM_ID = programIds().token;
const account = Keypair.generate();
instructions.push(
SystemProgram.createAccount({
fromPubkey: payer,
newAccountPubkey: account.publicKey,
lamports: amount,
space: AccountLayout.span,
programId: TOKEN_PROGRAM_ID,
}),
);

instructions.push(
Token.createInitAccountInstruction(
TOKEN_PROGRAM_ID,
WRAPPED_SOL_MINT,
account.publicKey,
payer,
),
);

cleanupInstructions.push(
Token.createCloseAccountInstruction(
TOKEN_PROGRAM_ID,
account.publicKey,
payer,
payer,
[],
),
);

signers.push(account);

return account.publicKey.toBase58();
}

// TODO: check if one of to accounts needs to be native sol ... if yes unwrap it ...
export function findOrCreateAccountByMint(
payer: PublicKey,
Expand All @@ -237,19 +286,20 @@ export function findOrCreateAccountByMint(
cleanupInstructions: TransactionInstruction[],
accountRentExempt: number,
mint: PublicKey, // use to identify same type
signers: Account[],
signers: Keypair[],
excluded?: Set<string>,
): PublicKey {
const accountToFind = mint.toBase58();
const ownerKey = owner.toBase58();
const account = cache
.byParser(TokenAccountParser)
.map(id => cache.get(id))
.find(
acc =>
acc !== undefined &&
acc.info.mint.toBase58() === accountToFind &&
acc.info.owner.toBase58() === owner.toBase58() &&
(excluded === undefined || !excluded.has(acc.pubkey.toBase58())),
acc.info.owner.toBase58() === ownerKey &&
(excluded === undefined || !excluded.has(acc.pubkey.toString())),
);
const isWrappedSol = accountToFind === WRAPPED_SOL_MINT.toBase58();

Expand Down
Loading