Skip to content

Commit

Permalink
Merge branch 'develop' into fix-plugin-asterai-clean
Browse files Browse the repository at this point in the history
  • Loading branch information
AIFlowML authored Jan 31, 2025
2 parents e4c150c + 32cb314 commit 3bd6b5c
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 33 deletions.
41 changes: 41 additions & 0 deletions packages/plugin-avail/biome.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
"$schema": "https://biomejs.dev/schemas/1.5.3/schema.json",
"organizeImports": {
"enabled": false
},
"linter": {
"enabled": true,
"rules": {
"recommended": true,
"correctness": {
"noUnusedVariables": "error"
},
"suspicious": {
"noExplicitAny": "error"
},
"style": {
"useConst": "error",
"useImportType": "off"
}
}
},
"formatter": {
"enabled": true,
"indentStyle": "space",
"indentWidth": 4,
"lineWidth": 100
},
"javascript": {
"formatter": {
"quoteStyle": "single",
"trailingCommas": "es5"
}
},
"files": {
"ignore": [
"dist/**/*",
"extra/**/*",
"node_modules/**/*"
]
}
}
9 changes: 7 additions & 2 deletions packages/plugin-avail/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,18 @@
"tsup": "8.3.5",
"@polkadot/types": "^10.11.3",
"vitest": "^2.1.8",
"@vitest/coverage-v8": "^2.1.8"
"@vitest/coverage-v8": "^2.1.8",
"@biomejs/biome": "1.9.4"
},
"scripts": {
"build": "tsup --format esm --dts",
"dev": "tsup --format esm --dts --watch",
"test": "vitest run",
"test:watch": "vitest watch",
"test:coverage": "vitest run --coverage"
"test:coverage": "vitest run --coverage",
"lint": "biome lint .",
"lint:fix": "biome check --apply .",
"format": "biome format .",
"format:fix": "biome format --write ."
}
}
25 changes: 13 additions & 12 deletions packages/plugin-avail/src/actions/submitData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
getKeyringFromSeed,
} from "avail-js-sdk";
import type { H256 } from "@polkadot/types/interfaces/runtime";
import { ISubmittableResult } from "@polkadot/types/types";
import type { ISubmittableResult } from "@polkadot/types/types";

export interface DataContent extends Content {
data: string;
Expand Down Expand Up @@ -79,15 +79,16 @@ export default {
elizaLogger.log("Starting SUBMIT_DATA handler...");

// Initialize or update state
if (!state) {
state = (await runtime.composeState(message)) as State;
let currentState = state;
if (!currentState) {
currentState = (await runtime.composeState(message)) as State;
} else {
state = await runtime.updateRecentMessageState(state);
currentState = await runtime.updateRecentMessageState(currentState);
}

// Compose transfer context
const submitDataContext = composeContext({
state,
state: currentState,
template: submitDataTemplate,
});

Expand All @@ -112,7 +113,8 @@ export default {
// }
if (content.data != null) {
try {
const SEED = runtime.getSetting("AVAIL_SEED")!;
const SEED = runtime.getSetting("AVAIL_SEED");
if (!SEED) throw new Error("AVAIL_SEED not set");
//const ACCOUNT = runtime.getSetting("AVAIL_ADDRESS")!;
const ENDPOINT = runtime.getSetting("AVAIL_RPC_URL");
const APP_ID = runtime.getSetting("AVAIL_APP_ID");
Expand All @@ -134,7 +136,7 @@ export default {
`);

//submit data
const txResult:ISubmittableResult = await new Promise(
const txResult:ISubmittableResult = await new Promise<ISubmittableResult>(
(res) => {
api.tx.dataAvailability
.submitData(data)
Expand All @@ -146,7 +148,7 @@ export default {
`Tx status: ${result.status}`
);
if (result.isFinalized || result.isError) {
res(result as any);
res(result);
}
}
);
Expand All @@ -155,12 +157,12 @@ export default {

// Rejected Transaction handling
if (txResult.isError) {
console.log(`Transaction was not executed`);
console.log('Transaction was not executed');
}

// Failed Transaction handling
const error = txResult.dispatchError;
if (error != undefined) {
if (error !== undefined) {
if (error.isModule) {
const decoded = api.registry.findMetaError(
error.asModule
Expand All @@ -173,8 +175,7 @@ export default {
}

elizaLogger.success(
"Data submitted successfully! tx: \n " +
`Tx Hash: ${txResult.txHash as H256}, Block Hash: ${txResult.status.asFinalized as H256}`
`Data submitted successfully! tx: \nTx Hash: ${txResult.txHash as H256}, Block Hash: ${txResult.status.asFinalized as H256}`
);
if (callback) {
callback({
Expand Down
35 changes: 16 additions & 19 deletions packages/plugin-avail/src/actions/transfer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,16 @@ export default {
elizaLogger.log("Starting SEND_TOKEN handler...");

// Initialize or update state
if (!state) {
state = (await runtime.composeState(message)) as State;
let currentState = state;
if (!currentState) {
currentState = (await runtime.composeState(message)) as State;
} else {
state = await runtime.updateRecentMessageState(state);
currentState = await runtime.updateRecentMessageState(currentState);
}

// Compose transfer context
const transferContext = composeContext({
state,
state: currentState,
template: transferTemplate,
});

Expand All @@ -126,7 +127,8 @@ export default {

if (content.amount != null && content.recipient != null) {
try {
const SEED = runtime.getSetting("AVAIL_SEED")!;
const SEED = runtime.getSetting("AVAIL_SEED");
if (!SEED) throw new Error("AVAIL_SEED not set");
//const PUBLIC_KEY = runtime.getSetting("AVAIL_ADDRESS")!;
const ENDPOINT = runtime.getSetting("AVAIL_RPC_URL");

Expand All @@ -136,15 +138,13 @@ export default {
const decimals = getDecimals(api);
const amount = formatNumberToBalance(content.amount, decimals);

const oldBalance: any = await api.query.system.account(
content.recipient
);
const oldBalance = await api.query.system.account(content.recipient);
elizaLogger.log(
`Balance before the transfer call: ${oldBalance["data"]["free"].toHuman()}`
`Balance before the transfer call: ${oldBalance.toString()}`
);

// Transaction call
const txResult:ISubmittableResult = await new Promise(
const txResult:ISubmittableResult = await new Promise<ISubmittableResult>(
(res) => {
api.tx.balances
.transferKeepAlive(content.recipient, amount)
Expand All @@ -156,7 +156,7 @@ export default {
`Tx status: ${result.status}`
);
if (result.isFinalized || result.isError) {
res(result as any);
res(result);
}
}
);
Expand All @@ -166,8 +166,8 @@ export default {
// Error handling
const error = txResult.dispatchError;
if (txResult.isError) {
elizaLogger.log(`Transaction was not executed`);
} else if (error != undefined) {
elizaLogger.log("Transaction was not executed");
} else if (error !== undefined) {
if (error.isModule) {
const decoded = api.registry.findMetaError(
error.asModule
Expand All @@ -181,16 +181,13 @@ export default {
}
}

const newBalance: any = await api.query.system.account(
content.recipient
);
const newBalance = await api.query.system.account(content.recipient);
elizaLogger.log(
`Balance after the transfer call: ${newBalance["data"]["free"].toHuman()}`
`Balance after the transfer call: ${newBalance.toString()}`
);

elizaLogger.success(
"Transfer completed successfully! tx: \n " +
`Tx Hash: ${txResult.txHash as H256}, Block Hash: ${txResult.status.asFinalized as H256}`
`Transfer completed successfully! tx: \nTx Hash: ${txResult.txHash as H256}, Block Hash: ${txResult.status.asFinalized as H256}`
);
if (callback) {
callback({
Expand Down

0 comments on commit 3bd6b5c

Please sign in to comment.