Borrowing
Repay Loan
Repay loan debt, interest first then principal
POST
/
lending-external-securities
/
api
/
positions
/
{marketId}
/
repay
curl -X POST "https://api.trusset.org/lending-external-securities/api/positions/{marketId}/repay" \
-H "X-API-Key: trusset_your_key_here" \
-H "Content-Type: application/json" \
-d '{"loanId": 5, "amount": "10000"}'
const res = await fetch(
`https://api.trusset.org/lending-external-securities/api/positions/${marketId}/repay`,
{
method: 'POST',
headers: {
'X-API-Key': 'trusset_your_key_here',
'Content-Type': 'application/json'
},
body: JSON.stringify({ loanId: 5, all: true })
}
);
const { data } = await res.json();
const hashes = [];
for (const step of data.steps) {
const tx = await wallet.sendTransaction(step.transaction);
await tx.wait();
hashes.push(tx.hash);
}
await fetch(
`https://api.trusset.org/lending-external-securities/api/positions/${marketId}/repay`,
{
method: 'POST',
headers: {
'X-API-Key': 'trusset_your_key_here',
'Content-Type': 'application/json'
},
body: JSON.stringify({ loanId: 5, txHash: hashes[data.confirmStepIndex] })
}
);
{
"success": true,
"data": {
"action": "SIGN_TRANSACTIONS",
"steps": [
{
"action": "SIGN_TRANSACTION",
"transaction": {
"to": "0x98ad0ca091552e23c564b41c74282e5343d03e8a",
"data": "0x...",
"value": "0",
"chainId": 11155111
},
"functionName": "approve",
"description": "Approve the market to pull USDC"
},
{
"action": "SIGN_TRANSACTION",
"transaction": {
"to": "0x70a0e25c7b768b87e658348b3b577678a173e038",
"data": "0x...",
"value": "0",
"chainId": 11155111
},
"functionName": "repay"
},
{
"action": "SIGN_TRANSACTION",
"transaction": {
"to": "0x98ad0ca091552e23c564b41c74282e5343d03e8a",
"data": "0x...",
"value": "0",
"chainId": 11155111
},
"functionName": "approve",
"description": "Clear the leftover USDC allowance granted for this repayment"
}
],
"fullRepayment": true,
"confirmStepIndex": 1,
"confirmWith": "repay",
"outstandingDebt": "50412.881000",
"repayAmount": "50513.706763"
}
}
{
"success": true,
"data": {
"txHash": "0x9f2c41d8b7e05a3164c2870fbd935e1a4c7802db6135ea9048f7c21b5d3ea41b",
"loanId": 5,
"repaidAmount": "50412.913422"
}
}
{
"success": false,
"error": {
"code": "TX_WRONG_TARGET",
"message": "This transaction does not target the market. A full repayment is signed as several steps and only the repay step can be confirmed — use the hash of the step whose functionName is \"repay\" (index 1 of the steps returned for this repayment), not the last one."
}
}
Repays part or all of a loan’s outstanding debt. Accrued interest is settled before principal. Anyone can repay any loan, so a third party can rescue a position approaching liquidation.
At least two transactions are required: an approval on the borrow asset, then the repayment. They come back as ordered steps.
Path Parameters
string
required
Market ID.
Body Parameters
integer
required
On-chain loan ID. A positive integer. Accepted as a JSON number or as a numeric string, so the value Get Loan returns passes straight back.
string
Amount to repay as a decimal string in borrow asset units. The contract caps repayment at the total debt, so overpaying does not transfer more than owed. Required unless
all is true or you are confirming with txHash.boolean
Repay the loan in full. The API reads the current debt and sizes the approval with a small overshoot, so interest accruing between the quote and the mine cannot leave dust behind. Use this instead of computing a payoff yourself.
string
Hash of the transaction you broadcast for this operation. Send it to confirm the transaction and record the result. Omit it to receive the calldata.
Transaction Shape
A partial repayment returns two steps:approve on the borrow asset, then repay.
A full repayment (all: true) returns three. The third resets the allowance to zero. repay caps the pull at the true debt, so without it the unused approval would stay live against the payer’s funds after the loan closed.
On a full repayment the confirmable step is no longer the last one. Confirm with the hash of the step named by
confirmStepIndex, which is always 1 here and always the step whose functionName is repay. Confirming with the allowance-reset hash returns TX_WRONG_TARGET.Response Fields
object
Show child attributes
Show child attributes
string
SIGN_TRANSACTIONS. Returned when txHash is omitted.array
Ordered transactions to broadcast. Returned when
txHash is omitted.boolean
Whether the payload settles the loan in full.
integer
Index of the step to confirm with. Always
1.string
repay.string
Principal plus accrued interest at the time the payload was built.
string
Amount the
repay step is sized for, including the overshoot on a full repayment.object
Present on a full repayment when releasing the collateral is restricted. Describes what the collateral token would do.
string
Human-readable form of
collateralRelease, present alongside it.boolean
Nested inside
data on the calldata response.string
Transaction hash. Returned when confirming.
integer
Loan ID, echoed back. Returned when confirming.
string
Amount that actually moved, read from the
LoanRepaid event rather than from the calldata. On a full repayment this is lower than repayAmount, because the contract pulls only the true debt. Returned when confirming.Repaying the full debt does not release collateral on its own. Follow with Close Loan to unlock the pledged tokens and mark the position repaid.
curl -X POST "https://api.trusset.org/lending-external-securities/api/positions/{marketId}/repay" \
-H "X-API-Key: trusset_your_key_here" \
-H "Content-Type: application/json" \
-d '{"loanId": 5, "amount": "10000"}'
const res = await fetch(
`https://api.trusset.org/lending-external-securities/api/positions/${marketId}/repay`,
{
method: 'POST',
headers: {
'X-API-Key': 'trusset_your_key_here',
'Content-Type': 'application/json'
},
body: JSON.stringify({ loanId: 5, all: true })
}
);
const { data } = await res.json();
const hashes = [];
for (const step of data.steps) {
const tx = await wallet.sendTransaction(step.transaction);
await tx.wait();
hashes.push(tx.hash);
}
await fetch(
`https://api.trusset.org/lending-external-securities/api/positions/${marketId}/repay`,
{
method: 'POST',
headers: {
'X-API-Key': 'trusset_your_key_here',
'Content-Type': 'application/json'
},
body: JSON.stringify({ loanId: 5, txHash: hashes[data.confirmStepIndex] })
}
);
{
"success": true,
"data": {
"action": "SIGN_TRANSACTIONS",
"steps": [
{
"action": "SIGN_TRANSACTION",
"transaction": {
"to": "0x98ad0ca091552e23c564b41c74282e5343d03e8a",
"data": "0x...",
"value": "0",
"chainId": 11155111
},
"functionName": "approve",
"description": "Approve the market to pull USDC"
},
{
"action": "SIGN_TRANSACTION",
"transaction": {
"to": "0x70a0e25c7b768b87e658348b3b577678a173e038",
"data": "0x...",
"value": "0",
"chainId": 11155111
},
"functionName": "repay"
},
{
"action": "SIGN_TRANSACTION",
"transaction": {
"to": "0x98ad0ca091552e23c564b41c74282e5343d03e8a",
"data": "0x...",
"value": "0",
"chainId": 11155111
},
"functionName": "approve",
"description": "Clear the leftover USDC allowance granted for this repayment"
}
],
"fullRepayment": true,
"confirmStepIndex": 1,
"confirmWith": "repay",
"outstandingDebt": "50412.881000",
"repayAmount": "50513.706763"
}
}
{
"success": true,
"data": {
"txHash": "0x9f2c41d8b7e05a3164c2870fbd935e1a4c7802db6135ea9048f7c21b5d3ea41b",
"loanId": 5,
"repaidAmount": "50412.913422"
}
}
{
"success": false,
"error": {
"code": "TX_WRONG_TARGET",
"message": "This transaction does not target the market. A full repayment is signed as several steps and only the repay step can be confirmed — use the hash of the step whose functionName is \"repay\" (index 1 of the steps returned for this repayment), not the last one."
}
}
Error Codes
| Code | HTTP | Cause |
|---|---|---|
NO_MARKET_ADDRESS | 400 | The market has no on-chain address recorded |
VALIDATION_ERROR | 400 | loanId is not a positive integer, or neither amount nor all was supplied |
INVALID_AMOUNT | 400 | amount carries more decimal places than the borrow asset supports |
NO_OUTSTANDING_DEBT | 400 | all was set on a loan that owes nothing |
LOAN_NOT_ACTIVE | 400 | The loan is closed and can no longer be modified |
TX_WRONG_TARGET | 400 | The confirmed transaction does not target the market. On a full repayment, confirm the repay step |
TX_NOT_VERIFIED | 400 | The confirmed transaction repays a different loan |
REPAY_FAILED | 400 | The repayment could not be prepared and no more specific code applied |
LOAN_NOT_FOUND | 404 | No such loan on this market |
LOAN_STATE_UNAVAILABLE | 503 | The loan could not be read from the network right now. Retry shortly |
⌘I
curl -X POST "https://api.trusset.org/lending-external-securities/api/positions/{marketId}/repay" \
-H "X-API-Key: trusset_your_key_here" \
-H "Content-Type: application/json" \
-d '{"loanId": 5, "amount": "10000"}'
const res = await fetch(
`https://api.trusset.org/lending-external-securities/api/positions/${marketId}/repay`,
{
method: 'POST',
headers: {
'X-API-Key': 'trusset_your_key_here',
'Content-Type': 'application/json'
},
body: JSON.stringify({ loanId: 5, all: true })
}
);
const { data } = await res.json();
const hashes = [];
for (const step of data.steps) {
const tx = await wallet.sendTransaction(step.transaction);
await tx.wait();
hashes.push(tx.hash);
}
await fetch(
`https://api.trusset.org/lending-external-securities/api/positions/${marketId}/repay`,
{
method: 'POST',
headers: {
'X-API-Key': 'trusset_your_key_here',
'Content-Type': 'application/json'
},
body: JSON.stringify({ loanId: 5, txHash: hashes[data.confirmStepIndex] })
}
);
{
"success": true,
"data": {
"action": "SIGN_TRANSACTIONS",
"steps": [
{
"action": "SIGN_TRANSACTION",
"transaction": {
"to": "0x98ad0ca091552e23c564b41c74282e5343d03e8a",
"data": "0x...",
"value": "0",
"chainId": 11155111
},
"functionName": "approve",
"description": "Approve the market to pull USDC"
},
{
"action": "SIGN_TRANSACTION",
"transaction": {
"to": "0x70a0e25c7b768b87e658348b3b577678a173e038",
"data": "0x...",
"value": "0",
"chainId": 11155111
},
"functionName": "repay"
},
{
"action": "SIGN_TRANSACTION",
"transaction": {
"to": "0x98ad0ca091552e23c564b41c74282e5343d03e8a",
"data": "0x...",
"value": "0",
"chainId": 11155111
},
"functionName": "approve",
"description": "Clear the leftover USDC allowance granted for this repayment"
}
],
"fullRepayment": true,
"confirmStepIndex": 1,
"confirmWith": "repay",
"outstandingDebt": "50412.881000",
"repayAmount": "50513.706763"
}
}
{
"success": true,
"data": {
"txHash": "0x9f2c41d8b7e05a3164c2870fbd935e1a4c7802db6135ea9048f7c21b5d3ea41b",
"loanId": 5,
"repaidAmount": "50412.913422"
}
}
{
"success": false,
"error": {
"code": "TX_WRONG_TARGET",
"message": "This transaction does not target the market. A full repayment is signed as several steps and only the repay step can be confirmed — use the hash of the step whose functionName is \"repay\" (index 1 of the steps returned for this repayment), not the last one."
}
}
