Skip to content

Commit

Permalink
Added compute budget to the transaction builder
Browse files Browse the repository at this point in the history
  • Loading branch information
BifrostTitan committed Oct 21, 2024
1 parent 07ce498 commit 8720c61
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
14 changes: 11 additions & 3 deletions Client/RaydiumClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,22 @@ public RaydiumAmmClient(IRpcClient rpcClient)
RpcClient = rpcClient;
}

public async Task<RequestResult<string>> BuildSignSend(Account trader, PublicKey feePayer, TransactionInstruction instr)
public async Task<RequestResult<string>> BuildSignSend(Account trader, PublicKey feePayer, TransactionInstruction instr, ulong computeprice = 0, ulong computebudget = 0)
{
try
{

var blockhash = await RpcClient.GetLatestBlockHashAsync();
TransactionBuilder builder = new TransactionBuilder();
builder.SetFeePayer(feePayer);
builder.SetRecentBlockHash(blockhash.Result.Value.Blockhash);
if (computebudget > 0 && computeprice > 0)
{
TransactionInstruction computeBudget = RaydiumAmmProgram.SetCUlimit(computebudget);
TransactionInstruction computePrice = ComputeBudgetProgram.SetComputeUnitPrice(computeprice);
builder.AddInstruction(computeBudget);
builder.AddInstruction(computePrice);
}
builder.AddInstruction(instr);
var tx = builder.Build(trader);

Expand Down Expand Up @@ -138,7 +146,7 @@ public async Task<RequestResult<string>> SendWithdrawSrmAsync(WithdrawSrmAccount
return await BuildSignSend(trader, feePayer, instr);
}

public async Task<RequestResult<string>> SendSwapAsync(string _poolAddress, ulong amountIn, ulong minimumAmountOut, OrderSide side, PublicKey feePayer, Account trader)
public async Task<RequestResult<string>> SendSwapAsync(string _poolAddress, ulong amountIn, ulong minimumAmountOut, OrderSide side, PublicKey feePayer, Account trader, ulong computebudget = 0, ulong computeprice = 0)
{
PublicKey poolAddress = new PublicKey(_poolAddress);
var ammInfo = await GetAmmInfoAsync(poolAddress);
Expand Down Expand Up @@ -181,7 +189,7 @@ public async Task<RequestResult<string>> SendSwapAsync(string _poolAddress, ulon
swapBaseInAccounts.UerDestinationTokenAccount = quoteTokenAccount;
}
TransactionInstruction instr = RaydiumAmmProgram.PerformSwap(swapBaseInAccounts, amountIn, minimumAmountOut, programId);
return await BuildSignSend(trader, feePayer, instr);
return await BuildSignSend(trader, feePayer, instr, computeprice, computebudget);
}

}
Expand Down
22 changes: 15 additions & 7 deletions Client/RaydiumProgram.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,26 @@
using Solnet.Programs.Abstract;
using Solnet.Programs.Utilities;
using Solnet.Rpc;
using Solnet.Rpc.Builders;
using Solnet.Rpc.Core.Http;
using Solnet.Rpc.Core.Sockets;
using Solnet.Rpc.Types;
using Solnet.Programs.Utilities;
using Solnet.Rpc.Models;
using Solnet.Wallet;
using Solnet.Raydium.Types;
using Solnet.Programs;

namespace Solnet.Raydium.Client
{
public static class RaydiumAmmProgram
{
public static TransactionInstruction SetCUlimit(ulong units)
{
List<AccountMeta> keys = new List<AccountMeta>();
byte[] data = new byte[9];
data.WriteU8(2, 0);
data.WriteU64(units, 1);
return new TransactionInstruction
{
ProgramId = ComputeBudgetProgram.ProgramIdKey,
Keys = keys,
Data = data
};
}
public static TransactionInstruction Initialize(InitializeAccounts accounts, byte nonce, ulong openTime, PublicKey programId)
{
List<AccountMeta> keys = new()
Expand Down
10 changes: 4 additions & 6 deletions Solnet.Raydium.csproj
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Solnet.Extensions" Version="6.1.0" />
<PackageReference Include="Solnet.KeyStore" Version="6.1.0" />
<PackageReference Include="Solnet.Programs" Version="6.1.0" />
<PackageReference Include="Solnet.Rpc" Version="6.1.0" />
<PackageReference Include="Solnet.Wallet" Version="6.1.0" />
<PackageReference Include="Solana.Programs" Version="8.0.2" />
<PackageReference Include="Solana.Rpc" Version="8.0.2" />
<PackageReference Include="Solana.Wallet" Version="8.0.2" />
</ItemGroup>

</Project>

0 comments on commit 8720c61

Please sign in to comment.