Skip to content

Commit

Permalink
1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mateolafalce committed Jan 9, 2024
1 parent 81f46c6 commit 3c9db8c
Show file tree
Hide file tree
Showing 11 changed files with 135 additions and 21 deletions.
9 changes: 6 additions & 3 deletions Anchor.toml
Original file line number Diff line number Diff line change
@@ -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]
[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"
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ codegen-units = 1
[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1
ahash = "=0.8.6"
codegen-units = 1
20 changes: 20 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
2 changes: 1 addition & 1 deletion programs/sube_crypto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "sube_crypto"
version = "1.0.0"
version = "1.0.1"
description = "."
edition = "2021"

Expand Down
8 changes: 4 additions & 4 deletions programs/sube_crypto/src/instructions/initialize_bus_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SubeAdminAccount> = &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>,
Expand Down
11 changes: 5 additions & 6 deletions programs/sube_crypto/src/instructions/take_a_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,15 @@ use anchor_lang::{
};

pub fn take_a_trip(ctx: Context<TakeATrip>, 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(),
Expand Down
6 changes: 3 additions & 3 deletions programs/sube_crypto/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<InitializeBusLine>,
to3km: u64,
to6km: u64,
Expand All @@ -26,7 +26,7 @@ pub mod sube_crypto {
initialize_bus_line(ctx, to3km, to6km, to12km, to27km, more27km)
}

pub fn take_a_trip_(ctx: Context<TakeATrip>, km: u8) -> Result<()> {
pub fn take_trip(ctx: Context<TakeATrip>, km: u8) -> Result<()> {
take_a_trip(ctx, km)
}
}
19 changes: 17 additions & 2 deletions programs/sube_crypto/src/state/accounts.rs
Original file line number Diff line number Diff line change
@@ -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<u64>, // 4 + 8 * 5
pub prices: Vec<u64>, // 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();
}
}
35 changes: 35 additions & 0 deletions tests/init_bus_line.ts
Original file line number Diff line number Diff line change
@@ -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<SubeCrypto>;
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);
});
});
32 changes: 32 additions & 0 deletions tests/trip.ts
Original file line number Diff line number Diff line change
@@ -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<SubeCrypto>;
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);
});
});
11 changes: 11 additions & 0 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"compilerOptions": {
"types": ["mocha", "chai"],
"typeRoots": ["./node_modules/@types"],
"lib": ["es2015"],
"module": "commonjs",
"target": "es6",
"esModuleInterop": true
}
}

0 comments on commit 3c9db8c

Please sign in to comment.