跳到主要内容

封装 SOL

POST https://sol.shrine.trade/api/wrap-sol

把 SOL 转入钱包的封装 SOL 账户,或者关闭该账户完成解封;解封总是取回账户里的每一个 lamport。

{ "publicKey": "<wallet>", "action": "wrap", "amount": 0.5 }
{ "publicKey": "<wallet>", "action": "unwrap" }

示例​

构建、签名、通过中继发送。中继通过我们自己的 Solana 节点广播,所以脚本不需要 RPC。

import { Keypair, VersionedTransaction } from "@solana/web3.js";
import bs58 from "bs58";

const wallet = Keypair.fromSecretKey(bs58.decode("<your wallet private key, base58>"));

// 把半个 SOL 封装成封装 SOL;{ action: "unwrap" } 把全部换回来。
const { tx } = await (await fetch("https://sol.shrine.trade/api/wrap-sol", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ publicKey: wallet.publicKey.toBase58(), action: "wrap", amount: 0.5 }),
})).json();

const transaction = VersionedTransaction.deserialize(Buffer.from(tx, "base64"));
transaction.sign([wallet]);

const sent = await (await fetch("https://sol.shrine.trade/api/send", {
method: "POST",
headers: { "content-type": "application/json" },
body: JSON.stringify({ signedTx: Buffer.from(transaction.serialize()).toString("base64") }),
})).json();
console.log(sent.status, `https://solscan.io/tx/${sent.signature}`);