Skip to content
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
16 changes: 8 additions & 8 deletions packages/polygon-agent-cli/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@
"typecheck": "tsc --noEmit"
},
"dependencies": {
"@0xsequence/abi": "3.0.0-beta.16",
"@0xsequence/api": "3.0.0-beta.16",
"@0xsequence/builder": "3.0.0-beta.16",
"@0xsequence/dapp-client": "3.0.0-beta.16",
"@0xsequence/abi": "3.0.0-beta.17",
"@0xsequence/api": "3.0.0-beta.17",
"@0xsequence/builder": "3.0.0-beta.17",
"@0xsequence/dapp-client": "3.0.0-beta.17",
"@0xsequence/dapp-client-cli": "0.1.5",
"@0xsequence/indexer": "^2.3.40",
"@0xsequence/network": "^2.3.40",
"@0xsequence/relayer": "3.0.0-beta.16",
"@0xsequence/wallet-core": "3.0.0-beta.16",
"@0xsequence/wallet-primitives": "3.0.0-beta.16",
"@0xsequence/wallet-wdk": "3.0.0-beta.16",
"@0xsequence/relayer": "3.0.0-beta.17",
"@0xsequence/wallet-core": "3.0.0-beta.17",
"@0xsequence/wallet-primitives": "3.0.0-beta.17",
"@0xsequence/wallet-wdk": "3.0.0-beta.17",
"@0xtrails/api": "^0.10.4",
"@x402/core": "^2.3.1",
"@x402/evm": "^2.3.1",
Expand Down
14 changes: 4 additions & 10 deletions packages/polygon-agent-cli/skills/SKILL.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,6 @@ export SEQUENCE_INDEXER_ACCESS_KEY=$SEQUENCE_PROJECT_ACCESS_KEY
export TRAILS_API_KEY=$SEQUENCE_PROJECT_ACCESS_KEY
```

### Optional

| Variable | Default |
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why these lines are removed - were they causing issues on the skill?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes

| ---------------------------------- | ----------------------------------------------------- |
| `SEQUENCE_ECOSYSTEM_CONNECTOR_URL` | `https://agentconnect.polygon.technology/` |
| `SEQUENCE_DAPP_ORIGIN` | Same as connector URL origin |
| `TRAILS_TOKEN_MAP_JSON` | Token-directory lookup |
| `POLYGON_AGENT_DEBUG_FETCH` | Off — logs HTTP to `~/.polygon-agent/fetch-debug.log` |
| `POLYGON_AGENT_DEBUG_FEE` | Off — dumps fee options to stderr |

## Complete Setup Flow

```bash
Expand All @@ -66,6 +56,10 @@ polygon-agent setup --name "MyAgent"
# Phase 2: Create ecosystem wallet (auto-waits for browser approval)
export SEQUENCE_PROJECT_ACCESS_KEY=<accessKey>
polygon-agent wallet create --usdc-limit 100 --native-limit 5
# → IMPORTANT: The command outputs an `approvalUrl`. You MUST send the COMPLETE,
# UNTRUNCATED URL to the user and wait for them to open it in a browser and approve.
# Do NOT proceed to the next step until the user confirms approval (or the CLI
# automatically detects the callback). The wallet address is only available after approval.

# Phase 3: Fund wallet
polygon-agent fund
Expand Down
108 changes: 6 additions & 102 deletions packages/polygon-agent-cli/src/lib/dapp-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -290,110 +290,14 @@ export async function runDappClientTx({
return { walletAddress, dryRun: true };
}

const debugFee = ['1', 'true', 'yes'].includes(
String(
process.env.SEQ_ECO_DEBUG_FEE_OPTIONS || process.env.POLYGON_AGENT_DEBUG_FEE || ''
).toLowerCase()
);

// Let the SDK handle fee selection — including counterfactual (undeployed) wallets.
// The SDK routes undeployed wallets through the guest executor and bundles deploy+execute.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let feeOpt: any;

if (preferNativeFee) {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const feeOptions = await client.getFeeOptions(chainId, transactions as any);
if (debugFee) console.error(JSON.stringify({ debug: 'feeOptions', feeOptions }, null, 2));
const nativeOpt = (feeOptions || []).find(isNativeFeeOption);
if (nativeOpt) feeOpt = nativeOpt;
} catch {
// Fall through to ERC20 fee path
}
}

if (!feeOpt) {
try {
const feeTokens = await client.getFeeTokens(chainId);
if (debugFee) console.error(JSON.stringify({ debug: 'feeTokens', feeTokens }, null, 2));

const paymentAddress = feeTokens?.paymentAddress;
const tokens = Array.isArray(feeTokens?.tokens) ? feeTokens.tokens : [];

const USDC_POLYGON = '0x3c499c542cef5e3811e1192ce70d8cc03d5c3359';
// eslint-disable-next-line @typescript-eslint/no-explicit-any
let erc20Token: any = null;
if (tokens.length > 0) {
try {
const { SequenceIndexer } = await import('@0xsequence/indexer');
const chainIndexerUrl = `https://polygon-indexer.sequence.app`;
const indexerKey = process.env.SEQUENCE_INDEXER_ACCESS_KEY || projectAccessKey;
const indexer = new SequenceIndexer(chainIndexerUrl, indexerKey);
const balRes = await indexer.getTokenBalances({
accountAddress: walletAddress,
includeMetadata: false
});
const heldAddresses = new Set(
(balRes?.balances || []).map((b: { contractAddress?: string }) =>
b.contractAddress?.toLowerCase()
)
);
const heldFeeTokens = tokens.filter(
(t: { contractAddress?: string }) =>
t?.contractAddress && heldAddresses.has(t.contractAddress.toLowerCase())
);
erc20Token =
heldFeeTokens.find(
(t: { contractAddress?: string }) => t.contractAddress?.toLowerCase() === USDC_POLYGON
) ||
heldFeeTokens.find((t: { symbol?: string }) => t?.symbol === 'USDC') ||
heldFeeTokens[0] ||
null;
} catch {
// Indexer unavailable — fall back to symbol matching
}
if (!erc20Token) {
erc20Token =
tokens.find(
(t: { contractAddress?: string }) =>
t?.contractAddress?.toLowerCase() === USDC_POLYGON
) ||
tokens.find(
(t: { contractAddress?: string; symbol?: string }) =>
t?.contractAddress && t?.symbol === 'USDC'
) ||
tokens.find((t: { contractAddress?: string }) => t?.contractAddress) ||
null;
}
}

if (paymentAddress && erc20Token) {
const decimals = typeof erc20Token.decimals === 'number' ? erc20Token.decimals : 6;
const feeValue = decimals >= 2 ? 10 ** (decimals - 2) : 1;
feeOpt = {
token: erc20Token,
to: paymentAddress,
value: String(feeValue),
gasLimit: 0
};
if (debugFee) console.error(JSON.stringify({ debug: 'selectedFee', feeOpt }, null, 2));
}
} catch (e) {
if (debugFee)
console.error(
JSON.stringify({ debug: 'getFeeTokens failed', error: (e as Error)?.message }, null, 2)
);
}
}

if (!feeOpt) {
try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const feeOptions = await client.getFeeOptions(chainId, transactions as any);
feeOpt = feeOptions?.[0];
} catch (e) {
throw new Error(`Unable to determine fee option: ${(e as Error)?.message}`);
}
const feeOptions = await client.getFeeOptions(chainId, transactions as any);
if (!feeOptions || feeOptions.length === 0) {
throw new Error('No fee options available for this transaction.');
}
const feeOpt = (preferNativeFee && feeOptions.find(isNativeFeeOption)) || feeOptions[0];

// eslint-disable-next-line @typescript-eslint/no-explicit-any
const txHash = await client.sendTransaction(chainId, transactions as any, feeOpt);
Expand Down
Loading