forked from p2p-org/solanaj
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to call Anchor programs, including example from the basic…
…-0 tutorial. Added AnchorTest.
- Loading branch information
skynetcapital
committed
May 7, 2021
1 parent
3b51c1e
commit 1c0b279
Showing
5 changed files
with
124 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,5 +6,5 @@ build | |
.settings | ||
.idea | ||
*.iml | ||
secretkey.dat | ||
*.dat | ||
*.properties |
73 changes: 73 additions & 0 deletions
73
src/main/java/org/p2p/solanaj/programs/anchor/AnchorBasicTutorialProgram.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package org.p2p.solanaj.programs.anchor; | ||
|
||
import org.p2p.solanaj.core.Account; | ||
import org.p2p.solanaj.core.AccountMeta; | ||
import org.p2p.solanaj.core.PublicKey; | ||
import org.p2p.solanaj.core.TransactionInstruction; | ||
import org.p2p.solanaj.programs.Program; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* Implements the "initialize" call from Anchor's basic-0 tutorial. | ||
*/ | ||
public class AnchorBasicTutorialProgram extends Program { | ||
|
||
// Testnet address of basic-0 = EkEwddr34fqnv2SJREPynyC335PE32PAfjY4LVW5bTJS (has a method called initialize) | ||
private static final PublicKey PROGRAM_ID = new PublicKey("EkEwddr34fqnv2SJREPynyC335PE32PAfjY4LVW5bTJS"); | ||
private static final String FUNCTION_NAMESPACE = "global::initialize"; | ||
|
||
/** | ||
* Calls basic_0::initialize | ||
* | ||
* @param caller account signing the transaction | ||
* @return tx id | ||
*/ | ||
public static TransactionInstruction initialize(Account caller) { | ||
final List<AccountMeta> keys = new ArrayList<>(); | ||
keys.add(new AccountMeta(caller.getPublicKey(),true, false)); | ||
|
||
byte[] transactionData = encodeInitializeData(); | ||
|
||
return createTransactionInstruction( | ||
PROGRAM_ID, | ||
keys, | ||
transactionData | ||
); | ||
} | ||
|
||
/** | ||
* Encodes the "global::initialize" sighash | ||
* @return byte array containing sighash for "global::initialize" | ||
*/ | ||
private static byte[] encodeInitializeData() { | ||
MessageDigest digest = null; | ||
byte[] encodedhash = null; | ||
int sigHashStart = 0; | ||
int sigHashEnd = 8; | ||
|
||
try { | ||
digest = MessageDigest.getInstance("SHA-256"); | ||
encodedhash = Arrays.copyOfRange( | ||
digest.digest( | ||
FUNCTION_NAMESPACE.getBytes( | ||
StandardCharsets.UTF_8 | ||
) | ||
), | ||
sigHashStart, | ||
sigHashEnd | ||
); | ||
} catch (NoSuchAlgorithmException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
return encodedhash; | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package org.p2p.solanaj.core; | ||
|
||
import org.junit.Test; | ||
import org.p2p.solanaj.programs.MemoProgram; | ||
import org.p2p.solanaj.programs.anchor.AnchorBasicTutorialProgram; | ||
import org.p2p.solanaj.rpc.Cluster; | ||
import org.p2p.solanaj.rpc.RpcClient; | ||
import org.p2p.solanaj.rpc.RpcException; | ||
|
||
import java.util.List; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
public class AnchorTest extends AccountBasedTest { | ||
|
||
private final RpcClient client = new RpcClient(Cluster.TESTNET); | ||
|
||
/** | ||
* Calls a testnet Anchor program: (tutorials/basic-0)'s 'initialize" call. | ||
* Also attaches a memo. | ||
*/ | ||
@Test | ||
public void basicInitializeTest() { | ||
final Account feePayer = testAccount; | ||
|
||
final Transaction transaction = new Transaction(); | ||
transaction.addInstruction( | ||
AnchorBasicTutorialProgram.initialize(feePayer) | ||
); | ||
|
||
transaction.addInstruction( | ||
MemoProgram.writeUtf8(feePayer, "I just called an Anchor program from SolanaJ.") | ||
); | ||
|
||
final List<Account> signers = List.of(feePayer); | ||
String result = null; | ||
try { | ||
result = client.getApi().sendTransaction(transaction, signers, null); | ||
} catch (RpcException e) { | ||
e.printStackTrace(); | ||
} | ||
|
||
assertNotNull(result); | ||
} | ||
|
||
|
||
|
||
} |