Skip to content

Commit

Permalink
logging stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
boid-com committed Mar 10, 2024
1 parent f281cf4 commit de77d5f
Show file tree
Hide file tree
Showing 11 changed files with 110 additions and 74 deletions.
Binary file modified .DS_Store
Binary file not shown.
14 changes: 13 additions & 1 deletion power.boid.types.ts

Large diffs are not rendered by default.

4 changes: 1 addition & 3 deletions src/assembly/actions/2-oracle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ export class OracleActions extends GlobalActions {
this.oraclesT.update(existing, this.receiver)
} else {
check(isAccount(oracle), "oracle must be existing chain account")
check(adding_collateral >= config.collateral.oracle_collateral_minimum, "collateral must be >= the oracle collateral minimum for first deposit")
const collateralData:OracleCollateral = {
locked: adding_collateral,
min_unlock_start_round: this.currentRound() + config.waits.collateral_unlock_wait_rounds,
Expand Down Expand Up @@ -186,7 +187,6 @@ export class OracleActions extends GlobalActions {

/**
* After the withdraw waiting period has passed anyone can call this action to push the funds to the oracle
*
* @param {Name} oracle
*/
@action("withdraw")
Expand All @@ -204,8 +204,6 @@ export class OracleActions extends GlobalActions {
funds.claimed += funds.withdrawing
funds.unclaimed -= funds.withdrawing

// mint tokens and then send to the oracle acct
// 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
9 changes: 5 additions & 4 deletions src/assembly/actions/3-pwrreport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ export class PowerAddParams extends ActionData {
export class ReportSentParams extends ActionData {
constructor(
public report:PwrReportRow,
public adding_power:u16 = 0
public adding_power:u16 = 0,
public target_boid_id:Name = EMPTY_NAME
) {
super()
}
Expand All @@ -31,18 +32,18 @@ export class PwrReportActions extends OracleActions {
const proto = this.protocolsT.requireGet(u64(report.report.protocol_id), "invalid protocol_id")
const power:u16 = u16(proto.unitPowerMult * f32(report.report.units))
// check(false, proto.unitPowerMult.toString() + " " + report.units.toString() + " " + power.toString())
const logData = new ReportSentParams(report, power)
const logData = new ReportSentParams(report, power, boid_id)
const logAct = new Action(this.receiver, Name.fromString("reportsent"), [this.codePerm], logData.pack())
logAct.send()
// silently fail if no power was generated
if (power == 0) return
const data = new PowerAddParams(boid_id, power)
const action = new Action(Name.fromString("boid"), Name.fromString("power.add"), [new PermissionLevel(Name.fromString("boid"), Name.fromString("active"))], data.pack())
const action = new Action(Name.fromString("boid"), Name.fromString("power.add"), [new PermissionLevel(Name.fromString("boid"), Name.fromString("worker.boid"))], data.pack())
action.send()
}

@action("reportsent")
reportSent(report:PwrReportRow, adding_power:u16):void {
reportSent(report:PwrReportRow, adding_power:u16, target_boid_id:Name):void {
requireAuth(this.receiver)
}

Expand Down
16 changes: 9 additions & 7 deletions src/assembly/actions/6-ostats.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Action, ActionData, EMPTY_NAME, Name, SAME_PAYER, check, requireAuth } from "proton-tsc"
import { DepositActions } from "./5-deposit"
import { Reports } from "../tables/oracleStats"

export class OStatsActions extends DepositActions {
/**
Expand All @@ -9,8 +10,8 @@ export class OStatsActions extends DepositActions {
* @param {u16} round
*/

sendPayOracle(oracle:Name, basePay:u32, bonusPay:u32, round:u16):void {
const data = new PayOracleParams(oracle, basePay, bonusPay, round)
sendPayOracle(oracle:Name, basePay:u32, bonusPay:u32, round:u16, reports:Reports):void {
const data = new PayOracleParams(oracle, basePay, bonusPay, round, reports)
const action = new Action(this.receiver, Name.fromString("payoracle"), [this.codePerm], data.pack())
action.send()
}
Expand All @@ -23,14 +24,14 @@ export class OStatsActions extends DepositActions {
* @param {u32} bonusPay
*/
@action("payoracle")
payOracle(oracle:Name, basePay:u32, bonusPay:u32, round:u16):void {
payOracle(oracle:Name, basePay:u32, bonusPay:u32, round:u16, reports:Reports):void {
requireAuth(this.receiver)
const oracleRow = this.oraclesT.requireGet(oracle.value, "oracle doesn't exist")
const payQuantity = basePay + bonusPay
oracleRow.funds.unclaimed += payQuantity
this.oraclesT.update(oracleRow, this.receiver)
// this.sendWholeBoid(Name.fromString("tknmint.boid"), this.receiver, payQuantity, "payoracle:" + oracle.toString() + " round:" + round.toString())
this.sendWholeBoid(Name.fromString("mint.boid"), this.receiver, payQuantity, "payoracle:" + oracle.toString() + " round:" + round.toString())
this.sendWholeBoid(Name.fromString("tknmint.boid"), this.receiver, payQuantity, "payoracle:" + oracle.toString() + " round:" + round.toString())
// this.sendWholeBoid(Name.fromString("mint.boid"), this.receiver, payQuantity, "payoracle:" + oracle.toString() + " round:" + round.toString())
}

@action("payoutround")
Expand All @@ -46,7 +47,7 @@ export class OStatsActions extends DepositActions {
let bonusPay:u32 = 0
if (oRoundData.reports.reported_or_merged > 0 || validProposed > 0) bonusPay = u32(f64(config.payment.round_bonus_pay_reports) * Math.pow(oRoundData.reports.reported_or_merged, config.payment.reports_proposed_adjust_pwr) + f64(config.payment.round_bonus_pay_proposed) * Math.pow(validProposed, config.payment.reports_proposed_adjust_pwr))
oStatsT.update(oRoundData, SAME_PAYER)
this.sendPayOracle(oracle, basePay, bonusPay, round)
this.sendPayOracle(oracle, basePay, bonusPay, round, oRoundData.reports)
}
}
@packer
Expand All @@ -55,6 +56,7 @@ export class PayOracleParams extends ActionData {
public oracle:Name = EMPTY_NAME,
public basePay:u32 = 0,
public bonusPay:u32 = 0,
public round:u16 = 0
public round:u16 = 0,
public reports:Reports = new Reports()
) { super() }
}
75 changes: 33 additions & 42 deletions tests/testRoundPayout.js
Original file line number Diff line number Diff line change
@@ -1,47 +1,38 @@
export function calculateBonusAndAPR(roundBonusPayReports, roundBonusPayProposed, reportsProposedAdjustPwr,numOraclesAdjustBase,collateralPctPay, reportedOrMerged, proposed, investedAmount, periodsPerYear,activeOracles) {
// Calculate the bonus payment
let bonusPay = roundBonusPayReports * Math.pow(reportedOrMerged, reportsProposedAdjustPwr) + roundBonusPayProposed * Math.pow(proposed, reportsProposedAdjustPwr);
bonusPay = bonusPay / Math.pow(numOraclesAdjustBase, activeOracles);
// bonusPay = bonusPay / activeOracles
const configPayment = {
collateral_pct_pay_per_round_mult: 0.000075, // 5% APR approximation
round_bonus_pay_reports: 10,
round_bonus_pay_proposed: 2,
reports_proposed_adjust_pwr: 0.42,
num_oracles_adjust_base: 1.015
};

// Calculate APR based on the bonus and invested amount
let basePay = collateralPctPay * investedAmount;
// let apr = (basePay / investedAmount) * periodsPerYear * 100; // Multiply by 100 to convert to percentage
let apr = ((basePay+bonusPay) / investedAmount) * periodsPerYear * 100; // Multiply by 100 to convert to percentage
return { bonusPay, apr, basePay };
}
function calculateOracleEarnings(numReports, proposed, collateral) {
const roundsPerYear = 675;

// Configuration
const configPayment = {
collateral_pct_pay_per_round_mult: 0.000037075, // 2.5% APR
round_bonus_pay_reports: 10,
round_bonus_pay_proposed: 2,
reports_proposed_adjust_pwr: .42,
num_oracles_adjust_base:1.015
}
// Additional Parameters
const activeOracles = 20
const oracleCollateral = 5000000
const periodsPerYear = 674.31 // for calculating oracle collateral APR
// Base pay
const basePay = collateral * configPayment.collateral_pct_pay_per_round_mult
// const basePay = u32(f32(oracleRow.trueCollateral) * config.payment.collateral_pct_pay_per_round_mult)
console.log(basePay)

// Scenarios
const scenarios = [100,500, 1000, 2050, 5000, 10000, 100000];
const proposedReports = 200; // Assuming constant for simplicity
const bonusPay = configPayment.round_bonus_pay_reports * Math.pow(proposed, configPayment.reports_proposed_adjust_pwr) + configPayment.round_bonus_pay_proposed * Math.pow(numReports, configPayment.reports_proposed_adjust_pwr)
console.log(bonusPay)
// Total paid per round
const totalPaidPerRound = basePay + bonusPay;

// Calculate and print the bonus and APR for each scenario
scenarios.forEach(reportedOrMerged => {
const {bonusPay, apr,basePay} = calculateBonusAndAPR(
configPayment.round_bonus_pay_reports,
configPayment.round_bonus_pay_proposed,
configPayment.reports_proposed_adjust_pwr,
configPayment.num_oracles_adjust_base,
configPayment.collateral_pct_pay_per_round_mult,
reportedOrMerged,
proposedReports,
oracleCollateral,
periodsPerYear,
activeOracles
);
// APR calculation
const totalPaidPerYear = totalPaidPerRound * roundsPerYear;
const apr = (totalPaidPerYear / collateral) * 100;

return {
bonusPay: bonusPay.toFixed(2),
basePay: basePay.toFixed(2),
totalPaid: totalPaidPerRound.toFixed(2),
apr: apr.toFixed(2) + '%'
};
}

console.log(`Reported/Merged: ${reportedOrMerged}, Bonus: ${bonusPay.toLocaleString(undefined, {maximumFractionDigits: 2})}, Base: ${ basePay } APR: ${apr.toFixed(2)}%, total paid: ${(bonusPay*activeOracles).toLocaleString()}`);
});
// Example usage
const numReports = 50; // Number of reports made by the oracle
const collateral = 5000000; // Collateral of the oracle
const numProposals = 10
console.log(calculateOracleEarnings(numReports, numProposals, collateral));
2 changes: 1 addition & 1 deletion util/deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ async function pushWasm(chain, eosjs, data) {
const methods = {
async default(chain, target) {
const eosjs = require('./lib/eosjs')(env.keys[chain], conf.endpoints[chain][0])
const authorization = [{ actor: conf.accountName[chain], permission: 'active' }]
const authorization = [{ actor: conf.accountName[chain], permission: 'owner' }]
console.log('Deploying to:', chain)
let abiData = { actions: [setAbiAction(`../build/${conf.contractName}.abi`, authorization)] }
let wasmData = { actions: [setCodeAction(`../build/${conf.contractName}.wasm`, authorization)] }
Expand Down
33 changes: 26 additions & 7 deletions util/do.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@ let config = {
},
collateral: {
oracle_collateral_deposit_increment: 1_000_000,
oracle_collateral_minimum: 1_000_000
oracle_collateral_minimum: 5_000_000
},
keep_finalized_stats_rows: 2,
standby_toggle_interval_rounds: 5,
reports_accumulate_weight_round_pct: 0
reports_accumulate_weight_round_pct: 0.1,
}
function getReportId(report = {protocol_id:0,round:0,units:0}) {
return Number((BigInt(report.protocol_id) << BigInt(48)) + (BigInt(report.round) << BigInt(32)) + BigInt(report.units))
}
console.log(getReportId({protocol_id:4,round:56,units:990}))
console.log(getReportId({protocol_id:4,round:56,units:1000}))
// function getReportId(report = {protocol_id:0,round:0,units:0}) {
// return Number((BigInt(report.protocol_id) << BigInt(48)) + (BigInt(report.round) << BigInt(32)) + BigInt(report.units))
// }
// console.log(getReportId({protocol_id:4,round:56,units:990}))
// console.log(getReportId({protocol_id:4,round:56,units:1000}))
const methods = {
async configset() {
await doAction('configset',{config})
Expand Down Expand Up @@ -103,6 +103,12 @@ const methods = {
async statsclear() {
await doAction("statsclear")
},
async withdraw(oracle) {
await doAction("withdraw",{oracle})
},
async withdrawinit(oracle) {
await doAction("withdrawinit",{oracle},null,oracle)
},
async ostatsclean(scope) {
await doAction("ostatsclean",{scope})
},
Expand Down Expand Up @@ -166,6 +172,19 @@ const methods = {
await this.boincset(...Object.values(item))
}
},
async protoSetAll(){
const proto = [
{protocol:{protocol_id:0,protocol_name:"fah",unitPowerMult:0.0002,active:true}},
{protocol:{protocol_id:1,protocol_name:"rosetta",unitPowerMult:0.001,active:true}},
{protocol:{protocol_id:2,protocol_name:"denisathome",unitPowerMult:0.001,active:true}},
{protocol:{protocol_id:3,protocol_name:"sidock",unitPowerMult:0.001,active:true}},
{protocol:{protocol_id:4,protocol_name:"rnaworld",unitPowerMult:0.001,active:true}},
{protocol:{protocol_id:5,protocol_name:"wcg",unitPowerMult:0.001,active:true}},
]
for (let item of proto){
await doAction("protoset",item)
}
},
async addcommit(){
await doAction("addcommit",{oracle:"boidworker11",commit:{round_commit_id:6,protocol_id:1,round:3,boid_id:"test"}})
},
Expand Down
7 changes: 5 additions & 2 deletions util/eosioConfig.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
module.exports = {
chains: ['eos', 'kylin', 'wax', 'jungle', 'telosTest', 'waxTest'],
chains: ['eos', 'kylin', 'wax', 'jungle', 'telosTest', 'waxTest','telos'],
endpoints: {
eos: ['https://eos.eosn.io','https://eos.api.animus.is', 'https://eos.dfuse.eosnation.io/'],
kylin: ['https://kylin.eosn.io', 'https://kylin.eossweden.org'],
jungle: ['https://jungle3.cryptolions.io', 'https://jungle3.greymass.com'],
telosTest: ['https://testnet.telos.caleos.io'],
telos: ['https://mainnet.telos.net'],
waxTest: ['https://testnet.waxsweden.org']
},
accountName: {
telosTest: "power.boid",
telos: "power.boid",
},
explorer: {
waxTest: "https://wax-test.bloks.io",
telosTest: "https://explorer-test.telos.net"
telosTest: "https://explorer-test.telos.net",
telos: "https://explorer.telos.net"
},
contractName: 'boid.contract'
}
2 changes: 1 addition & 1 deletion util/lib/eosjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function doAction(name, data, account, auth) {
if (!account) account = contractAccount
if (!auth) auth = account
console.log("Do Action:", account,name, data)
const authorization = [{ actor: auth, permission: 'active' }]
const authorization = [{ actor: auth, permission: 'owner' }]
const result = await api.transact({
// "delay_sec": 0,
actions: [{ account, name, data, authorization }]
Expand Down
22 changes: 16 additions & 6 deletions util/setup.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,21 @@ const contract = require('./do.js')


const methods = {
async updateOwner() {

const auth = {
"threshold": 1,
"keys": [],
"accounts": [{ permission: { actor: "dac.boid", permission: "active" }, "weight": 1 }],
"waits": []
}
await doAction('updateauth', { account: contractAccount, auth, parent: '', permission: 'owner' }, 'eosio', contractAccount)
},
async updateAuth(pubkey) {
if (!pubkey) pubkey = "EOS6FoTSwiKk27SJ1kANdJFmso3KbECASAMDpEka4dG9p1ub6GqiH"
// if (!pubkey) pubkey = "EOS6FoTSwiKk27SJ1kANdJFmso3KbECASAMDpEka4dG9p1ub6GqiH"
const auth = {
"threshold": 1,
"keys": [{ key: pubkey, weight: 1 }],
"keys": [],
"accounts": [{ "permission": { "actor": contractAccount, "permission": "eosio.code" }, "weight": 1 }],
"waits": []
}
Expand All @@ -21,15 +31,15 @@ const methods = {
if (!pubkey) pubkey = "EOS6FoTSwiKk27SJ1kANdJFmso3KbECASAMDpEka4dG9p1ub6GqiH"
const auth = {
"threshold": 1,
"keys": [{ key: pubkey, weight: 1 }],
"keys": [],
"accounts": [
{ "permission": { "actor": "boid", permission: "eosio.code" }, "weight": 1 },
{ "permission": { "actor": "power.boid", permission: "eosio.code" }, "weight": 1 }
{ "permission": { "actor": "boid", "permission": "eosio.code" }, "weight": 1 },
{ "permission": { "actor": "power.boid", "permission": "eosio.code" }, "weight": 1 }
],
"waits": []
}
await doAction('updateauth', { account: "tknmint.boid", auth, parent: 'owner', permission: 'active' }, 'eosio', "tknmint.boid")
await doAction('updateauth', { account: "mint.boid", auth, parent: 'owner', permission: 'active' }, 'eosio', "mint.boid")
// await doAction('updateauth', { account: "mint.boid", auth, parent: 'owner', permission: 'active' }, 'eosio', "mint.boid")
// await doAction('linkauth', { account: contractAccount, code:'avatar.boid',type:"finalize",requirement:'avataroracle' }, 'eosio', contractAccount)
},
async pwrAdd(pubkey) {
Expand Down

0 comments on commit de77d5f

Please sign in to comment.