curl --request POST \
--url https://tx-builder-testnet.avantisfi.com/v2/margin/update \
--header 'Content-Type: application/json' \
--data '
{
"trader": "<string>",
"tradeIndex": 1,
"collateralUsdc": "<string>",
"pairIndex": 1,
"pair": "<string>",
"delegate": "<string>",
"priceUpdateData": "<string>",
"priceSourcing": 0,
"oracleFeeWei": "<string>"
}
'import requests
url = "https://tx-builder-testnet.avantisfi.com/v2/margin/update"
payload = {
"trader": "<string>",
"tradeIndex": 1,
"collateralUsdc": "<string>",
"pairIndex": 1,
"pair": "<string>",
"delegate": "<string>",
"priceUpdateData": "<string>",
"priceSourcing": 0,
"oracleFeeWei": "<string>"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
trader: '<string>',
tradeIndex: 1,
collateralUsdc: '<string>',
pairIndex: 1,
pair: '<string>',
delegate: '<string>',
priceUpdateData: '<string>',
priceSourcing: 0,
oracleFeeWei: '<string>'
})
};
fetch('https://tx-builder-testnet.avantisfi.com/v2/margin/update', 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://tx-builder-testnet.avantisfi.com/v2/margin/update",
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([
'trader' => '<string>',
'tradeIndex' => 1,
'collateralUsdc' => '<string>',
'pairIndex' => 1,
'pair' => '<string>',
'delegate' => '<string>',
'priceUpdateData' => '<string>',
'priceSourcing' => 0,
'oracleFeeWei' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$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://tx-builder-testnet.avantisfi.com/v2/margin/update"
payload := strings.NewReader("{\n \"trader\": \"<string>\",\n \"tradeIndex\": 1,\n \"collateralUsdc\": \"<string>\",\n \"pairIndex\": 1,\n \"pair\": \"<string>\",\n \"delegate\": \"<string>\",\n \"priceUpdateData\": \"<string>\",\n \"priceSourcing\": 0,\n \"oracleFeeWei\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
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://tx-builder-testnet.avantisfi.com/v2/margin/update")
.header("Content-Type", "application/json")
.body("{\n \"trader\": \"<string>\",\n \"tradeIndex\": 1,\n \"collateralUsdc\": \"<string>\",\n \"pairIndex\": 1,\n \"pair\": \"<string>\",\n \"delegate\": \"<string>\",\n \"priceUpdateData\": \"<string>\",\n \"priceSourcing\": 0,\n \"oracleFeeWei\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://tx-builder-testnet.avantisfi.com/v2/margin/update")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"trader\": \"<string>\",\n \"tradeIndex\": 1,\n \"collateralUsdc\": \"<string>\",\n \"pairIndex\": 1,\n \"pair\": \"<string>\",\n \"delegate\": \"<string>\",\n \"priceUpdateData\": \"<string>\",\n \"priceSourcing\": 0,\n \"oracleFeeWei\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"ok": true,
"data": {
"to": "0x0000000000000000000000000000000000000000",
"from": "0x0000000000000000000000000000000000000000",
"data": "<string>",
"value": "0x0",
"chainId": 123,
"description": "<string>",
"meta": {
"pair": "<string>",
"pairIndex": 123,
"tradeIndex": 123,
"action": "<string>",
"collateralUsdc": "<string>",
"priceSourcing": 123,
"oracleFeeWei": "<string>",
"delegated": true
}
}
}{
"ok": false,
"error": {
"code": "BAD_REQUEST",
"message": "<string>",
"details": "<unknown>"
}
}Build a transaction that adds or removes position collateral
Returns an unsigned transaction that deposits USDC into (or withdraws from) an open position’s margin, changing its effective leverage and liquidation price. Executes atomically against a fresh oracle price — the attached value is a tiny oracle update fee (1 wei default), not an operator fee. Withdrawals are blocked on PnL-type (market_pnl) positions.
curl --request POST \
--url https://tx-builder-testnet.avantisfi.com/v2/margin/update \
--header 'Content-Type: application/json' \
--data '
{
"trader": "<string>",
"tradeIndex": 1,
"collateralUsdc": "<string>",
"pairIndex": 1,
"pair": "<string>",
"delegate": "<string>",
"priceUpdateData": "<string>",
"priceSourcing": 0,
"oracleFeeWei": "<string>"
}
'import requests
url = "https://tx-builder-testnet.avantisfi.com/v2/margin/update"
payload = {
"trader": "<string>",
"tradeIndex": 1,
"collateralUsdc": "<string>",
"pairIndex": 1,
"pair": "<string>",
"delegate": "<string>",
"priceUpdateData": "<string>",
"priceSourcing": 0,
"oracleFeeWei": "<string>"
}
headers = {"Content-Type": "application/json"}
response = requests.post(url, json=payload, headers=headers)
print(response.text)const options = {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
trader: '<string>',
tradeIndex: 1,
collateralUsdc: '<string>',
pairIndex: 1,
pair: '<string>',
delegate: '<string>',
priceUpdateData: '<string>',
priceSourcing: 0,
oracleFeeWei: '<string>'
})
};
fetch('https://tx-builder-testnet.avantisfi.com/v2/margin/update', 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://tx-builder-testnet.avantisfi.com/v2/margin/update",
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([
'trader' => '<string>',
'tradeIndex' => 1,
'collateralUsdc' => '<string>',
'pairIndex' => 1,
'pair' => '<string>',
'delegate' => '<string>',
'priceUpdateData' => '<string>',
'priceSourcing' => 0,
'oracleFeeWei' => '<string>'
]),
CURLOPT_HTTPHEADER => [
"Content-Type: application/json"
],
]);
$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://tx-builder-testnet.avantisfi.com/v2/margin/update"
payload := strings.NewReader("{\n \"trader\": \"<string>\",\n \"tradeIndex\": 1,\n \"collateralUsdc\": \"<string>\",\n \"pairIndex\": 1,\n \"pair\": \"<string>\",\n \"delegate\": \"<string>\",\n \"priceUpdateData\": \"<string>\",\n \"priceSourcing\": 0,\n \"oracleFeeWei\": \"<string>\"\n}")
req, _ := http.NewRequest("POST", url, payload)
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://tx-builder-testnet.avantisfi.com/v2/margin/update")
.header("Content-Type", "application/json")
.body("{\n \"trader\": \"<string>\",\n \"tradeIndex\": 1,\n \"collateralUsdc\": \"<string>\",\n \"pairIndex\": 1,\n \"pair\": \"<string>\",\n \"delegate\": \"<string>\",\n \"priceUpdateData\": \"<string>\",\n \"priceSourcing\": 0,\n \"oracleFeeWei\": \"<string>\"\n}")
.asString();require 'uri'
require 'net/http'
url = URI("https://tx-builder-testnet.avantisfi.com/v2/margin/update")
http = Net::HTTP.new(url.host, url.port)
http.use_ssl = true
request = Net::HTTP::Post.new(url)
request["Content-Type"] = 'application/json'
request.body = "{\n \"trader\": \"<string>\",\n \"tradeIndex\": 1,\n \"collateralUsdc\": \"<string>\",\n \"pairIndex\": 1,\n \"pair\": \"<string>\",\n \"delegate\": \"<string>\",\n \"priceUpdateData\": \"<string>\",\n \"priceSourcing\": 0,\n \"oracleFeeWei\": \"<string>\"\n}"
response = http.request(request)
puts response.read_body{
"ok": true,
"data": {
"to": "0x0000000000000000000000000000000000000000",
"from": "0x0000000000000000000000000000000000000000",
"data": "<string>",
"value": "0x0",
"chainId": 123,
"description": "<string>",
"meta": {
"pair": "<string>",
"pairIndex": 123,
"tradeIndex": 123,
"action": "<string>",
"collateralUsdc": "<string>",
"priceSourcing": 123,
"oracleFeeWei": "<string>",
"delegated": true
}
}
}{
"ok": false,
"error": {
"code": "BAD_REQUEST",
"message": "<string>",
"details": "<unknown>"
}
}Body
The trader (position owner). USDC collateral is pulled from and paid out to this address.
Position slot index for this trader + pair (0-based; a trader can hold several positions on the same pair). Read it from GET /v2/positions → trades[].trade.index.
x >= 0deposit adds collateral to the position, withdraw removes it.
deposit, withdraw USDC to add to or remove from the position's margin.
Pair index (alternative to pair).
x >= 0Pair symbol, e.g. ETH/USD (separators /, -, _ accepted; case-insensitive).
1Optional pre-authorized delegate. When set, the call is wrapped in delegatedAction(trader, …) and from becomes this address — the delegate signs the transaction and pays gas. Register one via /v2/delegate/set.
Pre-fetched oracle price-update bytes (0x hex). Fetched from the Avantis price feed automatically when omitted.
^0x[0-9a-fA-F]*$Price oracle leg: 0 = Pyth Hermes (core), 1 = Pyth Lazer (pro). Auto-detected when omitted.
0 Oracle update fee attached as msg.value (NOT an operator fee). Default 1 wei.
Response
Success envelope: ok is true and data carries the payload documented below.