-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8369ac1
commit 966b71b
Showing
8 changed files
with
291 additions
and
64 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
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,39 @@ | ||
using Solnet.Wallet; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Solnet.Programs.Abstract | ||
{ | ||
/// <summary> | ||
/// A class to abstract some of the core program commonality | ||
/// </summary> | ||
public abstract class BaseProgram : Program | ||
{ | ||
private PublicKey _programIdKey; | ||
private string _programName; | ||
|
||
/// <summary> | ||
/// The public key of the program. | ||
/// </summary> | ||
public virtual PublicKey ProgramIdKey => _programIdKey; | ||
|
||
/// <summary> | ||
/// The program's name. | ||
/// </summary> | ||
public virtual string ProgramName => _programName; | ||
|
||
/// <summary> | ||
/// Creates an instance of the base program class with specified id and name | ||
/// </summary> | ||
/// <param name="programIdKey">The program key</param> | ||
/// <param name="programName">The program name</param> | ||
protected BaseProgram(PublicKey programIdKey, string programName) | ||
{ | ||
_programIdKey = programIdKey; | ||
_programName = programName; | ||
} | ||
} | ||
} |
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,21 @@ | ||
using Solnet.Wallet; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Solnet.Programs.Abstract | ||
{ | ||
public interface Program | ||
{ | ||
/// <summary> | ||
/// The program's key | ||
/// </summary> | ||
PublicKey ProgramIdKey { get; } | ||
/// <summary> | ||
/// The name of the program | ||
/// </summary> | ||
string ProgramName { get; } | ||
} | ||
} |
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
115 changes: 115 additions & 0 deletions
115
src/Solnet.Programs/TokenSwap/Models/TokenSwapAccount.cs
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,115 @@ | ||
using Solnet.Wallet; | ||
|
||
namespace Solnet.Programs.TokenSwap.Models | ||
{ | ||
|
||
/// <summary> | ||
/// TokenSwap program state | ||
/// </summary> | ||
public class TokenSwapAccount | ||
{ | ||
/// <summary> | ||
/// the size of this account in bytes | ||
/// </summary> | ||
public const int TOKEN_SWAP_DATA_LEN = 324; | ||
|
||
/// <summary> | ||
/// Versions of this state account | ||
/// </summary> | ||
public enum SwapVersion { SwapV1 = 1 } | ||
|
||
/// <summary> | ||
/// Version of this state account | ||
/// </summary> | ||
public SwapVersion Version; | ||
|
||
/// <summary> | ||
/// Initialized state | ||
/// </summary> | ||
public bool IsInitialized; | ||
|
||
/// <summary> | ||
/// Nonce used in program address. | ||
/// The program address is created deterministically with the nonce, | ||
/// swap program id, and swap account pubkey. This program address has | ||
/// authority over the swap's token A account, token B account, and pool | ||
/// token mint. | ||
/// </summary> | ||
public byte Nonce; | ||
|
||
/// <summary> | ||
/// Program ID of the tokens being exchanged. | ||
/// </summary> | ||
public PublicKey TokenProgramId; | ||
|
||
/// <summary> | ||
/// Token A | ||
/// </summary> | ||
public PublicKey TokenAAccount; | ||
|
||
/// <summary> | ||
/// Token B | ||
/// </summary> | ||
public PublicKey TokenBAccount; | ||
|
||
/// <summary> | ||
/// Pool tokens are issued when A or B tokens are deposited. | ||
/// Pool tokens can be withdrawn back to the original A or B token. | ||
/// </summary> | ||
public PublicKey PoolMint; | ||
|
||
/// <summary> | ||
/// Mint information for token A | ||
/// </summary> | ||
public PublicKey TokenAMint; | ||
|
||
/// <summary> | ||
/// Mint information for token B | ||
/// </summary> | ||
public PublicKey TokenBMint; | ||
|
||
/// <summary> | ||
/// Pool token account to receive trading and / or withdrawal fees | ||
/// </summary> | ||
public PublicKey PoolFeeAccount; | ||
|
||
/// <summary> | ||
/// All fee information | ||
/// </summary> | ||
public Fees Fees; | ||
|
||
/// <summary> | ||
/// Swap curve parameters, to be unpacked and used by the SwapCurve, which | ||
/// calculates swaps, deposits, and withdrawals | ||
/// </summary> | ||
public SwapCurve SwapCurve; | ||
|
||
/// <summary> | ||
/// Deserilize a token swap from the bytes of an account | ||
/// </summary> | ||
/// <param name="data"></param> | ||
/// <returns></returns> | ||
public static TokenSwapAccount Deserialize(byte[] data) | ||
{ | ||
if (data.Length != TOKEN_SWAP_DATA_LEN) | ||
return null; | ||
|
||
var ret = new TokenSwapAccount() | ||
{ | ||
Version = SwapVersion.SwapV1, | ||
IsInitialized = data[1] == 1, | ||
Nonce = data[2], | ||
TokenProgramId = new PublicKey(data[3..35]), | ||
TokenAAccount = new PublicKey(data[35..67]), | ||
TokenBAccount = new PublicKey(data[67..99]), | ||
PoolMint = new PublicKey(data[99..131]), | ||
TokenAMint = new PublicKey(data[131..163]), | ||
TokenBMint = new PublicKey(data[163..195]), | ||
PoolFeeAccount = new PublicKey(data[195..227]), | ||
Fees = Fees.Deserialize(data[227..291]), | ||
SwapCurve = SwapCurve.Deserialize(data[291..]), | ||
}; | ||
return ret; | ||
} | ||
} | ||
} |
Oops, something went wrong.