Skip to main content

Clawbacks

Clawbacks let an asset issuer burn a specific amount of a clawback-enabled asset. Introduced in CAP-0035, issuers can clawback from account trustlines or claimable balances. Clawbacks effectively destroy the assets by removing them from a recipient’s balance.

They allow asset issuers or their designated transfer agent to meet securities regulations, which in many jurisdictions require the ability to revoke assets in the event of a mistake, fraudulent transaction, or other regulatory action regarding a specific person or asset.

Clawbacks are useful for:

  • Recovering assets that have been fraudulently obtained,
  • Responding to regulatory actions, and
  • Enabling identity-proofed persons to recover an enabled asset in the event of loss of key custody or theft.

Operations

Set Options

The issuer sets up their account to enable clawbacks using the AUTH_CLAWBACK_ENABLED flag. This causes every subsequent trustline established to any assets issued by that account to have the TRUSTLINE_CLAWBACK_ENABLED_FLAG set automatically.

If an issuing account wants to set the AUTH_CLAWBACK_ENABLED_FLAG, it must have the AUTH_REVOCABLE_FLAG set. This allows an asset issuer to claw back balances locked up in offers by first revoking authorization from a trustline, which pulls all offers that involve that trustline. The issuer can then perform the clawback.

Clawback

The issuing account uses this operation to claw back some or all of an asset. Once an account holds a particular asset for which clawbacks have been enabled, the issuing account can claw it back, burning it. You need to provide the asset, a quantity, and the account from which you’re clawing back the asset. For more details, refer to the Clawback operation.

Clawback Claimable Balance

This operation claws back a claimable balance, returning the asset to the issuer account, burning it. You must claw back the entire claimable balance, not just part of it. Once a claimable balance has been claimed, use the regular clawback operation to claw it back. Clawback claimable balances require the claimable balance ID. For more details, refer to the Clawback Claimable Balance operation.

Set Trust Line Flag

The issuing account uses this operation to remove clawback capabilities on a specific trustline by removing the TRUSTLINE_CLAWBACK_ENABLED_FLAG via the SetTrustLineFlags operation.

You can only clear a flag, not set it. Thus, clearing a clawback flag on a trustline is irreversible. This is done so that you don’t retroactively change the rules for your asset holders. If you’d like to enable clawbacks again, holders must reissue their trustlines.

Examples

Here we’ll cover the following approaches to clawing back an asset.

  • Example 1: Issuing account A\mathcal{A} creates a clawback-enabled asset and sends it to Account B\mathcal{B}. Then, B\mathcal{B} sends that asset to Account C\mathcal{C}. Lastly, A\mathcal{A} will clawback the asset from C\mathcal{C}.
  • Example 2: B\mathcal{B} creates a claimable balance for C\mathcal{C}, and A\mathcal{A} claws back the new claimable balance.
  • Example 3: A\mathcal{A} issues a clawback-enabled asset to B\mathcal{B}. Then, A\mathcal{A} claws back some of the asset from B\mathcal{B}. Next, A\mathcal{A} removes the clawback-enabled flag from the trustline and can no longer clawback the asset.

Preamble: Issuing a Clawback-able Asset

First, we’ll set up an account to enable clawbacks and issue an asset accordingly. Properly issuing an asset with separate issuer and distributor accounts is a little more involved. We’ll start with a simpler method as an example.

note

We first need to enable clawbacks and then establish trustlines, since you cannot retroactively enable clawback on existing trustlines.

from stellar_sdk import Server, Keypair, Asset, Network, TransactionBuilder, Operation

server = Server("https://horizon-testnet.stellar.org")

A = Keypair.from_secret("SAQLZCQA6AYUXK6JSKVPJ2MZ5K5IIABJOEQIG4RVBHX4PG2KMRKWXCHJ")
B = Keypair.from_secret("SAAY2H7SANIS3JLFBFPLJRTYNLUYH4UTROIKRVFI4FEYV4LDW5Y7HDZ4")
C = Keypair.from_secret("SCZANGBA5YHTNYVVV4C3U252E2B6P6F5T3U6MM63WBSBZATAQI3EBTQ4")

AstroToken = Asset("ClawbackCoin", A.public_key)

def enableClawback(account, keys):
tx = buildTx(account, keys, [
Operation.set_options(
set_flags=Operation.Flag.AUTH_CLAWBACK_ENABLED_FLAG | Operation.Flag.AUTH_REVOCABLE_FLAG
# Also add `revocable` for control over who can hold the asset.
)
])
return server.submit_transaction(tx)

def establishTrustline(recipient, key):
tx = buildTx(recipient, key, [
Operation.change_trust(
asset=AstroToken,
)
])
return server.submit_transaction(tx)

def getAccounts():
return [
server.load_account(A.public_key),
server.load_account(B.public_key),
server.load_account(C.public_key)
]

def preamble():
accounts = getAccounts()
accountA, accountB, accountC = accounts
enableClawback(accountA, A)
return establishTrustline(accountB, B), establishTrustline(accountC, C)

def buildTx(source, signer, ops):
tx = TransactionBuilder(
source,
network_passphrase = Network.TESTNET,
base_fee = Network.BASE_FEE
)
for op in ops:
tx.append_operation(op)
tx.set_timeout(3600)
tx = tx.build()
tx.sign(signer)
return tx

def showBalances(accounts):
for accs in accounts:
print(f"{accs.account_id}: {getBalance(accs)}")

def getBalance(account):
for balance in account.balances:
if(
balance["asset_type"] != "native" and
balance["asset_code"] == AstroToken.code and
balance["asset_issuer"] == AstroToken.issuer
):
return balance["balance"]
return "0"

Example 1: Payments

With the shared setup code out of the way, we can now demonstrate how clawback works for payments. This example will highlight how the asset issuer holds control over their asset regardless of how it gets distributed to the world.

In our scenario, Account A\mathcal{A} will pay Account B\mathcal{B} with 1000 AstroToken; then, B\mathcal{B} will pay Account C\mathcal{C} 500 tokens in turn. Finally, A\mathcal{A} will claw back half of C\mathcal{C}’s balance, burning 250 tokens forever. Let’s dive into the helper functions:

# Make a payment to `toAccount` from `fromAccount` for `amount`.
def makePayment(toAccount, fromAccount, fromKey, amount):
tx = buildTx(fromAccount, fromKey, [
Operation.payment(
destination=toAccount.account_id,
asset=AstroToken,
amount=amount,
)
])
return server.submit_transaction(tx)

# Perform a clawback by `byAccount` of `amount` from `fromAccount`.
def doClawback(byAccount, byKey, fromAccount, amount):
tx = buildTx(byAccount, byKey, [
Operation.clawback(
from_=fromAccount.account_id,
asset=AstroToken,
amount=amount,
)
])
return server.submit_transaction(tx)

These snippets will help us with the final composition: making some payments to distribute the asset to the world and clawing some of it back.

def examplePaymentClawback():
accounts = getAccounts()
accountA, accountB, accountC = accounts

makePayment(accountB, accountA, A, "1000")
makePayment(accountC, accountB, B, "500")
doClawback(accountA, A, accountC, "250")

accounts = getAccounts()
showBalances(accounts)

After running our example, we should see the balances reflect the example flow:

A - GCIHA...72MJN: 0
B - GDS5N...C7KKX: 500
C - GC2BK...CQVGF: 250
Full Clawback Flow Chart

Notice that A\mathcal{A} (the issuer) holds none of the asset despite clawing back 250 from C\mathcal{C}. Thus, the clawed-back assets are burned, not transferred.

info

It may be strange that A\mathcal{A} never holds any AstroToken, but that’s exactly how issuing works: you create value where there used to be none. Sending an asset to its issuing account is equivalent to burning it, and auditing the total amount of an asset in existence is one of the benefits of properly distributing an asset.

Example 2: Claimable Balances

Direct payments aren’t the only way to transfer assets between accounts: claimable balances also do this. Since they are a separate payment mechanism, they need a separate clawback mechanism. For our example, you should be familiar with resolving balance IDs.

We need some additional helper methods to get started working efficiently with claimable balances:

def createClaimable(fromAccount, fromKey, toAccount, amount):
tx = buildTx(fromAccount, fromKey, [
Operation.create_claimable_balance(
asset = AstroToken,
amount = amount,
claimants = [
Claimant(
destination = toAccount.account_id
)
]
)
])
response = server.submit_transaction(tx)
return response

def getBalanceId(txResponse):
txResult = xdr.TransactionResult.from_xdr(txResponse["result_xdr"], "base64")
operationResult = txResult.result.results[0]
creationResult = operationResult.tr.create_claimable_balance_result
return creationResult.balance_id.to_xdr()

def clawbackClaimable(issuerAccount, issuerKey, balanceId):
tx = buildTx(issuerAccount, issuerKey, [
Operation.clawback_claimable_balance(balance_id = balanceId)
])
return server.submit_transaction(tx)

Now we can fulfill the flow: A\mathcal{A} pays B\mathcal{B}, who sends a claimable balance to C\mathcal{C}, who gets it clawed back by A\mathcal{A}. (Note that we rely on the makePayment helper from the earlier example.)

def exampleClaimableBalanceClawback():
accounts = getAccounts()
accountA, accountB, accountC = accounts

makePayment(accountB, accountA, A, "1000")
txResp = createClaimable(accountB, B, accountC, "500")
balanceId = getBalanceId(txResp)
clawbackClaimable(accountA, A, balanceId)

accounts = getAccounts()
showBalances(accounts)

After running preamble().then(examplePaymentClawback), we should see the balances reflect our flow:

A - GCIHA...72MJN: 0
B - GDS5N...C7KKX: 500
C - GC2BK...CQVGF: 0

Example 3: Selectively Enabling Clawback

When you enable the AUTH_CLAWBACK_ENABLED_FLAG on your account, it will make all future trustlines have clawback enabled for any of your issued assets. This may not always be desirable, as you may want certain assets to behave as they did before. Though you could work around this by reissuing assets from a “dedicated clawback” account, you can also simply disable clawbacks for certain trustlines by clearing the TRUSTLINE_CLAWBACK_ENABLED_FLAG on a trustline.

In the following example, we’ll have an account A\mathcal{A} issue a new asset and distribute it to a second account B\mathcal{B}. Next, we’ll demonstrate how A\mathcal{A} claws back some of the assets from B\mathcal{B}, then clears the trustline and can no longer claw back the asset.

First, let’s prepare the accounts using the helper functions defined in the earlier examples:

def preambleRedux():
accounts = getAccounts()
enableClawback(accounts[0], A)
establishTrustline(accounts[1], B)

Now, let’s distribute some of our asset to B\mathcal{B}, just to claw it back. Then, we’ll clear the flag from the trustline and show that another clawback isn’t possible:

def disableClawback(issuerAccount, issuerKeys, forTrustor):
tx = buildTx(issuerAccount, issuerKeys, [
Operation.set_trust_line_flags(
trustor = forTrustor.account_id,
asset = AstroToken,
clear_flags = Operation.Flag.TRUSTLINE_CLAWBACK_ENABLED_FLAG,
)
])
response = server.submit_transaction(tx)
return response

def exampleSelectiveClawback():
accounts = getAccounts()
accountA, accountB = accounts

makePayment(accountB, accountA, A, "1000")
accounts = getAccounts()
showBalances(accounts)

doClawback(accountA, A, accountB, "500")
accounts = getAccounts()
showBalances(accounts)

disableClawback(accountA, A, accountB)

try:
doClawback(accountA, A, accountB, "500")
except Exception as e:
if 'op_not_clawback_enabled' in str(e):
print("Clawback failed, as expected!")
else:
print("Uh-oh, other failure occurred")

accounts = getAccounts()
showBalances(accounts)

Next, we'll run the example:

preambleRedux()
exampleSelectiveClawback()

And then we can observe its result:

A - GCIHA...72MJN: 0
B - GDS5N...C7KKX: 1000

A - GCIHA...72MJN: 0
B - GDS5N...C7KKX: 500

Clawback failed, as expected!

A - GCIHA...72MJN: 0
B - GDS5N...C7KKX: 500

Guides in this category: