Skip to content
Merged
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
13 changes: 13 additions & 0 deletions apps/web/content/cookbook/accounts/calculate-rent.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ if the account is closed.

<CodeTabs storage="cookbook" flags="r">

```ts !! title="Gill"
import { createSolanaClient } from "gill";

const { rpc } = createSolanaClient({
urlOrMoniker: "localnet"
});

const space = 1500n; // bytes
// !mark
const lamports = await rpc.getMinimumBalanceForRentExemption(space).send();
console.log("Minimum balance for rent exemption:", lamports);
```

```ts !! title="Kit"
import { createSolanaRpc } from "@solana/kit";

Expand Down
66 changes: 66 additions & 0 deletions apps/web/content/cookbook/accounts/create-account.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,72 @@ can then initialize the account data through its own instructions.

<CodeTabs storage="cookbook" flags="r">

```ts !! title="Gill"
import {
airdropFactory,
appendTransactionMessageInstructions,
createSolanaRpc,
createSolanaRpcSubscriptions,
createTransaction,
generateKeyPairSigner,
getSignatureFromTransaction,
lamports,
pipe,
sendAndConfirmTransactionFactory,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners
} from "gill";
import {
getCreateAccountInstruction,
SYSTEM_PROGRAM_ADDRESS
} from "@solana-program/system";

const { rpc, sendAndConfirmTransaction } = createSolanaClient({
urlOrMoniker: "localnet"
});
const rpcSubscriptions = createSolanaRpcSubscriptions("ws://localhost:8900");

const sender = await generateKeyPairSigner();
const LAMPORTS_PER_SOL = 1_000_000_000n;
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: sender.address,
lamports: lamports(LAMPORTS_PER_SOL), // 1 SOL
commitment: "confirmed"
});

const newAccount = await generateKeyPairSigner();

const space = 0n;
const rentLamports = await rpc.getMinimumBalanceForRentExemption(space).send();

// !mark(1:7)
const createAccountInstruction = getCreateAccountInstruction({
payer: sender,
newAccount: newAccount,
lamports: rentLamports,
programAddress: SYSTEM_PROGRAM_ADDRESS,
space
});

const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const transactionMessage = createTransaction({
feePayer: sender,
version: "legacy",
instructions: [createAccountInstruction],
latestBlockhash
});

const signedTransaction =
await signTransactionMessageWithSigners(transactionMessage);
await sendAndConfirmTransactionFactory({ rpc, rpcSubscriptions })(
signedTransaction,
{ commitment: "confirmed" }
);
const transactionSignature = getSignatureFromTransaction(signedTransaction);
console.log("Transaction Signature for create account:", transactionSignature);
```

```ts !! title="Kit"
import {
airdropFactory,
Expand Down
14 changes: 14 additions & 0 deletions apps/web/content/cookbook/accounts/get-account-balance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@ Every Solana account is required to maintain a minimum balance of native SOL

<CodeTabs storage="cookbook" flags="r">

```ts !! title="Gill"
import { createSolanaClient, Address } from "gill";

const { rpc } = createSolanaClient({
urlOrMoniker: "mainnet"
});

const addresss = "TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA" as Address;
// !mark
const { value } = await rpc.getBalance(addresss).send();

console.log(`Balance: ${Number(value) / 1_000_000_000} SOL`);
```

```ts !! title="Kit"
import { createSolanaRpc, Address } from "@solana/kit";

Expand Down
7 changes: 4 additions & 3 deletions apps/web/content/cookbook/development/subscribing-events.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,20 @@ from solders.keypair import Keypair

async def main():
keypair = Keypair()

async with connect("wss://api.devnet.solana.com") as websocket:
# Subscribe to account changes
await websocket.account_subscribe(keypair.pubkey())

# Subscribe to logs
await websocket.logs_subscribe()

# Listen for messages
async for message in websocket:
print(f"Received: {message}")

if __name__ == "__main__":
asyncio.run(main())
```

</CodeTabs>
60 changes: 60 additions & 0 deletions apps/web/content/cookbook/tokens/get-all-token-accounts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,38 @@ You can fetch token accounts by owner. There are two ways to do it.

<CodeTabs storage="cookbook" flags="r">

```ts !! title="Gill"
import { address, createSolanaClient } from "gill";
import { TOKEN_PROGRAM_ADDRESS } from "gill/programs/token";

const { rpc } = createSolanaClient({
urlOrMoniker: "devnet"
});

let owner = address("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF");

let response = await rpc
.getTokenAccountsByOwner(
owner,
{ programId: TOKEN_PROGRAM_ADDRESS },
{ encoding: "jsonParsed" }
)
.send();

response.value.forEach((accountInfo) => {
console.log(`pubkey:${accountInfo.pubkey}`);
console.log(`Mint:${accountInfo.account.data["parsed"]["info"]["mint"]}`);
console.log(`Owner:${accountInfo.account.data["parsed"]["info"]["owner"]}`);
console.log(
`Decimals:${accountInfo.account.data["parsed"]["info"]["tokenAmount"]["decimals"]}`
);
console.log(
`Amount:${accountInfo.account.data["parsed"]["info"]["tokenAmount"]["amount"]}`
);
console.log("=====================");
});
```

```ts !! title="Kit"
import { TOKEN_PROGRAM_ADDRESS } from "@solana-program/token";
import { address, createSolanaRpc } from "@solana/kit";
Expand Down Expand Up @@ -131,6 +163,34 @@ async fn main() -> anyhow::Result<()> {

<CodeTabs storage="cookbook" flags="r">

```ts !! title="Gill"
import { address, createSolanaClient } from "gill";

const { rpc } = createSolanaClient({
urlOrMoniker: "devnet"
});

let owner = address("4kg8oh3jdNtn7j2wcS7TrUua31AgbLzDVkBZgTAe44aF");
let mint = address("6sgxNSdXgkEFVLA2YEQFnuFHU3WGafhu9WYzXAXY7yCq");

let response = await rpc
.getTokenAccountsByOwner(owner, { mint }, { encoding: "jsonParsed" })
.send();

response.value.forEach((accountInfo) => {
console.log(`pubkey: ${accountInfo.pubkey}`);
console.log(`mint: ${accountInfo.account.data["parsed"]["info"]["mint"]}`);
console.log(`owner: ${accountInfo.account.data["parsed"]["info"]["owner"]}`);
console.log(
`decimals: ${accountInfo.account.data["parsed"]["info"]["tokenAmount"]["decimals"]}`
);
console.log(
`amount: ${accountInfo.account.data["parsed"]["info"]["tokenAmount"]["amount"]}`
);
console.log("====================");
});
```

```ts !! title="Kit"
import { address, createSolanaRpc } from "@solana/kit";

Expand Down
28 changes: 28 additions & 0 deletions apps/web/content/cookbook/tokens/get-token-account.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,34 @@ amount(balance).

<CodeTabs storage="cookbook" flags="r">

```ts !! title="Gill"
import { createSolanaClient, address } from "gill";
import {
TOKEN_2022_PROGRAM_ADDRESS,
fetchToken,
getAssociatedTokenAccountAddress
} from "gill/programs";

const { rpc } = createSolanaClient({
urlOrMoniker: "mainnet"
});

const mintAddress = address("2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo");

const authority = address("AC5RDfQFmDS1deWZos921JfqscXdByf8BKHs5ACWjtW2");

const associatedTokenAddress = await getAssociatedTokenAccountAddress(
mintAddress,
authority,
TOKEN_2022_PROGRAM_ADDRESS
);

// !mark
const ataDetails = await fetchToken(rpc, associatedTokenAddress);

console.log(ataDetails);
```

```ts !! title="Kit"
import {
fetchToken,
Expand Down
22 changes: 20 additions & 2 deletions apps/web/content/cookbook/tokens/get-token-balance.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,23 @@ PRC call

<CodeTabs storage="cookbook" flags="r">

```ts !! title="Gill"
import { createSolanaClient, address } from "gill";

const { rpc } = createSolanaClient({
urlOrMoniker: "mainnet"
});

const tokenAccountAddress = address(
"GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ"
);

// !mark
const balance = await rpc.getTokenAccountBalance(tokenAccountAddress).send();

console.log(balance);
```

```typescript !! title="Kit"
import { address, createSolanaRpc } from "@solana/kit";

Expand Down Expand Up @@ -83,10 +100,10 @@ from solders.pubkey import Pubkey

async def main():
rpc = AsyncClient("https://api.mainnet-beta.solana.com")

# Use a real token account address from mainnet
token_account_address = Pubkey.from_string("GfVPzUxMDvhFJ1Xs6C9i47XQRSapTd8LHw5grGuTquyQ")

async with rpc:
try:
balance = await rpc.get_token_account_balance(token_account_address)
Expand All @@ -98,6 +115,7 @@ async def main():
if __name__ == "__main__":
asyncio.run(main())
```

</CodeTabs>

<Callout type="info">
Expand Down
16 changes: 16 additions & 0 deletions apps/web/content/cookbook/tokens/get-token-mint.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,22 @@ need to get the account info for the token mint.

<CodeTabs storage="cookbook" flags="r">

```ts !! title="Gill"
import { address, createSolanaClient } from "gill";
import { fetchMint } from "gill/programs";

const { rpc } = createSolanaClient({
urlOrMoniker: "mainnet"
});

const mintAddress = address("2b1kV6DkPAnxd5ixfnxCpjxmKwqjjaYmCZfHsFu24GXo");

// !mark
const mint = await fetchMint(rpc, mintAddress);

console.log(mint);
```

```ts !! title="Kit"
import { Address, createSolanaRpc } from "@solana/kit";
import { fetchMint } from "@solana-program/token-2022";
Expand Down
49 changes: 49 additions & 0 deletions apps/web/content/cookbook/transactions/add-memo.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,55 @@ Any transaction can add a message making use of the SPL memo program.

<CodeTabs storage="cookbook" flags="r">

```ts !! title="Gill"
import {
airdropFactory,
appendTransactionMessageInstructions,
createSolanaClient,
createTransaction,
generateKeyPairSigner,
getSignatureFromTransaction,
lamports,
pipe,
setTransactionMessageFeePayerSigner,
setTransactionMessageLifetimeUsingBlockhash,
signTransactionMessageWithSigners
} from "gill";
import { getAddMemoInstruction } from "gill/programs";

const { rpc, rpcSubscriptions, sendAndConfirmTransaction } = createSolanaClient(
{
urlOrMoniker: "localnet"
}
);

const sender = await generateKeyPairSigner();
const LAMPORTS_PER_SOL = 1_000_000_000n;
await airdropFactory({ rpc, rpcSubscriptions })({
recipientAddress: sender.address,
lamports: lamports(LAMPORTS_PER_SOL), // 1 SOL
commitment: "confirmed"
});

// !mark(1:3)
const memoInstruction = getAddMemoInstruction({
memo: "Hello, world!"
});

const { value: latestBlockhash } = await rpc.getLatestBlockhash().send();
const tx = createTransaction({
feePayer: sender,
version: "legacy",
instructions: [memoInstruction],
latestBlockhash
});

const signedTransaction = await signTransactionMessageWithSigners(tx);

const transactionSignature = getSignatureFromTransaction(signedTransaction);
console.log("Transaction Signature:", transactionSignature);
```

```ts !! title="Kit"
import {
airdropFactory,
Expand Down
Loading