@coinbase/agentkit
Version:
Coinbase AgentKit core primitives
110 lines (107 loc) • 4.68 kB
JavaScript
;
/**
* Onramp Action Provider
*
* This file contains the implementation of the OnrampActionProvider,
* which provides actions for onramp operations.
*
* @module onramp
*/
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.onrampActionProvider = exports.OnrampActionProvider = void 0;
const zod_1 = require("zod");
const actionProvider_1 = require("../actionProvider");
const actionDecorator_1 = require("../actionDecorator");
const wallet_providers_1 = require("../../wallet-providers");
const schemas_1 = require("./schemas");
const utils_1 = require("./utils");
/**
* OnrampActionProvider provides actions for onramp operations.
*
* @description
* This provider is designed to work with EvmWalletProvider for blockchain interactions.
* It supports all evm networks.
*/
class OnrampActionProvider extends actionProvider_1.ActionProvider {
/**
* Constructor for the OnrampActionProvider.
*
* @param props - The props for the OnrampActionProvider
* @param props.projectId - The project ID for the OnrampActionProvider
*/
constructor(props) {
super("onramp", []);
this.projectId = props.projectId;
}
/**
* This action provides a link to buy more cryptocurrency (ETH, USDC, or BTC) using fiat currency (regular money like USD).
*
* @param walletProvider - The wallet provider instance for blockchain interactions
* @param _ - The arguments for the action (not used)
* @returns A promise that resolves to a string describing the action result
*/
async getOnrampBuyUrl(walletProvider, _ = {}) {
const networkId = walletProvider.getNetwork().networkId;
if (!networkId) {
throw new Error("Network ID is not set");
}
const network = (0, utils_1.convertNetworkIdToOnrampNetworkId)(networkId);
if (!network) {
throw new Error("Network ID is not supported. Make sure you are using a supported mainnet network.");
}
return (0, utils_1.getOnrampBuyUrl)({
projectId: this.projectId,
addresses: {
[walletProvider.getAddress()]: [network],
},
defaultNetwork: network,
});
}
/**
* Checks if this provider supports the given network.
*
* @param network - The network to check support for
* @returns True if the network is supported
*/
supportsNetwork(network) {
return Boolean(network.networkId &&
(0, utils_1.convertNetworkIdToOnrampNetworkId)(network.networkId) !== null &&
network.protocolFamily === "evm");
}
}
exports.OnrampActionProvider = OnrampActionProvider;
__decorate([
(0, actionDecorator_1.CreateAction)({
name: "get_onramp_buy_url",
description: `
Get a URL to purchase more cryptocurrency when funds are low. This action provides a link to buy more
cryptocurrency, defaulting to ETH, using fiat currency (regular money like USD).
Use this when:
- You detect that the wallet has insufficient funds for a transaction
- You need to guide the user to purchase more cryptocurrency
- The user asks how to buy more crypto
The URL will direct to a secure Coinbase-powered purchase interface.
`,
schema: schemas_1.GetOnrampBuyUrlActionSchema,
}),
__metadata("design:type", Function),
__metadata("design:paramtypes", [wallet_providers_1.EvmWalletProvider, void 0]),
__metadata("design:returntype", Promise)
], OnrampActionProvider.prototype, "getOnrampBuyUrl", null);
/**
* Factory function to create a new OnrampActionProvider instance.
*
* @param props - The props for the OnrampActionProvider
* @returns A new OnrampActionProvider instance
*/
const onrampActionProvider = (props) => new OnrampActionProvider(props);
exports.onrampActionProvider = onrampActionProvider;