PHP code example of kmcpg / multiversx-sdk-laravel

1. Go to this page and download the library: Download kmcpg/multiversx-sdk-laravel library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

kmcpg / multiversx-sdk-laravel example snippets


// config/multiversx.php
return [
    // 'mainnet', 'testnet', or 'devnet'
    'network' => env('MULTIVERSX_NETWORK', 'devnet'),

    // Public API URL
    'api_url' => env('MULTIVERSX_API_URL', 'https://devnet-api.multiversx.com'),

    // Default gas price for transactions
    'default_gas_price' => env('MULTIVERSX_DEFAULT_GAS_PRICE', 1000000000),

    // Default transaction version
    'default_tx_version' => env('MULTIVERSX_DEFAULT_TX_VERSION', 1),
];

use KmcpG\MultiversxSdkLaravel\Utils\Converter;

// Convert EGLD to atomic units (string)
$atomicValue = Converter::egldToAtomic('1.5'); // "1500000000000000000"

// Convert ESDT amount to atomic units (string)
// Requires knowing the token decimals (fetch via TokenService)
$atomicEsdt = Converter::esdtToAtomic('123.45', 6); // Assuming 6 decimals: "123450000"

// Encode arguments for a Smart Contract call (array of hex strings)
$rawArgs = [
    gmp_init(1000),
    'erd1...', // Bech32 address
    true,
    'hello'
];
$encodedArgs = Converter::encodeSmartContractArgs($rawArgs);
// Result: ['03e8', 'publicKeyHex...', '01', '68656c6c6f']

// Build the data field for a Smart Contract call (string)
$dataField = Converter::buildContractDataField('myFunction', $encodedArgs);
// Result: "myFunction@03e8@publicKeyHex...@01@68656c6c6f"

use KmcpG\MultiversxSdkLaravel\Facades\Multiversx;
// or inject KmcpG\MultiversxSdkLaravel\Services\WalletService

// Get account details (Nonce, Balance, etc.)
$account = Multiversx::getAccount('erd1...');
// $nonce = $account['nonce'];
// $balance = $account['balance'];

// Create a new wallet (generates private/public keys and address)
$wallet = Multiversx::createWallet();
// $privateKey = $wallet->privateKey;
// $publicKey = $wallet->publicKey;
// $address = $wallet->address;

// Validate an address
$isValid = Multiversx::isValidAddress('erd1...'); // Returns true or false

// Convert Bech32 address to Public Key Hex (Static Utility)
use KmcpG\MultiversxSdkLaravel\Services\WalletService;
$publicKeyHex = WalletService::bech32ToPublicKeyHex('erd1...');

use KmcpG\MultiversxSdkLaravel\Services\TransactionService;
use KmcpG\MultiversxSdkLaravel\Services\WalletService;
use KmcpG\MultiversxSdkLaravel\Utils\Converter; // Import Converter

class MyController
{
    protected TransactionService $txService;
    protected WalletService $walletService;

    public function __construct(TransactionService $txService, WalletService $walletService)
    {
        $this->txService = $txService;
        $this->walletService = $walletService; // Typically, you'd retrieve the private key securely
    }

    // Helper to get address (implementation specific)
    private function getAddressFromPrivateKey(string $privateKeyHex): string
    {
        // You might implement this using WalletService or keep it separate
        // Requires mdanter/ecc currently
        $adapter = \Mdanter\Ecc\EccFactory::getAdapter();
        $generator = \Mdanter\Ecc\EccFactory::getSecgCurves()->generator256k1();
        $key = $generator->getPrivateKeyFrom(gmp_init($privateKeyHex, 16));
        $point = $key->getPublicKey()->getPoint();
        $pkBin = substr((new \Mdanter\Ecc\Serializer\Point\UncompressedPointSerializer($adapter))->serialize($point), 1, 32);
        $data = array_values(unpack('C*', $pkBin));
        $convertedData = \BitWasp\Bech32\convertBits($data, count($data), 8, 5, true);
        return \BitWasp\Bech32\encode('erd', $convertedData);
    }

    public function sendEgldExample(string $senderPrivateKeyHex, string $receiverAddress, string $amountEgld, int $nonce)
    {
        // 1. Prepare basic EGLD transfer
        $params = [
            'sender' => $this->getAddressFromPrivateKey($senderPrivateKeyHex),
            'contractAddress' => $receiverAddress,
            'functionName' => '', // Empty function name for EGLD transfer
            'arguments' => [], // No arguments for EGLD transfer
            'value' => Converter::egldToAtomic($amountEgld), // Use Converter
            'nonce' => $nonce,
            'gasLimit' => 50000,
        ];
        // Note: Sending EGLD uses prepareSmartContractCall structure
        $preparedTx = $this->txService->prepareSmartContractCall($params);

        // 2. Sign
        $signedTx = $this->txService->signTransaction($preparedTx, $senderPrivateKeyHex);

        // 3. Send
        $result = $this->txService->sendTransaction($signedTx);
        return $result;
    }

    public function sendEsdtExample(string $senderPrivateKeyHex, string $receiverAddress, string $token, string $amount, int $decimals, int $nonce)
    {
        // 1. Prepare ESDT transfer
        $params = [
            'sender' => $this->getAddressFromPrivateKey($senderPrivateKeyHex),
            'receiver' => $receiverAddress,
            'tokenIdentifier' => $token,
            // Use GMP and Converter::esdtToAtomic or just pass GMP amount
            'amount' => gmp_init(Converter::esdtToAtomic($amount, $decimals)), 
            'nonce' => $nonce,
            'gasLimit' => 600000,
        ];
        $preparedTx = $this->txService->prepareEsdtTransfer($params);

        // 2. Sign & 3. Send (same as above)
        $signedTx = $this->txService->signTransaction($preparedTx, $senderPrivateKeyHex);
        $result = $this->txService->sendTransaction($signedTx);
        return $result;
    }

     public function sendNftExample(string $senderPrivateKeyHex, string $receiverAddress, string $collection, int $nftNonce, int $txNonce)
    {
        // 1. Prepare NFT transfer
        $params = [
            'sender' => $this->getAddressFromPrivateKey($senderPrivateKeyHex),
            'receiver' => $receiverAddress,
            'collection' => $collection,
            'nonce' => $nftNonce, // The nonce of the specific NFT
            'quantity' => gmp_init(1), // Quantity is 1 for NFTs
            'txNonce' => $txNonce,
            'gasLimit' => 1000000,
        ];
        $preparedTx = $this->txService->prepareNftTransfer($params);

        // 2. Sign & 3. Send (same as above)
        $signedTx = $this->txService->signTransaction($preparedTx, $senderPrivateKeyHex);
        $result = $this->txService->sendTransaction($signedTx);
        return $result;
    }

    public function callContractExample(string $senderPrivateKeyHex, string $contractAddress, string $function, array $rawArgs, string $valueEgld, int $nonce)
    {
        // 1. Prepare SC call
        $params = [
            'sender' => $this->getAddressFromPrivateKey($senderPrivateKeyHex),
            'contractAddress' => $contractAddress,
            'functionName' => $function,
            'arguments' => $rawArgs, // Pass raw arguments here
            'value' => Converter::egldToAtomic($valueEgld), // Use Converter
            'nonce' => $nonce,
            'gasLimit' => 5000000,
        ];
        // prepareSmartContractCall now handles encoding via Converter internally
        $preparedTx = $this->txService->prepareSmartContractCall($params);

        // 2. Sign & 3. Send (same as above)
        $signedTx = $this->txService->signTransaction($preparedTx, $senderPrivateKeyHex);
        $result = $this->txService->sendTransaction($signedTx);
        return $result;
    }

    public function checkStatusExample(string $txHash)
    {
        $status = $this->txService->getTransactionStatus($txHash);
        // e.g., $status['status'] can be 'pending', 'success', 'fail'
        return $status;
    }

}

use KmcpG\MultiversxSdkLaravel\Services\TokenService;

// Get properties of a token (ESDT/NFT/SFT)
$properties = $tokenService->getTokenProperties('WEGLD-bd4d79');
// $name = $properties['name'];
// $decimals = $properties['decimals'];

// Get the balance of a specific token for an address
$balanceInfo = $tokenService->getTokenBalance('erd1...', 'WEGLD-bd4d79');
// $balanceAtomic = $balanceInfo['balance']; // Balance in atomic units (string)
bash
php artisan vendor:publish --tag=multiversx-config