Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 185 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| SnsInstruction | |
0.00% |
0 / 185 |
|
0.00% |
0 / 5 |
182 | |
0.00% |
0 / 1 |
| createInstruction | |
0.00% |
0 / 63 |
|
0.00% |
0 / 1 |
20 | |||
| updateInstruction | |
0.00% |
0 / 24 |
|
0.00% |
0 / 1 |
2 | |||
| transferInstruction | |
0.00% |
0 / 40 |
|
0.00% |
0 / 1 |
42 | |||
| reallocInstruction | |
0.00% |
0 / 32 |
|
0.00% |
0 / 1 |
2 | |||
| deleteInstruction | |
0.00% |
0 / 26 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Attestto\SolanaPhpSdk\Programs\SNS; |
| 4 | |
| 5 | use Attestto\SolanaPhpSdk\PublicKey; |
| 6 | use Attestto\SolanaPhpSdk\SystemProgram; |
| 7 | use Attestto\SolanaPhpSdk\TransactionInstruction; |
| 8 | use Attestto\SolanaPhpSdk\Interfaces\AccountKeyInterface; |
| 9 | |
| 10 | class SnsInstruction { |
| 11 | |
| 12 | |
| 13 | use Attestto\SolanaPhpSdk\Buffer; |
| 14 | use Attestto\SolanaPhpSdk\Numberu32; |
| 15 | use Attestto\SolanaPhpSdk\Numberu64; |
| 16 | use Attestto\SolanaPhpSdk\PublicKey; |
| 17 | use Attestto\SolanaPhpSdk\TransactionInstruction; |
| 18 | |
| 19 | /** |
| 20 | * Creates a transaction instruction. |
| 21 | * |
| 22 | * @param PublicKey $nameProgramId The public key of the name program. |
| 23 | * @param PublicKey $systemProgramId The public key of the system program. |
| 24 | * @param PublicKey $nameKey The public key of the name. |
| 25 | * @param PublicKey $nameOwnerKey The public key of the name owner. |
| 26 | * @param PublicKey $payerKey The public key of the payer. |
| 27 | * @param Buffer $hashed_name The hashed name. |
| 28 | * @param Numberu64 $lamports The amount of lamports. |
| 29 | * @param Numberu32 $space The amount of space. |
| 30 | * @param PublicKey|null $nameClassKey The public key of the name class. |
| 31 | * @param PublicKey|null $nameParent The public key of the name parent. |
| 32 | * @param PublicKey|null $nameParentOwner The public key of the name parent owner. |
| 33 | * @return TransactionInstruction The created transaction instruction. |
| 34 | */ |
| 35 | function createInstruction( |
| 36 | PublicKey $nameProgramId, |
| 37 | PublicKey $systemProgramId, |
| 38 | PublicKey $nameKey, |
| 39 | PublicKey $nameOwnerKey, |
| 40 | PublicKey $payerKey, |
| 41 | Buffer $hashed_name, |
| 42 | Numberu64 $lamports, |
| 43 | Numberu32 $space, |
| 44 | ?PublicKey $nameClassKey = null, |
| 45 | ?PublicKey $nameParent = null, |
| 46 | ?PublicKey $nameParentOwner = null |
| 47 | ): TransactionInstruction { |
| 48 | $buffers = [ |
| 49 | Buffer::fromArray([0]), |
| 50 | (new Numberu32($hashed_name->length))->toBuffer(), |
| 51 | $hashed_name, |
| 52 | $lamports->toBuffer(), |
| 53 | $space->toBuffer() |
| 54 | ]; |
| 55 | |
| 56 | $data = Buffer::concat($buffers); |
| 57 | |
| 58 | $keys = [ |
| 59 | [ |
| 60 | 'pubkey' => $systemProgramId, |
| 61 | 'isSigner' => false, |
| 62 | 'isWritable' => false |
| 63 | ], |
| 64 | [ |
| 65 | 'pubkey' => $payerKey, |
| 66 | 'isSigner' => true, |
| 67 | 'isWritable' => true |
| 68 | ], |
| 69 | [ |
| 70 | 'pubkey' => $nameKey, |
| 71 | 'isSigner' => false, |
| 72 | 'isWritable' => true |
| 73 | ], |
| 74 | [ |
| 75 | 'pubkey' => $nameOwnerKey, |
| 76 | 'isSigner' => false, |
| 77 | 'isWritable' => false |
| 78 | ] |
| 79 | ]; |
| 80 | |
| 81 | if ($nameClassKey) { |
| 82 | $keys[] = [ |
| 83 | 'pubkey' => $nameClassKey, |
| 84 | 'isSigner' => true, |
| 85 | 'isWritable' => false |
| 86 | ]; |
| 87 | } else { |
| 88 | $keys[] = [ |
| 89 | 'pubkey' => new PublicKey(Buffer::alloc(32)), |
| 90 | 'isSigner' => false, |
| 91 | 'isWritable' => false |
| 92 | ]; |
| 93 | } |
| 94 | |
| 95 | if ($nameParent) { |
| 96 | $keys[] = [ |
| 97 | 'pubkey' => $nameParent, |
| 98 | 'isSigner' => false, |
| 99 | 'isWritable' => false |
| 100 | ]; |
| 101 | } else { |
| 102 | $keys[] = [ |
| 103 | 'pubkey' => new PublicKey(Buffer::alloc(32)), |
| 104 | 'isSigner' => false, |
| 105 | 'isWritable' => false |
| 106 | ]; |
| 107 | } |
| 108 | |
| 109 | if ($nameParentOwner) { |
| 110 | $keys[] = [ |
| 111 | 'pubkey' => $nameParentOwner, |
| 112 | 'isSigner' => true, |
| 113 | 'isWritable' => false |
| 114 | ]; |
| 115 | } |
| 116 | |
| 117 | return new TransactionInstruction([ |
| 118 | 'keys' => $keys, |
| 119 | 'programId' => $nameProgramId, |
| 120 | 'data' => $data |
| 121 | ]); |
| 122 | } |
| 123 | |
| 124 | |
| 125 | /** |
| 126 | * Updates an instruction. |
| 127 | * |
| 128 | * @param PublicKey $nameProgramId The public key of the name program. |
| 129 | * @param PublicKey $nameAccountKey The public key of the name account. |
| 130 | * @param Numberu32 $offset The offset. |
| 131 | * @param Buffer $input_data The input data. |
| 132 | * @param PublicKey $nameUpdateSigner The public key of the name update signer. |
| 133 | * @return TransactionInstruction The created transaction instruction. |
| 134 | */ |
| 135 | function updateInstruction( |
| 136 | PublicKey $nameProgramId, |
| 137 | PublicKey $nameAccountKey, |
| 138 | Numberu32 $offset, |
| 139 | Buffer $input_data, |
| 140 | PublicKey $nameUpdateSigner |
| 141 | ): TransactionInstruction { |
| 142 | $buffers = [ |
| 143 | Buffer::fromArray([1]), |
| 144 | $offset->toBuffer(), |
| 145 | (new Numberu32($input_data->length))->toBuffer(), |
| 146 | $input_data |
| 147 | ]; |
| 148 | |
| 149 | $data = Buffer::concat($buffers); |
| 150 | $keys = [ |
| 151 | [ |
| 152 | 'pubkey' => $nameAccountKey, |
| 153 | 'isSigner' => false, |
| 154 | 'isWritable' => true |
| 155 | ], |
| 156 | [ |
| 157 | 'pubkey' => $nameUpdateSigner, |
| 158 | 'isSigner' => true, |
| 159 | 'isWritable' => false |
| 160 | ] |
| 161 | ]; |
| 162 | |
| 163 | return new TransactionInstruction([ |
| 164 | 'keys' => $keys, |
| 165 | 'programId' => $nameProgramId, |
| 166 | 'data' => $data |
| 167 | ]); |
| 168 | } |
| 169 | |
| 170 | |
| 171 | /** |
| 172 | * Creates a transfer instruction. |
| 173 | * |
| 174 | * @param PublicKey $nameProgramId The public key of the name program. |
| 175 | * @param PublicKey $nameAccountKey The public key of the name account. |
| 176 | * @param PublicKey $newOwnerKey The public key of the new owner. |
| 177 | * @param PublicKey $currentNameOwnerKey The public key of the current name owner. |
| 178 | * @param PublicKey|null $nameClassKey The public key of the name class. |
| 179 | * @param PublicKey|null $nameParent The public key of the name parent. |
| 180 | * @param PublicKey|null $parentOwner The public key of the parent owner. |
| 181 | * @return TransactionInstruction The created transaction instruction. |
| 182 | */ |
| 183 | function transferInstruction( |
| 184 | PublicKey $nameProgramId, |
| 185 | PublicKey $nameAccountKey, |
| 186 | PublicKey $newOwnerKey, |
| 187 | PublicKey $currentNameOwnerKey, |
| 188 | ?PublicKey $nameClassKey = null, |
| 189 | ?PublicKey $nameParent = null, |
| 190 | ?PublicKey $parentOwner = null |
| 191 | ): TransactionInstruction { |
| 192 | $buffers = [ |
| 193 | Buffer::fromArray([2]), |
| 194 | $newOwnerKey->toBuffer() |
| 195 | ]; |
| 196 | |
| 197 | $data = Buffer::concat($buffers); |
| 198 | $keys = [ |
| 199 | [ |
| 200 | 'pubkey' => $nameAccountKey, |
| 201 | 'isSigner' => false, |
| 202 | 'isWritable' => true |
| 203 | ], |
| 204 | [ |
| 205 | 'pubkey' => $parentOwner ? $parentOwner : $currentNameOwnerKey, |
| 206 | 'isSigner' => true, |
| 207 | 'isWritable' => false |
| 208 | ] |
| 209 | ]; |
| 210 | |
| 211 | if ($nameClassKey) { |
| 212 | $keys[] = [ |
| 213 | 'pubkey' => $nameClassKey, |
| 214 | 'isSigner' => true, |
| 215 | 'isWritable' => false |
| 216 | ]; |
| 217 | } |
| 218 | |
| 219 | if ($parentOwner && $nameParent) { |
| 220 | if (!$nameClassKey) { |
| 221 | $keys[] = [ |
| 222 | 'pubkey' => new PublicKey(Buffer::alloc(32)), |
| 223 | 'isSigner' => false, |
| 224 | 'isWritable' => false |
| 225 | ]; |
| 226 | } |
| 227 | $keys[] = [ |
| 228 | 'pubkey' => $nameParent, |
| 229 | 'isSigner' => false, |
| 230 | 'isWritable' => false |
| 231 | ]; |
| 232 | } |
| 233 | |
| 234 | return new TransactionInstruction([ |
| 235 | 'keys' => $keys, |
| 236 | 'programId' => $nameProgramId, |
| 237 | 'data' => $data |
| 238 | ]); |
| 239 | } |
| 240 | |
| 241 | |
| 242 | /** |
| 243 | * Creates a realloc instruction. |
| 244 | * |
| 245 | * @param PublicKey $nameProgramId The public key of the name program. |
| 246 | * @param PublicKey $systemProgramId The public key of the system program. |
| 247 | * @param PublicKey $payerKey The public key of the payer. |
| 248 | * @param PublicKey $nameAccountKey The public key of the name account. |
| 249 | * @param PublicKey $nameOwnerKey The public key of the name owner. |
| 250 | * @param Numberu32 $space The amount of space. |
| 251 | * @return TransactionInstruction The created transaction instruction. |
| 252 | */ |
| 253 | function reallocInstruction( |
| 254 | PublicKey $nameProgramId, |
| 255 | PublicKey $systemProgramId, |
| 256 | PublicKey $payerKey, |
| 257 | PublicKey $nameAccountKey, |
| 258 | PublicKey $nameOwnerKey, |
| 259 | Numberu32 $space |
| 260 | ): TransactionInstruction { |
| 261 | $buffers = [ |
| 262 | Buffer::fromArray([4]), |
| 263 | $space->toBuffer() |
| 264 | ]; |
| 265 | |
| 266 | $data = Buffer::concat($buffers); |
| 267 | $keys = [ |
| 268 | [ |
| 269 | 'pubkey' => $systemProgramId, |
| 270 | 'isSigner' => false, |
| 271 | 'isWritable' => false |
| 272 | ], |
| 273 | [ |
| 274 | 'pubkey' => $payerKey, |
| 275 | 'isSigner' => true, |
| 276 | 'isWritable' => true |
| 277 | ], |
| 278 | [ |
| 279 | 'pubkey' => $nameAccountKey, |
| 280 | 'isSigner' => false, |
| 281 | 'isWritable' => true |
| 282 | ], |
| 283 | [ |
| 284 | 'pubkey' => $nameOwnerKey, |
| 285 | 'isSigner' => true, |
| 286 | 'isWritable' => false |
| 287 | ] |
| 288 | ]; |
| 289 | |
| 290 | return new TransactionInstruction([ |
| 291 | 'keys' => $keys, |
| 292 | 'programId' => $nameProgramId, |
| 293 | 'data' => $data |
| 294 | ]); |
| 295 | } |
| 296 | |
| 297 | /** |
| 298 | * Creates a delete instruction. |
| 299 | * |
| 300 | * @param PublicKey $nameProgramId The public key of the name program. |
| 301 | * @param PublicKey $nameAccountKey The public key of the name account. |
| 302 | * @param PublicKey $refundTargetKey The public key of the refund target. |
| 303 | * @param PublicKey $nameOwnerKey The public key of the name owner. |
| 304 | * @return TransactionInstruction The created transaction instruction. |
| 305 | */ |
| 306 | function deleteInstruction( |
| 307 | PublicKey $nameProgramId, |
| 308 | PublicKey $nameAccountKey, |
| 309 | PublicKey $refundTargetKey, |
| 310 | PublicKey $nameOwnerKey |
| 311 | ): TransactionInstruction { |
| 312 | $buffers = [ |
| 313 | Buffer::fromArray([3]) |
| 314 | ]; |
| 315 | |
| 316 | $data = Buffer::concat($buffers); |
| 317 | $keys = [ |
| 318 | [ |
| 319 | 'pubkey' => $nameAccountKey, |
| 320 | 'isSigner' => false, |
| 321 | 'isWritable' => true |
| 322 | ], |
| 323 | [ |
| 324 | 'pubkey' => $nameOwnerKey, |
| 325 | 'isSigner' => true, |
| 326 | 'isWritable' => false |
| 327 | ], |
| 328 | [ |
| 329 | 'pubkey' => $refundTargetKey, |
| 330 | 'isSigner' => false, |
| 331 | 'isWritable' => true |
| 332 | ] |
| 333 | ]; |
| 334 | |
| 335 | return new TransactionInstruction([ |
| 336 | 'keys' => $keys, |
| 337 | 'programId' => $nameProgramId, |
| 338 | 'data' => $data |
| 339 | ]); |
| 340 | } |
| 341 | |
| 342 | |
| 343 | } |