forked from elizaOS/eliza
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mateusz Marcinkowski <mateusz.marcinkowski@arianelabs.com>
- Loading branch information
1 parent
1d9fce6
commit 0935695
Showing
6 changed files
with
209 additions
and
0 deletions.
There are no files selected for viewing
117 changes: 117 additions & 0 deletions
117
packages/plugin-hedera/src/actions/claim-airdrop/claim-airdrop.ts
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,117 @@ | ||
import { | ||
Action, | ||
composeContext, | ||
elizaLogger, | ||
generateObjectDeprecated, | ||
HandlerCallback, | ||
IAgentRuntime, | ||
Memory, | ||
ModelClass, | ||
State, | ||
} from "@elizaos/core"; | ||
import { HederaProvider } from "../../providers/client"; | ||
import { claimAirdropTemplate } from "../../templates"; | ||
import { claimAirdropParamsSchema } from "./schema.ts"; | ||
import { ClaimAirdropService } from "./services/claim-airdrop-service.ts"; | ||
|
||
export const claimAirdropAction: Action = { | ||
name: "HEDERA_CLAIM_AIRDROP", | ||
description: "Claim available pending token airdrop", | ||
handler: async ( | ||
runtime: IAgentRuntime, | ||
_message: Memory, | ||
state: State, | ||
_options: { [key: string]: unknown }, | ||
_callback?: HandlerCallback | ||
) => { | ||
console.log("invoke"); | ||
|
||
const claimAirdropContext = composeContext({ | ||
state: state, | ||
template: claimAirdropTemplate, | ||
templatingEngine: "handlebars", | ||
}); | ||
|
||
console.log("test"); | ||
|
||
const claimAirdropContent = await generateObjectDeprecated({ | ||
runtime: runtime, | ||
context: claimAirdropContext, | ||
modelClass: ModelClass.SMALL, | ||
}); | ||
|
||
try { | ||
console.log(claimAirdropContent); | ||
|
||
const claimAirdropData = | ||
claimAirdropParamsSchema.parse(claimAirdropContent); | ||
|
||
console.log(claimAirdropData); | ||
|
||
const accountId = runtime.getSetting("HEDERA_ACCOUNT_ID"); | ||
|
||
const hederaProvider = new HederaProvider(runtime); | ||
const action = new ClaimAirdropService(hederaProvider); | ||
|
||
await action.execute(claimAirdropData, accountId); | ||
|
||
await _callback({ | ||
text: `Successfully claimed airdrop for token ${claimAirdropData.tokenId}`, | ||
}); | ||
|
||
return true; | ||
} catch (error) { | ||
elizaLogger.error("Error during claiming airdrop:", error); | ||
|
||
if (_callback) { | ||
await _callback({ | ||
text: `Error during claiming airdrop: ${error.message}`, | ||
content: { error: error.message }, | ||
}); | ||
} | ||
return false; | ||
} | ||
}, | ||
validate: async (runtime: IAgentRuntime) => { | ||
const privateKey = runtime.getSetting("HEDERA_PRIVATE_KEY"); | ||
const accountAddress = runtime.getSetting("HEDERA_ACCOUNT_ID"); | ||
const selectedNetworkType = runtime.getSetting("HEDERA_NETWORK_TYPE"); | ||
|
||
return !!(privateKey && accountAddress && selectedNetworkType); | ||
}, | ||
examples: [ | ||
[ | ||
{ | ||
user: "{{user1}}", | ||
content: { | ||
text: "Claim airdrop (1) 5 Tokens ({{0.0.5445766}}) from {{0.0.5393076}}", | ||
action: "HEDERA_CLAIM_AIRDROP", | ||
}, | ||
}, | ||
{ | ||
user: "{{user2}}", | ||
content: { | ||
text: "", | ||
action: "HEDERA_CLAIM_AIRDROP", | ||
}, | ||
}, | ||
], | ||
[ | ||
{ | ||
user: "{{user1}}", | ||
content: { | ||
text: "Claim airdrop (2) 50 Tokens ({{0.0.5447843}}) from {{0.0.5393076}}", | ||
action: "HEDERA_CLAIM_AIRDROP", | ||
}, | ||
}, | ||
{ | ||
user: "{{user2}}", | ||
content: { | ||
text: "", | ||
action: "HEDERA_CLAIM_AIRDROP", | ||
}, | ||
}, | ||
], | ||
], | ||
similes: ["CLAIM_AIRDROP", "CLAIM_TOKEN_AIRDROP", "CLAIM_TOKEN"], | ||
}; |
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,6 @@ | ||
import { z } from "zod"; | ||
|
||
export const claimAirdropParamsSchema = z.object({ | ||
senderId: z.string(), | ||
tokenId: z.string(), | ||
}); |
35 changes: 35 additions & 0 deletions
35
packages/plugin-hedera/src/actions/claim-airdrop/services/claim-airdrop-service.ts
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,35 @@ | ||
import { HederaProvider } from "../../../providers/client"; | ||
import { AccountId, PendingAirdropId, TokenId } from "@hashgraph/sdk"; | ||
import { ClaimAirdropData } from "../types.ts"; | ||
|
||
export class ClaimAirdropService { | ||
constructor(private hederaProvider: HederaProvider) {} | ||
|
||
async execute(params: ClaimAirdropData, accountId: string): Promise<void> { | ||
if (!params.tokenId) { | ||
throw new Error("No tokenId provided"); | ||
} | ||
|
||
if (!params.senderId) { | ||
throw new Error("No senderId provided"); | ||
} | ||
|
||
if (!accountId) { | ||
throw new Error("No accountId provided"); | ||
} | ||
|
||
const tokenId = TokenId.fromString(params.tokenId); | ||
const senderId = AccountId.fromString(params.senderId); | ||
const receiverId = AccountId.fromString(accountId); | ||
|
||
const pendingAirdrop = new PendingAirdropId({ | ||
senderId, | ||
tokenId, | ||
receiverId, | ||
}); | ||
|
||
const agentKit = this.hederaProvider.getHederaAgentKit(); | ||
|
||
return agentKit.claimAirdrop(pendingAirdrop); | ||
} | ||
} |
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,4 @@ | ||
import { claimAirdropParamsSchema } from "./schema.ts"; | ||
import { z } from "zod"; | ||
|
||
export type ClaimAirdropData = z.infer<typeof claimAirdropParamsSchema>; |
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