One-Click Trading
One-click trading solves a fundamental usability problem for dApp developers, enabling seamless application sessions without constant user interruption. This Super Session pattern allows users to pre-authorize a trusted app service to execute swaps on their behalf, creating a one-click UX that rivals traditional web2 applications.
The problem with traditional DeFi UX: In conventional DeFi applications, every action requires a separate wallet signature. Users must sign to approve tokens, sign to execute swaps, and sign again for any parameter changes. This creates friction that breaks user flow and makes complex multi-step operations cumbersome.
The one-click solution: With Intentify’s one-click trading, users sign once during onboarding to authorize an app service. From that point forward, the app can execute swaps with different parameters without requiring additional signatures. This enables:
- Seamless trading interfaces: Users can execute multiple swaps in rapid succession
- Dynamic portfolio management: Apps can rebalance portfolios based on market conditions
- Gaming and NFT applications: In-game economies can function without constant signature prompts
- Automated strategies: Complex trading strategies can execute without user intervention
How it works:
- The workflow authorizes a specific app service to execute swaps on the user’s behalf.
- Security checks ensure only the intended service can solve the user’s intent.
- Each swap execution requires a corresponding solution workflow with specific parameters.
# Main script: Authorizes a dApp service to perform swaps on the user's behalf
contracts:
uniswap_v2:
address: 0x...
abi: uniswap_v2.abi.json
tokenIn:
abi: std/erc20
tokenOut:
abi: std/erc20
imports:
solution:
path: ./solution.yaml
pid: vm.pid+1
constants:
dapp_tx_service: !Address 0x... # Address of the authorized dApp service
variables:
amount: !Uint
script_hash: !Bytes32 # Unique identifier for replay protection
workflow:
# Ensure only the dApp service can execute
- require(solution.this.account == dapp_tx_service)
- script_hash: solution.script_hash
- require(solution.script_hash == this.ihash)
# Load swap parameters from the solution
- tokenIn.address: solution.tokenIn
- tokenOut.address: solution.tokenOut
- amount: solution.amount
# Approve and perform the swap
- tokenIn.approve(uniswap_v2, amount)
- uniswap_v2.swapExactTokensForTokens(amount, 0, [tokenIn.address, tokenOut.address], this.account, time.blockstamp)The following script provides the parameters for the user’s swap:
# Solution script: Supplies swap parameters for a single use
constants:
tokenIn: !Address 0x... # Token to swap from
tokenOut: !Address 0x... # Token to swap to
amount: !Uint 0 # Amount to swap
script_hash: !Bytes32 0x... # Must match the main script's hash
workflow:
# Can only be used once
- terminate(this.ihash)Bundling requirement:
Both the main script and the solution script must be signed and bundled together in the same transaction, in the correct order, for this workflow to function as intended. This ensures that all checks and variable assignments are properly enforced during execution.
SDK Integration Example
The IAMC SDK integration requires just three steps: compile, sign, and execute. Here’s the minimal code needed:
import init, { IamcWasm } from 'iamc-wasm';
// 1. Initialize SDK (once per app)
await init();
const iamc = new IamcWasm();
// 2. Create intent from IAML script
iamc.setSources({ 'swap.iaml': swapScript });
const prepareResult = iamc.prepareData({
target: 'swap.iaml',
chain_id: 1
});
// 3. Get user signature
const signature = await window.ethereum.request({
method: 'personal_sign',
params: [prepareResult.data.message_hash, userAddress]
});
// 4. Create living intent
const intent = iamc.intentify({
target: 'swap.iaml',
signature,
chain_id: 1
});
// 5. Generate transaction calldata
const transaction = iamc.compile({
targets: ['swap.iaml', 'solution.yaml']
});
// Ready to submit: transaction.data.calldata