Skip to content

Instantly share code, notes, and snippets.

@yusufusta
Last active August 3, 2022 20:56
Show Gist options
  • Save yusufusta/5c80106ccf2fa9d166b5ed169fc32ada to your computer and use it in GitHub Desktop.
Save yusufusta/5c80106ccf2fa9d166b5ed169fc32ada to your computer and use it in GitHub Desktop.
<?php
class ChainGateway
{
/**
* @var string
*/
public $ChainGateway = "https://eu.trx.chaingateway.io/v1"; # Default ChainGateway
/**
* @var string
*/
private $ApiKey = "";
/**
* @var string
*/
public $Url = "";
/**
* @param $apiKey
* @param $from
*/
public function __construct($apiKey, $server = "eu", $coin = "trx")
{
$this->ChainGateway = "https://" . $server . "." . $coin . ".chaingateway.io/v1";
$this->ApiKey = $apiKey;
$this->Curl = curl_init();
$this->Url = $this->ChainGateway;
curl_setopt($this->Curl, CURLOPT_HTTPHEADER, ['Authorization: ' . $this->ApiKey, 'Content-Type: application/json']);
curl_setopt($this->Curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($this->Curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($this->Curl, CURLOPT_SSL_VERIFYHOST, false);
}
/**
* @param $method
* @param $args
*/
public function __call($method, $args)
{
$method = strtoupper($method);
$this->Url .= $args[0];
echo $this->Url;
if ($method == "GET") {
curl_setopt($this->Curl, CURLOPT_CUSTOMREQUEST, "GET");
if (!empty($args[1])) {
$this->Url = sprintf("%s?%s", $this->Url, http_build_query($args[1]));
}
} else {
curl_setopt($this->Curl, CURLOPT_CUSTOMREQUEST, $method);
if (!empty($args[1])) {
curl_setopt($this->Curl, CURLOPT_POSTFIELDS, json_encode($args[1]));
}
}
curl_setopt($this->Curl, CURLOPT_URL, $this->Url);
$response = curl_exec($this->Curl);
$err = curl_error($this->Curl);
if ($err) {
throw new \Exception($err);
}
$this->Url = $this->ChainGateway;
return json_decode($response, true);
}
/**
* Generates a new tron address to send or receive funds. Do not lose the
* privatekey! We can't restore access to an address if you lose it. We recommend to generate
* addresses offline for security reasons.
*/
public function newAddress(): ?array
{
return $this->POST("/newAddress", [
"password" => "yusuf"
]);
}
/**
* Returns the tron balance of a given tron address.
*
*
* @param string $tronaddress Tron address you want to get the balance of
*/
public function getTronBalance(string $tronaddress): ?array
{
return $this->POST("/getTronBalance", ['tronaddress' => $tronaddress]);
}
/**
* Returns the TRC10 token balance of a given tron address
*
*
* @param int $tokenid Token id of the TRC10 token
* @param string $tronaddress Tron address you want to get the balance of
*/
public function getTRC10Balance(int $tokenid, string $tronaddress): ?array
{
return $this->POST("/getTRC10Balance", ['tokenid' => $tokenid, 'tronaddress' => $tronaddress]);
}
/**
* Returns the TRC20 token balance of a given tron address
*
*
* @param string $contractaddress Contractaddress of the TRC20 token
* @param string $tronaddress Tron address you want to get the balance of
*/
public function getTRC20Balance(string $contractaddress, string $tronaddress): ?array
{
return $this->POST("/getTRC20Balance", ['contractaddress' => $contractaddress, 'tronaddress' => $tronaddress]);
}
/**
* Returns the current Tron price in Euro or US Dollar
*
*
* @param string $currency eur or usd
*/
public function getExchangeRate(string $currency): ?array
{
return $this->POST("/getExchangeRate", ['currency' => $currency]);
}
/**
* Returns information about a specific transaction by transaction hash
*
*
* @param string $txid transaction hash
*/
public function getTransaction(string $txid): ?array
{
return $this->POST("/getTransaction", ['txid' => $txid]);
}
/**
* Returns the owner address of a specificied TRC721 token id.
*
*
* @param string $contractaddress Tron contract address of the TRC721 token
* @param int $tokenid id of the token you want to get the owner address of
*/
public function getNFTOwner(string $contractaddress, int $tokenid): ?array
{
return $this->Beta("/getNFTOwner", ['contractaddress' => $contractaddress, 'tokenid' => $tokenid]);
}
/**
* Returns the URI of the specified NFT. The returned URI leads to more details of the NFT, like
* name, description and image.
*
*
* @param string $contractaddress Tron contract address of the TRC721 token
* @param int $tokenid id of the token you want to get the owner address of
*/
public function getNFTURI(string $contractaddress, int $tokenid): ?array
{
return $this->Beta("/getNFTURI", ['contractaddress' => $contractaddress, 'tokenid' => $tokenid]);
}
/**
* Creates a new subscription/IPN for the given address (and contractaddress or tokenid). You will
* receive a notification to the given url every time a deposit is received or a withdrawal has
* been made. Please return {"ok": true} in JSON format and
* with JSON header to signal our API that the IPN has been received successfully. For an
* example, check out this tutorial.
*
*
* @param string $tronaddress The tron address you want to watch
* @param string $contractaddress (Optional) Contractaddress of the TRC20 token you want to watch (Will watch
* TRON/TRX by default)
* @param int $tokenid (Optional) Tokenid of the TRC10 token you want to watch (Will watch TRON/TRX by
* default).
* @param string $url The URL you want to receive notifications on.
*/
public function subscribeAddress(string $tronaddress, string $url): ?array
{
return $this->POST("/subscribeAddress", ['tronaddress' => $tronaddress, 'url' => $url]);
}
/**
* Delete a subscription/IPN for the given address (and contractaddress or tokenid).
*
*
* @param string $tronaddress The tron address you want to watch
* @param string $contractaddress (Optional) Contractaddress of the TRC20 token you want to watch (Will watch
* TRON/TRX by default)
* @param int $tronid (Optional) Tokenid of the TRC10 token you want to watch (Will watch TRON/TRX by
* default).
* @param string $url The URL you want to receive notifications on.
*/
public function unsubscribeAddress(string $tronaddress, string $contractaddress, int $tronid, string $url): ?array
{
return $this->POST("/unsubscribeAddress", ['tronaddress' => $tronaddress, 'contractaddress' => $contractaddress, 'tronid' => $tronid, 'url' => $url]);
}
/**
* Returns all subscriptions/IPNs created with an account.
*/
public function listSubscribedAddresses(): ?array
{
return $this->POST("/listSubscribedAddresses", []);
}
/**
* Returns all IPNs that couldn't be sent. Our system tries to send IPNs 10 times until they end up
* failed.
*/
public function listFailedIPNs(): ?array
{
return $this->POST("/listFailedIPNs", []);
}
/**
* Tries to resend a failed IPN
*
*
* @param int $id ID of the IPN to send again
*/
public function resendFailedIPN(int $id): ?array
{
return $this->POST("/resendFailedIPN", ['id' => $id]);
}
/**
* Sends Tron/TRX from an address to a specified receiver address by using privatekey.
*
*
* @param string $to Tron address you want to send to
* @param string $privatekey Privatekey of the sender address
* @param string $amount Amount of Tron/TRX you want to send
*/
public function sendTron(string $to, string $privatekey, string $amount, $identifier = null): ?array
{
return $this->POST("/sendTron", ['to' => $to, 'privatekey' => $privatekey, 'amount' => $amount, 'identifier' => $identifier]);
}
/**
* Sends a TRC10 token from an address to a specified receiver address by using privatekey.
*
*
* @param int $tokenid Token id of the TRC10 token
* @param string $to Tron address you want to send to
* @param string $privatekey Privatekey of the sender address
* @param string $amount Amount of TRC10 tokens you want to send
*/
public function sendTRC10(int $tokenid, string $to, string $privatekey, string $amount, $identifier = null): ?array
{
return $this->POST("/sendTRC10", ['tokenid' => $tokenid, 'to' => $to, 'privatekey' => $privatekey, 'amount' => $amount, 'identifier' => $identifier]);
}
/**
* Sends a TRC20 token from an address to a specified receiver address by using privatekey.
*
*
* @param string $contractaddress Contract address of the TRC20 token
* @param string $from Tron address you want to send from
* @param string $to Tron address you want to send to
* @param string $privatekey Privatekey of the sender address
* @param string $amount Amount of TRC20 tokens you want to send
*/
public function sendTRC20(
string $contractaddress,
string $from,
string $to,
string $privatekey,
string $amount,
$identifier = null
): ?array {
return $this->POST("/sendTRC20", ['contractaddress' => $contractaddress, 'from' => $from, 'to' => $to, 'privatekey' => $privatekey, 'amount' => $amount, 'identifier' => $identifier]);
}
/**
* Freezes Tron for bandwidth or energy and power
*
*
* @param string $tronaddress Tron address to freeze balance of
* @param string $amount Amount of Tron to freeze
* @param int $duration Duration in days to freeze Tron (minimum 3)
* @param string $type Resource type to freeze Tron for. Either 'BANDWIDTH' or 'ENERGY'
* @param string $to (Optional) Tron address which receives the resources. Will use tronaddress if
* not provided.
* @param string $privatekey Privatekey of the sender address
*/
public function freezeBalance(
string $tronaddress,
string $amount,
int $duration,
string $type,
string $to,
string $privatekey
): ?array {
return $this->POST("/freezeBalance", ['tronaddress' => $tronaddress, 'amount' => $amount, 'duration' => $duration, 'type' => $type, 'to' => $to, 'privatekey' => $privatekey]);
}
/**
* Unfreeze Tron that has passed the minimum freeze duration.
*
*
* @param string $tronaddress Tron address to unfreeze Tron of
* @param string $type Resource type to unfreeze Tron for. Either 'BANDWIDTH' or 'ENERGY'
* @param string $to (Optional) Tron address which will lose the resources. Will use tronaddress if
* not provided.
* @param string $privatekey Privatekey of the sender address
*/
public function unfreezeBalance(string $tronaddress, string $type, string $to, string $privatekey): ?array
{
return $this->POST("/unfreezeBalance", ['tronaddress' => $tronaddress, 'type' => $type, 'to' => $to, 'privatekey' => $privatekey]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment