diff --git a/Anchor.toml b/Anchor.toml index 46e9da9..917088b 100644 --- a/Anchor.toml +++ b/Anchor.toml @@ -1,14 +1,17 @@ [features] seeds = false skip-lint = false + [programs.devnet] -sube_crypto = "2A65abYAkY9pBw6rAZyu5q4uwLdjyjd66WWwazewHPmv" +sube_crypto = "CPxMkDjjywp6FQdKenw92zVJEDc6TVFEXhG7yLc7Xw4N" [registry] url = "https://api.apr.dev" [provider] -cluster = "devnet" +cluster = "Devnet" wallet = "/home/mateo/.config/solana/id.json" -[scripts] \ No newline at end of file +[scripts] +init_bus_line = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/init_bus_line.ts" +trip = "yarn run ts-mocha -p ./tsconfig.json -t 1000000 tests/trip.ts" \ No newline at end of file diff --git a/Cargo.toml b/Cargo.toml index 3256b62..474a9c0 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,5 +10,4 @@ codegen-units = 1 [profile.release.build-override] opt-level = 3 incremental = false -codegen-units = 1 -ahash = "=0.8.6" \ No newline at end of file +codegen-units = 1 \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..bdcde4d --- /dev/null +++ b/package.json @@ -0,0 +1,20 @@ +{ + "scripts": { + "lint:fix": "prettier */*.js \"*/**/*{.js,.ts}\" -w", + "lint": "prettier */*.js \"*/**/*{.js,.ts}\" --check" + }, + "dependencies": { + "@coral-xyz/anchor": "^0.29.0", + "yarn": "^1.22.21" + }, + "devDependencies": { + "@types/bn.js": "^5.1.0", + "@types/chai": "^4.3.0", + "@types/mocha": "^9.0.0", + "chai": "^4.3.4", + "mocha": "^9.2.2", + "prettier": "^2.6.2", + "ts-mocha": "^10.0.0", + "typescript": "^4.3.5" + } +} \ No newline at end of file diff --git a/programs/sube_crypto/Cargo.toml b/programs/sube_crypto/Cargo.toml index 583ed00..a231b85 100644 --- a/programs/sube_crypto/Cargo.toml +++ b/programs/sube_crypto/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "sube_crypto" -version = "1.0.0" +version = "1.0.1" description = "." edition = "2021" diff --git a/programs/sube_crypto/src/instructions/initialize_bus_line.rs b/programs/sube_crypto/src/instructions/initialize_bus_line.rs index 7929f51..b92b6f5 100644 --- a/programs/sube_crypto/src/instructions/initialize_bus_line.rs +++ b/programs/sube_crypto/src/instructions/initialize_bus_line.rs @@ -17,15 +17,15 @@ pub fn initialize_bus_line( let (_services_pda, bump): (Pubkey, u8) = Pubkey::find_program_address(&[signer.as_ref()], program_id); let sube: &mut Account = &mut ctx.accounts.sube; - sube.authority = signer; - sube.bump_original = bump; - sube.prices = [to3km, to6km, to12km, to27km, more27km].to_vec(); + sube.set_authority(signer); + sube.set_bump_original(bump); + sube.set_prices(to3km, to6km, to12km, to27km, more27km); Ok(()) } #[derive(Accounts)] pub struct InitializeBusLine<'info> { - #[account(init, seeds = [signer.key().as_ref()], bump, payer = signer, space = 8 + SubeAdminAccount::SIZE)] + #[account(init, seeds = [signer.key().as_ref()], bump, payer = signer, space = SubeAdminAccount::SIZE)] pub sube: Account<'info, SubeAdminAccount>, #[account(mut)] pub signer: Signer<'info>, diff --git a/programs/sube_crypto/src/instructions/take_a_trip.rs b/programs/sube_crypto/src/instructions/take_a_trip.rs index 2fd2d17..d85b48a 100644 --- a/programs/sube_crypto/src/instructions/take_a_trip.rs +++ b/programs/sube_crypto/src/instructions/take_a_trip.rs @@ -7,16 +7,15 @@ use anchor_lang::{ }; pub fn take_a_trip(ctx: Context, km: u8) -> Result<()> { - let amount: u64 = ctx.accounts.sube.prices[km as usize]; - let sube: Pubkey = ctx.accounts.sube.key(); + let authority: Pubkey = ctx.accounts.sube.authority.key(); let to: Pubkey = ctx.accounts.to.key(); let from: Pubkey = ctx.accounts.from.key(); // validations - require_gte!(4, km); - require_keys_eq!(sube, to); - let transfer = transfer(&from, &to, amount); + require_gte!(MAX_KM, km); + require_keys_eq!(authority, to); + let amount: u64 = ctx.accounts.sube.prices[km as usize]; invoke( - &transfer, + &transfer(&from, &to, amount), &[ ctx.accounts.from.to_account_info(), ctx.accounts.to.to_account_info().clone(), diff --git a/programs/sube_crypto/src/lib.rs b/programs/sube_crypto/src/lib.rs index e2df1f9..fd36ff7 100644 --- a/programs/sube_crypto/src/lib.rs +++ b/programs/sube_crypto/src/lib.rs @@ -9,13 +9,13 @@ use instructions::{ TakeATrip, }; -declare_id!("6aARpcVGa9htfRgEgvNe55nFNAkyPykoUXFctpjJ2A1z"); +declare_id!("CPxMkDjjywp6FQdKenw92zVJEDc6TVFEXhG7yLc7Xw4N"); #[program] pub mod sube_crypto { use super::*; - pub fn initialize_bus_line_( + pub fn initialize_bus( ctx: Context, to3km: u64, to6km: u64, @@ -26,7 +26,7 @@ pub mod sube_crypto { initialize_bus_line(ctx, to3km, to6km, to12km, to27km, more27km) } - pub fn take_a_trip_(ctx: Context, km: u8) -> Result<()> { + pub fn take_trip(ctx: Context, km: u8) -> Result<()> { take_a_trip(ctx, km) } } diff --git a/programs/sube_crypto/src/state/accounts.rs b/programs/sube_crypto/src/state/accounts.rs index 663708e..8766c5c 100644 --- a/programs/sube_crypto/src/state/accounts.rs +++ b/programs/sube_crypto/src/state/accounts.rs @@ -1,12 +1,27 @@ use anchor_lang::prelude::*; +const ANCHOR_BUFFER: usize = 8; +pub const MAX_KM: u8 = 4; //(0,1,2,3,4) + #[account] pub struct SubeAdminAccount { pub authority: Pubkey, // 32 pub bump_original: u8, // 1 - pub prices: Vec, // 4 + 8 * 5 + pub prices: Vec, // 4 + [8 * 5] } impl SubeAdminAccount { - pub const SIZE: usize = 32 + 1 + 4 + (8 * 5); + pub const SIZE: usize = 32 + 1 + 4 + (8 * 5) + ANCHOR_BUFFER; + + pub fn set_authority(&mut self, authority: Pubkey) { + self.authority = authority; + } + + pub fn set_bump_original(&mut self, bump: u8) { + self.bump_original = bump; + } + + pub fn set_prices(&mut self, to3km: u64, to6km: u64, to12km: u64, to27km: u64, more27km: u64) { + self.prices = [to3km, to6km, to12km, to27km, more27km].to_vec(); + } } diff --git a/tests/init_bus_line.ts b/tests/init_bus_line.ts new file mode 100644 index 0000000..94f8bc6 --- /dev/null +++ b/tests/init_bus_line.ts @@ -0,0 +1,35 @@ +import * as anchor from "@coral-xyz/anchor"; +import { Program } from "@coral-xyz/anchor"; +import { SubeCrypto } from "../target/types/sube_crypto"; +import { SystemProgram, PublicKey } from "@solana/web3.js"; + +describe("Register a business", () => { + const provider = anchor.AnchorProvider.env(); + anchor.setProvider(provider); + const payer = provider.wallet as anchor.Wallet; + const program = anchor.workspace.SubeCrypto as Program; + const programId = program.programId; + + it("Is initialized!", async () => { + const sube = PublicKey.findProgramAddressSync( + [payer.publicKey.toBuffer()], + programId + )[0]; + + const tx = await program.methods + .initializeBus( + new anchor.BN(2), + new anchor.BN(5), + new anchor.BN(7), + new anchor.BN(12), + new anchor.BN(23) + ) + .accounts({ + sube: sube, + signer: payer.publicKey, + systemProgram: SystemProgram.programId, + }) + .rpc(); + console.log("Transaction signature", tx); + }); +}); diff --git a/tests/trip.ts b/tests/trip.ts new file mode 100644 index 0000000..be886cd --- /dev/null +++ b/tests/trip.ts @@ -0,0 +1,32 @@ +import * as anchor from "@coral-xyz/anchor"; +import { Program } from "@coral-xyz/anchor"; +import { SubeCrypto } from "../target/types/sube_crypto"; +import { SystemProgram, PublicKey } from "@solana/web3.js"; + +describe("Register a business", () => { + const provider = anchor.AnchorProvider.env(); + anchor.setProvider(provider); + const payer = provider.wallet as anchor.Wallet; + const program = anchor.workspace.SubeCrypto as Program; + const programId = program.programId; + + it("Is initialized!", async () => { + const sube = PublicKey.findProgramAddressSync( + [payer.publicKey.toBuffer()], + programId + )[0]; + + const tx = await program.methods + .takeTrip( + 2, + ) + .accounts({ + sube: sube, + from: payer.publicKey, + to: payer.publicKey, + systemProgram: SystemProgram.programId, + }) + .rpc(); + console.log("Transaction signature", tx); + }); +}); diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..558b83e --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,11 @@ +{ + "compilerOptions": { + "types": ["mocha", "chai"], + "typeRoots": ["./node_modules/@types"], + "lib": ["es2015"], + "module": "commonjs", + "target": "es6", + "esModuleInterop": true + } + } + \ No newline at end of file