Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeffrieh committed May 1, 2024
1 parent 8df5d79 commit c8911f4
Show file tree
Hide file tree
Showing 13 changed files with 214 additions and 107 deletions.
Binary file modified bun.lockb
Binary file not shown.
123 changes: 62 additions & 61 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,63 +1,64 @@
{
"name": "@effectai/effect-js",
"version": "2.0.0",
"description": "Effect Network Javscript/Typescript SDK (for [https://effect.network](https://effect.network))",
"main": "./dist/exports/index.js",
"module": "./dist/exports/index.js",
"browser": "dist/exports/index.js",
"types": "./dist/exports/index.d.ts",
"files": ["dist"],
"scripts": {
"test": "bun --env-file=.env test",
"test:watch": "bun --env-file=.env test --watch",
"test:coverage": "bun --env-file=.env test --coverage",
"test:mainnet": "bun --env-file=.env.mainnet test",
"dev": "tsc -w",
"build": "tsc --module es2020",
"lint": "bun biome lint --apply .",
"format": "biome format --write .",
"check": "bun biome check --apply .",
"changeset": "changeset",
"changeset:public": "bun scripts/updateVersion.ts && bun build && changeset publish",
"changeset:version": "changeset version && bun install --frozen-lockfile && bun scripts/updateVersion.ts",
"docs:dev": "cd docs && bun run dev",
"docs:build": "cd docs && bun run build",
"docs:preview": "cd docs && bun run preview"
},
"repository": {
"type": "git",
"url": "git+https://github.com/effectai/effect-js.git"
},
"keywords": ["efx", "AI", "blockchain", "microtasks"],
"author": {
"name": "Effect-AI",
"url": "https://effect.network",
"email": "[email protected]"
},
"publishConfig": {
"access": "public"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/effectai/effect-js/issues"
},
"homepage": "https://github.com/effectai/effect-js#readme",
"devDependencies": {
"@biomejs/biome": "1.7.0",
"@changesets/changelog-github": "^0.4.5",
"@changesets/cli": "^2.23.2",
"@size-limit/preset-big-lib": "^11.1.2",
"@greymass/abi2core": "^2.0.1",
"typescript": "^5.4.3"
},
"dependencies": {
"@wharfkit/antelope": "^1.0.7",
"@wharfkit/session": "^1.2.8",
"@wharfkit/wallet-plugin-privatekey": "^1.0.0",
"ajv": "^8.12.0",
"atomicassets": "^1.5.1",
"idb-keyval": "^6.2.1",
"node-localstorage": "^3.0.5"
},
"trustedDependencies": ["@biomejs/biome"]
"name": "@effectai/effect-js",
"version": "2.0.0",
"description": "Effect Network Javscript/Typescript SDK (for [https://effect.network](https://effect.network))",
"main": "./dist/exports/index.js",
"module": "./dist/exports/index.js",
"browser": "dist/exports/index.js",
"types": "./dist/exports/index.d.ts",
"files": ["dist"],
"scripts": {
"test": "bun --env-file=.env test",
"test:watch": "bun --env-file=.env test --watch",
"test:coverage": "bun --env-file=.env test --coverage",
"test:mainnet": "bun --env-file=.env.mainnet test",
"dev": "tsc -w",
"build": "tsc --module es2020",
"lint": "bun biome lint --apply .",
"format": "biome format --write .",
"check": "bun biome check --apply .",
"changeset": "changeset",
"changeset:public": "bun scripts/updateVersion.ts && bun build && changeset publish",
"changeset:version": "changeset version && bun install --frozen-lockfile && bun scripts/updateVersion.ts",
"docs:dev": "cd docs && bun run dev",
"docs:build": "cd docs && bun run build",
"docs:preview": "cd docs && bun run preview"
},
"repository": {
"type": "git",
"url": "git+https://github.com/effectai/effect-js.git"
},
"keywords": ["efx", "AI", "blockchain", "microtasks"],
"author": {
"name": "Effect-AI",
"url": "https://effect.network",
"email": "[email protected]"
},
"publishConfig": {
"access": "public"
},
"license": "MIT",
"bugs": {
"url": "https://github.com/effectai/effect-js/issues"
},
"homepage": "https://github.com/effectai/effect-js#readme",
"devDependencies": {
"@biomejs/biome": "1.7.0",
"@changesets/changelog-github": "^0.4.5",
"@changesets/cli": "^2.23.2",
"@greymass/abi2core": "^2.0.1",
"@size-limit/preset-big-lib": "^11.1.2",
"@types/bun": "^1.1.1",
"typescript": "^5.4.3"
},
"dependencies": {
"@wharfkit/antelope": "^1.0.7",
"@wharfkit/session": "^1.2.8",
"@wharfkit/wallet-plugin-privatekey": "^1.0.0",
"ajv": "^8.12.0",
"atomicassets": "^1.5.1",
"idb-keyval": "^6.2.1",
"node-localstorage": "^3.0.5"
},
"trustedDependencies": ["@biomejs/biome"]
}
36 changes: 25 additions & 11 deletions src/actions/tasks/batch/createBatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,26 +81,34 @@ export const publishBatchAction = ({

export type CreateBatchArgs = {
client: Client;
batch: Omit<Mkbatch, "content">;
data: Record<string, unknown>;
campaignId: number;
repetitions: number;
reward: number;
taskData: Record<string, unknown>;
};

export const createBatch = async ({ client, batch, data }: CreateBatchArgs) => {
export const createBatch = async ({
client,
campaignId,
repetitions,
reward,
taskData,
}: CreateBatchArgs) => {
try {
if (!client.session) {
throw new SessionNotFoundError("Session is required for this method.");
}

const forceSettings = await getForceSettings({ client });
const { transact, vAccount } = client.session;
const { transact, vAccount, actor } = client.session;

if (!vAccount) {
throw new Error("No vAccountId found");
throw new Error("vAccount not found");
}

const campaign = await getCampaignById({ client, id: batch.campaign_id });
const assetQuantity = Asset.from(campaign.reward.quantity);
const batchPrice = assetQuantity.value * batch.repetitions;
const campaign = await getCampaignById({ client, id: campaignId });
const assetQuantity = Asset.from(reward, " EFX");
const batchPrice = assetQuantity.value * repetitions;

// Check if the user has enough funds to pay for the batch
// if (Asset.from(vacc.balance.quantity).value < batchPrice) {
Expand All @@ -114,12 +122,18 @@ export const createBatch = async ({ client, batch, data }: CreateBatchArgs) => {
// }

const newBatchId = campaign.num_batches + 1;
const hash = await uploadIpfsResource({ client, data });
const hash = await uploadIpfsResource({ client, data: taskData });

const makeBatch = createBatchAction({
client,
forceSettings,
batch,
batch: {
id: newBatchId,
payer: actor.toString(),
campaign_id: campaignId,
repetitions,
content: { field_0: 0, field_1: hash },
},
hash,
});

Expand All @@ -133,7 +147,7 @@ export const createBatch = async ({ client, batch, data }: CreateBatchArgs) => {
const publishBatch = publishBatchAction({
client,
batchId: newBatchId,
numTasks: batch.repetitions,
numTasks: repetitions,
});

// TODO Check if batchId is correct.
Expand Down
77 changes: 51 additions & 26 deletions src/actions/tasks/campaigns/createCampaign.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,81 @@
import type {
Content,
Mkcampaign,
} from "../../../@generated/types/effecttasks2";
import type { AnyAction } from "@wharfkit/antelope";
import type { Client } from "../../../client";
import { SessionNotFoundError } from "../../../errors";
import { useEFXContracts } from "../../../utils/state";
import { uploadIpfsResource } from "../../ipfs/uploadIpfsResource";
import type { CampaignInfo } from "./getCampaigns";
import type { Mkcampaign, Quali } from "../../../@generated/types/effecttasks2";

export type CreateCampaignActionArgs = {
client: Client;
campaign: Mkcampaign;
};

export const createCampaignAction = ({
client,
campaign,
}: CreateCampaignActionArgs): AnyAction => {
if (!client.session) {
throw new SessionNotFoundError("Session is required for this method.");
}

const { actor } = client.session;
const { tasks } = useEFXContracts(client);

return {
account: tasks,
name: "mkcampaign",
authorization: [
{
actor,
permission: "active",
},
],
data: campaign,
};
};

export type CreateCampaignArgs = {
client: Client;
campaign: Omit<Mkcampaign, "content">;
data: CampaignInfo;
campaign: CampaignInfo;
reward: number;
maxTaskTime: number;
qualitications?: Quali[];
};

export const createCampaign = async ({
client,
campaign,
data,
reward,
maxTaskTime,
qualitications,
}: CreateCampaignArgs) => {
if (!data) {
throw new Error("Data is required for this method.");
}

if (!client.session) {
throw new SessionNotFoundError("Session is required for this method.");
}

const { transact, actor, authorization } = client.session;
const { tasks, token } = useEFXContracts(client);
const { transact } = client.session;
const { token } = useEFXContracts(client);

try {
// Upload Campaign data to IPFS
const hash = await uploadIpfsResource({ client, data });
const hash = await uploadIpfsResource({ client, data: campaign });

const response = await transact({
action: {
account: tasks,
name: "mkcampaign",
authorization,
data: {
owner: actor,
content: { field_0: 0, field_1: hash },
max_task_time: campaign.max_task_time,
action: createCampaignAction({
client,
campaign: {
max_task_time: maxTaskTime,
reward: {
quantity: campaign.reward.quantity,
quantity: `${reward.toFixed(4)} EFX`, // `1.0000 EFX`
contract: token,
},
qualis: campaign.qualis ?? [],
payer: actor,
payer: client.session.actor.toString(),
content: { field_0: maxTaskTime, field_1: hash },
qualis: qualitications ?? [],
owner: ["name", client.session.actor.toString()],
},
},
}),
});

return response;
Expand Down
25 changes: 25 additions & 0 deletions src/actions/token/getBalance.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { describe, test, expect } from "bun:test";
import { getPrice } from "./getPrice";
import { getBalance } from "./getBalance";
import { swap, type SwapArgs } from "./swap";
import { createClient } from "../../client";
import { testClientSession } from "../../testHelper";
import { eos, jungle4 } from "../../exports";
import { Name } from "@wharfkit/antelope";

describe("getBalance", async () => {
test("getBalance() should retrieve balance from user on mainnet", async () => {
const client = createClient({ network: jungle4 });
const actor = Name.from("forcedev1234");
const balance = await getBalance({ client, actor });
expect(balance).toBeDefined();
expect(balance.toString()).toBeDefined();
expect(balance.toString()).toContain("EFX");
});

test("getBalance() should throw Error retrieving balance from unknown user.", async () => {
const client = createClient({ network: eos });
const actor = Name.from("cryptonode99");
expect(async () => await getBalance({ client, actor })).toThrowError();
});
});
Empty file.
8 changes: 8 additions & 0 deletions src/actions/token/getPrice.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
describe("getPrice", async () => {
test("getPrice() should retrieve price on mainnet", async () => {
const client = createClient({ network: eos });
const price = await getPrice();
expect(price).toBeDefined();
expect(price).toBeNumber();
});
});
29 changes: 29 additions & 0 deletions src/actions/token/swap.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
describe("buildSwapAction", async () => {
test.todo("buildSwapAction() should return a swap action object.");
});

describe("Swap", async () => {
// Use Mainnet

test("swap() should throw an error when Session is not set on Client.", async () => {
const swapArgs: SwapArgs = {
client: createClient({ network: jungle4 }),
amount: 1,
direction: "UsdtToEfx",
};

expect(async () => await swap(swapArgs)).toThrow(
new Error("Error swapping: Error: Session is required for this method."),
);
});

test("swap() should fail when amount is 0", async () => {
const swapArgs: SwapArgs = {
client: await testClientSession({ testEnvNetwork: jungle4 }),
amount: 0,
direction: "UsdtToEfx",
};

expect(async () => await swap(swapArgs)).toThrow();
});
});
7 changes: 5 additions & 2 deletions src/actions/vaccount/createAccount.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { Name } from "@wharfkit/antelope";

import { expect, test, describe, mock } from "bun:test";
import { destructureEnv, testClientSession } from "../../testHelper";

import { createClient } from "../../client";
import { createVAccount } from "./createAccount";
import { Name } from "@wharfkit/antelope";

import { jungle4 } from "../../exports";
import { destructureEnv, testClientSession } from "../../../test/src/utils.js";

describe("Create Virtual account", () => {
const testEnvNetwork = jungle4;
Expand Down
2 changes: 1 addition & 1 deletion src/actions/vaccount/deposit.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { describe, test, expect } from "bun:test";
import { deposit } from "./deposit";
import { destructureEnv, testClientSession } from "../../testHelper";
import { eos, jungle4 } from "../../exports";
import { getOrCreateVAccount } from "./getOrCreate";
import { Name } from "@wharfkit/antelope";
import { testClientSession, destructureEnv } from "../../../test/src/utils";

describe("deposit", async () => {
test.todo("Should throw an error when Session is not set on Client.", () => {
Expand Down
Loading

0 comments on commit c8911f4

Please sign in to comment.