Skip to content

Commit

Permalink
Start implementation of SerumProgram.consumeEvents
Browse files Browse the repository at this point in the history
  • Loading branch information
skynetcapital committed May 6, 2021
1 parent 32f4fe3 commit 3b51c1e
Show file tree
Hide file tree
Showing 4 changed files with 120 additions and 5 deletions.
59 changes: 59 additions & 0 deletions src/main/java/org/p2p/solanaj/programs/SerumProgram.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

import static org.p2p.solanaj.serum.SerumUtils.OWN_ADDRESS_OFFSET;

Expand All @@ -26,6 +29,42 @@ public class SerumProgram extends Program {
private static final PublicKey SYSVAR_RENT_PUBKEY = new PublicKey("SysvarRent111111111111111111111111111111111");
private static final PublicKey SERUM_PROGRAM_ID_V3 = new PublicKey("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin");


public static TransactionInstruction consumeEvents(RpcClient client, Market market) {
// Add signer to AccountMeta keys

// openOrders Pubkeys
// market Pubkey
// eventQueue Pubkey
List<PublicKey> openOrdersPubkeys = findOpenOrdersAccounts(client, market.getOwnAddress());

// convert to account metas
List<AccountMeta> accountMetas = openOrdersPubkeys.stream()
.map(publicKey -> new AccountMeta(publicKey, false, true))
.collect(Collectors.toList());

int limit = 5;
byte[] transactionData = encodeConsumeEventsTransactionData(
limit
);

return createTransactionInstruction(
SERUM_PROGRAM_ID_V3,
accountMetas,
transactionData
);
}

private static byte[] encodeConsumeEventsTransactionData(int limit) {
ByteBuffer result = ByteBuffer.allocate(3);
result.order(ByteOrder.LITTLE_ENDIAN);

result.put((byte) 4);
result.putShort((short) limit);

return result.array();
}

public static TransactionInstruction placeOrder(RpcClient client, Account account, Account payer, Account openOrders, Market market, Order order) {
/*
See: https://github.com/project-serum/serum-ts/blob/e51e3d9af0ab7026155b76a1824cea6507fc7ef7/packages/serum/src/instructions.js#L118
Expand Down Expand Up @@ -262,4 +301,24 @@ private static PublicKey findOpenOrdersAccountForOwner(RpcClient client, PublicK
return new PublicKey(base58Pubkey);
}

private static List<PublicKey> findOpenOrdersAccounts(RpcClient client, PublicKey marketAddress) {
int dataSize = 3228;

List<ProgramAccount> programAccounts = null;
ConfigObjects.Memcmp marketFilter = new ConfigObjects.Memcmp(OWN_ADDRESS_OFFSET, marketAddress.toBase58());
List<ConfigObjects.Memcmp> memcmpList = List.of(marketFilter);

try {
programAccounts = client.getApi().getProgramAccounts(SERUM_PROGRAM_ID_V3, memcmpList, dataSize);
} catch (RpcException e) {
e.printStackTrace();
}

List<PublicKey> results = programAccounts.stream()
.map(programAccount -> new PublicKey(programAccount.getPubkey()))
.collect(Collectors.toList());

return results;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import java.util.List;
import java.util.logging.Logger;

public class OrderManager {
public class SerumManager {

private static final Logger LOGGER = Logger.getLogger(OrderManager.class.getName());
private static final Logger LOGGER = Logger.getLogger(SerumManager.class.getName());
private final RpcClient client = new RpcClient(Cluster.MAINNET);

/**
Expand Down Expand Up @@ -64,6 +64,34 @@ public String placeOrder(Account account, Account openOrders, Market market, Ord
return result;
}

/**
* Cranks a given market with the ConsumeEvents instruction.
*
* @param market market to run crank against
* @return transaction id of ConsumeEvents call
*/
public String consumeEvents(Market market, Account payerAccount) {
// Get all open orders accounts TODO - fix this
final Transaction transaction = new Transaction();

transaction.addInstruction(
SerumProgram.consumeEvents(
client,
market
)
);

final List<Account> signers = List.of(payerAccount);
String result = null;
try {
result = client.getApi().sendTransaction(transaction, signers, null);
} catch (RpcException e) {
e.printStackTrace();
}

return result;
}



}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.p2p.solanaj.rpc.RpcClient;

/**
* Builds Serum market {@link Transaction}s which are used by {@link OrderManager} to create Serum order transactions.
* Builds Serum market {@link Transaction}s which are used by {@link SerumManager} to create Serum order transactions.
*
*/
public class TransactionBuilder {
Expand Down
32 changes: 30 additions & 2 deletions src/test/java/org/p2p/solanaj/core/OrderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public class OrderTest {

private static final Logger LOGGER = Logger.getLogger(OrderTest.class.getName());
private final OrderManager orderManager = new OrderManager();
private final SerumManager serumManager = new SerumManager();
private final RpcClient client = new RpcClient(Cluster.MAINNET);
private static final PublicKey SOL_USDC_MARKET_V3 = new PublicKey("9wFFyRfZBsuAha4YcuxcXLKwMxJR43S7fPfQLusDBzvT");

Expand Down Expand Up @@ -48,7 +48,7 @@ public void placeOrderTest() {
final Account openOrders = new Account();

// Place order
String transactionId = orderManager.placeOrder(account, openOrders, solUsdcMarket, new Order(1, 1, 1));
String transactionId = serumManager.placeOrder(account, openOrders, solUsdcMarket, new Order(1, 1, 1));

// Verify we got a txId
assertNotNull(transactionId);
Expand All @@ -65,4 +65,32 @@ public void testOpenOrdersAccount() throws IOException {

System.out.println(accountInfo.toString());
}

@Test
@Ignore
public void consumeEventsTest() {
LOGGER.info("Consuming events");

// Build account from secretkey.dat
byte[] data = new byte[0];
try {
data = Files.readAllBytes(Paths.get("secretkey.dat"));
} catch (IOException e) {
e.printStackTrace();
}

// Create account from private key
final Account account = new Account(Base58.decode(new String(data)));

// Get SOL/USDC market
final Market solUsdcMarket = new MarketBuilder()
.setPublicKey(SOL_USDC_MARKET_V3)
.setRetrieveOrderBooks(false)
.build();
// Place order
String transactionId = serumManager.consumeEvents(solUsdcMarket, account);

// Verify we got a txId
assertNotNull(transactionId);
}
}

0 comments on commit 3b51c1e

Please sign in to comment.