Local Trade API

This API allows users to trade SunPump tokens without passing a private key or sensitive information. It works by building transaction instructions for local signing and broadcasting instead.

SunPump Trade Code Example - JS/TS
import TronWeb from 'tronweb';
import axios from 'axios';
import dotenv from 'dotenv';

dotenv.config();

const privateKey = process.env.PRIVATE_KEY || 'YOUR-TRON-PRIVATE-KEY'; // Your Private Key Hex
const tronWeb = new TronWeb({
  fullHost: 'YOUR_RPC_ENDPOINT', // you can use https://api.trongrid.io 
  privateKey,
});

export const sendLocalTradeRequest = async () => {
  try {
    console.log('Requesting unsigned transaction...');
    const userAddress = tronWeb.defaultAddress.base58;

    const response = await axios.post(
      'https://api.dangeracorn.com/sunpump/localTrade',
      {
        action: "buy", // Use "buy" or "purchase" for buying tokens | Use "sell" or "sale" when selling
        tokenAddress: "token-contract-address", // Token Contract Address in Base58 Format
        amount: "100%", // Amount of TRX to buy | Token Amount or % if Selling (ex. 1000000 = SELL 1,000,000 TOKENS) 
        slippage: "1", // Slippage % in whole number (1 = 1% Slippage)
        userAddress: "your-address" // Signer's public key in Base58 Format
      },
      {
        headers: {
          'Content-Type': 'application/json',
          'api-key': 'your-api-key' // If you don't have you you can get one at https://t.me/+pE-h-2eMeoRhZDBh
        }
      }
    );

    const { unsignedTx } = response.data;

    if (unsignedTx) {
      console.log('Raw transaction:', unsignedTx);

      // Step 3: Sign the transaction 
      console.log('Signing the transaction...');
      const signedTx = await tronWeb.trx.sign(unsignedTx);

      // Step 4: Broadcast the transaction 
      console.log('Broadcasting the signed transaction...');
      const broadcastResponse = await tronWeb.trx.sendRawTransaction(signedTx);

      if (broadcastResponse.result) {
        console.log('Transaction broadcasted successfully:', 'https://tronscan.org/#/transaction/' + broadcastResponse.txid);
      } else {
        console.log('Failed to broadcast the transaction:', broadcastResponse);
      }
    } else {
      console.log('Error: No unsigned transaction returned.');
    }
  } catch (error) {
    console.error('Error during the local trade request or broadcasting:', error);
  }
};

Endpoint

  • URL: /sunpump/localTrade

  • Method: POST

  • Content-Type: application/json

  • API Key Header: api-key: your-api-key-here

Parameters

ParameterTypeRequiredDescription

type

string

Yes

Type of transaction: buy or sell.

tokenAddress

string

Yes

Token Contract Address

amount

number/string

Yes

Amount to trade (in TRX for tokens (or %) for sale).

userAddress

string

Yes

Public Tron Address of sender

slippage

number

No

Max allowed price change before the transaction is reverted

Example Buy Request

curl -X POST https://api.dangeracorn.com/sunpump/localTrade \
-H "Content-Type: application/json" \
-H "api-key: your-api-key" \
-d '{
  "action": "buy",
  "tokenAddress": "token-contract-address",
  "amount": 100,
  "slippage": 1,
  "userAddress": "sender-public-tron-address"
}'

Example Sell Request

curl -X POST https://api.dangeracorn.com/sunpump/localTrade \
-H "Content-Type: application/json" \
-H "api-key: your-api-key" \
-d '{
  "action": "sell",
  "tokenAddress": "token-contract-address",
  "amount": "50%",
  "slippage": 2,
  "userAddress": "sender-public-tron-address"
}'

Example Response JSON

{
  "unsignedTx": {
    "raw_data": { /* transaction data */ },
    "txID": "b69f27c7bfcf54a464294f1df99aaeff1b53714a12a80460709b20923a24486c"
  },
  "status": "unsigned",
  "code": "200"
}

Error Responses

Invalid Parameters

{
  "error": "Invalid parameters",
  "message": "You must provide type, tokenAddress, amount, and userAddress",
  "code": "400"
}

Max Slippage Reached

{
  "error": "Simulation failed",
  "message": "Max slippage reached. Transaction reverted.",
  "code": "500"
}

Insufficient Funds

{
  "error": "Insufficient funds",
  "message": "Your account does not have enough funds to perform this transaction.",
  "code": "400"
}

Invalid Type

{
  "error": "Invalid type",
  "message": "Type must be either 'buy' or 'sell'.",
  "code": "400"
}

Notes

  • Rate Limiting: This endpoint is rate-limited according to your plan's API key limits. Ensure that you handle these limits in your application.

  • Unsigned Transactions: The API returns unsigned transactions that users must sign before broadcasting.

  • Slippage Tolerance: Ensure the slippage is reasonable to avoid reverted transactions.

  • Fees: A small fee is applied on each trade. Please refer to Plans to see what fee your plan uses.

Last updated