feat: миграция smart-updown на anchor 0.31.1 + solana 2.x (SBPFv3)
- anchor-lang 0.31.1 вместо 2.0-rc/anchor-next (git) - Address -> Pubkey (state/events/instructions) - убран #[discrim], добавлен #[derive(InitSpace)] + space=8+INIT_SPACE - тесты переписаны на solana-program-test 2.x (10 passed) - сборка SBPFv3 через cargo build-sbf --tools-version v1.57 - deploy успешен в нашу сеть (agave 4.2.2, SBPFv3)
This commit is contained in:
Generated
+5362
-1175
File diff suppressed because it is too large
Load Diff
@@ -15,19 +15,20 @@ cpi = ["no-entrypoint"]
|
||||
no-entrypoint = []
|
||||
no-log-ix-name = []
|
||||
idl-build = []
|
||||
profile = ["anchor-v2-testing/profile"]
|
||||
|
||||
[dependencies]
|
||||
# Once anchor-lang is published to crates.io, swap to: anchor-lang = "2.0.0-rc.1"
|
||||
anchor-lang = { git = "https://github.com/otter-sec/anchor.git", branch = "anchor-next" }
|
||||
solana-program-log = { version = "1.1", features = ["macro"] }
|
||||
wincode = { version = "0.5", features = ["derive"] }
|
||||
anchor-lang = "0.31.1"
|
||||
|
||||
[dev-dependencies]
|
||||
anchor-v2-testing = { git = "https://github.com/otter-sec/anchor.git", branch = "anchor-next" }
|
||||
solana-clock = "3.0.1"
|
||||
solana-pubkey = "3.0.0"
|
||||
|
||||
anchor-lang = "0.31.1"
|
||||
solana-program-test = "2.1"
|
||||
solana-client = "2.1"
|
||||
solana-pubkey = "2.1"
|
||||
solana-signer = "2.1"
|
||||
solana-keypair = "2.1"
|
||||
solana-clock = "2.1"
|
||||
solana-transaction = "2.1"
|
||||
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
|
||||
|
||||
[lints.rust]
|
||||
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(target_os, values("solana"))'] }
|
||||
|
||||
@@ -2,8 +2,8 @@ use anchor_lang::prelude::*;
|
||||
|
||||
#[event]
|
||||
pub struct BetCreated {
|
||||
pub bet: Address,
|
||||
pub bettor: Address,
|
||||
pub bet: Pubkey,
|
||||
pub bettor: Pubkey,
|
||||
pub side: u64,
|
||||
pub amount: u64,
|
||||
pub bet_time: i64,
|
||||
@@ -13,7 +13,7 @@ pub struct BetCreated {
|
||||
|
||||
#[event]
|
||||
pub struct BetClosed {
|
||||
pub bet: Address,
|
||||
pub bet: Pubkey,
|
||||
pub exit_price: u64,
|
||||
pub user_won: bool,
|
||||
pub payout: u64,
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use anchor_lang::{prelude::*, Lamports};
|
||||
use anchor_lang::prelude::*;
|
||||
|
||||
use crate::{
|
||||
constants::{
|
||||
@@ -12,33 +12,33 @@ use crate::{
|
||||
|
||||
#[derive(Accounts)]
|
||||
#[instruction(id: u64, exit_price: u64)]
|
||||
pub struct CloseBet {
|
||||
pub relayer: Signer,
|
||||
pub struct CloseBet<'info> {
|
||||
pub relayer: Signer<'info>,
|
||||
#[account(
|
||||
mut,
|
||||
seeds = [BET_SEED, id.to_le_bytes()],
|
||||
seeds = [BET_SEED, &id.to_le_bytes()],
|
||||
bump
|
||||
)]
|
||||
pub bet: Account<Bet>,
|
||||
pub bet: Account<'info, Bet>,
|
||||
#[account(
|
||||
mut,
|
||||
seeds = [GLOBAL_SEED],
|
||||
bump
|
||||
)]
|
||||
pub global: Account<Global>,
|
||||
pub global: Account<'info, Global>,
|
||||
/// CHECK: recipient must be the bet's bettor and is validated in the handler.
|
||||
#[account(mut)]
|
||||
pub bettor: SystemAccount,
|
||||
pub clock: Sysvar<Clock>,
|
||||
pub bettor: SystemAccount<'info>,
|
||||
pub clock: Sysvar<'info, Clock>,
|
||||
}
|
||||
|
||||
pub fn handler(ctx: &mut Context<CloseBet>, id: u64, exit_price: u64) -> Result<()> {
|
||||
require!(
|
||||
anchor_lang::address_eq(ctx.accounts.relayer.address(), &ctx.accounts.global.relayer),
|
||||
ctx.accounts.relayer.key() == ctx.accounts.global.relayer,
|
||||
SmartUpdownError::NotRelayer
|
||||
);
|
||||
require!(
|
||||
anchor_lang::address_eq(ctx.accounts.bettor.address(), &ctx.accounts.bet.bettor),
|
||||
ctx.accounts.bettor.key() == ctx.accounts.bet.bettor,
|
||||
SmartUpdownError::NotRelayer
|
||||
);
|
||||
require!(
|
||||
@@ -59,24 +59,24 @@ pub fn handler(ctx: &mut Context<CloseBet>, id: u64, exit_price: u64) -> Result<
|
||||
let user_won = (side == SIDE_UP && exit_price > entry)
|
||||
|| (side == SIDE_DOWN && exit_price < entry);
|
||||
|
||||
let payout;
|
||||
if user_won {
|
||||
let multiplier = ctx.accounts.global.multiplier_bps;
|
||||
let payout_amount =
|
||||
(amount as u128)
|
||||
.checked_mul(multiplier as u128)
|
||||
.unwrap()
|
||||
.checked_div(BPS_DIVISOR as u128)
|
||||
.unwrap() as u64;
|
||||
require!(
|
||||
ctx.accounts.global.view().lamports() >= payout_amount,
|
||||
SmartUpdownError::InsufficientVault
|
||||
);
|
||||
payout = payout_amount;
|
||||
ctx.accounts.global.sub_lamports(payout)?;
|
||||
ctx.accounts.bettor.add_lamports(payout)?;
|
||||
ctx.accounts.bet.status = STATUS_PAYOUT_DONE;
|
||||
} else {
|
||||
let payout;
|
||||
if user_won {
|
||||
let multiplier = ctx.accounts.global.multiplier_bps;
|
||||
let payout_amount =
|
||||
(amount as u128)
|
||||
.checked_mul(multiplier as u128)
|
||||
.unwrap()
|
||||
.checked_div(BPS_DIVISOR as u128)
|
||||
.unwrap() as u64;
|
||||
require!(
|
||||
ctx.accounts.global.get_lamports() >= payout_amount,
|
||||
SmartUpdownError::InsufficientVault
|
||||
);
|
||||
payout = payout_amount;
|
||||
ctx.accounts.global.sub_lamports(payout)?;
|
||||
ctx.accounts.bettor.add_lamports(payout)?;
|
||||
ctx.accounts.bet.status = STATUS_PAYOUT_DONE;
|
||||
} else {
|
||||
payout = 0;
|
||||
ctx.accounts.bet.status = STATUS_HOUSE_WON;
|
||||
}
|
||||
@@ -84,7 +84,7 @@ pub fn handler(ctx: &mut Context<CloseBet>, id: u64, exit_price: u64) -> Result<
|
||||
ctx.accounts.bet.exit_price = exit_price;
|
||||
|
||||
emit!(BetClosed {
|
||||
bet: *ctx.accounts.bet.address(),
|
||||
bet: ctx.accounts.bet.key(),
|
||||
exit_price,
|
||||
user_won,
|
||||
payout,
|
||||
|
||||
@@ -9,24 +9,25 @@ use crate::{
|
||||
|
||||
#[derive(Accounts)]
|
||||
#[instruction(id: u64, side: u64, amount: u64, entry_price: u64)]
|
||||
pub struct CreateBet {
|
||||
pub struct CreateBet<'info> {
|
||||
#[account(mut)]
|
||||
pub bettor: Signer,
|
||||
pub bettor: Signer<'info>,
|
||||
#[account(
|
||||
mut,
|
||||
seeds = [GLOBAL_SEED],
|
||||
bump
|
||||
)]
|
||||
pub global: Account<Global>,
|
||||
pub global: Account<'info, Global>,
|
||||
#[account(
|
||||
init,
|
||||
payer = bettor,
|
||||
seeds = [BET_SEED, id.to_le_bytes()],
|
||||
space = 8 + Bet::INIT_SPACE,
|
||||
seeds = [BET_SEED, &id.to_le_bytes()],
|
||||
bump
|
||||
)]
|
||||
pub bet: Account<Bet>,
|
||||
pub clock: Sysvar<Clock>,
|
||||
pub system_program: Program<System>,
|
||||
pub bet: Account<'info, Bet>,
|
||||
pub clock: Sysvar<'info, Clock>,
|
||||
pub system_program: Program<'info, System>,
|
||||
}
|
||||
|
||||
pub fn handler(
|
||||
@@ -52,8 +53,8 @@ pub fn handler(
|
||||
);
|
||||
|
||||
let now = ctx.accounts.clock.unix_timestamp;
|
||||
let bettor_address = *ctx.accounts.bettor.address();
|
||||
let bet_address = *ctx.accounts.bet.address();
|
||||
let bettor_address = ctx.accounts.bettor.key();
|
||||
let bet_address = ctx.accounts.bet.key();
|
||||
let expiry_seconds = ctx.accounts.global.expiry_seconds;
|
||||
|
||||
{
|
||||
@@ -71,12 +72,15 @@ pub fn handler(
|
||||
ctx.accounts.global.counter = ctx.accounts.global.counter.checked_add(1).unwrap();
|
||||
|
||||
{
|
||||
let cpi_accounts = system_program::Transfer {
|
||||
from: ctx.accounts.bettor.cpi_handle_mut(),
|
||||
to: ctx.accounts.global.cpi_handle_mut(),
|
||||
let cpi_accounts = anchor_lang::system_program::Transfer {
|
||||
from: ctx.accounts.bettor.to_account_info(),
|
||||
to: ctx.accounts.global.to_account_info(),
|
||||
};
|
||||
let cpi_ctx = CpiContext::new(ctx.accounts.system_program.address(), cpi_accounts);
|
||||
system_program::transfer(cpi_ctx, amount)?;
|
||||
let cpi_ctx = CpiContext::new(
|
||||
ctx.accounts.system_program.to_account_info(),
|
||||
cpi_accounts,
|
||||
);
|
||||
anchor_lang::system_program::transfer(cpi_ctx, amount)?;
|
||||
}
|
||||
|
||||
emit!(BetCreated {
|
||||
|
||||
@@ -3,17 +3,18 @@ use anchor_lang::prelude::*;
|
||||
use crate::{constants::GLOBAL_SEED, state::Global};
|
||||
|
||||
#[derive(Accounts)]
|
||||
pub struct Initialize {
|
||||
pub struct Initialize<'info> {
|
||||
#[account(mut)]
|
||||
pub admin: Signer,
|
||||
pub admin: Signer<'info>,
|
||||
#[account(
|
||||
init,
|
||||
payer = admin,
|
||||
space = 8 + Global::INIT_SPACE,
|
||||
seeds = [GLOBAL_SEED],
|
||||
bump
|
||||
)]
|
||||
pub global: Account<Global>,
|
||||
pub system_program: Program<System>,
|
||||
pub global: Account<'info, Global>,
|
||||
pub system_program: Program<'info, System>,
|
||||
}
|
||||
|
||||
pub fn handler(
|
||||
@@ -24,8 +25,8 @@ pub fn handler(
|
||||
expiry_seconds: i64,
|
||||
) -> Result<()> {
|
||||
let global = &mut ctx.accounts.global;
|
||||
global.admin = *ctx.accounts.admin.address();
|
||||
global.relayer = *ctx.accounts.admin.address();
|
||||
global.admin = ctx.accounts.admin.key();
|
||||
global.relayer = ctx.accounts.admin.key();
|
||||
global.counter = 0;
|
||||
global.min_amount = min_amount;
|
||||
global.max_amount = max_amount;
|
||||
|
||||
@@ -7,19 +7,19 @@ use crate::{
|
||||
};
|
||||
|
||||
#[derive(Accounts)]
|
||||
pub struct Update {
|
||||
pub admin: Signer,
|
||||
pub struct Update<'info> {
|
||||
pub admin: Signer<'info>,
|
||||
#[account(
|
||||
mut,
|
||||
seeds = [GLOBAL_SEED],
|
||||
bump
|
||||
)]
|
||||
pub global: Account<Global>,
|
||||
pub global: Account<'info, Global>,
|
||||
}
|
||||
|
||||
pub fn handler(
|
||||
ctx: &mut Context<Update>,
|
||||
new_relayer: Address,
|
||||
new_relayer: Pubkey,
|
||||
min_amount: u64,
|
||||
max_amount: u64,
|
||||
multiplier_bps: u64,
|
||||
@@ -27,7 +27,7 @@ pub fn handler(
|
||||
expiry_seconds: i64,
|
||||
) -> Result<()> {
|
||||
require!(
|
||||
anchor_lang::address_eq(ctx.accounts.admin.address(), &ctx.accounts.global.admin),
|
||||
ctx.accounts.admin.key() == ctx.accounts.global.admin,
|
||||
SmartUpdownError::NotAdmin
|
||||
);
|
||||
let global = &mut ctx.accounts.global;
|
||||
|
||||
@@ -14,41 +14,37 @@ declare_id!("9ALsnxXNzDBv3mokngCHbruRTWUZiWR7pf7vswS1fCpf");
|
||||
pub mod smart_updown {
|
||||
use super::*;
|
||||
|
||||
#[discrim = 0]
|
||||
pub fn initialize(
|
||||
ctx: &mut Context<Initialize>,
|
||||
mut ctx: Context<Initialize>,
|
||||
min_amount: u64,
|
||||
max_amount: u64,
|
||||
multiplier_bps: u64,
|
||||
expiry_seconds: i64,
|
||||
) -> Result<()> {
|
||||
initialize::handler(ctx, min_amount, max_amount, multiplier_bps, expiry_seconds)
|
||||
initialize::handler(&mut ctx, min_amount, max_amount, multiplier_bps, expiry_seconds)
|
||||
}
|
||||
|
||||
#[discrim = 1]
|
||||
pub fn create_bet(
|
||||
ctx: &mut Context<CreateBet>,
|
||||
mut ctx: Context<CreateBet>,
|
||||
id: u64,
|
||||
side: u64,
|
||||
amount: u64,
|
||||
entry_price: u64,
|
||||
) -> Result<()> {
|
||||
create_bet::handler(ctx, id, side, amount, entry_price)
|
||||
create_bet::handler(&mut ctx, id, side, amount, entry_price)
|
||||
}
|
||||
|
||||
#[discrim = 2]
|
||||
pub fn close_bet(
|
||||
ctx: &mut Context<CloseBet>,
|
||||
mut ctx: Context<CloseBet>,
|
||||
id: u64,
|
||||
exit_price: u64,
|
||||
) -> Result<()> {
|
||||
close_bet::handler(ctx, id, exit_price)
|
||||
close_bet::handler(&mut ctx, id, exit_price)
|
||||
}
|
||||
|
||||
#[discrim = 3]
|
||||
pub fn update(
|
||||
ctx: &mut Context<Update>,
|
||||
new_relayer: Address,
|
||||
mut ctx: Context<Update>,
|
||||
new_relayer: Pubkey,
|
||||
min_amount: u64,
|
||||
max_amount: u64,
|
||||
multiplier_bps: u64,
|
||||
@@ -56,7 +52,7 @@ pub mod smart_updown {
|
||||
expiry_seconds: i64,
|
||||
) -> Result<()> {
|
||||
update::handler(
|
||||
ctx,
|
||||
&mut ctx,
|
||||
new_relayer,
|
||||
min_amount,
|
||||
max_amount,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
use anchor_lang::prelude::*;
|
||||
|
||||
#[account]
|
||||
#[derive(InitSpace)]
|
||||
pub struct Global {
|
||||
pub counter: u64,
|
||||
pub min_amount: u64,
|
||||
@@ -8,11 +9,12 @@ pub struct Global {
|
||||
pub multiplier_bps: u64,
|
||||
pub expiry_seconds: i64,
|
||||
pub paused: u64,
|
||||
pub admin: Address,
|
||||
pub relayer: Address,
|
||||
pub admin: Pubkey,
|
||||
pub relayer: Pubkey,
|
||||
}
|
||||
|
||||
#[account]
|
||||
#[derive(InitSpace)]
|
||||
pub struct Bet {
|
||||
pub amount: u64,
|
||||
pub bet_time: i64,
|
||||
@@ -21,5 +23,5 @@ pub struct Bet {
|
||||
pub exit_price: u64,
|
||||
pub side: u64,
|
||||
pub status: u64,
|
||||
pub bettor: Address,
|
||||
pub bettor: Pubkey,
|
||||
}
|
||||
|
||||
@@ -1,10 +1,18 @@
|
||||
use {
|
||||
anchor_lang::{
|
||||
bytemuck, cpi::find_program_address, solana_program::instruction::Instruction, Id,
|
||||
InstructionData, ToAccountMetas,
|
||||
solana_program::{
|
||||
instruction::Instruction,
|
||||
pubkey::Pubkey,
|
||||
system_instruction,
|
||||
},
|
||||
AccountDeserialize, InstructionData, ToAccountMetas,
|
||||
},
|
||||
anchor_v2_testing::{Keypair, LiteSVM, Message, Signer, VersionedMessage, VersionedTransaction},
|
||||
smart_updown::state::{Bet, Global},
|
||||
solana_clock::Clock,
|
||||
solana_keypair::Keypair,
|
||||
solana_program_test::{BanksClient, ProgramTest, ProgramTestContext},
|
||||
solana_signer::Signer,
|
||||
solana_transaction::Transaction,
|
||||
};
|
||||
|
||||
const CLOCK_ADDR: &str = "SysvarC1ock11111111111111111111111111111111";
|
||||
@@ -12,40 +20,62 @@ const SYSTEM_ADDR: &str = "11111111111111111111111111111111";
|
||||
const MIN_AMOUNT: u64 = 1_000_000_000; // 1 SOL
|
||||
const MULTIPLIER_BPS: u64 = 13_000; // 1.3x
|
||||
const EXPIRY_SECONDS: i64 = 15;
|
||||
const START_TIME: i64 = 1_700_000_000;
|
||||
|
||||
type Addr = anchor_lang::prelude::Address;
|
||||
|
||||
fn pid() -> Addr {
|
||||
fn pid() -> Pubkey {
|
||||
smart_updown::id()
|
||||
}
|
||||
|
||||
fn global_pda() -> Addr {
|
||||
find_program_address(&[b"global"], &pid()).0
|
||||
fn global_pda() -> Pubkey {
|
||||
Pubkey::find_program_address(&[b"global"], &pid()).0
|
||||
}
|
||||
|
||||
fn bet_pda(id: u64) -> Addr {
|
||||
find_program_address(&[b"bet", &id.to_le_bytes()], &pid()).0
|
||||
fn bet_pda(id: u64) -> Pubkey {
|
||||
Pubkey::find_program_address(&[b"bet", &id.to_le_bytes()], &pid()).0
|
||||
}
|
||||
|
||||
fn setup() -> (LiteSVM, Keypair) {
|
||||
let mut svm = LiteSVM::new();
|
||||
let bytes = include_bytes!("../../../target/deploy/smart_updown.so");
|
||||
svm.add_program(pid(), bytes).expect("deploy program");
|
||||
let payer = Keypair::new();
|
||||
svm.airdrop(&payer.pubkey(), 100_000_000_000).unwrap();
|
||||
(svm, payer)
|
||||
async fn setup() -> (ProgramTestContext, Keypair) {
|
||||
let mut pt = ProgramTest::default();
|
||||
pt.add_program("smart_updown", pid(), None);
|
||||
// Fund the deploy keypair separately in the test; the validator in CI funds it.
|
||||
let context = pt.start_with_context().await;
|
||||
let admin = Keypair::new();
|
||||
context.banks_client.process_transaction(Transaction::new_signed_with_payer(
|
||||
&[system_instruction::transfer(
|
||||
&context.payer.pubkey(),
|
||||
&admin.pubkey(),
|
||||
100_000_000_000,
|
||||
)],
|
||||
Some(&context.payer.pubkey()),
|
||||
&[&context.payer],
|
||||
context.last_blockhash,
|
||||
)).await.unwrap();
|
||||
(context, admin)
|
||||
}
|
||||
|
||||
fn send(svm: &mut LiteSVM, signers: &[&Keypair], instruction: Instruction) -> Result<(), String> {
|
||||
let blockhash = svm.latest_blockhash();
|
||||
let msg = Message::new_with_blockhash(&[instruction], Some(&signers[0].pubkey()), &blockhash);
|
||||
let tx = VersionedTransaction::try_new(VersionedMessage::Legacy(msg), signers).unwrap();
|
||||
let res = svm.send_transaction(tx);
|
||||
res.map(|_| ()).map_err(|e| format!("{:?}", e))
|
||||
async fn set_clock(ctx: &mut ProgramTestContext, unix: i64) {
|
||||
let mut clk: Clock = ctx.banks_client.get_sysvar().await.unwrap();
|
||||
clk.unix_timestamp = unix;
|
||||
ctx.set_sysvar(&clk);
|
||||
}
|
||||
|
||||
fn initialize(svm: &mut LiteSVM, admin: &Keypair) {
|
||||
let ix = Instruction::new_with_bytes(
|
||||
async fn read_account_data(
|
||||
bank: &BanksClient,
|
||||
addr: &Pubkey,
|
||||
) -> Vec<u8> {
|
||||
bank.get_account(*addr).await.unwrap().expect("account exists").data
|
||||
}
|
||||
|
||||
fn read_global_typed(data: &[u8]) -> Global {
|
||||
Global::try_deserialize(&mut &data[..]).unwrap()
|
||||
}
|
||||
|
||||
fn read_bet_typed(data: &[u8]) -> Bet {
|
||||
Bet::try_deserialize(&mut &data[..]).unwrap()
|
||||
}
|
||||
|
||||
fn make_initialize_ix(admin: Pubkey) -> Instruction {
|
||||
Instruction::new_with_bytes(
|
||||
pid(),
|
||||
&smart_updown::instruction::Initialize {
|
||||
min_amount: MIN_AMOUNT,
|
||||
@@ -55,37 +85,15 @@ fn initialize(svm: &mut LiteSVM, admin: &Keypair) {
|
||||
}
|
||||
.data(),
|
||||
smart_updown::accounts::Initialize {
|
||||
admin: admin.pubkey(),
|
||||
admin,
|
||||
global: global_pda(),
|
||||
system_program: SYSTEM_ADDR.parse().unwrap(),
|
||||
}
|
||||
.to_account_metas(None),
|
||||
);
|
||||
send(svm, &[admin], ix).expect("initialize");
|
||||
)
|
||||
}
|
||||
|
||||
fn set_clock(svm: &mut LiteSVM, unix: i64) {
|
||||
use solana_clock::Clock;
|
||||
let mut clk = svm.get_sysvar::<Clock>();
|
||||
clk.unix_timestamp = unix;
|
||||
svm.set_sysvar(&clk);
|
||||
}
|
||||
|
||||
fn fund_vault(svm: &mut LiteSVM, lamports: u64) {
|
||||
svm.airdrop(&global_pda(), lamports).unwrap();
|
||||
}
|
||||
|
||||
fn read_global(svm: &LiteSVM) -> Global {
|
||||
let acc = svm.get_account(&global_pda()).expect("global account");
|
||||
*bytemuck::from_bytes::<Global>(&acc.data[8..])
|
||||
}
|
||||
|
||||
fn read_bet(svm: &LiteSVM, id: u64) -> Bet {
|
||||
let acc = svm.get_account(&bet_pda(id)).expect("bet account");
|
||||
*bytemuck::from_bytes::<Bet>(&acc.data[8..])
|
||||
}
|
||||
|
||||
fn make_create_ix(id: u64, side: u64, amount: u64, entry_price: u64, bettor: Addr) -> Instruction {
|
||||
fn make_create_ix(id: u64, side: u64, amount: u64, entry_price: u64, bettor: Pubkey) -> Instruction {
|
||||
Instruction::new_with_bytes(
|
||||
pid(),
|
||||
&smart_updown::instruction::CreateBet {
|
||||
@@ -106,7 +114,7 @@ fn make_create_ix(id: u64, side: u64, amount: u64, entry_price: u64, bettor: Add
|
||||
)
|
||||
}
|
||||
|
||||
fn make_close_ix(id: u64, exit_price: u64, relayer: Addr, bettor: Addr) -> Instruction {
|
||||
fn make_close_ix(id: u64, exit_price: u64, relayer: Pubkey, bettor: Pubkey) -> Instruction {
|
||||
Instruction::new_with_bytes(
|
||||
pid(),
|
||||
&smart_updown::instruction::CloseBet { id, exit_price }.data(),
|
||||
@@ -121,22 +129,68 @@ fn make_close_ix(id: u64, exit_price: u64, relayer: Addr, bettor: Addr) -> Instr
|
||||
)
|
||||
}
|
||||
|
||||
fn create_up_bet(svm: &mut LiteSVM, admin: &Keypair, side: u64) -> (Keypair, u64, u64) {
|
||||
async fn run_ix(
|
||||
ctx: &mut ProgramTestContext,
|
||||
signer: &Keypair,
|
||||
ix: Instruction,
|
||||
) -> Result<(), String> {
|
||||
let tx = Transaction::new_signed_with_payer(
|
||||
&[ix],
|
||||
Some(&signer.pubkey()),
|
||||
&[signer],
|
||||
ctx.last_blockhash,
|
||||
);
|
||||
ctx.banks_client
|
||||
.process_transaction(tx)
|
||||
.await
|
||||
.map(|_| ())
|
||||
.map_err(|e| format!("{:?}", e))
|
||||
}
|
||||
|
||||
async fn initialize(ctx: &mut ProgramTestContext, admin: &Keypair) {
|
||||
run_ix(ctx, admin, make_initialize_ix(admin.pubkey()))
|
||||
.await
|
||||
.expect("initialize");
|
||||
}
|
||||
|
||||
async fn airdrop_to(ctx: &mut ProgramTestContext, to: &Pubkey, lamports: u64) {
|
||||
let tx = Transaction::new_signed_with_payer(
|
||||
&[system_instruction::transfer(
|
||||
&ctx.payer.pubkey(),
|
||||
to,
|
||||
lamports,
|
||||
)],
|
||||
Some(&ctx.payer.pubkey()),
|
||||
&[&ctx.payer],
|
||||
ctx.last_blockhash,
|
||||
);
|
||||
ctx.banks_client.process_transaction(tx).await.unwrap();
|
||||
}
|
||||
|
||||
async fn fund_vault(ctx: &mut ProgramTestContext, lamports: u64) {
|
||||
airdrop_to(ctx, &global_pda(), lamports).await;
|
||||
}
|
||||
|
||||
async fn create_up_bet(
|
||||
ctx: &mut ProgramTestContext,
|
||||
side: u64,
|
||||
) -> (Keypair, u64, u64) {
|
||||
let bettor = Keypair::new();
|
||||
svm.airdrop(&bettor.pubkey(), 100_000_000_000).unwrap();
|
||||
set_clock(svm, 1_700_000_000);
|
||||
airdrop_to(ctx, &bettor.pubkey(), 100_000_000_000).await;
|
||||
set_clock(ctx, START_TIME).await;
|
||||
let amount = MIN_AMOUNT;
|
||||
let entry_price = 100_000_000u64;
|
||||
let create_ix = make_create_ix(0, side, amount, entry_price, bettor.pubkey());
|
||||
send(svm, &[&bettor], create_ix).expect("create_bet");
|
||||
run_ix(ctx, &bettor, create_ix).await.expect("create_bet");
|
||||
(bettor, amount, entry_price)
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn initialize_creates_global() {
|
||||
let (mut svm, admin) = setup();
|
||||
initialize(&mut svm, &admin);
|
||||
let g = read_global(&svm);
|
||||
#[tokio::test]
|
||||
async fn initialize_creates_global() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let acc = read_account_data(&ctx.banks_client, &global_pda()).await;
|
||||
let g = read_global_typed(&acc);
|
||||
assert_eq!(g.admin, admin.pubkey());
|
||||
assert_eq!(g.relayer, admin.pubkey());
|
||||
assert_eq!(g.counter, 0);
|
||||
@@ -145,139 +199,173 @@ fn initialize_creates_global() {
|
||||
assert_eq!(g.paused, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_bet_escrows_sol() {
|
||||
let (mut svm, admin) = setup();
|
||||
initialize(&mut svm, &admin);
|
||||
let (bettor, amount, entry_price) = create_up_bet(&mut svm, &admin, 0);
|
||||
#[tokio::test]
|
||||
async fn create_bet_escrows_sol() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let (bettor, amount, entry_price) = create_up_bet(&mut ctx, 0).await;
|
||||
|
||||
let vault_balance = svm.get_balance(&global_pda()).unwrap();
|
||||
let vault_balance = ctx.banks_client.get_balance(global_pda()).await.unwrap();
|
||||
assert!(vault_balance >= amount);
|
||||
let b = read_bet(&svm, 0);
|
||||
let b = read_bet_typed(&read_account_data(&ctx.banks_client, &bet_pda(0)).await);
|
||||
assert_eq!(b.bettor, bettor.pubkey());
|
||||
assert_eq!(b.side, 0);
|
||||
assert_eq!(b.amount, amount);
|
||||
assert_eq!(b.entry_price, entry_price);
|
||||
assert_eq!(b.bet_time, 1_700_000_000);
|
||||
assert_eq!(b.expire_time, 1_700_000_000 + EXPIRY_SECONDS);
|
||||
assert_eq!(b.bet_time, START_TIME);
|
||||
assert_eq!(b.expire_time, START_TIME + EXPIRY_SECONDS);
|
||||
assert_eq!(b.status, 0);
|
||||
assert_eq!(read_global(&svm).counter, 1);
|
||||
assert_eq!(read_global_typed(&read_account_data(&ctx.banks_client, &global_pda()).await).counter, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_bet_user_wins_up() {
|
||||
let (mut svm, admin) = setup();
|
||||
initialize(&mut svm, &admin);
|
||||
let (bettor, amount, entry_price) = create_up_bet(&mut svm, &admin, 0);
|
||||
#[tokio::test]
|
||||
async fn close_bet_user_wins_up() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let (bettor, amount, entry_price) = create_up_bet(&mut ctx, 0).await;
|
||||
|
||||
set_clock(&mut svm, 1_700_000_000 + EXPIRY_SECONDS + 1);
|
||||
set_clock(&mut ctx, START_TIME + EXPIRY_SECONDS + 1).await;
|
||||
let exit_price = entry_price + 1_000;
|
||||
let before = svm.get_balance(&bettor.pubkey()).unwrap();
|
||||
let before = ctx.banks_client.get_balance(bettor.pubkey()).await.unwrap();
|
||||
let payout = amount * MULTIPLIER_BPS / 10_000;
|
||||
fund_vault(&mut svm, 10 * amount);
|
||||
fund_vault(&mut ctx, 10 * amount).await;
|
||||
|
||||
let close_ix = make_close_ix(0, exit_price, admin.pubkey(), bettor.pubkey());
|
||||
send(&mut svm, &[&admin], close_ix).expect("close_bet");
|
||||
run_ix(&mut ctx, &admin, close_ix).await.expect("close_bet");
|
||||
|
||||
assert_eq!(svm.get_balance(&bettor.pubkey()).unwrap(), before + payout);
|
||||
let b = read_bet(&svm, 0);
|
||||
assert_eq!(ctx.banks_client.get_balance(bettor.pubkey()).await.unwrap(), before + payout);
|
||||
let b = read_bet_typed(&read_account_data(&ctx.banks_client, &bet_pda(0)).await);
|
||||
assert_eq!(b.status, 1);
|
||||
assert_eq!(b.exit_price, exit_price);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_bet_house_wins_down() {
|
||||
let (mut svm, admin) = setup();
|
||||
initialize(&mut svm, &admin);
|
||||
let (bettor, _amount, entry_price) = create_up_bet(&mut svm, &admin, 1);
|
||||
#[tokio::test]
|
||||
async fn close_bet_house_wins_down() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let (bettor, _amount, entry_price) = create_up_bet(&mut ctx, 1).await;
|
||||
|
||||
set_clock(&mut svm, 1_700_000_000 + EXPIRY_SECONDS + 1);
|
||||
set_clock(&mut ctx, START_TIME + EXPIRY_SECONDS + 1).await;
|
||||
let exit_price = entry_price + 1_000; // side=DOWN but price rose -> lose
|
||||
let before = svm.get_balance(&bettor.pubkey()).unwrap();
|
||||
let before = ctx.banks_client.get_balance(bettor.pubkey()).await.unwrap();
|
||||
|
||||
let close_ix = make_close_ix(0, exit_price, admin.pubkey(), bettor.pubkey());
|
||||
send(&mut svm, &[&admin], close_ix).expect("close_bet");
|
||||
run_ix(&mut ctx, &admin, close_ix).await.expect("close_bet");
|
||||
|
||||
assert_eq!(svm.get_balance(&bettor.pubkey()).unwrap(), before);
|
||||
assert_eq!(read_bet(&svm, 0).status, 2);
|
||||
assert_eq!(ctx.banks_client.get_balance(bettor.pubkey()).await.unwrap(), before);
|
||||
assert_eq!(read_bet_typed(&read_account_data(&ctx.banks_client, &bet_pda(0)).await).status, 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_bet_equal_price_stays_open() {
|
||||
let (mut svm, admin) = setup();
|
||||
initialize(&mut svm, &admin);
|
||||
let (bettor, amount, entry_price) = create_up_bet(&mut svm, &admin, 0);
|
||||
#[tokio::test]
|
||||
async fn close_bet_equal_price_stays_open() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let (bettor, amount, entry_price) = create_up_bet(&mut ctx, 0).await;
|
||||
|
||||
set_clock(&mut svm, 1_700_000_000 + EXPIRY_SECONDS + 1);
|
||||
let before = svm.get_balance(&bettor.pubkey()).unwrap();
|
||||
set_clock(&mut ctx, START_TIME + EXPIRY_SECONDS + 1).await;
|
||||
let before = ctx.banks_client.get_balance(bettor.pubkey()).await.unwrap();
|
||||
let payout = amount * MULTIPLIER_BPS / 10_000;
|
||||
fund_vault(&mut svm, 10 * amount);
|
||||
fund_vault(&mut ctx, 10 * amount).await;
|
||||
|
||||
let equal_ix = make_close_ix(0, entry_price, admin.pubkey(), bettor.pubkey());
|
||||
send(&mut svm, &[&admin], equal_ix).expect("equal close");
|
||||
assert_eq!(read_bet(&svm, 0).status, 0);
|
||||
assert_eq!(svm.get_balance(&bettor.pubkey()).unwrap(), before);
|
||||
run_ix(&mut ctx, &admin, equal_ix).await.expect("equal close");
|
||||
assert_eq!(read_bet_typed(&read_account_data(&ctx.banks_client, &bet_pda(0)).await).status, 0);
|
||||
assert_eq!(ctx.banks_client.get_balance(bettor.pubkey()).await.unwrap(), before);
|
||||
|
||||
let exit_price = entry_price + 1_000;
|
||||
let second_ix = make_close_ix(0, exit_price, admin.pubkey(), bettor.pubkey());
|
||||
send(&mut svm, &[&admin], second_ix).expect("second close");
|
||||
run_ix(&mut ctx, &admin, second_ix).await.expect("second close");
|
||||
assert_eq!(
|
||||
svm.get_balance(&bettor.pubkey()).unwrap(),
|
||||
ctx.banks_client.get_balance(bettor.pubkey()).await.unwrap(),
|
||||
before + payout
|
||||
);
|
||||
assert_eq!(read_bet(&svm, 0).status, 1);
|
||||
assert_eq!(read_bet_typed(&read_account_data(&ctx.banks_client, &bet_pda(0)).await).status, 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_bet_before_expiry_rejected() {
|
||||
let (mut svm, admin) = setup();
|
||||
initialize(&mut svm, &admin);
|
||||
let (bettor, _amount, entry_price) = create_up_bet(&mut svm, &admin, 0);
|
||||
#[tokio::test]
|
||||
async fn close_bet_already_finalized() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let (bettor, amount, entry_price) = create_up_bet(&mut ctx, 0).await;
|
||||
|
||||
set_clock(&mut ctx, START_TIME + EXPIRY_SECONDS + 1).await;
|
||||
let win_price = entry_price + 500;
|
||||
fund_vault(&mut ctx, 10 * amount).await;
|
||||
run_ix(&mut ctx, &admin, make_close_ix(0, win_price, admin.pubkey(), bettor.pubkey()))
|
||||
.await
|
||||
.expect("first close");
|
||||
|
||||
let res = run_ix(
|
||||
&mut ctx,
|
||||
&admin,
|
||||
make_close_ix(0, win_price + 1, admin.pubkey(), bettor.pubkey()),
|
||||
)
|
||||
.await;
|
||||
assert!(res.is_err(), "expected close after finalization to be rejected");
|
||||
let b = read_bet_typed(&read_account_data(&ctx.banks_client, &bet_pda(0)).await);
|
||||
assert_eq!(b.status, 1);
|
||||
assert_eq!(b.exit_price, win_price);
|
||||
assert_eq!(b.status, 1);
|
||||
assert_eq!(b.exit_price, win_price);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn close_bet_before_expiry_rejected() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let (bettor, _amount, entry_price) = create_up_bet(&mut ctx, 0).await;
|
||||
|
||||
let close_ix = make_close_ix(0, entry_price + 500, admin.pubkey(), bettor.pubkey());
|
||||
let res = send(&mut svm, &[&admin], close_ix);
|
||||
let res = run_ix(&mut ctx, &admin, close_ix).await;
|
||||
assert!(res.is_err(), "expected premature close to be rejected");
|
||||
assert_eq!(read_bet(&svm, 0).status, 0);
|
||||
assert_eq!(read_bet_typed(&read_account_data(&ctx.banks_client, &bet_pda(0)).await).status, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_bet_wrong_relayer_rejected() {
|
||||
let (mut svm, admin) = setup();
|
||||
initialize(&mut svm, &admin);
|
||||
let (bettor, _amount, entry_price) = create_up_bet(&mut svm, &admin, 0);
|
||||
#[tokio::test]
|
||||
async fn close_bet_wrong_relayer_rejected() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let (bettor, _amount, entry_price) = create_up_bet(&mut ctx, 0).await;
|
||||
let attacker = Keypair::new();
|
||||
svm.airdrop(&attacker.pubkey(), 100_000_000_000).unwrap();
|
||||
airdrop_to(&mut ctx, &attacker.pubkey(), 100_000_000_000).await;
|
||||
|
||||
set_clock(&mut svm, 1_700_000_000 + EXPIRY_SECONDS + 1);
|
||||
set_clock(&mut ctx, START_TIME + EXPIRY_SECONDS + 1).await;
|
||||
let close_ix = make_close_ix(0, entry_price + 500, attacker.pubkey(), bettor.pubkey());
|
||||
let res = send(&mut svm, &[&attacker], close_ix);
|
||||
let res = run_ix(&mut ctx, &attacker, close_ix).await;
|
||||
assert!(res.is_err(), "expected wrong relayer to be rejected");
|
||||
assert_eq!(read_bet(&svm, 0).status, 0);
|
||||
assert_eq!(read_bet_typed(&read_account_data(&ctx.banks_client, &bet_pda(0)).await).status, 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn close_bet_replay_rejected() {
|
||||
let (mut svm, admin) = setup();
|
||||
initialize(&mut svm, &admin);
|
||||
let (bettor, _amount, entry_price) = create_up_bet(&mut svm, &admin, 0);
|
||||
#[tokio::test]
|
||||
async fn close_bet_replay_rejected() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let (bettor, _amount, entry_price) = create_up_bet(&mut ctx, 0).await;
|
||||
|
||||
set_clock(&mut svm, 1_700_000_000 + EXPIRY_SECONDS + 1);
|
||||
fund_vault(&mut svm, 10 * MIN_AMOUNT);
|
||||
set_clock(&mut ctx, START_TIME + EXPIRY_SECONDS + 1).await;
|
||||
fund_vault(&mut ctx, 10 * MIN_AMOUNT).await;
|
||||
let close_ix = make_close_ix(0, entry_price + 500, admin.pubkey(), bettor.pubkey());
|
||||
send(&mut svm, &[&admin], close_ix).expect("first close");
|
||||
let res = send(&mut svm, &[&admin], make_close_ix(0, entry_price + 500, admin.pubkey(), bettor.pubkey()));
|
||||
run_ix(&mut ctx, &admin, close_ix).await.expect("first close");
|
||||
let before = ctx.banks_client.get_balance(bettor.pubkey()).await.unwrap();
|
||||
let res = run_ix(
|
||||
&mut ctx,
|
||||
&admin,
|
||||
make_close_ix(0, entry_price + 501, admin.pubkey(), bettor.pubkey()),
|
||||
)
|
||||
.await;
|
||||
assert!(res.is_err(), "expected replay close to be rejected");
|
||||
assert_eq!(ctx.banks_client.get_balance(bettor.pubkey()).await.unwrap(), before);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn create_bet_rejects_out_of_range() {
|
||||
let (mut svm, admin) = setup();
|
||||
initialize(&mut svm, &admin);
|
||||
#[tokio::test]
|
||||
async fn create_bet_rejects_out_of_range() {
|
||||
let (mut ctx, admin) = setup().await;
|
||||
initialize(&mut ctx, &admin).await;
|
||||
let bettor = Keypair::new();
|
||||
svm.airdrop(&bettor.pubkey(), 100_000_000_000).unwrap();
|
||||
set_clock(&mut svm, 1_700_000_000);
|
||||
airdrop_to(&mut ctx, &bettor.pubkey(), 100_000_000_000).await;
|
||||
set_clock(&mut ctx, START_TIME).await;
|
||||
|
||||
let ix = make_create_ix(0, 0, 1, 100_000_000u64, bettor.pubkey());
|
||||
let res = send(&mut svm, &[&bettor], ix);
|
||||
let res = run_ix(&mut ctx, &bettor, ix).await;
|
||||
assert!(res.is_err(), "expected below-min bet to be rejected");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user