Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
87 changes: 87 additions & 0 deletions solana/usdfswap/accounts_pool.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package usdf_swap

import (
"bytes"
"crypto/ed25519"
"fmt"

"github.com/mr-tron/base58"
)

const (
PoolAccountSize = (8 + // discriminator
32 + // authority
MaxPoolNameLength + // name
32 + // usdf_mint
32 + // other_mint
32 + // usdf_vault
32 + // other_vault
1 + // bump
1 + // usdf_vault_bump
1 + // other_vault_bump
1 + // usdf_decimals
1 + // other_decimals
3) // padding
)

var PoolAccountDiscriminator = []byte{byte(AccountTypePool), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}

type PoolAccount struct {
Authority ed25519.PublicKey
Name string
UsdfMint ed25519.PublicKey
OtherMint ed25519.PublicKey
UsdfVault ed25519.PublicKey
OtherVault ed25519.PublicKey
Bump uint8
UsdfVaultBump uint8
OtherVaultBump uint8
UsdfDecimals uint8
OtherDecimals uint8
}

func (obj *PoolAccount) Unmarshal(data []byte) error {
if len(data) < PoolAccountSize {
return ErrInvalidAccountData
}

var offset int

var discriminator []byte
getDiscriminator(data, &discriminator, &offset)
if !bytes.Equal(discriminator, PoolAccountDiscriminator) {
return ErrInvalidAccountData
}

getKey(data, &obj.Authority, &offset)
getFixedString(data, &obj.Name, MaxPoolNameLength, &offset)
getKey(data, &obj.UsdfMint, &offset)
getKey(data, &obj.OtherMint, &offset)
getKey(data, &obj.UsdfVault, &offset)
getKey(data, &obj.OtherVault, &offset)
getUint8(data, &obj.Bump, &offset)
getUint8(data, &obj.UsdfVaultBump, &offset)
getUint8(data, &obj.OtherVaultBump, &offset)
getUint8(data, &obj.UsdfDecimals, &offset)
getUint8(data, &obj.OtherDecimals, &offset)
offset += 3 // padding

return nil
}

func (obj *PoolAccount) String() string {
return fmt.Sprintf(
"Pool{authority=%s,name=%s,usdf_mint=%s,other_mint=%s,usdf_vault=%s,other_vault=%s,bump=%d,usdf_vault_bump=%d,other_vault_bump=%d,usdf_decimals=%d,other_decimals=%d}",
base58.Encode(obj.Authority),
obj.Name,
base58.Encode(obj.UsdfMint),
base58.Encode(obj.OtherMint),
base58.Encode(obj.UsdfVault),
base58.Encode(obj.OtherVault),
obj.Bump,
obj.UsdfVaultBump,
obj.OtherVaultBump,
obj.UsdfDecimals,
obj.OtherDecimals,
)
}
44 changes: 44 additions & 0 deletions solana/usdfswap/address.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package usdf_swap

import (
"crypto/ed25519"

"github.com/code-payments/ocp-server/solana"
)

var (
PoolPrefix = []byte("pool")
VaultPrefix = []byte("vault")
)

type GetPoolAddressArgs struct {
Authority ed25519.PublicKey
Name string
UsdfMint ed25519.PublicKey
OtherMint ed25519.PublicKey
}

func GetPoolAddress(args *GetPoolAddressArgs) (ed25519.PublicKey, uint8, error) {
return solana.FindProgramAddressAndBump(
PROGRAM_ID,
PoolPrefix,
args.Authority,
[]byte(toFixedString(args.Name, MaxPoolNameLength)),
args.UsdfMint,
args.OtherMint,
)
}

type GetVaultAddressArgs struct {
Pool ed25519.PublicKey
Mint ed25519.PublicKey
}

func GetVaultAddress(args *GetVaultAddressArgs) (ed25519.PublicKey, uint8, error) {
return solana.FindProgramAddressAndBump(
PROGRAM_ID,
VaultPrefix,
args.Pool,
args.Mint,
)
}
102 changes: 102 additions & 0 deletions solana/usdfswap/instructions_initialize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package usdf_swap

import (
"crypto/ed25519"

"github.com/code-payments/ocp-server/solana"
)

const (
InitializeInstructionArgsSize = (MaxPoolNameLength + // name
1 + // bump
1 + // usdf_vault_bump
1) // other_vault_bump
)

type InitializeInstructionArgs struct {
Name string
Bump uint8
UsdfVaultBump uint8
OtherVaultBump uint8
}

type InitializeInstructionAccounts struct {
Authority ed25519.PublicKey
UsdfMint ed25519.PublicKey
OtherMint ed25519.PublicKey
Pool ed25519.PublicKey
UsdfVault ed25519.PublicKey
OtherVault ed25519.PublicKey
}

func NewInitializeInstruction(
accounts *InitializeInstructionAccounts,
args *InitializeInstructionArgs,
) solana.Instruction {
var offset int

// Serialize instruction arguments
data := make([]byte, 1+InitializeInstructionArgsSize)

putInstructionType(data, InstructionTypeInitialize, &offset)
putFixedString(data, args.Name, MaxPoolNameLength, &offset)
putUint8(data, args.Bump, &offset)
putUint8(data, args.UsdfVaultBump, &offset)
putUint8(data, args.OtherVaultBump, &offset)

return solana.Instruction{
Program: PROGRAM_ADDRESS,

// Instruction args
Data: data,

// Instruction accounts
Accounts: []solana.AccountMeta{
{
PublicKey: accounts.Authority,
IsWritable: true,
IsSigner: true,
},
{
PublicKey: accounts.UsdfMint,
IsWritable: false,
IsSigner: false,
},
{
PublicKey: accounts.OtherMint,
IsWritable: false,
IsSigner: false,
},
{
PublicKey: accounts.Pool,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: accounts.UsdfVault,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: accounts.OtherVault,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: SPL_TOKEN_PROGRAM_ID,
IsWritable: false,
IsSigner: false,
},
{
PublicKey: SYSTEM_PROGRAM_ID,
IsWritable: false,
IsSigner: false,
},
{
PublicKey: SYSVAR_RENT_PUBKEY,
IsWritable: false,
IsSigner: false,
},
},
}
}
86 changes: 86 additions & 0 deletions solana/usdfswap/instructions_swap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
package usdf_swap

import (
"crypto/ed25519"

"github.com/code-payments/ocp-server/solana"
)

const (
SwapInstructionArgsSize = (8 + // amount
1) // usdf_to_other
)

type SwapInstructionArgs struct {
Amount uint64
UsdfToOther bool
}

type SwapInstructionAccounts struct {
User ed25519.PublicKey
Pool ed25519.PublicKey
UsdfVault ed25519.PublicKey
OtherVault ed25519.PublicKey
UserUsdfToken ed25519.PublicKey
UserOtherToken ed25519.PublicKey
}

func NewSwapInstruction(
accounts *SwapInstructionAccounts,
args *SwapInstructionArgs,
) solana.Instruction {
var offset int

// Serialize instruction arguments
data := make([]byte, 1+SwapInstructionArgsSize)

putInstructionType(data, InstructionTypeSwap, &offset)
putUint64(data, args.Amount, &offset)
putBool(data, args.UsdfToOther, &offset)

return solana.Instruction{
Program: PROGRAM_ADDRESS,

// Instruction args
Data: data,

// Instruction accounts
Accounts: []solana.AccountMeta{
{
PublicKey: accounts.User,
IsWritable: true,
IsSigner: true,
},
{
PublicKey: accounts.Pool,
IsWritable: false,
IsSigner: false,
},
{
PublicKey: accounts.UsdfVault,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: accounts.OtherVault,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: accounts.UserUsdfToken,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: accounts.UserOtherToken,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: SPL_TOKEN_PROGRAM_ID,
IsWritable: false,
IsSigner: false,
},
},
}
}
74 changes: 74 additions & 0 deletions solana/usdfswap/instructions_transfer.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package usdf_swap

import (
"crypto/ed25519"

"github.com/code-payments/ocp-server/solana"
)

const (
TransferInstructionArgsSize = (8 + // amount
1) // is_usdf
)

type TransferInstructionArgs struct {
Amount uint64
IsUsdf bool
}

type TransferInstructionAccounts struct {
Authority ed25519.PublicKey
Pool ed25519.PublicKey
Vault ed25519.PublicKey
Destination ed25519.PublicKey
}

func NewTransferInstruction(
accounts *TransferInstructionAccounts,
args *TransferInstructionArgs,
) solana.Instruction {
var offset int

// Serialize instruction arguments
data := make([]byte, 1+TransferInstructionArgsSize)

putInstructionType(data, InstructionTypeTransfer, &offset)
putUint64(data, args.Amount, &offset)
putBool(data, args.IsUsdf, &offset)

return solana.Instruction{
Program: PROGRAM_ADDRESS,

// Instruction args
Data: data,

// Instruction accounts
Accounts: []solana.AccountMeta{
{
PublicKey: accounts.Authority,
IsWritable: true,
IsSigner: true,
},
{
PublicKey: accounts.Pool,
IsWritable: false,
IsSigner: false,
},
{
PublicKey: accounts.Vault,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: accounts.Destination,
IsWritable: true,
IsSigner: false,
},
{
PublicKey: SPL_TOKEN_PROGRAM_ID,
IsWritable: false,
IsSigner: false,
},
},
}
}
Loading
Loading