Skip to content

Commit

Permalink
working on getting build to work
Browse files Browse the repository at this point in the history
  • Loading branch information
boid-com committed Feb 8, 2024
1 parent 14aa70e commit b598dbc
Show file tree
Hide file tree
Showing 15 changed files with 118 additions and 164 deletions.
16 changes: 8 additions & 8 deletions src/assembly/actions/1-global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,6 @@ export class GlobalActions extends Contract {
check(false, "boid system config not initialized")
return 0
} else return f32(currentTimeSec() - config.time.rounds_start_sec_since_epoch) / f32(config.time.round_length_sec)
// if (this.roundFloat == 0) {
// }
// return this.roundFloat
}

pwrReportsT(boid_id:Name):TableStore<PwrReportRow> {
Expand Down Expand Up @@ -101,7 +98,7 @@ export class GlobalActions extends Contract {
// delete old rows
const firstRow = this.statsT.first()
if (!firstRow) return true
const deleteBeforeRound = u16(Math.max(i32(this.currentRound()) - config.reports_finalized_after_rounds - config.keep_finalized_stats_rows, 0))
const deleteBeforeRound = u16(Math.max(i32(this.currentRound()) - i32(1) - config.keep_finalized_stats_rows, 0))
print("\n DeleteBeforeRound: " + deleteBeforeRound.toString())
if (firstRow.round < deleteBeforeRound) this.statsT.remove(firstRow)
const nextRow = this.statsT.first()
Expand All @@ -114,9 +111,12 @@ export class GlobalActions extends Contract {
@action("configset")
configSet(config:Config):void {
requireAuth(this.receiver)
check(config.slash.slash_quantity_collateral_pct >= 0, "slash_quantity_collateral_pct must be higher or equal zero")
check(config.slash.slash_quantity_collateral_pct <= f32(1), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.collateral.weight_collateral_divisor > 0, "weight_collateral_divisor must be higher than zero")
check(config.slashLow.slash_quantity_collateral_pct >= 0, "slash_quantity_collateral_pct must be greater than or equal zero")
check(config.slashLow.slash_quantity_collateral_pct <= f32(1), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.slashMed.slash_quantity_collateral_pct >= 0, "slash_quantity_collateral_pct must be greater than or equal zero")
check(config.slashMed.slash_quantity_collateral_pct <= f32(1), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.slashHigh.slash_quantity_collateral_pct >= 0, "slash_quantity_collateral_pct must be greater than or equal zero")
check(config.slashHigh.slash_quantity_collateral_pct <= f32(1), "slash_quantity_collateral_pct must be less or equal to 100%")
check(config.reports_accumulate_weight_round_pct >= 0, "reports_accumulate_weight_round_pct must be higher or equal zero")
check(config.reports_accumulate_weight_round_pct <= f32(1), "reports_accumulate_weight_round_pct must be less or equal to 100%")
check(config.payment.collateral_pct_pay_per_round >= 0, "collateral_pct_pay_per_round must be higher or equal zero")
Expand Down Expand Up @@ -146,7 +146,7 @@ export class GlobalActions extends Contract {
//DEBUG
@action("finalround")
finalRound():void {
check(false, (this.currentRound() - (this.configT.get().reports_finalized_after_rounds + 1)).toString())
check(false, (this.currentRound() - 1).toString())
// check(false, (this.currentRoundFloat() % f32(this.currentRound())).toString())
}

Expand Down
66 changes: 24 additions & 42 deletions src/assembly/actions/2-oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,23 +52,9 @@ export class OracleActions extends GlobalActions {
// add the new collateral and push the next available unlock time into the future
oracleRow.collateral.locked += depositQuantity
check(oracleRow.collateral.locked >= depositQuantity, "collateral locked max reached")
oracleRow.collateral.min_unlock_start_round = this.currentRound() + config.waits.unlock_wait_rounds

// save the current weight and calculate the new weight
const weightBefore = oracleRow.weight
const newWeight = this.getOracleWeight(oracleRow.trueCollateral, config)

// if weight has increased, update the global totals
if (newWeight > weightBefore) {
const difference = newWeight - weightBefore
const global = this.globalT.get()
global.expected_active_weight += difference
check(global.expected_active_weight >= difference, "global expected_active_weight max reached")
this.globalT.set(global, this.receiver)
}
oracleRow.collateral.min_unlock_start_round = this.currentRound() + config.waits.collateral_unlock_wait_rounds

// save the updated oracle row
oracleRow.weight = newWeight
this.oraclesT.update(oracleRow, this.receiver)
}

Expand All @@ -86,26 +72,25 @@ export class OracleActions extends GlobalActions {
*/
@action("setstandby")
setStandby(oracle:Name, standby:boolean):void {
if (!hasAuth(this.receiver)) requireAuth(oracle)
if (standby == false) requireAuth(this.receiver)
else if (!hasAuth(this.receiver)) requireAuth(oracle)
const oracleRow = this.oraclesT.requireGet(oracle.value, "oracle doesn't exist")
const global = this.globalT.get()
const config = this.getConfig()
check(oracleRow.standby != standby, "nothing to change")
if (!hasAuth(this.receiver)) check(this.currentRound() - oracleRow.last_standby_toggle_round > config.standby_toggle_interval_rounds, "can't toggle standby this quickly")
check(oracleRow.collateral.unlocking == 0, "can't toggle standby during unlocking")
oracleRow.standby = standby
if (standby) {
oracleRow.standby = standby
oracleRow.last_standby_toggle_round = this.currentRound()
global.expected_active_weight -= oracleRow.weight
global.standby_oracles++
check(global.standby_oracles >= 1, "max standy_oracles reached")
check(global.standby_oracles >= 1, "max standby_oracles reached")
const oracleIndex = global.expected_active_oracles.indexOf(oracle)
check(oracleIndex > -1, "problem setting oracle standby")
global.expected_active_oracles.splice(oracleIndex, 1)
} else {
check(oracleRow.weight > 0, "oracle must have positive weight. add more collateral")
oracleRow.standby = standby
oracleRow.expected_active_after_round = this.currentRound() + config.oracle_expected_active_after_rounds
check(oracleRow.weight > 0, "oracle must have positive weight.")
global.expected_active_weight += oracleRow.weight
check(global.expected_active_weight >= oracleRow.weight, "global expected_active_weight max reached")
global.expected_active_oracles.push(oracle)
Expand All @@ -124,18 +109,15 @@ export class OracleActions extends GlobalActions {
/**
* This action is triggered when a new oracle deposits collateral
*
* @param {Name} account
* @param {Name} oracle
* @param {u8} weight
* @param {u32} adding_collateral
* @memberof OracleActions
*/
@action("oracleset")
oracleSet(account:Name, weight:u8, adding_collateral:u32):void {
// TODO, this action originally handled new and existing oracles but now it only needs to handle new oracles
// still thinking about the best way to organize the actions...
// The original idea is that arbitrary weight could be added, like from the DAO or other sources of voting
oracleSet(oracle:Name, weight:u8, adding_collateral:u32):void {
requireAuth(this.receiver)
const existing = this.oraclesT.get(account.value)
const existing = this.oraclesT.get(oracle.value)
const global = this.globalT.get()
const config = this.getConfig()

Expand All @@ -144,24 +126,25 @@ export class OracleActions extends GlobalActions {
check(existing.collateral.unlocking == 0, "can't modify oracle during unlocking")

existing.collateral.locked += adding_collateral
existing.collateral.min_unlock_start_round = this.currentRound() + config.unlock_wait_rounds
existing.collateral.min_unlock_start_round = this.currentRound() + config.waits.collateral_unlock_wait_rounds

// update globals with new weight change
if (weight > existing.weight) {
global.expected_active_weight += (weight - existing.weight)
check(global.expected_active_weight >= (weight - existing.weight), "global expected_active_weight max reached")
} else {
check(global.expected_active_weight >= (existing.weight - weight), "global expected_active_weight cannot be negative")
global.expected_active_weight -= (existing.weight - weight)
if (!existing.standby) {
if (weight > existing.weight) {
global.expected_active_weight += (weight - existing.weight)
check(global.expected_active_weight >= (weight - existing.weight), "global expected_active_weight max reached")
} else {
check(global.expected_active_weight >= (existing.weight - weight), "global expected_active_weight cannot be negative")
global.expected_active_weight -= (existing.weight - weight)
}
}
existing.weight = weight

this.oraclesT.update(existing, this.receiver)
} else {
check(isAccount(account), "oracle must be existing account")
check(isAccount(oracle), "oracle must be existing chain account")
const collateralData:OracleCollateral = {
locked: adding_collateral,
min_unlock_start_round: this.currentRound() + config.first_unlock_wait_rounds,
min_unlock_start_round: this.currentRound() + config.waits.collateral_unlock_wait_rounds,
slashed: 0,
unlock_finished_round: 0,
unlocking: 0
Expand All @@ -172,9 +155,8 @@ export class OracleActions extends GlobalActions {
withdrawing: 0,
unclaimed: 0
}
const row = new Oracle(account, weight, collateralData, fundsData, true)
const row = new Oracle(oracle, weight, collateralData, fundsData, true)
this.oraclesT.store(row, this.receiver)
// global.total_weight += weight
global.standby_oracles++
check(global.standby_oracles >= 1, "max standy_oracles reached")
}
Expand All @@ -194,7 +176,7 @@ export class OracleActions extends GlobalActions {
const config = this.getConfig()
const funds = oracleRow.funds
check(funds.unclaimed > 0, "unclaimed funds must be greater than zero")
funds.withdrawable_after_round = this.currentRound() + config.withdraw_rounds_wait
funds.withdrawable_after_round = this.currentRound() + config.waits.withdraw_rounds_wait
funds.withdrawing = funds.unclaimed
this.oraclesT.update(oracleRow, this.receiver)
}
Expand All @@ -220,7 +202,7 @@ export class OracleActions extends GlobalActions {
funds.unclaimed -= funds.withdrawing

// mint tokens and then send to the oracle acct
this.sendWholeBoid(Name.fromString("mint.boid"), this.receiver, funds.withdrawing, "power.boid oracle reward mint for " + oracle.toString())
// this.sendWholeBoid(Name.fromString("tknmint.boid"), this.receiver, funds.withdrawing, "power.boid oracle reward mint for " + oracle.toString())
this.sendWholeBoid(this.receiver, oracle, funds.withdrawing, "power.boid oracle reward")

// update oracle row
Expand Down Expand Up @@ -250,7 +232,7 @@ export class OracleActions extends GlobalActions {
// move locked to unlocking and set the future unlock round
coll.unlocking = oracleRow.trueCollateral
coll.locked = 0
coll.unlock_finished_round = this.currentRound() + config.unlock_wait_rounds
coll.unlock_finished_round = this.currentRound() + config.waits.collateral_unlock_wait_rounds
oracleRow.weight = 0

// update the row
Expand Down
11 changes: 5 additions & 6 deletions src/assembly/actions/3-pwrreport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,8 @@ export class PwrReportActions extends OracleActions {
check(commitExists == null, "oracle already commited report for this user and round")

// ensure the report is for a round that is valid
check(this.currentRound() >= config.reports_finalized_after_rounds, "chain is too recent to generate reports")
check(report.round >= this.currentRound() - config.reports_finalized_after_rounds, "round is too far in the past")
check(report.round < this.currentRound(), "report round must target a past round")
check(report.round == this.currentRound() - 1, "round is too far in the past")

// ensure the oracle can make reports
const oracleRow = this.oraclesT.requireGet(oracle.value, "oracle not registered")
Expand Down Expand Up @@ -141,13 +140,13 @@ export class PwrReportActions extends OracleActions {
const existingOStats = oStatsT.get(u64(targetRound))
if (existingOStats) {
if (reportSent) {
existingOStats.reports.reported_merged++
existingOStats.reports.reported_or_merged++
existingOStats.reports.unreported_unmerged--
} else existingOStats.reports.unreported_unmerged++
if (proposed) existingOStats.reports.proposed++
oStatsT.update(existingOStats, this.receiver)
} else {
const oracleStatsRow = new OracleStat(targetRound, oracleRow.weight, { proposed: proposed ? 1 : 0, reported_merged: reportSent ? 1 : 0, unreported_unmerged: reportSent ? 0 : 1 })
const oracleStatsRow = new OracleStat(targetRound, oracleRow.weight, { proposed: proposed ? 1 : 0, reported_or_merged: reportSent ? 1 : 0, unreported_unmerged: reportSent ? 0 : 1 })
oStatsT.store(oracleStatsRow, this.receiver)
}
}
Expand Down Expand Up @@ -256,13 +255,13 @@ export class PwrReportActions extends OracleActions {
const oStatsT = this.oracleStatsT(oracle)
const existing = oStatsT.get(u64(targetRound))
if (existing) {
existing.reports.reported_merged++
existing.reports.reported_or_merged++
existing.reports.unreported_unmerged--
oStatsT.update(existing, this.receiver)
} else {
const oracleData = this.oraclesT.get(oracle.value)
if (!oracleData) continue
const oracleStatsRow = new OracleStat(u16(targetRound), oracleData.weight, { reported_merged: 1, unreported_unmerged: 0, proposed: 0 })
const oracleStatsRow = new OracleStat(u16(targetRound), oracleData.weight, { reported_or_merged: 1, unreported_unmerged: 0, proposed: 0 })
oStatsT.store(oracleStatsRow, this.receiver)
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/assembly/actions/4-protocol.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { Action, ActionData, check, Contract, EMPTY_NAME, Encoder, isAccount, Name, requireAuth, TableStore } from "proton-tsc"
import { check, requireAuth } from "proton-tsc"
import { Protocol } from "../tables/protocols"
import { PwrReportActions } from "./3-pwrreport"

export class ProtoActions extends PwrReportActions {
/**
* requires contract authority, for adding/editing protocols
*
* @param {Protocol} protocol
*/
@action("protoset")
Expand Down
10 changes: 4 additions & 6 deletions src/assembly/actions/5-deposit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { check, Name, Asset, TableStore, print, SAME_PAYER, unpackActionData, ActionData } from "proton-tsc"
import { Assets, TransferNfts, ATOMICASSETS_CONTRACT } from "../../external/atomicassets"
import { ActionData, Asset, Name, check, unpackActionData } from "proton-tsc"
import { ProtoActions } from "./4-protocol"

const boidSym:u64 = 293287707140
Expand All @@ -26,7 +25,7 @@ export class DepositActions extends ProtoActions {
let params = unpackActionData<TokenTransfer>()
if (params.from == this.receiver) return
if (params.from == Name.fromString("stake.boid")) return
if (params.from == Name.fromString("mint.boid")) return
if (params.from == Name.fromString("tknmint.boid")) return
check(params.to == this.receiver, "Invalid Deposit")
check(params.quantity.symbol.value == boidSym, "invalid token symbol")
check(params.quantity.isValid(), "invalid quantity")
Expand All @@ -36,11 +35,10 @@ export class DepositActions extends ProtoActions {
const config = this.getConfig()
check(params.quantity.amount <= i64(u32.MAX_VALUE) * i64(1e4), "max deposit collateral amount reached")
const quantity = u32(params.quantity.amount / u32(1e4))
check(quantity % config.oracle_collateral_deposit_increment == 0, "must deposit collateral in correct increments")

check(quantity % config.collateral.oracle_collateral_deposit_increment == 0, "must deposit collateral in correct increments")
// if oracle exists call oracldeposit otherwise call oracleset
if (this.oraclesT.exists(params.from.value)) this.sendOracleDeposit(params.from, quantity)
else this.sendOracleSet(params.from, this.getOracleWeight(quantity, config), quantity)
else this.sendOracleSet(params.from, 0, quantity)
} else check(false, "if depositing oracle collateral must specify 'collateral' as memo")
}
}
Expand Down
Loading

0 comments on commit b598dbc

Please sign in to comment.