Skip to content

Commit

Permalink
completed
Browse files Browse the repository at this point in the history
  • Loading branch information
RoSpaceDev committed Sep 9, 2024
1 parent 79efe24 commit 5b1a66d
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Anchor.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ resolution = true
skip-lint = false

[programs.localnet]
solana_hub = "68LstwZhYt14pDyVtzLKQ9yfXntNDauhAaH5TMQD1fRj"
solana_hub = "EsE5KPaDVc5wrrmJSDCUbaWSds35x56A3XkDUKkexHb6"

[registry]
url = "https://api.apr.dev"
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"license": "ISC",
"license": "ISC",
"scripts": {
"lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w",
"lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check"
Expand All @@ -8,13 +8,13 @@
"@coral-xyz/anchor": "^0.30.1"
},
"devDependencies": {
"chai": "^4.3.4",
"mocha": "^9.0.3",
"ts-mocha": "^10.0.0",
"@types/bn.js": "^5.1.0",
"@types/chai": "^4.3.0",
"@types/mocha": "^9.0.0",
"typescript": "^4.3.5",
"prettier": "^2.6.2"
"chai": "^4.3.4",
"mocha": "^9.0.3",
"prettier": "^2.6.2",
"ts-mocha": "^10.0.0",
"typescript": "^4.3.5"
}
}
28 changes: 15 additions & 13 deletions programs/solana-hub/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
use anchor_lang::prelude::*;
use solana_program::sysvar::clock::Clock;

// use anchor_spl::{
// associated_token::AssociatedToken,
// token::{Mint, Token, TokenAccount},
// };

declare_id!("68LstwZhYt14pDyVtzLKQ9yfXntNDauhAaH5TMQD1fRj");
declare_id!("EsE5KPaDVc5wrrmJSDCUbaWSds35x56A3XkDUKkexHb6");

#[program]
pub mod solana_hub {
use std::ops::Add;

use super::*;

pub fn register_collection(
ctx: Context<RegisterCollection>,
name: String,
quantity: u16,
timestamp_to_close: i64,
_name: String,
_quantity: u16,
seconds_to_close: u64,
) -> Result<()> {
msg!(
"{0},{1},{2},{3}",
ctx.accounts.auction.key(),
name,
quantity.to_string(),
timestamp_to_close.to_string()
);
let get_clock = Clock::get();
if let Ok(clock) = get_clock {
let current_timestamp = clock.unix_timestamp;
ctx.accounts.auction.creator = ctx.accounts.creator.key();
let timestamp_to_close = current_timestamp.add(seconds_to_close as i64);
ctx.accounts.auction.timestamp_to_close = timestamp_to_close;
msg!("{0} and {1}", current_timestamp, timestamp_to_close,)
}
Ok(())
}

Expand Down Expand Up @@ -58,7 +60,7 @@ pub mod solana_hub {
}

#[derive(Accounts)]
#[instruction(name:String, quantity:u16, timestamp_to_close:i64)]
#[instruction(name:String, quantity:u16, minutes_to_close:u64)]
pub struct RegisterCollection<'info> {
#[account(
init,
Expand Down
59 changes: 56 additions & 3 deletions tests/solana-hub.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,69 @@
import * as anchor from "@coral-xyz/anchor";
import { Program } from "@coral-xyz/anchor";
import { SolanaHub } from "../target/types/solana_hub";
import { bs58 } from "@coral-xyz/anchor/dist/cjs/utils/bytes";

//Address:H9KwD9eQakjKuqanLpqxfEgBHPLniGsZTjpKyA9mXKgz
const accountWithLamports = anchor.web3.Keypair.fromSecretKey(
bs58.decode(
"DS7PFJT3GGgHtsDdnCw5EynoQ7eZb41pUhrGH3GkYVztKZ4mHJaRth3eggrHChoWjHEWAxNhVgGEb2R2jgRJy9G"
)
);

const COLLECTION_NAME = "La Piedra Filosofal 6";

const predictAuction = (program: Program<SolanaHub>, name: string) => {
const SOLANA_HUB_PROGRAM_ID = new anchor.web3.PublicKey(program.programId);
const [auction] = anchor.web3.PublicKey.findProgramAddressSync(
[Buffer.from("auction"), Buffer.from(name)],
SOLANA_HUB_PROGRAM_ID
);
return auction;
};

function u16ToBigEndianBuffer(num: number): Buffer {
const buffer = Buffer.alloc(2); // Allocate 2 bytes for a 16-bit integer
buffer.writeUInt16BE(num, 0); // Write the number as big-endian at offset 0
return buffer;
}

const predictAuctionNft = (
program: Program<SolanaHub>,
name: string,
nftId: number
) => {
const SOLANA_HUB_PROGRAM_ID = new anchor.web3.PublicKey(program.programId);
const [auctionNft] = anchor.web3.PublicKey.findProgramAddressSync(
[Buffer.from("auction"), Buffer.from(name), u16ToBigEndianBuffer(nftId)],
SOLANA_HUB_PROGRAM_ID
);
return auctionNft;
};

describe("solana-hub", () => {
// Configure the client to use the local cluster.
anchor.setProvider(anchor.AnchorProvider.env());

const program = anchor.workspace.SolanaHub as Program<SolanaHub>;

it("Is initialized!", async () => {
it("Register collection!", async () => {
// Add your test here.
const tx = await program.methods.initialize().rpc();
console.log("Your transaction signature", tx);
const txInstruction = await program.methods
.registerCollection(COLLECTION_NAME, 10, new anchor.BN(5 * 60))
.accounts({ creator: accountWithLamports.publicKey })
.instruction();
const transaction = new anchor.web3.Transaction().add(txInstruction);
transaction.recentBlockhash = (
await program.provider.connection.getLatestBlockhash("finalized")
).blockhash;
transaction.sign(accountWithLamports);
const txHash = await program.provider.connection.sendRawTransaction(
transaction.serialize(),
{ preflightCommitment: "confirmed" }
);
console.log("Your transaction signature", txHash);
// const auctionAccount = predictAuction(program, COLLECTION_NAME);
// const accountInfo = await program.account.auction.fetch(auctionAccount);
// console.log(accountInfo.timestampToClose.toNumber());
});
});

0 comments on commit 5b1a66d

Please sign in to comment.