Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
85 changes: 40 additions & 45 deletions toolkits/payments-and-wallets/comparison-spl-light.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const ata = await getOrCreateAssociatedTokenAccount(
connection,
payer,
mint,
recipient,
recipient
);
// Share ata.address with sender

Expand All @@ -70,8 +70,8 @@ const tx = new Transaction().add(
payer.publicKey,
ata,
recipient,
mint,
),
mint
)
);
```

Expand Down Expand Up @@ -102,15 +102,15 @@ const tx = new Transaction().add(
ata,
recipient,
mint,
LIGHT_TOKEN_PROGRAM_ID,
LIGHT_TOKEN_PROGRAM_ID
),
...(await createLoadAtaInstructions(
rpc,
ata,
recipient,
mint,
payer.publicKey,
)),
payer.publicKey
))
);
```

Expand All @@ -134,7 +134,7 @@ await transfer(
destinationAta,
owner,
amount,
decimals,
decimals
);
```

Expand All @@ -156,55 +156,50 @@ const tx = new Transaction().add(
sourceAta,
destinationAta,
owner.publicKey,
amount,
),
amount
)
);
```

**Light:**

```typescript
const sourceAta = getAssociatedTokenAddressInterface(mint, owner.publicKey);
const destinationAta = getAssociatedTokenAddressInterface(mint, recipient);

await transferInterface(
rpc,
payer,
sourceAta,
mint,
destinationAta,
owner,
amount,
);
await transferInterface(rpc, payer, sourceAta, mint, recipient, owner, amount);
```

**Light:**
**Light (instruction-level):**

```typescript
import { Transaction, sendAndConfirmTransaction } from "@solana/web3.js";
import {
createLoadAtaInstructions,
createTransferInterfaceInstruction,
getAssociatedTokenAddressInterface,
createTransferInterfaceInstructions,
sliceLast,
} from "@lightprotocol/compressed-token/unified";

const sourceAta = getAssociatedTokenAddressInterface(mint, owner.publicKey);
const destinationAta = getAssociatedTokenAddressInterface(mint, recipient);

const tx = new Transaction().add(
...(await createLoadAtaInstructions(
rpc,
sourceAta,
owner.publicKey,
mint,
payer.publicKey,
)),
createTransferInterfaceInstruction(
sourceAta,
destinationAta,
owner.publicKey,
amount,
),
const batches = await createTransferInterfaceInstructions(
rpc,
payer.publicKey,
mint,
amount,
owner.publicKey,
recipient
);
const { rest: loadBatches, last: transferBatch } = sliceLast(batches);

await Promise.all(
loadBatches.map((batch) =>
sendAndConfirmTransaction(rpc, new Transaction().add(...batch), [
payer,
owner,
])
)
);
await sendAndConfirmTransaction(rpc, new Transaction().add(...transferBatch), [
payer,
owner,
]);
```

To ensure your recipient's ATA exists you can prepend an idempotent creation instruction in the same atomic transaction:
Expand All @@ -222,7 +217,7 @@ const createAtaIx = createAssociatedTokenAccountIdempotentInstruction(
payer.publicKey,
destinationAta,
recipient,
mint,
mint
);

new Transaction().add(createAtaIx, transferIx);
Expand All @@ -243,7 +238,7 @@ const createAtaIx = createAssociatedTokenAccountInterfaceIdempotentInstruction(
destinationAta,
recipient,
mint,
LIGHT_TOKEN_PROGRAM_ID,
LIGHT_TOKEN_PROGRAM_ID
);

new Transaction().add(createAtaIx, transferIx);
Expand Down Expand Up @@ -342,15 +337,15 @@ const tx = new Transaction().add(
lightTokenAta,
owner.publicKey,
mint,
payer.publicKey,
payer.publicKey
)),
createUnwrapInstruction(
lightTokenAta,
splAta,
owner.publicKey,
mint,
amount,
splInterfaceInfo,
),
splInterfaceInfo
)
);
```
14 changes: 7 additions & 7 deletions toolkits/payments-and-wallets/get-balance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const rpc = createRpc();

const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")),
),
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);

(async function () {
Expand All @@ -32,7 +32,7 @@ const payer = Keypair.fromSecretKey(
rpc,
payer,
mint,
payer,
payer
);

// 3. Mint to payer's ATA
Expand All @@ -44,7 +44,7 @@ const payer = Keypair.fromSecretKey(
rpc,
payer,
mint,
recipient,
recipient
);

// 5. Transfer from payer to recipient
Expand All @@ -53,17 +53,17 @@ const payer = Keypair.fromSecretKey(
payer,
sourceAta.address,
mint,
recipientAta.address,
recipient.publicKey,
payer,
bn(100),
bn(100)
);

// 6. Get recipient's balance after transfer
const { parsed: account } = await getAtaInterface(
rpc,
recipientAta.address,
recipient.publicKey,
mint,
mint
);
console.log("Recipient's balance:", account.amount);
})();
12 changes: 6 additions & 6 deletions toolkits/payments-and-wallets/get-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const rpc = createRpc();

const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")),
),
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);

(async function () {
Expand All @@ -31,7 +31,7 @@ const payer = Keypair.fromSecretKey(
rpc,
payer,
mint,
payer,
payer
);

// 3. Mint to payer's ATA
Expand All @@ -43,7 +43,7 @@ const payer = Keypair.fromSecretKey(
rpc,
payer,
mint,
recipient,
recipient
);

// 5. Transfer from payer to recipient
Expand All @@ -52,9 +52,9 @@ const payer = Keypair.fromSecretKey(
payer,
sourceAta.address,
mint,
recipientAta.address,
recipient.publicKey,
payer,
bn(100),
bn(100)
);

// 6. Get transaction history
Expand Down
12 changes: 6 additions & 6 deletions toolkits/payments-and-wallets/send-and-receive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const rpc = createRpc();

const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")),
),
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);

(async function () {
Expand All @@ -31,7 +31,7 @@ const payer = Keypair.fromSecretKey(
rpc,
payer,
mint,
payer,
payer
);

// 3. Mint to payer's ATA
Expand All @@ -43,7 +43,7 @@ const payer = Keypair.fromSecretKey(
rpc,
payer,
mint,
recipient,
recipient
);

// 5. Transfer from payer to recipient
Expand All @@ -52,9 +52,9 @@ const payer = Keypair.fromSecretKey(
payer,
sourceAta.address,
mint,
recipientAta.address,
recipient.publicKey,
payer,
bn(100),
bn(100)
);

console.log("Tx:", txId);
Expand Down
13 changes: 7 additions & 6 deletions typescript-client/actions/transfer-interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ const rpc = createRpc();

const payer = Keypair.fromSecretKey(
new Uint8Array(
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8")),
),
JSON.parse(readFileSync(`${homedir()}/.config/solana/id.json`, "utf8"))
)
);

(async function () {
Expand All @@ -30,26 +30,27 @@ const payer = Keypair.fromSecretKey(
await createAtaInterface(rpc, payer, mint, sender.publicKey);
const senderAta = getAssociatedTokenAddressInterface(
mint,
sender.publicKey,
sender.publicKey
);
await mintToInterface(rpc, payer, mint, senderAta, payer, 1_000_000_000);

const recipient = Keypair.generate();
await createAtaInterface(rpc, payer, mint, recipient.publicKey);
const recipientAta = getAssociatedTokenAddressInterface(
mint,
recipient.publicKey,
recipient.publicKey
);

// Transfer tokens between light-token associated token accounts
// destination is recipient wallet; transferInterface creates recipient ATA idempotently
const tx = await transferInterface(
rpc,
payer,
senderAta,
mint,
recipientAta,
recipient.publicKey,
sender,
500_000_000,
500_000_000
);

console.log("Tx:", tx);
Expand Down
Loading
Loading