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

Add btc support #360

Closed
wants to merge 3 commits into from
Closed
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
41 changes: 36 additions & 5 deletions packages/client-coinbase/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import express from "express";
import {
blockExplorerBaseAddressUrl,
blockExplorerBaseTxUrl,
supportedTickers,
WebhookEvent,
} from "./types";
import { Coinbase, Wallet } from "@coinbase/coinbase-sdk";
Expand All @@ -27,11 +28,12 @@ import {
type CoinbaseWallet,
} from "@elizaos/plugin-coinbase";
import { tokenSwap } from "@elizaos/plugin-0x";
import { createWalletClient, erc20Abi, http, publicActions } from "viem";
import { createWalletClient, erc20Abi, http, publicActions, formatUnits } from "viem";
import { getPriceInquiry } from "../../plugin-0x/src/actions/getIndicativePrice";
import { getQuoteObj } from "../../plugin-0x/src/actions/getQuote";
import { privateKeyToAccount } from "viem/accounts";
import { base } from "viem/chains";
import { TOKENS } from "../../plugin-0x/src/utils";

export type WalletType =
| "short_term_trading"
Expand Down Expand Up @@ -275,7 +277,7 @@ Generate only the tweet text, no commentary or markdown.`;

private async handleWebhookEvent(event: WebhookEvent) {
// for now just support ETH
if (event.ticker !== "ETH" && event.ticker !== "WETH") {
if (!supportedTickers.includes(event.ticker)) {
elizaLogger.info("Unsupported ticker:", event.ticker);
return;
}
Expand Down Expand Up @@ -520,12 +522,11 @@ export async function getTotalBalanceUSD(
elizaLogger.error("priceInquiry is null");
return 0;
}
// get latest quote
const quote = await getQuoteObj(runtime, priceInquiry, publicKey);
const ethBalanceUSD = Number(quote.buyAmount) / 1e6;
const usdcBalanceBaseUnits = await readContractWrapper(
runtime,
"0x833589fcd6edb6e08f4c7c32d4f71b54bda02913",
TOKENS.USDC.address as `0x${string}`,
"balanceOf",
{
account: publicKey,
Expand All @@ -534,7 +535,37 @@ export async function getTotalBalanceUSD(
erc20Abi
);
const usdcBalance = Number(usdcBalanceBaseUnits) / 1e6;
return ethBalanceUSD + usdcBalance;
// get cbbtc balance
const cbbtcBalanceBaseUnits = await readContractWrapper(
runtime,
TOKENS.cbBTC.address as `0x${string}`,
"balanceOf",
{
account: publicKey,
},
"base-mainnet",
erc20Abi
);
elizaLogger.info(`cbbtcBalanceBaseUnits ${cbbtcBalanceBaseUnits}`);
const cbbtcBalance = Number(cbbtcBalanceBaseUnits) / 1e18;
elizaLogger.info(`cbbtcBalance ${cbbtcBalance}`);
const cbbtcPriceInquiry = await getPriceInquiry(
runtime,
"CBBTC",
cbbtcBalance,
"USDC",
"base"
);
if (cbbtcPriceInquiry == null) {
elizaLogger.error("cbbtcPriceInquiry is null");
return 0;
}
const cbbtcQuote = await getQuoteObj(runtime, cbbtcPriceInquiry, publicKey);
const cbbtcBalanceUSD = Number(cbbtcQuote.buyAmount) / 1000000;
elizaLogger.info(`ethBalanceUSD ${ethBalanceUSD}`);
elizaLogger.info(`usdcBalance ${usdcBalance}`);
elizaLogger.info(`cbbtcBalanceUSD ${cbbtcBalanceUSD}`);
return ethBalanceUSD + usdcBalance + cbbtcBalanceUSD;
}

export const pnlProvider: Provider = {
Expand Down
2 changes: 1 addition & 1 deletion packages/client-coinbase/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ export const blockExplorerBaseTxUrl = (txHash: string) =>
export const blockExplorerBaseAddressUrl = (address: string) =>
`https://basescan.org/address/${address}`;

export const supportedTickers = ["ETH", "WETH"];
export const supportedTickers = ["ETH", "WETH", "BTC", "WBTC", "CBBTC"];
2 changes: 1 addition & 1 deletion packages/plugin-0x/src/actions/getIndicativePrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ export const getPriceInquiry = async (
buyTokenSymbol: string,
chain: string
): Promise<PriceInquiry | null> => {
if (sellAmount < 0.000000000000000001) {
if (sellAmount < 0.000000000000000000000001) {
elizaLogger.error(`sellAmount ${sellAmount} is too small`);
return null;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/plugin-0x/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export const TOKENS = {
name: "Coinbase Wrapped BTC",
symbol: "cbBTC",
decimals: 8,
address: "0xcbB7C0000aB88B473b1f5aFd9ef808440eed33Bf",
address: "0xcbb7c0000ab88b473b1f5afd9ef808440eed33bf",
type: "ERC20",
logoURI: "https://raw.githubusercontent.com/trustwallet/assets/master/blockchains/base/info/logo.png"
},
Expand Down
Loading
Loading