Skip to content

Commit

Permalink
feat: enhance blockchain token interactions with dynamic decimal hand…
Browse files Browse the repository at this point in the history
…ling

- Add dynamic token decimal retrieval for accurate amount parsing
- Update genTxDataForAllowance and placeBet to use token-specific decimals
- Replace hardcoded parseEther/formatEther with flexible parseUnits/formatUnits
- Introduce getDecimals helper function for retrieving token decimal precision
  • Loading branch information
nicky-ru committed Feb 1, 2025
1 parent 8edade6 commit 46237f5
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 7 deletions.
9 changes: 7 additions & 2 deletions packages/plugin-depin/src/actions/prepareBet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
parseTagContent,
} from "@elizaos/core";

import { genTxDataForAllowance } from "../helpers/blockchain";
import { genTxDataForAllowance, getNetwork } from "../helpers/blockchain";

interface ApprovalParams {
amount: number;
Expand Down Expand Up @@ -66,8 +66,13 @@ export const prepareBet: Action = {
return false;
}

const txData = await genTxDataForAllowance(
runtime,
getNetwork(),
betParams.amount
);
const betWithTx = {
txData: genTxDataForAllowance(betParams.amount),
txData,
address: betParams.address,
amount: betParams.amount,
predictionId: betParams.predictionId,
Expand Down
38 changes: 33 additions & 5 deletions packages/plugin-depin/src/helpers/blockchain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { elizaLogger, IAgentRuntime } from "@elizaos/core";
import { initWalletProvider, type SupportedChain } from "@elizaos/plugin-evm";
import { encodeFunctionData, formatEther, parseEther } from "viem";
import { encodeFunctionData, parseUnits, formatUnits } from "viem";
import { iotex, iotexTestnet } from "viem/chains";
import * as viemChains from "viem/chains";

Expand Down Expand Up @@ -116,6 +116,20 @@ export const createPrediction = async (
}
};

const getDecimals = async (
runtime: IAgentRuntime,
tokenAddress: `0x${string}`,
network: SupportedChain
) => {
const walletProvider = await initWalletProvider(runtime);
const publicClient = walletProvider.getPublicClient(network);
return publicClient.readContract({
address: tokenAddress,
abi: erc20Abi,
functionName: "decimals",
});
};

export const placeBet = async (
runtime: IAgentRuntime,
predictionId: number,
Expand All @@ -128,7 +142,12 @@ export const placeBet = async (
const publicClient = walletProvider.getPublicClient(network);
const account = walletProvider.getAddress();

const amountInWei = parseEther(amount);
const decimals = await getDecimals(
runtime,
process.env.PREDICTION_TOKEN as `0x${string}`,
network
);
const amountInWei = parseUnits(amount, decimals);

const betAmount = await getBetAmount(
amountInWei,
Expand Down Expand Up @@ -175,16 +194,25 @@ export const placeBet = async (
hash,
predictionId,
bettor,
betAmount: formatEther(betAmount),
betAmount: formatUnits(betAmount, decimals),
outcome,
};
} else {
throw new Error("Bet placement failed");
}
};

export const genTxDataForAllowance = (amount: number) => {
const amountInWei = parseEther(amount.toString());
export const genTxDataForAllowance = async (
runtime: IAgentRuntime,
network: SupportedChain,
amount: number
) => {
const decimals = await getDecimals(
runtime,
process.env.PREDICTION_TOKEN as `0x${string}`,
network
);
const amountInWei = parseUnits(amount.toString(), decimals);

return encodeFunctionData({
abi: erc20Abi,
Expand Down

0 comments on commit 46237f5

Please sign in to comment.