DEX API

在 Solana 链上发送多签交易#

import * as anchor from '@coral-xyz/anchor'
import { base64, bs58 } from '@coral-xyz/anchor/dist/cjs/utils/bytes';
async function sendtx() {
    const connection = new anchor.web3.Connection(
        anchor.web3.clusterApiUrl("mainnet-beta")
        // "https://solana-mainnet.g.alchemy.com/v2/EiyeW_PJ3a0Yig7E61nZHpfbMapVbV4m"
    )
    const keypair = anchor.web3.Keypair.fromSecretKey(
        bs58.decode("YOUR_USER_KEYPAIR")
    )
    // user’s wallet private key
    const wallet = new anchor.Wallet(keypair)
    const provider = new anchor.AnchorProvider(
        connection,
        wallet
    );
    console.log(wallet.publicKey.toString())
    let json =
    {
        "data": "hEFh3sGCFQKmLrMKDhDzr623WrtzawSrzHmtCqquGT2rX63Eh1bxU6UQJRT8WK1DqNJDA2hVvNvm3hpPCbkStRBs9qooQzUP5BSZJMqKGUxFPv5PT1p1kMNCvCc68jL1JQdsPNdhAFHaZa5UcEobMW62DeXorNgf74rCCYcYmH8Jd5TDQjE5Njp94iAqj12fv6WrWUSfHBZvRyrp4HxCUM55UfESi8d1tvFeVBGHXXn1kUdJPaUm4nia2WFxeaKS3RnrTwQeTfHSGK7j1sHvg1k5JStZQxZWaBi58tE7g4RWcEiuYedBKCGT4fjeNxCi8rBfsSk34QNSoRxxQpmayEzCMBFtMgwbpjXR3a1RuQHTBaEtpoUYaRoHtwHbLJ1C6eKBWULEyAxu1gQ8VmCQwh8qZHtuKBeamFGJxH2GK5mQ7rRftHXFhiTpPBfQHcYsFvZP73STDMGA6EMXFb5VKenocr6dG1tcxm4m4VbEiovqTuma6goegYMPA1RJV3fuVRqU4ZyD1AhiGGZyWCHpMpyjsRgFx65k51QBLZzNtmydPfTWnzPCPjYR5EYWsnNy2jGrj1RRTdKNryEE7S1DPB9hh5eHT6qchGyHEpi9WnzV2EntQqzuWz6u9DMVEt1n27ZeK7AcLRjyherRr5v4UvceZZf8ESqPo7Z2CZaMHYqfw463RLBjB5tLsWDR6hMv8RfSnEAy6dMEKkw4zm8h5Gp2JZTHX4ddo7Ag8TNhZkAzkGYk9GAbVKDooNTmVHD8kzQudj4dSCUAebJM7sgapVvbp2ucg7v6TUZvWUUwfvzUiCfxqTxP2z3cxrKmpwkffNZpsE5NnzVxvieGxQLFJqCUfk2eAct3XY6bFr6EkP5n6w8EDo399d3G2J7ohRDt9psCY9YZrgDY2JAYhLp2vGwApkhrfAPwK5voamxXuoghwkQcTmcK3MdU1A8gTH3jhxbFqEom4pmMKbgYaESEnSJpB2wsd1KdDwZjjoHB4HLFMH7fod6FZuH2VMfyjhoHLYEBWuq4DXid9RmWwsazH9qHyNpY4napPvxFcn2hGGtWzav3CMHCFHsz58wtg7ZzmF3yMS6KvGJq2UtqHfBCSd7xMJJPEmKHnftyUtGx23cL4f4J1NyHzwARqo9SFqrZHBVFwaKGAZn3666D9SUt3f6ukGEGDmBnkoK4N9bBpVrdb4zdbRoAPf59wWVCMZ4PHQtGrYYBbZ7omtG5s3BmZy41AxNaAaBEcnhxrzcthK2CqadxZtRooUkUnviWsrhyNH4Cu4xT8TTCJm7VuHsG3rkZSds3pGeMPkna2Ny8RsFtu4vhqQyhyvBzNbNo5ycoeaerFKaS87ny3kAXKzUDSyNFRqfFaoqwcu7fTtxvJgnyv5fLg1a3Ak9avNxKjfrZEc17n1Tq44Vtsbv9YsGjCLqTUEiAQMsvXC1WG6nbQ1DexwsyiUtxy7DcViG4bLQChhMvEM1AYP",
        "from": "Jk9fBdZBe83dsy5t8FWuk26LZhytWJCa7MXTqkiDEtF",
        "gasLimit": "622500",
        "gasPrice": "5000",
        "key": [
            "5B7Q21z2B5o2D1WVpp3LXNFq4NbY2HsVRnWbLTtj3mAdbXnPQe2QvJtrizW8expLE91HbXypzrw8vJsAxm3nHTrW"
        ],
        "maxPriorityFeePerGas": "",
        "to": "AqwtzPZxUmM6KoDCm5ceC7kje4DB2bLPRjKVJ8njCjKx",
        "value": "28000000"
    }
    const keypair2 = anchor.web3.Keypair.fromSecretKey(bs58.decode(json.key[0]))
    // randomKeyAccount parameters
    const wallet2 = new anchor.Wallet(keypair2)
    let transaction = anchor.web3.VersionedTransaction.deserialize(Buffer.from(bs58.decode(json.data)))
    const { blockhash } = await connection.getLatestBlockhash();
    transaction.message.recentBlockhash = blockhash
    transaction.sign([keypair, keypair2])
    // Using randomKeyAccount and user’s wallet private key for multi-signature operations
    const txId = await connection.sendTransaction(transaction, {skipPreflight: false, maxRetries: 20})
    console.log("txId: ", txId)
}
sendtx()