Skip to content

Commit 0637f6e

Browse files
author
Zero
committed
Add delegate resource method by iexbase#194
1 parent 274cc8f commit 0637f6e

File tree

2 files changed

+232
-95
lines changed

2 files changed

+232
-95
lines changed

src/TransactionBuilder.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,71 @@ public function unfreezeBalance(string $resource = 'BANDWIDTH', string $owner_ad
308308
]);
309309
}
310310

311+
/**
312+
* Delegate bandwidth or energy resources to other accounts in Stake2.0.
313+
* Will delegate bandwidth OR Energy resources to other accounts.
314+
*
315+
* @param string $owner_address
316+
* @param string $resource
317+
* @param string $receiver_address
318+
* @param int $balance
319+
* @param bool $lock
320+
* @param int $lock_period
321+
* @return array
322+
* @throws TronException
323+
*/
324+
public function delegateResource(string $owner_address = null, string $resource = 'BANDWIDTH', string $receiver_address = null,int $balance = 0, bool $lock = false, int $lock_period = 0)
325+
{
326+
if(empty($owner_address) or empty($receiver_address)) {
327+
throw new TronException('Address not specified');
328+
}
329+
if (!in_array($resource, ['BANDWIDTH', 'ENERGY'])) {
330+
throw new TronException('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"');
331+
}
332+
if (!is_int($balance) or !is_int($lock_period)) {
333+
throw new TronException('Invalid balance or lock_period provided');
334+
}
335+
if(!is_bool($lock)) {
336+
throw new TronException('Invalid lock value provided');
337+
}
338+
339+
return $this->tron->getManager()->request(
340+
'wallet/delegateresource',
341+
[
342+
'owner_address' => $this->tron->address2HexString($owner_address),
343+
'resource' => $resource,
344+
'receiver_address' => $this->tron->address2HexString($receiver_address),
345+
'balance' => $this->tron->toTron($balance),
346+
'lock' => $lock,
347+
'lock_period' => $lock_period,
348+
],
349+
);
350+
}
351+
352+
public function undelegateResource(string $owner_address = null, string $resource = 'BANDWIDTH', string $receiver_address = null,int $balance = 0)
353+
{
354+
if(empty($owner_address) or empty($receiver_address)) {
355+
throw new TronException('Address not specified');
356+
}
357+
if (!in_array($resource, ['BANDWIDTH', 'ENERGY'])) {
358+
throw new TronException('Invalid resource provided: Expected "BANDWIDTH" or "ENERGY"');
359+
}
360+
if (!is_int($balance)) {
361+
throw new TronException('Invalid balance');
362+
}
363+
364+
365+
return $this->tron->getManager()->request(
366+
'wallet/undelegateresource',
367+
[
368+
'owner_address' => $this->tron->address2HexString($owner_address),
369+
'resource' => $resource,
370+
'receiver_address' => $this->tron->address2HexString($receiver_address),
371+
'balance' => $this->tron->toTron($balance),
372+
],
373+
);
374+
}
375+
311376
/**
312377
* Withdraw Super Representative rewards, useable every 24 hours.
313378
*

0 commit comments

Comments
 (0)