@iai-skunks/ml-registry-solana
Advanced tools
Comparing version 0.3.0 to 0.4.0
@@ -11,7 +11,9 @@ import { Connection, PublicKey, TransactionInstruction } from "@solana/web3.js"; | ||
* @param coeffs The coefficients for the new GLM | ||
* @param space The space in bytes allocated to the account | ||
* @param usageFee The set usage fee per prediction with the model | ||
* @param feeAccountKey The account for to where fees should be deposited | ||
* @param intercept The intercept coeff for the GLM. If not specifed, default to 0 | ||
* @param lamports The budget to be set for the name account. If not specified, it'll be the minimum for rent exemption | ||
* @param space The space in bytes allocated to the account | ||
*/ | ||
export declare function createModelRegistry(connection: Connection, programId: PublicKey, payerKey: PublicKey, modelName: string, linkFunc: string, coeffs: number[], space: number, intercept?: number, lamports?: number): Promise<[TransactionInstruction, PublicKey]>; | ||
export declare function createModelRegistry(connection: Connection, programId: PublicKey, payerKey: PublicKey, modelName: string, linkFunc: string, coeffs: number[], space: number, usageFee: number, feeAccountKey: PublicKey, intercept?: number, lamports?: number): Promise<[TransactionInstruction, PublicKey]>; | ||
/** | ||
@@ -27,4 +29,6 @@ * Creates a predict with specified model instruction | ||
* @param space The space in bytes allocated to the account | ||
* @param mlServiceKey The wallet to where all proportion of fees to service are deposited | ||
* @param mlServiceCharge The percentage cut the service earns per prediction fee | ||
* @param lamports The budget to be set for the name account. If not specified, it'll be the minimum for rent exemption | ||
*/ | ||
export declare function predictWithModel(connection: Connection, programId: PublicKey, payerKey: PublicKey, modelName: string, scoreAccountName: string, indVars: number[], space: number, lamports?: number): Promise<[TransactionInstruction, PublicKey]>; | ||
export declare function predictWithModel(connection: Connection, programId: PublicKey, payerKey: PublicKey, modelName: string, scoreAccountName: string, indVars: number[], space: number, mlServiceKey: PublicKey, mlServiceCharge: number, lamports?: number): Promise<[TransactionInstruction, PublicKey]>; |
@@ -14,2 +14,3 @@ "use strict"; | ||
const web3_js_1 = require("@solana/web3.js"); | ||
const states_1 = require("../src/states"); | ||
const custom_types_1 = require("./custom_types"); | ||
@@ -30,7 +31,9 @@ const instructions_1 = require("./instructions"); | ||
* @param coeffs The coefficients for the new GLM | ||
* @param space The space in bytes allocated to the account | ||
* @param usageFee The set usage fee per prediction with the model | ||
* @param feeAccountKey The account for to where fees should be deposited | ||
* @param intercept The intercept coeff for the GLM. If not specifed, default to 0 | ||
* @param lamports The budget to be set for the name account. If not specified, it'll be the minimum for rent exemption | ||
* @param space The space in bytes allocated to the account | ||
*/ | ||
function createModelRegistry(connection, programId, payerKey, modelName, linkFunc, coeffs, space, intercept, lamports) { | ||
function createModelRegistry(connection, programId, payerKey, modelName, linkFunc, coeffs, space, usageFee, feeAccountKey, intercept, lamports) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -49,3 +52,3 @@ const hashedName = yield (0, utils_1.getHashedName)(modelName); | ||
} | ||
const instruction = (0, instructions_1.createInstruction)(programId, web3_js_1.SystemProgram.programId, payerKey, modelAccountPubkey, hashedName, coeffs, finalIntercept, linkFunc, new custom_types_1.Numberu64(balance), new custom_types_1.Numberu32(space)); | ||
const instruction = (0, instructions_1.createInstruction)(programId, web3_js_1.SystemProgram.programId, payerKey, modelAccountPubkey, hashedName, coeffs, finalIntercept, linkFunc, new custom_types_1.Numberu64(balance), new custom_types_1.Numberu32(space), new custom_types_1.Numberu64(usageFee), feeAccountKey); | ||
return [instruction, modelAccountPubkey]; | ||
@@ -65,5 +68,7 @@ }); | ||
* @param space The space in bytes allocated to the account | ||
* @param mlServiceKey The wallet to where all proportion of fees to service are deposited | ||
* @param mlServiceCharge The percentage cut the service earns per prediction fee | ||
* @param lamports The budget to be set for the name account. If not specified, it'll be the minimum for rent exemption | ||
*/ | ||
function predictWithModel(connection, programId, payerKey, modelName, scoreAccountName, indVars, space, lamports) { | ||
function predictWithModel(connection, programId, payerKey, modelName, scoreAccountName, indVars, space, mlServiceKey, mlServiceCharge, lamports) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -81,3 +86,5 @@ const balance = lamports | ||
const [scoreAccountPubkey, _2] = yield web3_js_1.PublicKey.findProgramAddress([scoreHashedName], programId); | ||
const instruction = (0, instructions_1.createPredictInstruction)(web3_js_1.SystemProgram.programId, programId, payerKey, modelAccountPubkey, modelHashedName, scoreAccountPubkey, scoreHashedName, indVars, new custom_types_1.Numberu64(balance), new custom_types_1.Numberu32(space)); | ||
// getting fee_wallet (ML model owner wallet) | ||
const modelState = yield states_1.ModelState.retrieve(connection, modelAccountPubkey); | ||
const instruction = (0, instructions_1.createPredictInstruction)(web3_js_1.SystemProgram.programId, programId, payerKey, modelAccountPubkey, modelHashedName, scoreAccountPubkey, scoreHashedName, indVars, new custom_types_1.Numberu64(balance), new custom_types_1.Numberu32(space), modelState.feeAccountKey, mlServiceKey, mlServiceCharge); | ||
return [instruction, scoreAccountPubkey]; | ||
@@ -84,0 +91,0 @@ }); |
/// <reference types="node" /> | ||
import { PublicKey, TransactionInstruction } from "@solana/web3.js"; | ||
import { Numberu32, Numberu64 } from "./custom_types"; | ||
export declare function createInstruction(mlProgramId: PublicKey, systemProgramId: PublicKey, payerKey: PublicKey, modelAccountKey: PublicKey, hashedName: Buffer, coeffs: number[], intercept: number, linkFunc: string, lamports: Numberu64, space: Numberu32): TransactionInstruction; | ||
export declare function createPredictInstruction(systemProgramId: PublicKey, mlProgramId: PublicKey, payerKey: PublicKey, modelAccountKey: PublicKey, modelHashedName: Buffer, scoreAccountKey: PublicKey, scoreHashedName: Buffer, indVars: number[], lamports: Numberu64, space: Numberu32): TransactionInstruction; | ||
export declare function createInstruction(mlProgramId: PublicKey, systemProgramId: PublicKey, payerKey: PublicKey, modelAccountKey: PublicKey, hashedName: Buffer, coeffs: number[], intercept: number, linkFunc: string, lamports: Numberu64, space: Numberu32, usageFee: Numberu64, feeAccountKey: PublicKey): TransactionInstruction; | ||
export declare function createPredictInstruction(systemProgramId: PublicKey, mlProgramId: PublicKey, payerKey: PublicKey, modelAccountKey: PublicKey, modelHashedName: Buffer, scoreAccountKey: PublicKey, scoreHashedName: Buffer, indVars: number[], lamports: Numberu64, space: Numberu32, feeAccountKey: PublicKey, mlServiceKey: PublicKey, mlServiceCharge: number): TransactionInstruction; |
@@ -6,3 +6,3 @@ "use strict"; | ||
const custom_types_1 = require("./custom_types"); | ||
function createInstruction(mlProgramId, systemProgramId, payerKey, modelAccountKey, hashedName, coeffs, intercept, linkFunc, lamports, space) { | ||
function createInstruction(mlProgramId, systemProgramId, payerKey, modelAccountKey, hashedName, coeffs, intercept, linkFunc, lamports, space, usageFee, feeAccountKey) { | ||
const buffers = [ | ||
@@ -19,2 +19,4 @@ Buffer.from(Int8Array.from([0])), | ||
space.toBuffer(), | ||
usageFee.toBuffer(), | ||
feeAccountKey.toBuffer(), | ||
]; | ||
@@ -46,3 +48,3 @@ const data = Buffer.concat(buffers); | ||
exports.createInstruction = createInstruction; | ||
function createPredictInstruction(systemProgramId, mlProgramId, payerKey, modelAccountKey, modelHashedName, scoreAccountKey, scoreHashedName, indVars, lamports, space) { | ||
function createPredictInstruction(systemProgramId, mlProgramId, payerKey, modelAccountKey, modelHashedName, scoreAccountKey, scoreHashedName, indVars, lamports, space, feeAccountKey, mlServiceKey, mlServiceCharge) { | ||
const buffers = [ | ||
@@ -58,2 +60,3 @@ Buffer.from(Int8Array.from([1])), | ||
space.toBuffer(), | ||
new custom_types_1.Float32(mlServiceCharge).toBuffer(), | ||
]; | ||
@@ -82,2 +85,12 @@ const data = Buffer.concat(buffers); | ||
}, | ||
{ | ||
pubkey: feeAccountKey, | ||
isSigner: false, | ||
isWritable: true, | ||
}, | ||
{ | ||
pubkey: mlServiceKey, | ||
isSigner: false, | ||
isWritable: true, | ||
}, | ||
]; | ||
@@ -84,0 +97,0 @@ return new web3_js_1.TransactionInstruction({ |
@@ -21,2 +21,4 @@ /// <reference types="node" /> | ||
counter: number; | ||
usageFee: number; | ||
feeAccountKey: PublicKey; | ||
data: Buffer | undefined; | ||
@@ -29,2 +31,4 @@ constructor(props: { | ||
counter: number; | ||
usageFee: number; | ||
feeAccountKey: Uint8Array; | ||
}); | ||
@@ -35,5 +39,5 @@ get coeffs(): number[]; | ||
kind: string; | ||
fields: string[][]; | ||
fields: (string | number[])[][]; | ||
}>; | ||
static retrieve(connection: Connection, modelAccountKey: PublicKey): Promise<ModelState>; | ||
} |
@@ -13,2 +13,3 @@ "use strict"; | ||
exports.ModelState = exports.ScoreState = void 0; | ||
const web3_js_1 = require("@solana/web3.js"); | ||
const borsh_1 = require("borsh"); | ||
@@ -51,2 +52,4 @@ // Score Data | ||
this.counter = props.counter; | ||
this.usageFee = props.usageFee; | ||
this.feeAccountKey = new web3_js_1.PublicKey(props.feeAccountKey); | ||
} | ||
@@ -84,2 +87,4 @@ get coeffs() { | ||
["counter", "u64"], | ||
["usageFee", "u64"], | ||
["feeAccountKey", [32]], | ||
], | ||
@@ -86,0 +91,0 @@ }, |
{ | ||
"name": "@iai-skunks/ml-registry-solana", | ||
"version": "0.3.0", | ||
"version": "0.4.0", | ||
"description": "SPL ML Model Registry/Inference JavaScript API", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -0,1 +1,12 @@ | ||
## v0.4.0 | ||
- added fee payments per prediction | ||
- added a service charge per fee when a model is used for prediction | ||
## v0.3.0 | ||
- added counter field to model data state | ||
## v0.2.0 | ||
- added finding for model parameters | ||
- updated the type for coeffs, intercept to string type for storage | ||
## v0.1.4 | ||
@@ -17,6 +28,2 @@ - actually build changes before publish | ||
- initial release of library | ||
- basic bindings to create models and use them for prediction | ||
## v0.2.0 | ||
- added finding for model parameters | ||
- updated the type for coeffs, intercept to string type for storage | ||
- basic bindings to create models and use them for prediction |
@@ -9,2 +9,4 @@ import { | ||
import { ModelState } from "../src/states"; | ||
import { Numberu32, Numberu64 } from "./custom_types"; | ||
@@ -26,6 +28,9 @@ import { createInstruction, createPredictInstruction } from "./instructions"; | ||
* @param coeffs The coefficients for the new GLM | ||
* @param space The space in bytes allocated to the account | ||
* @param usageFee The set usage fee per prediction with the model | ||
* @param feeAccountKey The account for to where fees should be deposited | ||
* @param intercept The intercept coeff for the GLM. If not specifed, default to 0 | ||
* @param lamports The budget to be set for the name account. If not specified, it'll be the minimum for rent exemption | ||
* @param space The space in bytes allocated to the account | ||
*/ | ||
export async function createModelRegistry( | ||
@@ -39,2 +44,4 @@ connection: Connection, | ||
space: number, | ||
usageFee: number, | ||
feeAccountKey: PublicKey, | ||
intercept?: number, | ||
@@ -79,3 +86,5 @@ lamports?: number | ||
new Numberu64(balance), | ||
new Numberu32(space) | ||
new Numberu32(space), | ||
new Numberu64(usageFee), | ||
feeAccountKey | ||
); | ||
@@ -96,2 +105,4 @@ | ||
* @param space The space in bytes allocated to the account | ||
* @param mlServiceKey The wallet to where all proportion of fees to service are deposited | ||
* @param mlServiceCharge The percentage cut the service earns per prediction fee | ||
* @param lamports The budget to be set for the name account. If not specified, it'll be the minimum for rent exemption | ||
@@ -107,2 +118,4 @@ */ | ||
space: number, | ||
mlServiceKey: PublicKey, | ||
mlServiceCharge: number, | ||
lamports?: number | ||
@@ -137,2 +150,5 @@ ): Promise<[TransactionInstruction, PublicKey]> { | ||
// getting fee_wallet (ML model owner wallet) | ||
const modelState = await ModelState.retrieve(connection, modelAccountPubkey); | ||
const instruction = createPredictInstruction( | ||
@@ -148,3 +164,6 @@ SystemProgram.programId, | ||
new Numberu64(balance), | ||
new Numberu32(space) | ||
new Numberu32(space), | ||
modelState.feeAccountKey, | ||
mlServiceKey, | ||
mlServiceCharge | ||
); | ||
@@ -151,0 +170,0 @@ |
@@ -15,3 +15,5 @@ import { PublicKey, TransactionInstruction } from "@solana/web3.js"; | ||
lamports: Numberu64, | ||
space: Numberu32 | ||
space: Numberu32, | ||
usageFee: Numberu64, | ||
feeAccountKey: PublicKey | ||
): TransactionInstruction { | ||
@@ -29,2 +31,4 @@ const buffers = [ | ||
space.toBuffer(), | ||
usageFee.toBuffer(), | ||
feeAccountKey.toBuffer(), | ||
]; | ||
@@ -69,3 +73,6 @@ | ||
lamports: Numberu64, | ||
space: Numberu32 | ||
space: Numberu32, | ||
feeAccountKey: PublicKey, | ||
mlServiceKey: PublicKey, | ||
mlServiceCharge: number | ||
): TransactionInstruction { | ||
@@ -82,2 +89,3 @@ const buffers = [ | ||
space.toBuffer(), | ||
new Float32(mlServiceCharge).toBuffer(), | ||
]; | ||
@@ -108,2 +116,12 @@ | ||
}, | ||
{ | ||
pubkey: feeAccountKey, | ||
isSigner: false, | ||
isWritable: true, | ||
}, | ||
{ | ||
pubkey: mlServiceKey, | ||
isSigner: false, | ||
isWritable: true, | ||
}, | ||
]; | ||
@@ -110,0 +128,0 @@ |
@@ -54,2 +54,4 @@ import { Connection, PublicKey } from "@solana/web3.js"; | ||
counter: number; | ||
usageFee: number; | ||
feeAccountKey: PublicKey; | ||
data: Buffer | undefined; | ||
@@ -63,2 +65,4 @@ | ||
counter: number; | ||
usageFee: number; | ||
feeAccountKey: Uint8Array; | ||
}) { | ||
@@ -70,2 +74,4 @@ this.coeffsStr = props.coeffsStr; | ||
this.counter = props.counter; | ||
this.usageFee = props.usageFee; | ||
this.feeAccountKey = new PublicKey(props.feeAccountKey); | ||
} | ||
@@ -92,2 +98,4 @@ | ||
["counter", "u64"], | ||
["usageFee", "u64"], | ||
["feeAccountKey", [32]], | ||
], | ||
@@ -94,0 +102,0 @@ }, |
@@ -9,2 +9,4 @@ import { | ||
import { PublicKey } from "@solana/web3.js"; | ||
async function main() { | ||
@@ -20,13 +22,35 @@ console.log("Testing ML Predict Smart Contract"); | ||
const indVars = [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0]; | ||
const usageFee = 50000000; | ||
const feeAccountKey = new PublicKey( | ||
"7TM1UycGuzs8GtYQEjaFTh5dEsfpNrH5J81KQkrMMSxX" | ||
); | ||
const mlServiceKey = new PublicKey( | ||
"AjXukoeGHeRacrmoV8hGFzYAqMh974uySrrji7Cid4jG" | ||
); | ||
const mlServiceCharge = 10.0; | ||
// meta data size | ||
let modelSpace = | ||
coeffs.join(" , ").toString().length + intercept.toString().length; | ||
let predictSpace = indVars.join(" , ").toString().length; | ||
coeffs.join(", ").toString().length + intercept.toString().length; | ||
let predictSpace = indVars.join(", ").toString().length; | ||
await createModel(coeffs, intercept, "normal", modelSpace); | ||
await predict(indVars, predictSpace); | ||
await createModel( | ||
coeffs, | ||
intercept, | ||
"normal", | ||
modelSpace, | ||
usageFee, | ||
feeAccountKey | ||
); | ||
await predict(indVars, predictSpace, mlServiceKey, mlServiceCharge); | ||
await createModel(coeffs, intercept, "logreg", modelSpace); | ||
await predict(indVars, predictSpace); | ||
await createModel( | ||
coeffs, | ||
intercept, | ||
"logreg", | ||
modelSpace, | ||
usageFee, | ||
feeAccountKey | ||
); | ||
await predict(indVars, predictSpace, mlServiceKey, mlServiceCharge); | ||
@@ -33,0 +57,0 @@ console.log("Success"); |
@@ -75,2 +75,4 @@ import path from "path"; | ||
space: number, | ||
usageFee: number, | ||
feeAccountKey: PublicKey, | ||
lamports?: number | ||
@@ -90,2 +92,4 @@ ): Promise<void> { | ||
space, | ||
usageFee, | ||
feeAccountKey, | ||
intercept, | ||
@@ -103,3 +107,3 @@ lamports | ||
// query prediction | ||
// query ml model parameters | ||
const modelState = await ModelState.retrieve(connection, modelAccountPubkey); | ||
@@ -109,2 +113,4 @@ console.log(`Model coefficients: ${modelState.coeffs}`); | ||
console.log(`Model counter: ${modelState.counter}`); | ||
console.log(`ML model usage fee: ${modelState.usageFee}`); | ||
console.log(`ML model owner key: ${modelState.feeAccountKey}`); | ||
} | ||
@@ -115,2 +121,4 @@ | ||
space: number, | ||
mlServiceKey: PublicKey, | ||
mlServiceCharge: number, | ||
lamports?: number | ||
@@ -129,2 +137,4 @@ ): Promise<void> { | ||
space, | ||
mlServiceKey, | ||
mlServiceCharge, | ||
lamports | ||
@@ -131,0 +141,0 @@ ); |
57290
1365