Buy on the SDK

1. Create order buy instance:

type TBuyEnergyOnTronSaveParams = {
    target:string,
    amount:number,
    price:"FAST"|"MEDIUM"|"SLOW"|number,
    duration: number,
    allowPartialFill:boolean,
    maxPrice?:number,
    fulfilledOnly?:boolean 
}

const buyEnergyOnTronsave = async (params:TBuyEnergyOnTronSaveParams) => {
        await tronSave.buyEnergy({
            target: params.target,
            amount: params.amount,
            price: params.price,
            duration: params.duration,
            allowPartialFill: params.allowPartialFill,
        })
    }
//Example params
const params:TBuyEnergyOnTronSaveParams ={
    target: "TXB4...9999",
    amount: 1000000, //Want to buy 1,000,000 energy 
    price:"MEDIUM",
    duration: 86400000, // rent duration is 1 day
    allowPartialFill: true,
    maxPrice: 80, //Maximun price afford is 80 SUN/1 Energy
    fulfilledOnly: true
}
ParamsTypeDescription

target

string

Resource receiving address.

amount

number

The number amount of resources

price

"FAST" | "MEDIUM" | "SLOW" | number

  • FAST: If market ready to fill = 100%, FAST = MEDIUM. If market ready to fill < 100%, FAST = MEDIUM + 10. If market ready to fill = 0%, FAST = SLOW + 20.

  • MEDIUM: The lowest price for the maximum market fill for this order. If market ready to fill = 0%, MEDIUM = SLOW + 10.

  • SLOW: The lowest price that can be set for this order.

  • If price is number, price unit is equal to sun.

duration

number

The duration of the bought resource, time unit is equal to millisecond.

allowPartialFill

boolean

Allow the order to be filled partially or not

maxPrice (optional)

number

The highest price allow sdk create an order, if user set price is "FAST", "MEDIUM" or "SLOW" and estimate price is bigger than maxPrice, this order creation will be canceled.

fulfilledOnly (optional)

boolean

If fulfilledOnly = true, the SDK will only create order if the system is ready to fill 100%

2. Auto create order buy energy for Bot

  • This feature is used to monitor and automatically create order buy energy for the Bots when the energy decreases to a threshold.

  • Whenever TronSave build a transaction, there will be a minimum waiting time of 15 seconds before the next call, unless TronSave determines that the buy order is erroneous, in which case there will be no waiting time.

2.1. Start this feature:

tronSave.useAutoBuyEnergy(frequency?: number);
ParamsTypeDescription

frequency

number

time interval TronSave check balance and energy to buy energy in millisecond, default is 10000. Minimum value is 3000

2.2. Add the Bots you want to monitor:

Step 1: Create buy config of each bot:

type TBuyEnergyBotConfig = {
    buyThreshold:number,
    target:string,
    amount:number,
    price:"FAST"|"MEDIUM"|"SLOW"|number,
    duration_millisec: number,
    allowPartialFill:boolean,
    maxPrice?:number,
    fulfilledOnly?:boolean 
}

const configBot1:TBuyEnergyBotConfig  =  {
            buyThreshold: 1000000,
            target: "TCtk...Zadwd",
            amount: 50000,
            price: "FAST",
            duration_millisec: 86400000,
            allowPartialFill: true,
            maxPrice: 80
        }
        
const configBot2:TBuyEnergyBotConfig =  {
            buyThreshold: 500000,
            target: "TXer...Fgr46g",
            amount: 200000,
            price: 65,
            duration_millisec: 86400000*3,
            allowPartialFill: false,
        }

const listBots:TBuyEnergyBotConfig[] = [configBot1, configBot2]

Parameter

ParamsTypeDescription

buyThreshold

number

If the energy falls below this threshold, TronSave will create an order to buy energy.

target

string

Resource receiving bot address.

amount

number

The number of resources

price

"FAST" | "MEDIUM" | "SLOW" | number

  • FAST: If market ready to fill = 100%, FAST = MEDIUM. If market ready to fill < 100%, FAST = MEDIUM + 10. If market ready to fill = 0%, FAST = SLOW + 20.

  • MEDIUM: The lowest price for the maximum market fill for this order. If market ready to fill = 0%, MEDIUM = SLOW + 10.

  • SLOW: The lowest price that can be set for this order.

  • If price is number, price unit is equal to sun.

duration_millisec

number

The duration of the bought resource, time unit is equal to millisecond.

allowPartialFill

boolean

Allow the order to be filled partially or not

maxPrice (optional)

number

The highest price allow sdk create an order, if user set price is "FAST", "MEDIUM" or "SLOW" and estimate price is bigger than maxPrice, this order creation will be canceled.

fulfilledOnly (optional)

boolean

If fulfilledOnly = true, the SDK will only create order if the system is ready to fill 100%

Step 2: Passing listBots to tronSave.autoBuyEnergy.startObserverTarget built-in function to start observer your bots.

tronSave.autoBuyEnergy.startObserverTarget(listBots)

2.3. Stop monitoring one bot:

To stop observer one bot address in your listBots passing your bot address to tronSave.autoBuyEnergy.cancelObserverTarget

tronSave.autoBuyEnergy.cancelObserverTarget(target)

Parameter

ParamsTypeDescription

target

string

Bot address.

Stop all Bot:

To stop observer all bot using tronSave.autoBuyEnergy.cancelObserverAllTarget

tronSave.autoBuyEnergy.cancelObserverAllTarget()

2.4. Listen events buy energy:

tronSave.on("buyerInfo", (event: any) => {
                if (event.error || event.type==="error"){
                  //Handle error
                } else {
                  // hand result of get trx balance of buyer
                } 
            });
tronSave.on("statusTarget", (event: any) => {
               if (event.error || event.type==="error"){
                  // handle error
                } else {
                  // handle result of get energy bot / threshold
                } 
            });
tronSave.on("prepairOrder", (event: any) => {
               if (event.error || event.type==="error"){
                  // handle error
                } else {
                  // handle result of get estimate trx + check order pending on market
                } 
            });
tronSave.on("executeOrder", (event: any) => {
               if (event.error || event.type==="error"){
                  // handle error
                } else {
                  // handle result of build transaction -> send api
                } 
            }); 
tronSave.on("resultOrder", (event: any) => {
               if (event.error || event.type==="error"){
                  // handle error
                } else {
                  // handle last result, if success => orderId on market
                } 
            });

Event lists

Event NameDescriptionResponse Type

buyerInfo

result of get trx balance of buyer

statusTarget

result of get energy bot/ threshold

prepairOrder

result of get estimate trx + check order pending on market

executeOrder

result of build transaction

resultOrder

last result, if success => orderId on market

3.Create order buy with API key of internal account

type ConfigBuyEnergyInternal = {
    target:string,
    amount:number,
    price:"FAST"|"MEDIUM"|"SLOW"|number,
    duration: number,
    allowPartialFill:boolean,
    sponsor?:string,
    apiKey?:string 
}

const buyEnergyOnTronsaveWithInternalAccount = async (params:ConfigBuyEnergyInternal) => {
        await tronSave.internalBuyEnergy({
            target: params.target,
            amount: params.amount,
            price: params.price,
            duration: params.duration,
            allowPartialFill: params.allowPartialFill,
            sponsor: params.sponsor,
            apiKey: params.apiKey
        })
    }
//Example params
const params:ConfigBuyEnergyInternal = {
    target: "TXB4...9999",
    amount: 1000000, //Want to buy 1,000,000 energy
    price:"MEDIUM",
    duration: 86400000, // rent duration is 1 day
    allowPartialFill: true,
    sponsor: "TD5T...35NJ", //Sponsor if available 
    apiKey: "33FDJ-GRETG..." //API key of internal account, 
}

Parameter

ParamsTypeDescription

target

string

Resource receiving bot address.

amount

number

The number of resources

price

"FAST" | "MEDIUM" | "SLOW" | number

  • FAST: If market ready to fill = 100%, FAST = MEDIUM. If market ready to fill < 100%, FAST = MEDIUM + 10. If market ready to fill = 0%, FAST = SLOW + 20.

  • MEDIUM: The lowest price for the maximum market fill for this order. If market ready to fill = 0%, MEDIUM = SLOW + 10.

  • SLOW: The lowest price that can be set for this order.

  • If price is number, price unit is equal to sun.

duration

number

The duration of the bought resource, time unit is equal to millisecond.

allowPartialFill

boolean

Allow the order to be filled partially or not

sponsor (optional)

string

Sponsor, if available.

apiKey (optional)

string

API key of internal account. If undefined, will take the internal account associated with the current address.

Last updated