Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

SPL Token Program #40

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 62 additions & 1 deletion src/Programs/SplTokenProgram.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,29 @@
namespace Tighten\SolanaPhpSdk\Programs;

use Tighten\SolanaPhpSdk\Program;
use Tighten\SolanaPhpSdk\PublicKey;
use Tighten\SolanaPhpSdk\Util\AccountMeta;
use Tighten\SolanaPhpSdk\Programs\SystemProgram;
use Tighten\SolanaPhpSdk\TransactionInstruction;
use Tighten\SolanaPhpSdk\Exceptions\InputValidationException;

class SplTokenProgram extends Program
{
public const SOLANA_TOKEN_PROGRAM_ID = 'TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA';

public const SOLANA_ASSOCIATED_TOKEN_PROGRAM_ID = 'ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL';

public static function programId(): PublicKey
{
return new PublicKey(self::SOLANA_TOKEN_PROGRAM_ID);
}

public static function associatedProgramId(): PublicKey
{
return new PublicKey(self::SOLANA_ASSOCIATED_TOKEN_PROGRAM_ID);
}

/**
* @param string $pubKey
* @return mixed
*/
public function getTokenAccountsByOwner(string $pubKey)
Expand All @@ -24,4 +40,49 @@ public function getTokenAccountsByOwner(string $pubKey)
],
]);
}

public static function getAssociatedTokenAddress(
PublicKey $associatedProgramId,
PublicKey $programId,
PublicKey $mint,
PublicKey $owner,
bool $allowOwnerOffCurve = false
): PublicKey {
if (! $allowOwnerOffCurve && ! PublicKey::isOnCurve($owner)) {
throw new InputValidationException("Owner cannot sign: {$owner->toBase58()}");
}

return PublicKey::findProgramAddress([
$owner->toBuffer(),
$programId->toBuffer(),
$mint->toBuffer()
], $associatedProgramId)[0];
}

public static function createAssociatedTokenAccountInstruction(
PublicKey $associatedProgramId,
PublicKey $programId,
PublicKey $mint,
PublicKey $associatedAccount,
PublicKey $owner,
PublicKey $payer,
): TransactionInstruction {
$data = new Buffer();

$keys = [
new AccountMeta($payer, true, true),
new AccountMeta($associatedAccount, false, true),
new AccountMeta($owner, false, false),
new AccountMeta($mint, false, false),
new AccountMeta(SystemProgram::programId(), false, false),
new AccountMeta($programId, false, false),
new AccountMeta(SysVar::rent(), false, false),
];

return new TransactionInstruction(
$associatedProgramId,
$keys,
$data
);
}
}
38 changes: 38 additions & 0 deletions tests/Unit/SplTokenProgramTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace Tighten\SolanaPhpSdk\Tests\Unit;

use Tighten\SolanaPhpSdk\Keypair;
use Tighten\SolanaPhpSdk\PublicKey;
use Tighten\SolanaPhpSdk\Tests\TestCase;
use Tighten\SolanaPhpSdk\Programs\SplTokenProgram;

class SplTokenProgramTest extends TestCase
{
/** @test */
public function it_hasCorrectProgramIds()
{
$this->assertEquals('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', SplTokenProgram::SOLANA_TOKEN_PROGRAM_ID);
$this->assertEquals('TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA', SplTokenProgram::programId()->toBase58());

$this->assertEquals('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL', SplTokenProgram::SOLANA_ASSOCIATED_TOKEN_PROGRAM_ID);
$this->assertEquals('ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL', SplTokenProgram::associatedProgramId()->toBase58());
}

/** @test */
public function it_getAssociatedTokenAddress()
{
$mint = new PublicKey('2hvKBnhXZVvZadKX5QKkiyU7pVXXe5ZNMZhAoeXJdHxj');
$owner = new PublicKey('Gk9HLj4qg1nTu2s7cdpm6PPgosxqUjya1cbhif4Lo6qv');

$tokenAccount = SplTokenProgram::getAssociatedTokenAddress(
SplTokenProgram::associatedProgramId(),
SplTokenProgram::programId(),
$mint,
$owner,
true
);

$this->assertEquals('27f5ubUFNrfXBwgqrH1sKMPuQVmm99qUuZxuJNtd5As4', $tokenAccount->toBase58());
}
}