Skip to content

Commit

Permalink
escrow/native - assertion, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
thewuhxyz committed Nov 2, 2024
1 parent e94b925 commit 060bf42
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 11 deletions.
4 changes: 2 additions & 2 deletions tokens/escrow/native/program/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use thiserror::Error;
pub enum EscrowError {
#[error("Offer key provided does not match expected")]
OfferKeyMismatch,

#[error("Token account provided does not match expected")]
TokenAccountMismatch,
}
Expand All @@ -14,4 +14,4 @@ impl From<EscrowError> for ProgramError {
fn from(e: EscrowError) -> Self {
ProgramError::Custom(e as u32)
}
}
}
11 changes: 5 additions & 6 deletions tokens/escrow/native/program/src/instructions/make_offer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl MakeOffer {
};

// ensure the maker signs the instruction
//
//
if !maker.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
Expand Down Expand Up @@ -141,16 +141,15 @@ impl MakeOffer {
)?;

let vault_token_amount = TokenAccount::unpack(&vault.data.borrow())?.amount;

solana_program::msg!("Amount in vault: {}", vault_token_amount);

assert_eq!(vault_token_amount, args.token_a_offered_amount);

// write data into offer account
//
offer.serialize(&mut *offer_info.data.borrow_mut())?;



Ok(())
}
}
2 changes: 1 addition & 1 deletion tokens/escrow/native/program/src/instructions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ pub mod make_offer;
pub use make_offer::*;

pub mod take_offer;
pub use take_offer::*;
pub use take_offer::*;
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ impl TakeOffer {
};

// ensure the taker signs the instruction
//
//
if !taker.is_signer {
return Err(ProgramError::MissingRequiredSignature);
}
Expand Down
74 changes: 74 additions & 0 deletions tokens/escrow/native/tests/account.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { PublicKey } from '@solana/web3.js';
import * as borsh from 'borsh';

export class OfferAccount {
id: bigint;
maker: Uint8Array;
token_mint_a: Uint8Array;
token_mint_b: Uint8Array;
token_b_wanted_amount: bigint;
bump: number;

constructor(offer: OfferRaw) {
this.id = offer.id;
this.maker = offer.maker;
this.token_b_wanted_amount = offer.token_b_wanted_amount;
this.token_mint_a = offer.token_mint_a;
this.token_mint_b = offer.token_mint_b;
this.bump = this.bump;
}

toBuffer() {
return Buffer.from(borsh.serialize(OfferSchema, this));
}

static fromBuffer(buffer: Uint8Array) {
return borsh.deserialize(OfferSchema, OfferAccount, Buffer.from(buffer));
}

toData(): Offer {
return {
id: this.id,
maker: new PublicKey(this.maker),
token_mint_a: new PublicKey(this.token_mint_a),
token_mint_b: new PublicKey(this.token_mint_b),
token_b_wanted_amount: this.token_b_wanted_amount,
bump: this.bump,
};
}
}

const OfferSchema = new Map([
[
OfferAccount,
{
kind: 'struct',
fields: [
['id', 'u64'],
['maker', [32]],
['token_mint_a', [32]],
['token_mint_b', [32]],
['token_b_wanted_amount', 'u64'],
['bump', 'u8'],
],
},
],
]);

type OfferRaw = {
id: bigint;
maker: Uint8Array;
token_mint_a: Uint8Array;
token_mint_b: Uint8Array;
token_b_wanted_amount: bigint;
bump: number;
};

type Offer = {
id: bigint;
maker: PublicKey;
token_mint_a: PublicKey;
token_mint_b: PublicKey;
token_b_wanted_amount: bigint;
bump: number;
};
32 changes: 31 additions & 1 deletion tokens/escrow/native/tests/test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { Buffer } from 'node:buffer';
import { describe, test } from 'node:test';
import { AccountLayout } from '@solana/spl-token';
import { Transaction } from '@solana/web3.js';
import { assert } from 'chai';
import { start } from 'solana-bankrun';
import { OfferAccount } from './account';
import { buildMakeOffer, buildTakeOffer } from './instruction';
import { createValues, mintingTokens } from './utils';

Expand Down Expand Up @@ -53,6 +55,19 @@ describe('Escrow!', async () => {
tx.recentBlockhash = blockhash;
tx.add(ix).sign(payer, values.maker);
await client.processTransaction(tx);

const offerInfo = await client.getAccount(values.offer);
const offer = OfferAccount.fromBuffer(offerInfo.data).toData();

const vaultInfo = await client.getAccount(values.vault);
const vaultTokenAccount = AccountLayout.decode(vaultInfo.data);

assert(offer.id.toString() === values.id.toString(), 'wrong id');
assert(offer.maker.toBase58() === values.maker.publicKey.toBase58(), 'maker key does not match');
assert(offer.token_mint_a.toBase58() === values.mintAKeypair.publicKey.toBase58(), 'wrong mint A');
assert(offer.token_mint_b.toBase58() === values.mintBKeypair.publicKey.toBase58(), 'wrong mint B');
assert(offer.token_b_wanted_amount.toString() === values.amountB.toString(), 'unexpected amount B');
assert(vaultTokenAccount.amount.toString() === values.amountA.toString(), 'unexpected amount A');
});

test('Take Offer', async () => {
Expand All @@ -76,5 +91,20 @@ describe('Escrow!', async () => {
tx.recentBlockhash = blockhash;
tx.add(ix).sign(payer, values.taker);
await client.processTransaction(tx);

const offerInfo = await client.getAccount(values.offer);
assert(offerInfo === null, 'offer account not closed');

const vaultInfo = await client.getAccount(values.vault);
assert(vaultInfo === null, 'vault account not closed');

const makerTokenBInfo = await client.getAccount(values.makerAccountB);
const makerTokenAccountB = AccountLayout.decode(makerTokenBInfo.data);

const takerTokenAInfo = await client.getAccount(values.takerAccountA);
const takerTokenAccountA = AccountLayout.decode(takerTokenAInfo.data);

assert(takerTokenAccountA.amount.toString() === values.amountA.toString(), 'unexpected amount a');
assert(makerTokenAccountB.amount.toString() === values.amountB.toString(), 'unexpected amount b');
});
});

0 comments on commit 060bf42

Please sign in to comment.