Tronsave
🇬🇧 English
🇬🇧 English
  • 📗INTRODUCTION
    • What is Tronsave ?
    • Why Tronsave?
  • Buyer
    • How to buy Energy ?
      • Buy on the Website
      • Buy on Telegram
        • 1. Create a Tronsave telegram account
        • 2. How To Deposit TRX
        • 3. Get the Tronsave API Key
        • 4. How to buy on Telegram
    • Extend
      • Quick extend
      • Advance
  • 🏬Seller
    • Get Energy by Staking 2.0
    • Permission
    • Sell Energy
      • Manual Sell
      • Auto Sell
      • Sell Suggestion
  • DEVELOPER
    • Get API key
      • On the Website
      • On Telegram
    • Buy Resources (v2)
      • Use Signed Transaction
        • Estimate TRX
        • Get Signed Transaction
        • Create order
      • Use API Key
        • Get Internal Account Info
        • Get Order Book
        • Estimate TRX
        • Create order
        • Get one order details
        • Get Internal Account Order History
    • Extend Orders (v2)
      • Step 1: Get Extendable Delegates
      • Step 2: Extend Request
    • Rest API v0
      • Buy on Rest API
        • Use Signed Transaction
          • Estimate TRX
          • Get Signed Transaction
          • Create order
          • Demo
        • Use API Key
          • Get Internal Account Info
          • Get Order Book
          • Estimate of TRX
          • Create order
          • Get one order details
          • Get Internal Account Order History
      • Extend with Rest API
  • 🤝Referrer
    • Referrals
  • 💡FAQ
    • Questions for Energy market
    • Calculate APY in TronSave
    • How to connect wallet in Tronsave?
    • Team of Service
  • 👨‍💻Full Code Example
    • Code Example (v2)
      • Buy Resource by API using private key
      • Buy Resources by API using api key
      • Extend order by API using api key
      • Extend order by API using private key
    • Code Example (v0)
      • Buy energy by API using private key
      • Buy energy by API using api key
      • Extend order by API using api key
Powered by GitBook
On this page
  1. Full Code Example
  2. Code Example (v0)

Extend order by API using api key

This is full example to extend order by API using API key

PreviousBuy energy by API using api key

Last updated 3 days ago

How to get the API key

Requirement: TronWeb version 5.3.2

npm i tronweb@5.3.2 @noble/secp256k1@1.7.1

(Read more: )

const API_KEY = `your_api_key`;
const TRONSAVE_API_URL = "https://api.tronsave.io/v0"
const RECEIVER = "the_address_that_receive_resource"

/**
 * @param {*} extend_to time in milliseconds you want to extend to
 * @param {*} max_price number maximum price you want to pay to extend
 * @returns 
 */
const GetEstimateExtendData = async (extend_to, max_price) => {
    const url = TRONSAVE_API_URL + `/get-extendable-delegates`
    const body = {
        "extend_to": extend_to, //time in milliseconds you want to extend to
        "receiver": RECEIVER,   //the address that receives the resource delegate
        "max_price": max_price, //Optional. Number maximum price you want to pay to extend
    }
    const data = await fetch(url, {
        method: "POST",
        headers: {
            'apikey': API_KEY,
            "content-type": "application/json",
        },
        body: JSON.stringify(body)
    })
    const response = await data.json()
    /**
     * Example response 
     * @link  //TODO
       {
            "extend_order_book": [
                {
                    "price": 133,
                    "value": 64319
                }
            ],
            "total_delegate_amount": 64319,
            "total_available_extend_amount": 64319,
            "total_estimate_trx": 8554427,
            "is_able_to_extend": true,
            "your_balance": 20000000,
            "extend_data": [
                {
                    "delegator": "TMN2uTdy6rQYaTm4A5g732kHRf72tKsA4w",
                    "is_extend": true,
                    "extra_amount": 0,
                    "extend_to": 1728459019000
                }
            ]
        }
     */
    return response
}

/**
 * @param {*} extend_to time in milliseconds you want to extend to
 * @param {*} max_price number maximum price you want to pay to extend
 * @returns 
 */
const SendInternalExtendRequest = async (extend_to, max_price) => {
    const url = TRONSAVE_API_URL + `/internal-extend-request`
    const estimate_response = await GetEstimateExtendData(extend_to, max_price)
    if (estimate_response.extend_data && estimate_response.extend_data.length) { 
        const body = {
            "extend_data": estimate_response.extend_data,
            "receiver": RECEIVER,
        }
        const data = await fetch(url, {
            method: "POST",
            headers: {
                'apikey': API_KEY,
                "content-type": "application/json",
            },
            body: JSON.stringify(body)
        })
        const response = await data.json()
        /**
         * Example response 
         * @link  //TODO
           [<order_id>]
         */
        return response
    }
    return []
}

//Example run code
const ClientCode = () => { 
    const extend_to = +new Date() + 86400 * 1000 //Extend to 1 next day
    const max_price = 200

    SendInternalExtendRequest(extend_to, max_price).then(console.log)
}


ClientCode()
👨‍💻
Option 1: Generate the API key on the Tronsave website.
Option 2: Generate the API key on Telegram.
https://tronweb.network/docu/docs/5.3.2/Release%20Note/