from compass_api_sdk import CompassAPI, models
with CompassAPI(
api_key_auth="<YOUR_API_KEY_HERE>",
) as compass_api:
res = compass_api.credit.credit_rebalance(owner="0x4D3c07d1db7E4A9E44285fae9810d6549655bc74", chain=models.CreditRebalanceRequestChain.ETHEREUM, targets=[
{
"protocol": models.CreditProtocol.AAVE,
"collateral_token": "WETH",
"borrow_token": "USDC",
"target_multiplier": 2.0,
},
], max_slippage_percent=0.5, gas_sponsorship=False)
# Handle response
print(res)import { CompassApiSDK } from "@compass-labs/api-sdk";
const compassApiSDK = new CompassApiSDK({
apiKeyAuth: "<YOUR_API_KEY_HERE>",
});
async function run() {
const result = await compassApiSDK.credit.creditRebalance({
owner: "0x4D3c07d1db7E4A9E44285fae9810d6549655bc74",
chain: "ethereum",
targets: [
{
protocol: "AAVE",
collateralToken: "WETH",
borrowToken: "USDC",
targetMultiplier: 2,
},
],
maxSlippagePercent: 0.5,
gasSponsorship: false,
});
console.log(result);
}
run();curl --request POST \
--url https://api.compasslabs.ai/v2/credit/rebalance \
--header 'Content-Type: application/json' \
--header 'x-api-key: <api-key>' \
--data '
{
"owner": "0x4D3c07d1db7E4A9E44285fae9810d6549655bc74",
"chain": "ethereum",
"max_slippage_percent": 0.5,
"gas_sponsorship": false,
"targets": [
{
"borrow_token": "USDC",
"collateral_token": "WETH",
"protocol": "AAVE",
"target_multiplier": 2
}
]
}
'const options = {
method: 'POST',
headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
owner: '0x4D3c07d1db7E4A9E44285fae9810d6549655bc74',
chain: 'ethereum',
max_slippage_percent: 0.5,
gas_sponsorship: false,
targets: [
{
borrow_token: 'USDC',
collateral_token: 'WETH',
protocol: 'AAVE',
target_multiplier: 2
}
]
})
};
fetch('https://api.compasslabs.ai/v2/credit/rebalance', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.compasslabs.ai/v2/credit/rebalance",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'owner' => '0x4D3c07d1db7E4A9E44285fae9810d6549655bc74',
'chain' => 'ethereum',
'max_slippage_percent' => 0.5,
'gas_sponsorship' => false,
'targets' => [
[
'borrow_token' => 'USDC',
'collateral_token' => 'WETH',
'protocol' => 'AAVE',
'target_multiplier' => 2
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.compasslabs.ai/v2/credit/rebalance"
payload := strings.NewReader("{\n \"owner\": \"0x4D3c07d1db7E4A9E44285fae9810d6549655bc74\",\n \"chain\": \"ethereum\",\n \"max_slippage_percent\": 0.5,\n \"gas_sponsorship\": false,\n \"targets\": [\n {\n \"borrow_token\": \"USDC\",\n \"collateral_token\": \"WETH\",\n \"protocol\": \"AAVE\",\n \"target_multiplier\": 2\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.compasslabs.ai/v2/credit/rebalance")
.header("x-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"owner\": \"0x4D3c07d1db7E4A9E44285fae9810d6549655bc74\",\n \"chain\": \"ethereum\",\n \"max_slippage_percent\": 0.5,\n \"gas_sponsorship\": false,\n \"targets\": [\n {\n \"borrow_token\": \"USDC\",\n \"collateral_token\": \"WETH\",\n \"protocol\": \"AAVE\",\n \"target_multiplier\": 2\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.compasslabs.ai/v2/credit/rebalance")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"owner\": \"0x4D3c07d1db7E4A9E44285fae9810d6549655bc74\",\n \"chain\": \"ethereum\",\n \"max_slippage_percent\": 0.5,\n \"gas_sponsorship\": false,\n \"targets\": [\n {\n \"borrow_token\": \"USDC\",\n \"collateral_token\": \"WETH\",\n \"protocol\": \"AAVE\",\n \"target_multiplier\": 2\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"preview": {
"net_equity_moved_usd": "<string>",
"total_legs": 123,
"estimated_max_dust": "<string>",
"per_target": [
{
"protocol": "AAVE",
"collateral_token": "<string>",
"borrow_token": "<string>",
"direction": "open",
"legs": 123,
"resulting_collateral": "<string>",
"resulting_debt": "<string>",
"resulting_multiplier": "<string>",
"resulting_health_factor": "<string>",
"health_factor_scope": "market",
"estimated_max_dust": "<string>",
"market_id": "<string>",
"sub_account_id": 123,
"collateral_vault": "<string>",
"borrow_vault": "<string>",
"unloop_legs": [],
"loop_legs": []
}
],
"routing_swaps": [],
"aave_account_health_factor": "<string>"
},
"transaction": {
"chainId": "0x2105",
"data": "0x1688f0b900000000000000000000000029fcb43b46531bca003ddc8fcb67ffe91900c762000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000675f4a3d",
"from": "0x4A83b4413CF41C3244027e1590E35a0F48403F0c",
"gas": "0x7a120",
"maxFeePerGas": "0x59682f00",
"maxPriorityFeePerGas": "0x3b9aca00",
"nonce": "0x5",
"to": "0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67",
"value": "0x0"
},
"eip_712": {
"domain": {
"chainId": 8453,
"verifyingContract": "0x6B90E8B4E3E971E74C1A47a3a20976377E2dB4b1"
},
"message": {
"baseGas": "0",
"data": "0x8d80ff0a0000000000000000000000000000000000000000000000000000000000000020",
"gasPrice": "0",
"gasToken": "0x0000000000000000000000000000000000000000",
"nonce": "7",
"operation": 1,
"refundReceiver": "0x0000000000000000000000000000000000000000",
"safeTxGas": "0",
"to": "0x93C23AAE4793C14D6DF35D2A2A2234204e1559dA",
"value": "0"
},
"primaryType": "SafeTx",
"types": {
"EIP712Domain": [
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"SafeTx": [
{
"name": "to",
"type": "address"
},
{
"name": "value",
"type": "uint256"
},
{
"name": "data",
"type": "bytes"
},
{
"name": "operation",
"type": "uint8"
},
{
"name": "safeTxGas",
"type": "uint256"
},
{
"name": "baseGas",
"type": "uint256"
},
{
"name": "gasPrice",
"type": "uint256"
},
{
"name": "gasToken",
"type": "address"
},
{
"name": "refundReceiver",
"type": "address"
},
{
"name": "nonce",
"type": "uint256"
}
]
}
}
}{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>"
}
]
}Rebalance the leveraged credit book
Move one or more leveraged positions to the state you want, in ONE atomic transaction.
List the positions to change and the end state you want for each. The API works out whether that means opening, growing, shrinking, delevering or closing, and does them all together.
Money freed by shrinking or closing one position pays for growing or opening another, so shifting funds between positions needs no new deposit. Any shortfall is taken from the Credit Account’s idle balance, and the call returns 422 if that does not cover it either.
See the Leveraged Looping guide for protocol and chain coverage, capital routing and the full error list.
from compass_api_sdk import CompassAPI, models
with CompassAPI(
api_key_auth="<YOUR_API_KEY_HERE>",
) as compass_api:
res = compass_api.credit.credit_rebalance(owner="0x4D3c07d1db7E4A9E44285fae9810d6549655bc74", chain=models.CreditRebalanceRequestChain.ETHEREUM, targets=[
{
"protocol": models.CreditProtocol.AAVE,
"collateral_token": "WETH",
"borrow_token": "USDC",
"target_multiplier": 2.0,
},
], max_slippage_percent=0.5, gas_sponsorship=False)
# Handle response
print(res)import { CompassApiSDK } from "@compass-labs/api-sdk";
const compassApiSDK = new CompassApiSDK({
apiKeyAuth: "<YOUR_API_KEY_HERE>",
});
async function run() {
const result = await compassApiSDK.credit.creditRebalance({
owner: "0x4D3c07d1db7E4A9E44285fae9810d6549655bc74",
chain: "ethereum",
targets: [
{
protocol: "AAVE",
collateralToken: "WETH",
borrowToken: "USDC",
targetMultiplier: 2,
},
],
maxSlippagePercent: 0.5,
gasSponsorship: false,
});
console.log(result);
}
run();curl --request POST \
--url https://api.compasslabs.ai/v2/credit/rebalance \
--header 'Content-Type: application/json' \
--header 'x-api-key: <api-key>' \
--data '
{
"owner": "0x4D3c07d1db7E4A9E44285fae9810d6549655bc74",
"chain": "ethereum",
"max_slippage_percent": 0.5,
"gas_sponsorship": false,
"targets": [
{
"borrow_token": "USDC",
"collateral_token": "WETH",
"protocol": "AAVE",
"target_multiplier": 2
}
]
}
'const options = {
method: 'POST',
headers: {'x-api-key': '<api-key>', 'Content-Type': 'application/json'},
body: JSON.stringify({
owner: '0x4D3c07d1db7E4A9E44285fae9810d6549655bc74',
chain: 'ethereum',
max_slippage_percent: 0.5,
gas_sponsorship: false,
targets: [
{
borrow_token: 'USDC',
collateral_token: 'WETH',
protocol: 'AAVE',
target_multiplier: 2
}
]
})
};
fetch('https://api.compasslabs.ai/v2/credit/rebalance', options)
.then(res => res.json())
.then(res => console.log(res))
.catch(err => console.error(err));<?php
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => "https://api.compasslabs.ai/v2/credit/rebalance",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => json_encode([
'owner' => '0x4D3c07d1db7E4A9E44285fae9810d6549655bc74',
'chain' => 'ethereum',
'max_slippage_percent' => 0.5,
'gas_sponsorship' => false,
'targets' => [
[
'borrow_token' => 'USDC',
'collateral_token' => 'WETH',
'protocol' => 'AAVE',
'target_multiplier' => 2
]
]
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json",
"x-api-key: <api-key>"
],
]);
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $response;
}package main
import (
"fmt"
"strings"
"net/http"
"io"
)
func main() {
url := "https://api.compasslabs.ai/v2/credit/rebalance"
payload := strings.NewReader("{\n \"owner\": \"0x4D3c07d1db7E4A9E44285fae9810d6549655bc74\",\n \"chain\": \"ethereum\",\n \"max_slippage_percent\": 0.5,\n \"gas_sponsorship\": false,\n \"targets\": [\n {\n \"borrow_token\": \"USDC\",\n \"collateral_token\": \"WETH\",\n \"protocol\": \"AAVE\",\n \"target_multiplier\": 2\n }\n ]\n}")
req, _ := http.NewRequest("POST", url, payload)
req.Header.Add("x-api-key", "<api-key>")
req.Header.Add("Content-Type", "application/json")
res, _ := http.DefaultClient.Do(req)
defer res.Body.Close()
body, _ := io.ReadAll(res.Body)
fmt.Println(string(body))
}HttpResponse<String> response = Unirest.post("https://api.compasslabs.ai/v2/credit/rebalance")
.header("x-api-key", "<api-key>")
.header("Content-Type", "application/json")
.body("{\n \"owner\": \"0x4D3c07d1db7E4A9E44285fae9810d6549655bc74\",\n \"chain\": \"ethereum\",\n \"max_slippage_percent\": 0.5,\n \"gas_sponsorship\": false,\n \"targets\": [\n {\n \"borrow_token\": \"USDC\",\n \"collateral_token\": \"WETH\",\n \"protocol\": \"AAVE\",\n \"target_multiplier\": 2\n }\n ]\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://api.compasslabs.ai/v2/credit/rebalance")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["x-api-key"] = '<api-key>'
request["Content-Type"] = 'application/json'
request.body = "{\n \"owner\": \"0x4D3c07d1db7E4A9E44285fae9810d6549655bc74\",\n \"chain\": \"ethereum\",\n \"max_slippage_percent\": 0.5,\n \"gas_sponsorship\": false,\n \"targets\": [\n {\n \"borrow_token\": \"USDC\",\n \"collateral_token\": \"WETH\",\n \"protocol\": \"AAVE\",\n \"target_multiplier\": 2\n }\n ]\n}"
response = http.request(request)
puts response.read_body{
"preview": {
"net_equity_moved_usd": "<string>",
"total_legs": 123,
"estimated_max_dust": "<string>",
"per_target": [
{
"protocol": "AAVE",
"collateral_token": "<string>",
"borrow_token": "<string>",
"direction": "open",
"legs": 123,
"resulting_collateral": "<string>",
"resulting_debt": "<string>",
"resulting_multiplier": "<string>",
"resulting_health_factor": "<string>",
"health_factor_scope": "market",
"estimated_max_dust": "<string>",
"market_id": "<string>",
"sub_account_id": 123,
"collateral_vault": "<string>",
"borrow_vault": "<string>",
"unloop_legs": [],
"loop_legs": []
}
],
"routing_swaps": [],
"aave_account_health_factor": "<string>"
},
"transaction": {
"chainId": "0x2105",
"data": "0x1688f0b900000000000000000000000029fcb43b46531bca003ddc8fcb67ffe91900c762000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000675f4a3d",
"from": "0x4A83b4413CF41C3244027e1590E35a0F48403F0c",
"gas": "0x7a120",
"maxFeePerGas": "0x59682f00",
"maxPriorityFeePerGas": "0x3b9aca00",
"nonce": "0x5",
"to": "0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67",
"value": "0x0"
},
"eip_712": {
"domain": {
"chainId": 8453,
"verifyingContract": "0x6B90E8B4E3E971E74C1A47a3a20976377E2dB4b1"
},
"message": {
"baseGas": "0",
"data": "0x8d80ff0a0000000000000000000000000000000000000000000000000000000000000020",
"gasPrice": "0",
"gasToken": "0x0000000000000000000000000000000000000000",
"nonce": "7",
"operation": 1,
"refundReceiver": "0x0000000000000000000000000000000000000000",
"safeTxGas": "0",
"to": "0x93C23AAE4793C14D6DF35D2A2A2234204e1559dA",
"value": "0"
},
"primaryType": "SafeTx",
"types": {
"EIP712Domain": [
{
"name": "chainId",
"type": "uint256"
},
{
"name": "verifyingContract",
"type": "address"
}
],
"SafeTx": [
{
"name": "to",
"type": "address"
},
{
"name": "value",
"type": "uint256"
},
{
"name": "data",
"type": "bytes"
},
{
"name": "operation",
"type": "uint8"
},
{
"name": "safeTxGas",
"type": "uint256"
},
{
"name": "baseGas",
"type": "uint256"
},
{
"name": "gasPrice",
"type": "uint256"
},
{
"name": "gasToken",
"type": "address"
},
{
"name": "refundReceiver",
"type": "address"
},
{
"name": "nonce",
"type": "uint256"
}
]
}
}
}{
"detail": [
{
"loc": [
"<string>"
],
"msg": "<string>",
"type": "<string>"
}
]
}Body
Rebalance one or more Credit Account positions to their named end-states — lever up or down, grow or shrink equity, open a new position, close an existing one, or migrate (close one and open another) — all in ONE atomic transaction from the Credit Account.
The address that owns the Credit Account.
"0x4D3c07d1db7E4A9E44285fae9810d6549655bc74"
Blockchain network. Rebalance is not available on HyperEVM yet; use loop/unloop there.
arbitrum, base, bsc, ethereum, tempo "ethereum"
The positions to change. Scoped: only positions named here are touched; any position not listed is left untouched.
1Show child attributes
Show child attributes
[
{
"borrow_token": "USDC",
"collateral_token": "WETH",
"protocol": "AAVE",
"target_multiplier": 2
}
]
Request-level per-swap slippage tolerance in percent (including routing swaps). A per-target max_slippage_percent overrides it where set.
0 < x <= 100.3
If true, returns EIP-712 typed data for gas-sponsored execution instead of an unsigned transaction.
false
Response
Successful Response
The atomic rebalance transaction plus its guaranteed-floor preview.
Projected end state, computed on guaranteed swap floors.
Show child attributes
Show child attributes
Unsigned transaction for direct execution by the owner. Present when gas_sponsorship=false. Null when the book is already at its target — nothing to execute.
Show child attributes
Show child attributes
{
"chainId": "0x2105",
"data": "0x1688f0b900000000000000000000000029fcb43b46531bca003ddc8fcb67ffe91900c762000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000675f4a3d",
"from": "0x4A83b4413CF41C3244027e1590E35a0F48403F0c",
"gas": "0x7a120",
"maxFeePerGas": "0x59682f00",
"maxPriorityFeePerGas": "0x3b9aca00",
"nonce": "0x5",
"to": "0x4e1DCf7AD4e460CfD30791CCC4F9c8a4f820ec67",
"value": "0x0"
}
EIP-712 typed data for gas-sponsored execution. Present when gas_sponsorship=true.
Show child attributes
Show child attributes
{
"domain": {
"chainId": 8453,
"verifyingContract": "0x6B90E8B4E3E971E74C1A47a3a20976377E2dB4b1"
},
"message": {
"baseGas": "0",
"data": "0x8d80ff0a0000000000000000000000000000000000000000000000000000000000000020",
"gasPrice": "0",
"gasToken": "0x0000000000000000000000000000000000000000",
"nonce": "7",
"operation": 1,
"refundReceiver": "0x0000000000000000000000000000000000000000",
"safeTxGas": "0",
"to": "0x93C23AAE4793C14D6DF35D2A2A2234204e1559dA",
"value": "0"
},
"primaryType": "SafeTx",
"types": {
"EIP712Domain": [
{ "name": "chainId", "type": "uint256" },
{
"name": "verifyingContract",
"type": "address"
}
],
"SafeTx": [
{ "name": "to", "type": "address" },
{ "name": "value", "type": "uint256" },
{ "name": "data", "type": "bytes" },
{ "name": "operation", "type": "uint8" },
{ "name": "safeTxGas", "type": "uint256" },
{ "name": "baseGas", "type": "uint256" },
{ "name": "gasPrice", "type": "uint256" },
{ "name": "gasToken", "type": "address" },
{
"name": "refundReceiver",
"type": "address"
},
{ "name": "nonce", "type": "uint256" }
]
}
}
Was this page helpful?