-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #935 from golemfactory/feature/JST-92/market-agree…
…ment-events feat(market): implemented agreement and offer events
- Loading branch information
Showing
57 changed files
with
1,628 additions
and
1,011 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { MarketOrderSpec, GolemNetwork, OfferProposal } from "@golem-sdk/golem-js"; | ||
import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; | ||
|
||
/** | ||
* Example demonstrating how to write a selector which choose the best provider based on scores provided as object: [providerName]: score | ||
* A higher score rewards the provider. | ||
*/ | ||
const scores = { | ||
"provider-1": 100, | ||
"golem-provider": 50, | ||
"super-provider": 25, | ||
}; | ||
|
||
const bestProviderSelector = (scores: { [providerName: string]: number }) => (proposals: OfferProposal[]) => { | ||
proposals.sort((a, b) => ((scores?.[a.provider.name] || 0) >= (scores?.[b.provider.name] || 0) ? -1 : 1)); | ||
return proposals[0]; | ||
}; | ||
|
||
const order: MarketOrderSpec = { | ||
demand: { | ||
workload: { imageTag: "golem/alpine:latest" }, | ||
}, | ||
market: { | ||
rentHours: 0.5, | ||
pricing: { | ||
model: "linear", | ||
maxStartPrice: 0.5, | ||
maxCpuPerHourPrice: 1.0, | ||
maxEnvPerHourPrice: 0.5, | ||
}, | ||
proposalSelector: bestProviderSelector(scores), | ||
}, | ||
}; | ||
|
||
(async () => { | ||
const glm = new GolemNetwork({ | ||
logger: pinoPrettyLogger({ | ||
level: "info", | ||
}), | ||
}); | ||
|
||
try { | ||
await glm.connect(); | ||
const lease = await glm.oneOf(order); | ||
await lease | ||
.getExeUnit() | ||
.then((exe) => exe.run(`echo [provider:${exe.provider.name}] Hello, Golem! 👋`)) | ||
.then((res) => console.log(res.stdout)); | ||
await lease.finalize(); | ||
} catch (err) { | ||
console.error("Failed to run the example", err); | ||
} finally { | ||
await glm.disconnect(); | ||
} | ||
})().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
/** | ||
* This example showcases how users can listen to various events exposed from golem-js | ||
*/ | ||
import { GolemNetwork } from "@golem-sdk/golem-js"; | ||
import { pinoPrettyLogger } from "@golem-sdk/pino-logger"; | ||
|
||
(async () => { | ||
const glm = new GolemNetwork({ | ||
logger: pinoPrettyLogger({ | ||
level: "info", | ||
}), | ||
payment: { | ||
driver: "erc20", | ||
network: "holesky", | ||
}, | ||
}); | ||
|
||
try { | ||
await glm.connect(); | ||
|
||
glm.market.events.on("agreementApproved", (event) => { | ||
console.log("Agreement '%s' approved at %s", event.agreement.id, event.timestamp); | ||
}); | ||
|
||
glm.market.events.on("agreementTerminated", (event) => { | ||
console.log( | ||
"Agreement '%s' terminated by '%s' with reason '%s'", | ||
event.agreement.id, | ||
event.terminatedBy, | ||
event.reason, | ||
); | ||
}); | ||
|
||
glm.market.events.on("offerCounterProposalRejected", (event) => { | ||
console.warn("Proposal rejected by provider", event); | ||
}); | ||
|
||
const lease = await glm.oneOf({ | ||
demand: { | ||
workload: { imageTag: "golem/alpine:latest" }, | ||
}, | ||
market: { | ||
rentHours: 0.5, | ||
pricing: { | ||
model: "linear", | ||
maxStartPrice: 0.5, | ||
maxCpuPerHourPrice: 1.0, | ||
maxEnvPerHourPrice: 0.5, | ||
}, | ||
}, | ||
}); | ||
|
||
await lease | ||
.getExeUnit() | ||
.then((exe) => exe.run("echo Hello, Golem! 👋")) | ||
.then((res) => console.log(res.stdout)); | ||
|
||
await lease.finalize(); | ||
} catch (err) { | ||
console.error("Failed to run the example", err); | ||
} finally { | ||
await glm.disconnect(); | ||
} | ||
})().catch(console.error); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.