Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(deals): support split deal rewards #1027

Merged
merged 5 commits into from
Sep 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"@fluencelabs/air-beautify-wasm": "0.3.9",
"@fluencelabs/aqua-api": "0.14.10",
"@fluencelabs/aqua-to-js": "0.3.13",
"@fluencelabs/deal-ts-clients": "0.18.7",
"@fluencelabs/deal-ts-clients": "0.19.0",
"@fluencelabs/fluence-network-environment": "1.2.2",
"@fluencelabs/js-client": "0.9.0",
"@fluencelabs/npm-aqua-compiler": "0.0.3",
Expand Down
6 changes: 4 additions & 2 deletions cli/src/commands/provider/deal-rewards-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ export default class DealRewardsInfo extends BaseCommand<
const rewardAmount = await deal.getRewardAmount(unitId);

commandObj.log(
`Deal reward amount: ${color.yellow(
await ptFormatWithSymbol(rewardAmount),
`Provider reward: ${color.yellow(
await ptFormatWithSymbol(rewardAmount.providerReward),
)}\n\nStaker reward: ${color.yellow(
await ptFormatWithSymbol(rewardAmount.stakerReward),
)}`,
);
}
Expand Down
78 changes: 61 additions & 17 deletions cli/src/commands/provider/deal-rewards-withdraw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

import { color } from "@oclif/color";

import { BaseCommand, baseFlags } from "../../baseCommand.js";
import { ptFormatWithSymbol } from "../../lib/chain/currencies.js";
import { commandObj } from "../../lib/commandObj.js";
Expand All @@ -26,11 +24,13 @@ import {
DEAL_IDS_FLAG_NAME,
PT_SYMBOL,
} from "../../lib/const.js";
import { getDealClient, sign } from "../../lib/dealClient.js";
import { getDealClient, getEventValue, sign } from "../../lib/dealClient.js";
import { commaSepStrToArr } from "../../lib/helpers/utils.js";
import { initCli } from "../../lib/lifeCycle.js";
import { input } from "../../lib/prompt.js";

const REWARD_WITHDRAWN_EVENT = "RewardWithdrawn";

export default class DealRewardsWithdraw extends BaseCommand<
typeof DealRewardsWithdraw
> {
Expand Down Expand Up @@ -63,28 +63,72 @@ export default class DealRewardsWithdraw extends BaseCommand<
const deal = dealClient.getDeal(dealId);
const workers = await deal.getWorkers();

const rewardSum = (
await Promise.all(
workers.map(({ onchainId }) => {
return deal.getRewardAmount(onchainId);
}),
)
).reduce((acc, reward) => {
return acc + reward;
}, 0n);
let providerRewards = 0n;
let stakerRewards = 0n;

for (const { onchainId } of workers) {
await sign({
title: `Withdraw rewards for compute unit ${onchainId}`,
const txReceipt = await sign({
title: `Withdraw rewards for worker ${onchainId}`,
method: deal.withdrawRewards,
args: [onchainId],
});

const providerReward = getEventValue({
txReceipt,
contract: deal,
eventName: REWARD_WITHDRAWN_EVENT,
value: "providerReward",
});

const stakerReward = getEventValue({
txReceipt,
contract: deal,
eventName: REWARD_WITHDRAWN_EVENT,
value: "stakerReward",
});

if (
typeof providerReward !== "bigint" ||
typeof stakerReward !== "bigint"
) {
throw new Error(
`Failed to get rewards amount from event ${REWARD_WITHDRAWN_EVENT}. Please make sure event signature is correct`,
);
}

providerRewards = providerRewards + providerReward;
stakerRewards = stakerRewards + stakerReward;
}

const allRewards = [
{ name: "Provider", val: providerRewards },
{ name: "Staker", val: stakerRewards },
].filter(({ val }) => {
return val > 0n;
});

const rewardsStr =
allRewards.length === 0
? "Rewards "
: `${(
await Promise.all(
allRewards.map(async ({ name, val }) => {
return `${name} rewards: ${await ptFormatWithSymbol(val)}`;
}),
)
).join("\n\n")}\n\n`;

const totalStr =
allRewards.length > 1
? `Total rewards: ${await ptFormatWithSymbol(
allRewards.reduce((acc, { val }) => {
return acc + val;
}, 0n),
)}\n\n`
: "";

commandObj.logToStderr(
`Reward ${color.yellow(
await ptFormatWithSymbol(rewardSum),
)} was withdrawn from the deal: ${dealId}`,
`\n${rewardsStr}${totalStr}were withdrawn from the deal: ${dealId}`,
);
}
}
Expand Down
6 changes: 3 additions & 3 deletions cli/src/versions.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"protocolVersion": 1,
"nox": "fluencelabs/nox:0.26.1",
"chain-rpc": "fluencelabs/chain-rpc:0.18.7",
"chain-deploy-script": "fluencelabs/chain-deploy-script:0.18.7",
"subgraph-deploy-script": "fluencelabs/subgraph-deploy-script:0.18.7",
"chain-rpc": "fluencelabs/chain-rpc:0.19.0",
"chain-deploy-script": "fluencelabs/chain-deploy-script:0.19.0",
"subgraph-deploy-script": "fluencelabs/subgraph-deploy-script:0.19.0",
"rust-toolchain": "nightly-2024-06-10",
"npm": {
"@fluencelabs/aqua-lib": "0.11.0",
Expand Down
2 changes: 2 additions & 0 deletions cli/test/helpers/sharedSteps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,8 @@ export async function deployDealAndWaitUntilDeployed(
},
RUN_DEPLOYED_SERVICES_TIMEOUT,
);

return dealId;
}

export async function createSpellAndAddToDeal(cwd: string, spellName: string) {
Expand Down
9 changes: 8 additions & 1 deletion cli/test/tests/deal/dealDeploy.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe("fluence deploy tests", () => {

await createSpellAndAddToDeal(cwd, NEW_SPELL_NAME);

await deployDealAndWaitUntilDeployed(cwd);
const dealId = await deployDealAndWaitUntilDeployed(cwd);

await waitUntilShowSubnetReturnsExpected(
cwd,
Expand All @@ -54,6 +54,13 @@ describe("fluence deploy tests", () => {
});

assertLogsAreValid(logs);

// works because the same account that deploys is also a provider
await fluence({
args: ["provider", "deal-rewards-withdraw"],
flags: { "deal-ids": dealId },
cwd,
});
},
);
});
10 changes: 5 additions & 5 deletions cli/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ __metadata:
"@fluencelabs/air-beautify-wasm": "npm:0.3.9"
"@fluencelabs/aqua-api": "npm:0.14.10"
"@fluencelabs/aqua-to-js": "npm:0.3.13"
"@fluencelabs/deal-ts-clients": "npm:0.18.7"
"@fluencelabs/deal-ts-clients": "npm:0.19.0"
"@fluencelabs/fluence-network-environment": "npm:1.2.2"
"@fluencelabs/js-client": "npm:0.9.0"
"@fluencelabs/npm-aqua-compiler": "npm:0.0.3"
Expand Down Expand Up @@ -509,9 +509,9 @@ __metadata:
languageName: unknown
linkType: soft

"@fluencelabs/deal-ts-clients@npm:0.18.7":
version: 0.18.7
resolution: "@fluencelabs/deal-ts-clients@npm:0.18.7"
"@fluencelabs/deal-ts-clients@npm:0.19.0":
version: 0.19.0
resolution: "@fluencelabs/deal-ts-clients@npm:0.19.0"
dependencies:
"@graphql-typed-document-node/core": "npm:^3.2.0"
debug: "npm:^4.3.4"
Expand All @@ -523,7 +523,7 @@ __metadata:
graphql-tag: "npm:^2.12.6"
ipfs-http-client: "npm:^60.0.1"
multiformats: "npm:^13.0.1"
checksum: 10c0/5c753da4e23902676e153e87a0584aff5fa07a9aadf86e355436e17c6cfad13bef1a7617c370666defce2ae2b9db0c160310eb16b101627185a1227cf3153ad3
checksum: 10c0/b2002613cb3fe3be36ee149779294990ea21526954f9ceb0d2e2f62e67b41b7790c14f5f13a94e2da1036e1de0717914158756626eb217e9c753dde899a7c1c6
languageName: node
linkType: hard

Expand Down
Loading