Skip to content

Commit

Permalink
feat(#632): Hexboosting by device type
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Apr 17, 2024
1 parent 12915a3 commit a5449ad
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 34 deletions.
2 changes: 1 addition & 1 deletion packages/helium-admin-cli/src/create-boost-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ export async function run(args: any = process.argv) {
rentReclaimAuthority: new PublicKey(argv.rentReclaimAuthority),
authority: subDaoAuth,
subDao,
startAuthority: new PublicKey(argv.startAuthority)
startAuthority: new PublicKey(argv.startAuthority),
})
.instruction(),
];
Expand Down
4 changes: 2 additions & 2 deletions packages/helium-admin-cli/src/update-boost-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ export async function run(args: any = process.argv) {
},
minimumPeriods: {
type: "number",
describe: "The new minimum number of periods"
describe: "The new minimum number of periods",
},
boostPrice: {
type: "string",
describe: "The boost price in bones"
describe: "The boost price in bones",
},
dntMint: {
type: "string",
Expand Down
18 changes: 16 additions & 2 deletions packages/hexboosting-sdk/src/pdas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,30 @@ import { PublicKey } from "@solana/web3.js";
import { PROGRAM_ID } from "./constants";
import BN from "bn.js";

const deviceTypeValues = {
cbrsIndoor: 0,
cbrsOutdoor: 1,
wifiIndoor: 2,
wifiOutdoor: 3,
};

export function boostedHexKey(
boostConfig: PublicKey,
carrier: PublicKey,
deviceType: any,
location: BN,
programId: PublicKey = PROGRAM_ID
) {
const locBuffer = Buffer.alloc(8);
locBuffer.writeBigUint64LE(BigInt(location.toString()));
const deviceTypeName = Object.keys(deviceType)[0];
let deviceTypeValue = deviceTypeValues[deviceTypeName];
return PublicKey.findProgramAddressSync(
[Buffer.from("boosted_hex", "utf-8"), boostConfig.toBuffer(), carrier.toBuffer(), locBuffer],
[
Buffer.from("boosted_hex", "utf-8"),
boostConfig.toBuffer(),
Buffer.from([deviceTypeValue]),
locBuffer,
],
programId
);
}
Expand Down
10 changes: 9 additions & 1 deletion packages/hexboosting-sdk/src/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,21 @@ import {
} from "@helium/anchor-resolvers";
import { subDaoKey } from "@helium/helium-sub-daos-sdk";
import { PublicKey } from "@solana/web3.js";
import { boostedHexKey } from "./pdas";

export const hexboostingResolvers = combineResolvers(
heliumCommonResolver,
resolveIndividual(async ({ path, accounts }) => {
resolveIndividual(async ({ path, accounts, args }) => {
if (path[path.length - 1] === "subDao" && accounts.dntMint) {
return subDaoKey(accounts.dntMint as PublicKey)[0];
}
if (path[path.length - 1] === "boostedHex" && accounts.boostConfig && args[0].deviceType && args[0].location) {
return boostedHexKey(
accounts.boostConfig as PublicKey,
args[0].deviceType,
args[0].location
)[0]
}
}),
ataResolver({
instruction: "boostV0",
Expand Down
11 changes: 6 additions & 5 deletions programs/hexboosting/src/instructions/boost_v0.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct BoostArgsV0 {
// invalid
pub version: u32,
pub amounts: Vec<BoostAmountV0>,
pub device_types: Vec<DeviceTypeV0>,
pub device_type: DeviceTypeV0,
}

#[derive(AnchorSerialize, AnchorDeserialize, Clone, Default)]
Expand Down Expand Up @@ -70,7 +70,7 @@ pub struct BoostV0<'info> {
init_if_needed,
payer = payer,
space = get_space(boosted_hex),
seeds = [b"boosted_hex", boost_config.key().as_ref(), carrier.key().as_ref(), &args.location.to_le_bytes()],
seeds = [b"boosted_hex", boost_config.key().as_ref(), &[(args.device_type as u8)], &args.location.to_le_bytes()],
bump,
constraint = boosted_hex.version == args.version @ ErrorCode::InvalidVersion,
)]
Expand All @@ -88,7 +88,7 @@ pub fn handler(ctx: Context<BoostV0>, args: BoostArgsV0) -> Result<()> {
ctx.accounts.boosted_hex.location = args.location;
ctx.accounts.boosted_hex.bump_seed = ctx.bumps["boosted_hex"];
ctx.accounts.boosted_hex.version += 1;
ctx.accounts.boosted_hex.device_types = args.device_types;
ctx.accounts.boosted_hex.device_type = args.device_type;

// Insert the new periods
let max_period = args
Expand All @@ -104,6 +104,7 @@ pub fn handler(ctx: Context<BoostV0>, args: BoostArgsV0) -> Result<()> {
.boosts_by_period
.resize(max_period + 1, 0);
}

let now = Clock::get()?.unix_timestamp;

for amount in args.amounts.clone() {
Expand Down Expand Up @@ -165,8 +166,8 @@ pub fn handler(ctx: Context<BoostV0>, args: BoostArgsV0) -> Result<()> {
let total_fee: u64 = args
.amounts
.iter()
.map(|amount| amount.amount as u64 * ctx.accounts.boost_config.boost_price)
.sum();
.map(|amount| (amount.amount as u64 * ctx.accounts.boost_config.boost_price))
.sum::<u64>();
let mobile_price_oracle =
load_price_feed_from_account_info(&ctx.accounts.price_oracle).map_err(|e| {
msg!("Pyth error {}", e);
Expand Down
9 changes: 5 additions & 4 deletions programs/hexboosting/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ pub struct BoostConfigV0 {
#[derive(AnchorSerialize, AnchorDeserialize, Clone, Copy, Default, PartialEq)]
pub enum DeviceTypeV0 {
#[default]
Cbrs,
WifiIndoor,
WifiOutdoor,
CbrsIndoor = 0,
CbrsOutdoor = 1,
WifiIndoor = 2,
WifiOutdoor = 3,
}

#[account]
Expand All @@ -42,7 +43,7 @@ pub struct BoostedHexV0 {
pub boosts_by_period: Vec<u8>,
// Track changes to the boosted hex so client can pass what version it made a change to
pub version: u32,
pub device_types: Vec<DeviceTypeV0>,
pub device_type: DeviceTypeV0,
}

impl BoostedHexV0 {
Expand Down
34 changes: 15 additions & 19 deletions tests/hexboosting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@ import {
} from "@solana/web3.js";
import { BN } from "bn.js";
import { expect } from "chai";
import {
init as initHeliumEntityManager
} from "../packages/helium-entity-manager-sdk/src";
import { init as initHeliumEntityManager } from "../packages/helium-entity-manager-sdk/src";
import {
boostConfigKey,
boostedHexKey,
init
init,
} from "../packages/hexboosting-sdk";
import { init as initMobileEntityManager } from "../packages/mobile-entity-manager-sdk/src";
import { DataCredits } from "../target/types/data_credits";
Expand All @@ -39,7 +37,7 @@ import {
ensureHEMIdl,
ensureHSDIdl,
ensureMemIdl,
initTestDataCredits
initTestDataCredits,
} from "./utils/fixtures";
import { random } from "./utils/string";

Expand Down Expand Up @@ -253,7 +251,7 @@ describe("hexboosting", () => {
.boostV0({
location: new BN(1),
version: 0,
deviceTypes: [{ wifiIndoor: {} }],
deviceType: { wifiIndoor: {} },
amounts: [
{
period: 0,
Expand Down Expand Up @@ -304,7 +302,7 @@ describe("hexboosting", () => {

const hex = await program.account.boostedHexV0.fetch(boostedHex!);

expect(Object.keys(hex.deviceTypes[0])[0]).to.eq("wifiIndoor");
expect(Object.keys(hex.deviceType)[0]).to.eq("wifiIndoor");
expect(hex.location.toNumber()).to.eq(1);
expect(hex.startTs.toNumber()).to.eq(0);
expect(hex.boostsByPeriod.toJSON().data).to.deep.eq([1, 1, 1, 1, 1, 1]);
Expand All @@ -316,7 +314,7 @@ describe("hexboosting", () => {
.boostV0({
location: new BN(1),
version: 0,
deviceTypes: [{ wifiIndoor: {} }],
deviceType: { wifiIndoor: {} },
amounts: [
{
period: 0,
Expand Down Expand Up @@ -364,7 +362,7 @@ describe("hexboosting", () => {
.boostV0({
location: new BN(1),
version: 1,
deviceTypes: [{ wifiIndoor: {} }],
deviceType: { wifiIndoor: {} },
amounts: [
{
period: 2,
Expand Down Expand Up @@ -399,18 +397,16 @@ describe("hexboosting", () => {
const hex = await program.account.boostedHexV0.fetch(boostedHex!);

expect(hex.boostsByPeriod.toJSON().data).to.deep.eq([
1,
1,
2,
1,
1,
1,
2,
1, 1, 2, 1, 1, 1, 2,
]);
});

it("allows starting a boost", async () => {
const boostedHex = boostedHexKey(boostConfigKey(mint)[0], carrier, new BN(1))[0];
const boostedHex = boostedHexKey(
boostConfigKey(mint)[0],
{ wifiIndoor: {} },
new BN(1)
)[0];
await program.methods
.startBoostV0({
startTs: new BN(1),
Expand All @@ -432,7 +428,7 @@ describe("hexboosting", () => {
beforeEach(async () => {
const boostedHex = boostedHexKey(
boostConfigKey(mint)[0],
carrier,
{ wifiIndoor: {} },
new BN(1)
)[0];
await program.methods
Expand All @@ -448,7 +444,7 @@ describe("hexboosting", () => {
it("allows closing the boost when it's done", async () => {
const boostedHex = boostedHexKey(
boostConfigKey(mint)[0],
carrier,
{ wifiIndoor: {} },
new BN(1)
)[0];
// Wait 7 seconds so it is fully expired
Expand Down

0 comments on commit a5449ad

Please sign in to comment.