Install any skill in seconds. Free to start, no credit card required.
Get Started Free →This skill generates production-ready code for the **complete on-chain swap flow** using the OKX DEX Aggregator:
SkillScore50/100
Test results 50
3/3 safety checks passed
5%50%of test cases passed +45 pts
Passed 11 of 22 test cases with the skill, and 1 of 22 without it.
10 fixed1 passed either way11 still failing0 broke
Adds every file the skill has, pinned so you can update it. No account needed.
pip install "decimalai>=0.15"
decimalai skills add okx-dex-swap
claude --plugin-dir "$(git rev-parse --show-toplevel 2>/dev/null || pwd)/.agents"The same install from Python, for a notebook or a setup script.
pip install "decimalai>=0.15"from decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
print("Added. Start Claude Code with the command below.")claude --plugin-dir "$(git rev-parse --show-toplevel 2>/dev/null || pwd)/.agents"Type these in a Claude Code session. No account or API key. It writes the skill and any files it bundles, and asks first when its safety rating isn't Passed.
/plugin marketplace add decimal-labs/decimalai-claude-plugins
/plugin install decimalai@decimalai/decimalai:install okx-dex-swapThe plugin needs python3 and curl on your machine.
No Python or account. Claude Code picks it up in its next session.
curl -fsSL --create-dirs -o .claude/skills/okx-dex-swap/SKILL.md https://app.decimal.ai/s/okx-dex-swap/SKILL.md.claude/skills/okx-dex-swap/SKILL.mdThis gets SKILL.md only. The skill's 1 other file is not included.
agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[claude-agent-sdk]"
decimalai skills add okx-dex-swapimport asyncio
from claude_agent_sdk import ClaudeAgentOptions
from decimalai.claude_agent_sdk import agent_config
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
async def main():
question = "Hello!"
async with support.run(question) as run: # one user message, one run
async for message in run.query(options=ClaudeAgentOptions(max_turns=6)):
print(message)
# In a notebook or async code: await main()
asyncio.run(main())Prefer its own loader? ClaudeAgentOptions(plugins=[skills_plugin()], skills="all") reads the same folder. Use one or the other: a skill delivered both ways reaches the prompt twice.
The same install from Python, for a notebook or a setup script. agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[claude-agent-sdk]"from decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
import asyncio
from claude_agent_sdk import ClaudeAgentOptions
from decimalai.claude_agent_sdk import agent_config
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
async def main():
question = "Hello!"
async with support.run(question) as run: # one user message, one run
async for message in run.query(options=ClaudeAgentOptions(max_turns=6)):
print(message)
# In a notebook or async code: await main()
asyncio.run(main())Prefer its own loader? ClaudeAgentOptions(plugins=[skills_plugin()], skills="all") reads the same folder. Use one or the other: a skill delivered both ways reaches the prompt twice.
No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdimport asyncio
from pathlib import Path
from claude_agent_sdk import ClaudeAgentOptions, query
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
options = ClaudeAgentOptions(system_prompt=f"You are a helpful agent.\n\n{skill}", max_turns=6)
async def main():
async for message in query(prompt="Hello!", options=options):
print(message)
# In a notebook or async code: await main()
asyncio.run(main())This gets SKILL.md only. The skill's 1 other file is not included.
agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[openai-agents]"
decimalai skills add okx-dex-swapfrom agents import Agent, Runner
from decimalai.openai_agents import agent_config
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
agent = Agent(
name=support.name,
instructions=support.instructions, # your prompt, then the skill list
tools=[*support.tools], # your tools, then load_skill
)
# In a notebook or async code: result = await Runner.run(agent, "Hello!")
result = Runner.run_sync(agent, "Hello!")Using SandboxAgent? capabilities=[Shell(), decimalai.openai_agents.sandbox_skills()] points its own loader at the same folder.
The same install from Python, for a notebook or a setup script. agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[openai-agents]"from decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
from agents import Agent, Runner
from decimalai.openai_agents import agent_config
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
agent = Agent(
name=support.name,
instructions=support.instructions, # your prompt, then the skill list
tools=[*support.tools], # your tools, then load_skill
)
# In a notebook or async code: result = await Runner.run(agent, "Hello!")
result = Runner.run_sync(agent, "Hello!")Using SandboxAgent? capabilities=[Shell(), decimalai.openai_agents.sandbox_skills()] points its own loader at the same folder.
No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
from agents import Agent, Runner
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
agent = Agent(name="support", instructions=f"You are a helpful agent.\n\n{skill}")
# In a notebook or async code: result = await Runner.run(agent, "Hello!")
result = Runner.run_sync(agent, "Hello!")This gets SKILL.md only. The skill's 1 other file is not included.
agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[openai]"
decimalai skills add okx-dex-swapimport json
from openai import OpenAI
from decimalai.openai import agent_config
client = OpenAI()
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
def lookup_order(order_id: str) -> str:
return f"Order {order_id} shipped yesterday."
# Your tools, as Chat Completions takes them, and what answers each.
TOOLS = [{"type": "function", "function": {
"name": "lookup_order",
"description": "Look up the status of an order.",
"parameters": {"type": "object", "properties": {"order_id": {"type": "string"}},
"required": ["order_id"]},
}}]
FUNCTIONS = {"lookup_order": lookup_order}
question = "Where is order A1001?"
with support.run(question) as run: # one user message, one run
# run.system is your prompt, then the skill list.
messages = [{"role": "system", "content": run.system}, run.message]
while True:
reply = client.chat.completions.create(
model="gpt-5.4-mini",
messages=messages,
tools=[*TOOLS, *run.tools], # your tools, then load_skill
).choices[0].message
messages.append(reply)
if not reply.tool_calls:
break
for call in reply.tool_calls:
# load_skill is answered by the run; your own tools by FUNCTIONS.
function = FUNCTIONS.get(call.function.name)
messages.append(run.tool_result(call) or {
"role": "tool", "tool_call_id": call.id,
"content": function(**json.loads(call.function.arguments)) if function
else f"There is no tool named {call.function.name!r}.",
})
print(reply.content)
# The Responses API takes run.response_tools instead of run.tools.The same install from Python, for a notebook or a setup script. agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[openai]"from decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
import json
from openai import OpenAI
from decimalai.openai import agent_config
client = OpenAI()
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
def lookup_order(order_id: str) -> str:
return f"Order {order_id} shipped yesterday."
# Your tools, as Chat Completions takes them, and what answers each.
TOOLS = [{"type": "function", "function": {
"name": "lookup_order",
"description": "Look up the status of an order.",
"parameters": {"type": "object", "properties": {"order_id": {"type": "string"}},
"required": ["order_id"]},
}}]
FUNCTIONS = {"lookup_order": lookup_order}
question = "Where is order A1001?"
with support.run(question) as run: # one user message, one run
# run.system is your prompt, then the skill list.
messages = [{"role": "system", "content": run.system}, run.message]
while True:
reply = client.chat.completions.create(
model="gpt-5.4-mini",
messages=messages,
tools=[*TOOLS, *run.tools], # your tools, then load_skill
).choices[0].message
messages.append(reply)
if not reply.tool_calls:
break
for call in reply.tool_calls:
# load_skill is answered by the run; your own tools by FUNCTIONS.
function = FUNCTIONS.get(call.function.name)
messages.append(run.tool_result(call) or {
"role": "tool", "tool_call_id": call.id,
"content": function(**json.loads(call.function.arguments)) if function
else f"There is no tool named {call.function.name!r}.",
})
print(reply.content)
# The Responses API takes run.response_tools instead of run.tools.No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
from openai import OpenAI
client = OpenAI()
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
reply = client.chat.completions.create(
model="gpt-5.4-mini",
messages=[
{"role": "system", "content": f"You are a helpful agent.\n\n{skill}"},
{"role": "user", "content": "Hello!"},
],
).choices[0].messageThis gets SKILL.md only. The skill's 1 other file is not included.
agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[anthropic]"
decimalai skills add okx-dex-swapimport anthropic
from decimalai.anthropic import agent_config
client = anthropic.Anthropic()
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
question = "Hello!"
with support.run(question) as run: # one user message, one run
answer = client.beta.messages.tool_runner(
model="claude-haiku-5-5",
max_tokens=4096,
system=run.system, # your prompt, then the skill list
tools=[*run.tools], # your tools, then load_skill
messages=[{"role": "user", "content": question}],
).until_done()The same install from Python, for a notebook or a setup script. agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[anthropic]"from decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
import anthropic
from decimalai.anthropic import agent_config
client = anthropic.Anthropic()
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
question = "Hello!"
with support.run(question) as run: # one user message, one run
answer = client.beta.messages.tool_runner(
model="claude-haiku-5-5",
max_tokens=4096,
system=run.system, # your prompt, then the skill list
tools=[*run.tools], # your tools, then load_skill
messages=[{"role": "user", "content": question}],
).until_done()No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
import anthropic
client = anthropic.Anthropic()
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
reply = client.messages.create(
model="claude-haiku-5-5",
max_tokens=4096,
system=f"You are a helpful agent.\n\n{skill}",
messages=[{"role": "user", "content": "Hello!"}],
)This gets SKILL.md only. The skill's 1 other file is not included.
agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[google-genai]"
decimalai skills add okx-dex-swapfrom google import genai
from google.genai import types
from decimalai.google_genai import agent_config
client = genai.Client()
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
def lookup_order(order_id: str) -> str:
return f"Order {order_id} shipped yesterday."
# Your functions, as declarations for the model, and what answers each.
TOOLS = [{"function_declarations": [{
"name": "lookup_order",
"description": "Look up the status of an order.",
"parameters_json_schema": {"type": "object", "properties": {"order_id": {"type": "string"}},
"required": ["order_id"]},
}]}]
FUNCTIONS = {"lookup_order": lookup_order}
question = "Where is order A1001?"
with support.run(question) as run: # one user message, one run
config = types.GenerateContentConfig(
system_instruction=run.system, # your prompt, then the skill list
tools=[*TOOLS, *run.tool_params], # your functions, then load_skill
)
contents = [{"role": "user", "parts": [{"text": run.message}]}]
while True:
response = client.models.generate_content(
model="gemini-3.5-flash", contents=contents, config=config,
)
if not response.function_calls:
break
contents.append(response.candidates[0].content)
parts = []
for call in response.function_calls:
# load_skill is answered by the run; your own functions by FUNCTIONS.
# A loop of your own rather than automatic function calling, which
# raises KeyError when the model asks for a function you did not pass.
function = FUNCTIONS.get(call.name)
parts.append(run.tool_result(call) or {"function_response": {
"name": call.name, "id": call.id,
"response": {"result": function(**(call.args or {})) if function
else f"There is no function named {call.name!r}."},
}})
contents.append({"role": "user", "parts": parts})
print(response.text)The same install from Python, for a notebook or a setup script. agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[google-genai]"from decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
from google import genai
from google.genai import types
from decimalai.google_genai import agent_config
client = genai.Client()
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
def lookup_order(order_id: str) -> str:
return f"Order {order_id} shipped yesterday."
# Your functions, as declarations for the model, and what answers each.
TOOLS = [{"function_declarations": [{
"name": "lookup_order",
"description": "Look up the status of an order.",
"parameters_json_schema": {"type": "object", "properties": {"order_id": {"type": "string"}},
"required": ["order_id"]},
}]}]
FUNCTIONS = {"lookup_order": lookup_order}
question = "Where is order A1001?"
with support.run(question) as run: # one user message, one run
config = types.GenerateContentConfig(
system_instruction=run.system, # your prompt, then the skill list
tools=[*TOOLS, *run.tool_params], # your functions, then load_skill
)
contents = [{"role": "user", "parts": [{"text": run.message}]}]
while True:
response = client.models.generate_content(
model="gemini-3.5-flash", contents=contents, config=config,
)
if not response.function_calls:
break
contents.append(response.candidates[0].content)
parts = []
for call in response.function_calls:
# load_skill is answered by the run; your own functions by FUNCTIONS.
# A loop of your own rather than automatic function calling, which
# raises KeyError when the model asks for a function you did not pass.
function = FUNCTIONS.get(call.name)
parts.append(run.tool_result(call) or {"function_response": {
"name": call.name, "id": call.id,
"response": {"result": function(**(call.args or {})) if function
else f"There is no function named {call.name!r}."},
}})
contents.append({"role": "user", "parts": parts})
print(response.text)No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
from google import genai
from google.genai import types
client = genai.Client()
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
response = client.models.generate_content(
model="gemini-3.5-flash",
contents="Hello!",
config=types.GenerateContentConfig(system_instruction=f"You are a helpful agent.\n\n{skill}"),
)This gets SKILL.md only. The skill's 1 other file is not included.
agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[langchain]" langchain-openai
decimalai skills add okx-dex-swapfrom langchain.agents import create_agent
from decimalai.langchain import agent_config
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
agent = create_agent(
"openai:gpt-5.4-mini",
tools=[], # your tools; the middleware adds load_skill
middleware=[support.middleware], # your prompt, the skill list and load_skill
)
result = agent.invoke({"messages": [{"role": "user", "content": "Hello!"}]})The same install from Python, for a notebook or a setup script. agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[langchain]" langchain-openaifrom decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
from langchain.agents import create_agent
from decimalai.langchain import agent_config
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
agent = create_agent(
"openai:gpt-5.4-mini",
tools=[], # your tools; the middleware adds load_skill
middleware=[support.middleware], # your prompt, the skill list and load_skill
)
result = agent.invoke({"messages": [{"role": "user", "content": "Hello!"}]})No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
from langchain.agents import create_agent
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
agent = create_agent("openai:gpt-5.4-mini", tools=[], system_prompt=f"You are a helpful agent.\n\n{skill}")
result = agent.invoke({"messages": [{"role": "user", "content": "Hello!"}]})This gets SKILL.md only. The skill's 1 other file is not included.
CrewAI's own skill loader reads the installed folder. No DecimalAI account needed.
pip install "decimalai>=0.16" "crewai>=1.15.27"
decimalai skills add okx-dex-swapimport decimalai.crewai
from crewai import Agent, Crew, Task
agent = Agent(
role="Support", goal="Help the customer", backstory="A support agent.",
llm=llm, # your own CrewAI LLM
skills=decimalai.crewai.skills(), # the installed folder
tools=decimalai.crewai.skill_tools(), # reads a skill's files, runs its scripts
)
task = Task(description="Hello!", expected_output="an answer", agent=agent)
Crew(agents=[agent], tasks=[task]).kickoff()The same install from Python, for a notebook or a setup script. CrewAI's own skill loader reads the installed folder. No DecimalAI account needed.
pip install "decimalai>=0.16" "crewai>=1.15.27"from decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
import decimalai.crewai
from crewai import Agent, Crew, Task
agent = Agent(
role="Support", goal="Help the customer", backstory="A support agent.",
llm=llm, # your own CrewAI LLM
skills=decimalai.crewai.skills(), # the installed folder
tools=decimalai.crewai.skill_tools(), # reads a skill's files, runs its scripts
)
task = Task(description="Hello!", expected_output="an answer", agent=agent)
Crew(agents=[agent], tasks=[task]).kickoff()No DecimalAI package. CrewAI's own loader reads the folder, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
from crewai import Agent, Crew, Task
agent = Agent(
role="Support", goal="Help the customer", backstory="A support agent.",
llm=llm, # your own CrewAI LLM
skills=[Path(".agents/skills")],
)
task = Task(description="Hello!", expected_output="an answer", agent=agent)
Crew(agents=[agent], tasks=[task]).kickoff()This gets SKILL.md only. The skill's 1 other file is not included.
agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[adk]"
decimalai skills add okx-dex-swapfrom decimalai.adk import DecimalAIPlugin, agent_config
from google.adk.agents import LlmAgent
from google.adk.apps import App
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
agent = LlmAgent(
name="support", # ADK needs a Python identifier
model="gemini-3.5-flash",
instruction=support.instruction, # your prompt, then the skill list
tools=[*support.tools], # your tools, then load_skill
)
# DecimalAIPlugin makes each invocation of runners built from this App one trace.
app = App(name="support", root_agent=agent, plugins=[DecimalAIPlugin()])Prefer ADK's own loader? SkillToolset(skills=decimalai.adk.skills(), ...) reads the same folder. Use one or the other: a skill delivered both ways reaches the prompt twice.
The same install from Python, for a notebook or a setup script. agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[adk]"from decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
from decimalai.adk import DecimalAIPlugin, agent_config
from google.adk.agents import LlmAgent
from google.adk.apps import App
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
agent = LlmAgent(
name="support", # ADK needs a Python identifier
model="gemini-3.5-flash",
instruction=support.instruction, # your prompt, then the skill list
tools=[*support.tools], # your tools, then load_skill
)
# DecimalAIPlugin makes each invocation of runners built from this App one trace.
app = App(name="support", root_agent=agent, plugins=[DecimalAIPlugin()])Prefer ADK's own loader? SkillToolset(skills=decimalai.adk.skills(), ...) reads the same folder. Use one or the other: a skill delivered both ways reaches the prompt twice.
No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
from google.adk.agents import LlmAgent
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
agent = LlmAgent(
name="support",
model="gemini-3.5-flash",
instruction=lambda context: f"You are a helpful agent.\n\n{skill}",
)This gets SKILL.md only. The skill's 1 other file is not included.
agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[pydantic-ai]"
decimalai skills add okx-dex-swapfrom pydantic_ai import Agent
from decimalai.pydantic_ai import agent_config
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
agent = Agent(
"openai:gpt-4o-mini",
name=support.name,
tools=[], # your tools; the capability adds load_skill
capabilities=[support], # your prompt, the skill list and load_skill
)
# In a notebook or async code: result = await agent.run("Hello!")
result = agent.run_sync("Hello!")On the Pydantic AI harness, decimalai.pydantic_ai_harness.skills(project) uses its own loader instead.
The same install from Python, for a notebook or a setup script. agent_config() hands your agent the skill. With no DecimalAI key it reads your project's files and sends nothing. Needs SDK 0.27 or newer.
pip install "decimalai[pydantic-ai]"from decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
from pydantic_ai import Agent
from decimalai.pydantic_ai import agent_config
# With no DECIMAL_API_KEY this reads the skills your project lists, from disk.
support = agent_config("support", fallback="You are a helpful agent.")
agent = Agent(
"openai:gpt-4o-mini",
name=support.name,
tools=[], # your tools; the capability adds load_skill
capabilities=[support], # your prompt, the skill list and load_skill
)
# In a notebook or async code: result = await agent.run("Hello!")
result = agent.run_sync("Hello!")On the Pydantic AI harness, decimalai.pydantic_ai_harness.skills(project) uses its own loader instead.
No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
from pydantic_ai import Agent
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
agent = Agent("openai:gpt-4o-mini", instructions=f"You are a helpful agent.\n\n{skill}")
# In a notebook or async code: result = await agent.run("Hello!")
result = agent.run_sync("Hello!")This gets SKILL.md only. The skill's 1 other file is not included.
Deep Agents' own skill loader reads the installed folder. No DecimalAI account needed.
pip install "decimalai>=0.16" deepagents
decimalai skills add okx-dex-swapimport decimalai.deepagents
from deepagents import create_deep_agent
agent = create_deep_agent(model=model, **decimalai.deepagents.skills())
agent.invoke({"messages": [{"role": "user", "content": "Hello!"}]})The default backend reads files but has no shell. To run a skill's scripts, pass skills(backend=LocalShellBackend(...)).
The same install from Python, for a notebook or a setup script. Deep Agents' own skill loader reads the installed folder. No DecimalAI account needed.
pip install "decimalai>=0.16" deepagentsfrom decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
import decimalai.deepagents
from deepagents import create_deep_agent
agent = create_deep_agent(model=model, **decimalai.deepagents.skills())
agent.invoke({"messages": [{"role": "user", "content": "Hello!"}]})The default backend reads files but has no shell. To run a skill's scripts, pass skills(backend=LocalShellBackend(...)).
No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
from deepagents import create_deep_agent
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
agent = create_deep_agent(model=model, system_prompt=f"You are a helpful agent.\n\n{skill}")
agent.invoke({"messages": [{"role": "user", "content": "Hello!"}]})This gets SKILL.md only. The skill's 1 other file is not included.
Its own skill loader reads the installed folder. No DecimalAI account needed.
pip install "decimalai>=0.16" agent-framework
decimalai skills add okx-dex-swapimport asyncio
from agent_framework import Agent
from decimalai.agent_framework import skills
agent = Agent(client=client, instructions="You are a helpful agent.", context_providers=[skills()])
asyncio.run(agent.run("Hello!"))It caches skills by default, so restart the process after decimalai skills update.
The same install from Python, for a notebook or a setup script. Its own skill loader reads the installed folder. No DecimalAI account needed.
pip install "decimalai>=0.16" agent-frameworkfrom decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
import asyncio
from agent_framework import Agent
from decimalai.agent_framework import skills
agent = Agent(client=client, instructions="You are a helpful agent.", context_providers=[skills()])
asyncio.run(agent.run("Hello!"))It caches skills by default, so restart the process after decimalai skills update.
No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdimport asyncio
from pathlib import Path
from agent_framework import Agent
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
agent = Agent(client=client, instructions=f"You are a helpful agent.\n\n{skill}")
asyncio.run(agent.run("Hello!"))This gets SKILL.md only. The skill's 1 other file is not included.
Strands' own skill loader reads the installed folder. No DecimalAI account needed.
pip install "decimalai>=0.16" strands-agents
decimalai skills add okx-dex-swapfrom decimalai.strands import skills
from strands import Agent
from strands.vended_tools import file_editor, shell
agent = Agent(
model=model, # always pass one: the default is Amazon Bedrock
system_prompt="You are a helpful agent.",
tools=[file_editor, shell],
plugins=[skills()],
)
agent("Hello!")Strands loads an agent's skills once, so build the agent again after decimalai skills update.
The same install from Python, for a notebook or a setup script. Strands' own skill loader reads the installed folder. No DecimalAI account needed.
pip install "decimalai>=0.16" strands-agentsfrom decimalai import skillpkg
# Writes .agents/skills and .decimal/skills.toml. Safe to run twice.
result = skillpkg.add("okx-dex-swap")
if result.exit_code:
raise SystemExit(result.problems)
from decimalai.strands import skills
from strands import Agent
from strands.vended_tools import file_editor, shell
agent = Agent(
model=model, # always pass one: the default is Amazon Bedrock
system_prompt="You are a helpful agent.",
tools=[file_editor, shell],
plugins=[skills()],
)
agent("Hello!")Strands loads an agent's skills once, so build the agent again after decimalai skills update.
No DecimalAI package. The whole skill goes in every prompt, and nothing pins or updates it.
.agents/skills/okx-dex-swap/SKILL.mdfrom pathlib import Path
from strands import Agent
skill = Path(".agents/skills/okx-dex-swap/SKILL.md").read_text()
agent = Agent(model=model, system_prompt=f"You are a helpful agent.\n\n{skill}")
agent("Hello!")This gets SKILL.md only. The skill's 1 other file is not included.
Adds every file the skill has, pinned so you can update it. Cursor, Copilot, Cline and Warp read this folder by themselves.
pip install "decimalai>=0.15"
decimalai skills add okx-dex-swapNo Python or account. Cursor, Copilot, Cline and Warp read this folder by themselves.
curl -fsSL --create-dirs -o .agents/skills/okx-dex-swap/SKILL.md https://app.decimal.ai/s/okx-dex-swap/SKILL.md.agents/skills/okx-dex-swap/SKILL.mdThis gets SKILL.md only. The skill's 1 other file is not included.
Give its text to your model as instructions. No account or package needed.
This gets SKILL.md only. The skill's 1 other file is not included.
Raw files: /s/okx-dex-swap/SKILL.md · /s/okx-dex-swap.json
Fetch the file and give its text to your model as instructions. No account or package needed.
curl -fsSL https://app.decimal.ai/s/okx-dex-swap/SKILL.mdcurl -fsSL https://app.decimal.ai/s/okx-dex-swap.jsonThis gets SKILL.md only. The skill's 1 other file is not included.
You don't need a DecimalAI account.
Saved prompts and skills, pinned versions, and traces of this skill in your runs. DecimalAI does not run your agent.
Claude Code reads files from your project, so DecimalAI config does not apply there.
On Claude Agent SDK: save the skill to an agent here, set DECIMAL_API_KEY, and the same code uses it with no command to run. Changes you save reach the next run with no redeploy.
On OpenAI Agents: save the skill to an agent here, set DECIMAL_API_KEY, and the same code uses it with no command to run. Changes you save reach the next run with no redeploy.
On OpenAI SDK: save the skill to an agent here, set DECIMAL_API_KEY, and the same code uses it with no command to run. Changes you save reach the next run with no redeploy.
On Anthropic: save the skill to an agent here, set DECIMAL_API_KEY, and the same code uses it with no command to run. Changes you save reach the next run with no redeploy.
On Gemini API: save the skill to an agent here, set DECIMAL_API_KEY, and the same code uses it with no command to run. Changes you save reach the next run with no redeploy.
On LangChain: save the skill to an agent here, set DECIMAL_API_KEY, and the same code uses it with no command to run. Changes you save reach the next run with no redeploy.
On CrewAI: set DECIMAL_API_KEY and add decimalai.init(crewai=True) to record runs and skill use. The skill version stays in your repo.
On Google ADK: save the skill to an agent here, set DECIMAL_API_KEY, and the same code uses it with no command to run. Changes you save reach the next run with no redeploy.
On Pydantic AI: save the skill to an agent here, set DECIMAL_API_KEY, and the same code uses it with no command to run. Changes you save reach the next run with no redeploy.
On Deep Agents: set DECIMAL_API_KEY and add decimalai.init(langchain=True) to record runs and skill use. The skill version stays in your repo.
On Microsoft Agent Framework: set DECIMAL_API_KEY and add decimalai.init(otel=True, agent_name="support") to record runs and skill use. The skill version stays in your repo.
On Strands: set DECIMAL_API_KEY and add decimalai.init(otel=True, agent_name="support") to record runs and skill use. The skill version stays in your repo.
Editors read files from your project, so DecimalAI config does not apply there.
Anywhere else: record a use with decimalai.log_skill_activation() inside a trace.
This skill generates production-ready code for the complete on-chain swap flow using the OKX DEX Aggregator:
┌─────────┐ ┌──────────┐ ┌──────────┐ ┌───────────┐
│ Quote │ ──▶ │ Swap │ ──▶ │ Sign │ ──▶ │ Broadcast │
│ (optional│ │ API Call │ │ Tx │ │ to Chain │
│ preview)│ │ │ │ │ │ │
└─────────┘ └──────────┘ └──────────┘ └───────────┘
Two API endpoints involved:
| Step | Endpoint | Method | Purpose |
|---|---|---|---|
| Swap | /api/v6/dex/aggregator/swap | GET | Get transaction calldata for the swap |
| Broadcast | /api/v6/dex/pre-transaction/broadcast-transaction | POST | Submit signed transaction to the chain |
Key features:
OKX_ACCESS_KEY — API keyOKX_SECRET_KEY — Secret key for HMAC signingOKX_PASSPHRASE — Account passphrasefromToken and native token for gasrequests, web3 (for EVM signing), solders / solana-py (for Solana)axios, ethers (for EVM signing), @solana/web3.js (for Solana)Endpoint:
GET https://web3.okx.com/api/v6/dex/aggregator/swap
Required parameters:
| Parameter | Type | Required | Description |
|---|---|---|---|
chainIndex | String | Yes | Chain ID (e.g., 1 = Ethereum, 501 = Solana) |
amount | String | Yes | Amount in raw units with decimals (e.g., 1000000 for 1 USDT) |
swapMode | String | Yes | exactIn (default) or exactOut |
fromTokenAddress | String | Yes | Sell token contract address |
toTokenAddress | String | Yes | Buy token contract address |
slippagePercent | String | Yes | Slippage tolerance (e.g., 0.5 = 0.5%). EVM: 0-100, Solana: 0 to <100 |
userWalletAddress | String | Yes | User's wallet address that will sign and send the tx |
Important optional parameters:
| Parameter | Type | Description |
|---|---|---|
approveTransaction | Boolean | Set true to get ERC-20 approval calldata in response |
approveAmount | String | Custom approval amount (raw units). If omitted, approves exact swap amount |
swapReceiverAddress | String | Recipient address if different from sender |
feePercent | String | Commission fee %. Max 3% (EVM) / 10% (Solana) |
fromTokenReferrerWalletAddress | String | Wallet to receive fromToken commission |
toTokenReferrerWalletAddress | String | Wallet to receive toToken commission |
autoSlippage | Boolean | Auto-calculate optimal slippage (overrides slippagePercent) |
maxAutoslippagePercent | String | Cap for auto-slippage |
priceImpactProtectionPercent | String | Max price impact allowed (0-100, default 90) |
gasLevel | String | slow, average (default), or fast |
gaslimit | String | Custom gas limit in wei (EVM only) |
dexIds | String | Restrict to specific DEX IDs (comma-separated) |
excludeDexIds | String | Exclude specific DEX IDs (comma-separated) |
directRoute | Boolean | Single-pool routing only (Solana only) |
disableRFQ | String | Disable time-sensitive RFQ liquidity sources |
callDataMemo | String | Custom 64-byte hex data to include on-chain |
Solana-specific parameters:
| Parameter | Type | Description |
|---|---|---|
computeUnitPrice | String | Priority fee (like gasPrice on EVM) |
computeUnitLimit | String | Compute budget (like gasLimit on EVM) |
tips | String | Jito tips in SOL (min 0.0000000001, max 2). Set computeUnitPrice=0 when using tips |
Swap API response structure:
{
"code": "0",
"data": [{
"routerResult": {
"chainIndex": "1",
"fromToken": { "tokenSymbol": "USDC", "decimal": "6", ... },
"toToken": { "tokenSymbol": "WBTC", "decimal": "8", ... },
"fromTokenAmount": "100000000000",
"toTokenAmount": "90281915",
"tradeFee": "1.35",
"estimateGasFee": "1248837",
"priceImpactPercent": "0.07",
"dexRouterList": [...]
},
"tx": {
"from": "0x77660f...",
"to": "0x5E1f62...",
"value": "0",
"data": "0xf2c42696...",
"gas": "1248837",
"gasPrice": "557703374",
"maxPriorityFeePerGas": "500000000",
"minReceiveAmount": "90191633",
"slippagePercent": "0.1",
"signatureData": [...]
}
}],
"msg": ""
}
Key fields in tx object:
| Field | Description |
|---|---|
from | Sender wallet address |
to | OKX DEX router contract address |
data | Transaction calldata (the swap instruction) |
value | Native token amount to send (in wei). "0" for ERC-20 swaps |
gas | Estimated gas limit (already padded +50%) |
gasPrice | Gas price in wei |
maxPriorityFeePerGas | EIP-1559 priority fee |
minReceiveAmount | Minimum output at max slippage |
signatureData | Approval calldata (if approveTransaction=true) or Jito tips calldata |
For ERC-20 tokens (not native ETH/BNB), you need to approve the DEX router to spend your tokens BEFORE the swap.
When approveTransaction=true in the request:
The response tx.signatureData contains the approval info:
{
"approveContract": "0x40aA958dd87FC8305b97f2BA922CDdCa374bcD7f",
"approveTxCalldata": "0x095ea7b3..."
}
Approval flow:
signatureData to get approveContract and approveTxCalldatato=approveContract, data=approveTxCalldataIMPORTANT: For native tokens (ETH, BNB, etc. using 0xeeee...eeee), no approval is needed.
EVM chains (Python / web3.py):
tx_params = {
"from": tx_data["from"],
"to": tx_data["to"],
"value": int(tx_data["value"]),
"data": tx_data["data"],
"gas": int(tx_data["gas"]),
"gasPrice": int(tx_data["gasPrice"]),
"nonce": w3.eth.get_transaction_count(wallet_address),
"chainId": chain_id,
}
signed = w3.eth.account.sign_transaction(tx_params, private_key)
signed_tx_hex = signed.raw_transaction.hex()
EVM chains (Node.js / ethers.js):
const tx = {
from: txData.from,
to: txData.to,
value: txData.value,
data: txData.data,
gasLimit: txData.gas,
gasPrice: txData.gasPrice,
nonce: await provider.getTransactionCount(walletAddress),
chainId: chainId,
};
const signedTx = await wallet.signTransaction(tx);
Solana:
Use solders or @solana/web3.js to deserialize, sign, and serialize the transaction from tx.data.
Endpoint:
POST https://web3.okx.com/api/v6/dex/pre-transaction/broadcast-transaction
IMPORTANT: This is a POST request with a JSON body (unlike the GET-based swap/quote endpoints).
Request body:
| Parameter | Type | Required | Description |
|---|---|---|---|
signedTx | String | Yes | The hex-encoded signed transaction string |
chainIndex | String | Yes | Chain ID (e.g., "1" for Ethereum) |
address | String | Yes | Sender wallet address |
extraData | String | No | JSON string with extra options (see below) |
extraData options (JSON string):
| Field | Type | Description |
|---|---|---|
enableMevProtection | Boolean | Enable MEV (sandwich) protection. Supported: ETH, BSC, SOL, BASE |
jitoSignedTx | String | Base58-encoded signed Jito transaction (Solana only). Required when tips > 0 |
Signing algorithm for POST requests:
prehash = timestamp + "POST" + request_path + json_body
signature = Base64(HMAC-SHA256(secret_key, prehash))
Note: For POST, the request_path has NO query string. The JSON body is appended directly to the prehash string.
Broadcast response:
{
"code": "0",
"data": [{
"orderId": "0e1d79837afce1e149b6ab54b6e2edce8130c3f8",
"txHash": "0xd394f356a16b618ed839c66c935c9cccc5dde0af832ff9b468677eea38759db5"
}],
"msg": ""
}
| Field | Description |
|---|---|
orderId | OKX internal order tracking ID |
txHash | On-chain transaction hash. Use this to check status on block explorer |
After broadcasting, verify the transaction was confirmed:
txHash on Etherscan / block explorer, or use web3.eth.wait_for_transaction_receipt()connection.confirmTransaction()isHoneyPot on both tokens before proceeding.priceImpactProtectionPercent (recommend setting to 10 or lower for safety).0.1% - 0.5%0.5% - 1%1% - 5%5% - 15% (or higher, but be cautious)autoSlippage=true with maxAutoslippagePercent for optimal auto-calculation.enableMevProtection: true for large trades on ETH, BSC, SOL, BASE.computeUnitPrice=0 to avoid wasting fees.approveAmount = swap amount).When swapping through Uniswap V3 pools, if the liquidity for the pair is drained mid-swap, the router will only consume part of your input tokens. The OKX DEX Router smart contract will automatically refund the remainder. Ensure your integration contract supports receiving token refunds.
int() for calculations, never float().BigInt or ethers.parseUnits() for amounts.raw_amount = human_amount * 10^decimalsresponse["code"] == "0" before processing.401 = Signature mismatch (check POST signing includes body)429 = Rate limitedfromTokenReferrerWalletAddress or toTokenReferrerWalletAddress per tx.import os, hmac, hashlib, base64, json, requests
from datetime import datetime, timezone
from urllib.parse import urlencode
from web3 import Web3
# === Configuration ===
API_KEY = os.environ["OKX_ACCESS_KEY"]
SECRET_KEY = os.environ["OKX_SECRET_KEY"]
PASSPHRASE = os.environ["OKX_PASSPHRASE"]
PRIVATE_KEY = os.environ["WALLET_PRIVATE_KEY"]
BASE_URL = "https://web3.okx.com"
CHAIN_INDEX = "1" # Ethereum
CHAIN_ID = 1
RPC_URL = "https://eth.llamarpc.com"
w3 = Web3(Web3.HTTPProvider(RPC_URL))
account = w3.eth.account.from_key(PRIVATE_KEY)
WALLET = account.address
def _sign_request(timestamp, method, request_path, body=""):
prehash = timestamp + method + request_path + body
mac = hmac.new(SECRET_KEY.encode(), prehash.encode(), hashlib.sha256)
return base64.b64encode(mac.digest()).decode()
def _headers(method, request_path, body=""):
timestamp = datetime.now(timezone.utc).strftime("%Y-%m-%dT%H:%M:%S.%f")[:-3] + "Z"
sig = _sign_request(timestamp, method, request_path, body)
return {
"OK-ACCESS-KEY": API_KEY,
"OK-ACCESS-SIGN": sig,
"OK-ACCESS-PASSPHRASE": PASSPHRASE,
"OK-ACCESS-TIMESTAMP": timestamp,
"Content-Type": "application/json",
}
# --- Step 1: Get swap calldata ---
def get_swap_data(from_token, to_token, amount, slippage="0.5"):
params = {
"chainIndex": CHAIN_INDEX,
"fromTokenAddress": from_token,
"toTokenAddress": to_token,
"amount": amount,
"swapMode": "exactIn",
"slippagePercent": slippage,
"userWalletAddress": WALLET,
"approveTransaction": "true",
}
query = urlencode(params)
path = f"/api/v6/dex/aggregator/swap?{query}"
headers = _headers("GET", path)
resp = requests.get(BASE_URL + path, headers=headers, timeout=30)
resp.raise_for_status()
data = resp.json()
if data["code"] != "0":
raise Exception(f"Swap API error: {data['msg']}")
return data["data"][0]
# --- Step 2: Handle approval (if needed) ---
def send_approval_if_needed(swap_result):
sig_data = swap_result["tx"].get("signatureData", [])
if not sig_data:
return None
for item in sig_data:
parsed = json.loads(item) if isinstance(item, str) else item
if "approveContract" in parsed:
approve_tx = {
"from": WALLET,
"to": Web3.to_checksum_address(parsed["approveContract"]),
"data": parsed["approveTxCalldata"],
"gas": 60000,
"gasPrice": w3.eth.gas_price,
"nonce": w3.eth.get_transaction_count(WALLET),
"chainId": CHAIN_ID,
}
signed = w3.eth.account.sign_transaction(approve_tx, PRIVATE_KEY)
tx_hash = w3.eth.send_raw_transaction(signed.raw_transaction)
receipt = w3.eth.wait_for_transaction_receipt(tx_hash, timeout=120)
print(f"Approval confirmed: {tx_hash.hex()}")
return receipt
return None
# --- Step 3: Sign the swap transaction ---
def sign_swap_tx(swap_result):
tx_data = swap_result["tx"]
tx_params = {
"from": Web3.to_checksum_address(tx_data["from"]),
"to": Web3.to_checksum_address(tx_data["to"]),
"value": int(tx_data["value"]),
"data": tx_data["data"],
"gas": int(tx_data["gas"]),
"gasPrice": int(tx_data["gasPrice"]),
"nonce": w3.eth.get_transaction_count(WALLET),
"chainId": CHAIN_ID,
}
signed = w3.eth.account.sign_transaction(tx_params, PRIVATE_KEY)
return signed.raw_transaction.hex()
# --- Step 4: Broadcast via OKX ---
def broadcast_tx(signed_tx_hex, enable_mev=True):
path = "/api/v6/dex/pre-transaction/broadcast-transaction"
body_dict = {
"chainIndex": CHAIN_INDEX,
"address": WALLET,
"signedTx": signed_tx_hex,
}
if enable_mev:
body_dict["extraData"] = json.dumps({"enableMevProtection": True})
body_str = json.dumps(body_dict)
headers = _headers("POST", path, body_str)
resp = requests.post(BASE_URL + path, headers=headers, data=body_str, timeout=30)
resp.raise_for_status()
data = resp.json()
if data["code"] != "0":
raise Exception(f"Broadcast error: {data['msg']}")
return data["data"][0]
# === Execute full swap ===
if __name__ == "__main__":
ETH = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee"
USDC = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48"
amount = str(10 ** 17) # 0.1 ETH
print("1. Getting swap data...")
swap = get_swap_data(ETH, USDC, amount, slippage="0.5")
router = swap["routerResult"]
to_dec = int(router["toToken"]["decimal"])
out_human = int(router["toTokenAmount"]) / (10 ** to_dec)
print(f" Expected output: {out_human:,.2f} {router['toToken']['tokenSymbol']}")
print(f" Price impact: {router.get('priceImpactPercent', 'N/A')}%")
print(f" Min receive: {int(swap['tx']['minReceiveAmount']) / (10 ** to_dec):,.2f}")
# Honeypot check
if router["toToken"].get("isHoneyPot"):
print(" HONEYPOT DETECTED — aborting!")
exit(1)
print("2. Handling approval...")
send_approval_if_needed(swap)
print("3. Signing swap transaction...")
signed_hex = sign_swap_tx(swap)
print("4. Broadcasting with MEV protection...")
result = broadcast_tx(signed_hex, enable_mev=True)
print(f" Order ID: {result['orderId']}")
print(f" Tx Hash: {result['txHash']}")
print(f" View: https://etherscan.io/tx/{result['txHash']}")
const crypto = require("crypto");
const https = require("https");
const { ethers } = require("ethers");
const API_KEY = process.env.OKX_ACCESS_KEY;
const SECRET_KEY = process.env.OKX_SECRET_KEY;
const PASSPHRASE = process.env.OKX_PASSPHRASE;
const PRIVATE_KEY = process.env.WALLET_PRIVATE_KEY;
const BASE_URL = "https://web3.okx.com";
const CHAIN_INDEX = "1";
const CHAIN_ID = 1;
const provider = new ethers.JsonRpcProvider("https://eth.llamarpc.com");
const wallet = new ethers.Wallet(PRIVATE_KEY, provider);
function sign(timestamp, method, path, body = "") {
return crypto
.createHmac("sha256", SECRET_KEY)
.update(timestamp + method + path + body)
.digest("base64");
}
function headers(method, path, body = "") {
const ts = new Date().toISOString();
return {
"OK-ACCESS-KEY": API_KEY,
"OK-ACCESS-SIGN": sign(ts, method, path, body),
"OK-ACCESS-PASSPHRASE": PASSPHRASE,
"OK-ACCESS-TIMESTAMP": ts,
"Content-Type": "application/json",
};
}
async function getSwapData(fromToken, toToken, amount, slippage = "0.5") {
const params = new URLSearchParams({
chainIndex: CHAIN_INDEX,
fromTokenAddress: fromToken,
toTokenAddress: toToken,
amount,
swapMode: "exactIn",
slippagePercent: slippage,
userWalletAddress: wallet.address,
approveTransaction: "true",
});
const path = `/api/v6/dex/aggregator/swap?${params}`;
const h = headers("GET", path);
const resp = await fetch(`${BASE_URL}${path}`, { headers: h });
const data = await resp.json();
if (data.code !== "0") throw new Error(`Swap error: ${data.msg}`);
return data.data[0];
}
async function broadcastTx(signedTx, enableMev = true) {
const path = "/api/v6/dex/pre-transaction/broadcast-transaction";
const bodyObj = {
chainIndex: CHAIN_INDEX,
address: wallet.address,
signedTx,
...(enableMev && {
extraData: JSON.stringify({ enableMevProtection: true }),
}),
};
const bodyStr = JSON.stringify(bodyObj);
const h = headers("POST", path, bodyStr);
const resp = await fetch(`${BASE_URL}${path}`, {
method: "POST",
headers: h,
body: bodyStr,
});
const data = await resp.json();
if (data.code !== "0") throw new Error(`Broadcast error: ${data.msg}`);
return data.data[0];
}
async function main() {
const ETH = "0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee";
const USDC = "0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48";
const amount = (10n ** 17n).toString(); // 0.1 ETH
console.log("1. Getting swap data...");
const swap = await getSwapData(ETH, USDC, amount);
const txData = swap.tx;
console.log(` Min receive: ${txData.minReceiveAmount}`);
console.log("2. Signing transaction...");
const tx = {
from: txData.from,
to: txData.to,
value: txData.value,
data: txData.data,
gasLimit: txData.gas,
gasPrice: txData.gasPrice,
nonce: await provider.getTransactionCount(wallet.address),
chainId: CHAIN_ID,
};
const signedTx = await wallet.signTransaction(tx);
console.log("3. Broadcasting with MEV protection...");
const result = await broadcastTx(signedTx);
console.log(` Tx Hash: ${result.txHash}`);
}
main().catch(console.error);
When using Jito tips on Solana:
tips parameter (e.g., "0.001" SOL) in the swap requestcomputeUnitPrice=0 (avoid double-paying for priority)signatureData in response contains the Jito tips calldatasignedTx and jitoSignedTx in extraData# Solana-specific broadcast with Jito
body = {
"chainIndex": "501",
"address": solana_wallet_pubkey,
"signedTx": base58_signed_main_tx,
"extraData": json.dumps({
"enableMevProtection": True,
"jitoSignedTx": base58_signed_jito_tx
})
}
IMPORTANT: For Solana, signedTx and jitoSignedTx must BOTH be provided when using tips.
| Problem | Cause | Solution |
|---|---|---|
401 Unauthorized on broadcast | POST signing error | Ensure prehash = timestamp + "POST" + path + jsonBody. The body must be the exact JSON string. |
401 Unauthorized on swap | GET signing error | Ensure prehash = timestamp + "GET" + path_with_query_string. |
| Approval tx fails | Insufficient ETH for gas | Ensure wallet has native token for gas fees. |
| Swap tx reverts | Slippage exceeded | Increase slippagePercent or use autoSlippage=true. |
minReceiveAmount is 0 | Extreme slippage or bad params | Check token addresses and amounts. Reduce trade size. |
| Broadcast returns no txHash | MEV protection routing delay | Wait and check orderId status. MEV-protected txs may take longer. |
| "Nonce too low" on broadcast | Tx already sent or nonce reused | Fetch fresh nonce before signing. Never reuse nonces. |
| Solana broadcast fails | Missing jitoSignedTx | When tips > 0, you must provide both signedTx and jitoSignedTx. |
| Token refund not received | Uni V3 liquidity drained | Ensure your contract supports receiving token refunds from the router. |
| Commission not working on BSC | Four.meme restriction | Commission is not supported for swaps through Four.meme on BSC. |
priceImpactPercent very negative | Low liquidity | Reduce amount or split into multiple smaller swaps. |
The OKX API uses different signing for GET and POST:
| Method | Prehash Format |
|---|---|
| GET | timestamp + "GET" + path_with_query |
| POST | timestamp + "POST" + path + json_body |
The Swap endpoint is GET, the Broadcast endpoint is POST. Getting this wrong is the #1 cause of 401 errors.
| Chain | enableMevProtection | Jito Tips |
|---|---|---|
| Ethereum | Yes | No |
| BSC | Yes | No |
| Solana | Yes | Yes |
| Base | Yes | No |
| Others | Not yet | Not yet |
| Chain | chainIndex | Chain ID (for tx signing) |
|---|---|---|
| Ethereum | 1 | 1 |
| BSC | 56 | 56 |
| Polygon | 137 | 137 |
| Arbitrum | 42161 | 42161 |
| Optimism | 10 | 10 |
| Base | 8453 | 8453 |
| Avalanche | 43114 | 43114 |
| Solana | 501 | N/A |
| Unichain | 130 | 130 |
Skills that do similar work, with their headline benchmark lift.
From the same source and nearby in the catalogue.