From 31006aae836231313bd872d7575ada82df419453 Mon Sep 17 00:00:00 2001 From: Abhimanyu Paldiwal Date: Tue, 27 Jul 2021 16:09:43 -0700 Subject: [PATCH 1/3] Update clawback.mdx --- content/docs/glossary/clawback.mdx | 139 +++++++++++++++++++++++++++++ 1 file changed, 139 insertions(+) diff --git a/content/docs/glossary/clawback.mdx b/content/docs/glossary/clawback.mdx index 23f51ee09..f204d877f 100644 --- a/content/docs/glossary/clawback.mdx +++ b/content/docs/glossary/clawback.mdx @@ -130,6 +130,94 @@ function showBalances(accounts) { }); } ``` + +```python +from stellar_sdk import ( + Server, + Keypair, + TransactionBuilder, + Network, + Asset, + AuthorizationFlag, + SetOptions, + ChangeTrust, + Payment, + Clawback +) + +server = Server("https://horizon-testnet.stellar.org") + +A = Keypair.from_secret("SC7E6KAA7QUOTKFWGFTGTVOHG4DSAEJERIAI3S66MTDUDMINGFCJCQA5") +B = Keypair.from_secret("SB7O2LAIWJGNRLFVJ3PSFU5NBA6XTYWKGAAO5SGC6O2CLQUVVVHMFFWU") +C = Keypair.from_secret("SDE32QTQ5HTDMFJDN4YFQLHBIH5IFJ5X2L6N6A2KK5JFX54GPKUTBGCS") + +ASSET = Asset(code = "CLAW", issuer = A.public_key) + +# Enables AuthClawbackEnabledFlag on an account. +def enableClawback(account, keys): + tx = buildTx( + source = account, + signer = keys, + ops = [ + SetOptions( + set_flags = AuthorizationFlag.AUTHORIZATION_CLAWBACK_ENABLED | + AuthorizationFlag.AUTHORIZATION_REVOCABLE + ) + ] + ) + return server.submit_transaction(tx) + +# Establishes a trustline for `recipient` for ASSET (from above). +def establishTrustLine(recipient, key): + tx = buildTx( + source = recipient, + signer = key, + ops = [ + ChangeTrust( + asset = ASSET, + limit = "5000" # arbitrary + ) + ] + ) + return server.submit_transaction(tx) + +# Retrieves latest account info for all accounts. +def getAccounts(): + return( + server.load_account(A.public_key), + server.load_account(B.public_key), + server.load_account(C.public_key) + ) + +# Enables clawback on A, and establishes trustlines from C, B -> A. +def preamble(): + accountA, accountB, accountC = getAccounts() + return( + enableClawback(accountA, A), + establishTrustLine(accountB, B), + establishTrustLine(accountC, C) + ) + +# Helps simplify creating & signing a transaction. +def buildTx(source, signer, ops): + tx = ( + TransactionBuilder ( + source_account = source, + network_passphrase = Network.TESTNET_NETWORK_PASSPHRASE, + base_fee = server.fetch_base_fee() + ) + ) + for op in ops: + tx.append_operation(op) + tx = tx.set_timeout(30).build() + tx.sign(signer) + return tx + +# Prints the balances of a list of accounts. +def showBalance(accounts): + for acc in accounts: + print(acc.account_id[0:5] + ": " + getBalance(acc)) +``` @@ -178,6 +266,42 @@ function getBalance(account) { return balances.length > 0 ? balances[0].balance : "0"; } ``` + +```python +# Make a payment to `toAccount` from `fromAccount` for `amount`. +def makePayment(toAccount, fromAccount, fromKey, amount): + tx = buildTx( + source = fromAccount, + signer = fromKey, + ops = [ + Payment( + destination = toAccount.account_id, + asset = ASSET, + amount = amount + ) + ] + ) + return server.submit_transaction(tx) + +# Perform a clawback by `byAccount` of `amount` from `fromAccount`. +def doClawback(byAccount, byKey, fromAccount, amount): + tx = buildTx( + source = byAccount, + signer = byKey, + ops = [ + Clawback( + asset = ASSET, + from_ = fromAccount.account_id, + amount = amount + ) + ] + ) + return server.submit_transaction(tx) + +# Retrieves the balance of ASSET in `account`. +def getBalance(account): + [(balance.asset_code, balance.asset_issuer) for balance in account.balances] +``` @@ -200,6 +324,21 @@ function examplePaymentClawback() { preamble().then(examplePaymentClawback); ``` +```python +def examplePaymentClawback(): + accountA, accountB, accountC = getAccounts() + return( + makePayment(accountB, accountA, A, "1000"), + makePayment(accountC, accountB, B, "500"), + doClawback(accountA, A, accountC, "250") + ) + +if __name__ == "__main__": + preamble() + examplePaymentClawback() + showBalances(getAccounts()) +``` + After running our example, we should see the balances reflect the example flow: From 41699916092c9b2329e7d6e25239a7a58e72309a Mon Sep 17 00:00:00 2001 From: Abhimanyu Paldiwal Date: Wed, 4 Aug 2021 16:12:06 -0700 Subject: [PATCH 2/3] Update clawback.mdx --- content/docs/glossary/clawback.mdx | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/content/docs/glossary/clawback.mdx b/content/docs/glossary/clawback.mdx index f204d877f..0fb1dd75b 100644 --- a/content/docs/glossary/clawback.mdx +++ b/content/docs/glossary/clawback.mdx @@ -216,7 +216,7 @@ def buildTx(source, signer, ops): # Prints the balances of a list of accounts. def showBalance(accounts): for acc in accounts: - print(acc.account_id[0:5] + ": " + getBalance(acc)) + print(acc.account_id()[0:5] + ": " + getBalance(acc)) ``` @@ -300,7 +300,15 @@ def doClawback(byAccount, byKey, fromAccount, amount): # Retrieves the balance of ASSET in `account`. def getBalance(account): - [(balance.asset_code, balance.asset_issuer) for balance in account.balances] + balances = server \ + .accounts() \ + .account_id(account.account_id()) \ + .call()["balances"] + + for i in range(len(balances)): + if balances[i].get("asset_code") == ASSET.code and balances[i].get("asset_issuer") == ASSET.issuer: + return balances[i]["balance"] + return "0" ``` From f8369553d17bb651ba672b5aabc082d990dce553 Mon Sep 17 00:00:00 2001 From: Abhimanyu Paldiwal Date: Wed, 4 Aug 2021 17:05:37 -0700 Subject: [PATCH 3/3] Update clawback.mdx --- content/docs/glossary/clawback.mdx | 42 +++++++++++++++--------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/content/docs/glossary/clawback.mdx b/content/docs/glossary/clawback.mdx index 0fb1dd75b..c0bbb7ded 100644 --- a/content/docs/glossary/clawback.mdx +++ b/content/docs/glossary/clawback.mdx @@ -133,10 +133,10 @@ function showBalances(accounts) { ```python from stellar_sdk import ( - Server, - Keypair, - TransactionBuilder, - Network, + Server, + Keypair, + TransactionBuilder, + Network, Asset, AuthorizationFlag, SetOptions, @@ -147,9 +147,9 @@ from stellar_sdk import ( server = Server("https://horizon-testnet.stellar.org") -A = Keypair.from_secret("SC7E6KAA7QUOTKFWGFTGTVOHG4DSAEJERIAI3S66MTDUDMINGFCJCQA5") -B = Keypair.from_secret("SB7O2LAIWJGNRLFVJ3PSFU5NBA6XTYWKGAAO5SGC6O2CLQUVVVHMFFWU") -C = Keypair.from_secret("SDE32QTQ5HTDMFJDN4YFQLHBIH5IFJ5X2L6N6A2KK5JFX54GPKUTBGCS") +A = Keypair.from_secret("SARQWRSMT5REPFQHNBHSPX7WICDJZZ43GD4YICYBNKIA7WZB3MTHAPZL") +B = Keypair.from_secret("SDFKWXO3WRD5ZHS4G5IZYFOBBUYPMUKFNMK5TTKGIMSGHAZ4CPDGJMM7") +C = Keypair.from_secret("SB7QOMOLKBXGOKE5JUK26ZO5ANPAF7OLE76ZT5Y7MSCK6CYQNVRCQIBR") ASSET = Asset(code = "CLAW", issuer = A.public_key) @@ -160,15 +160,15 @@ def enableClawback(account, keys): signer = keys, ops = [ SetOptions( - set_flags = AuthorizationFlag.AUTHORIZATION_CLAWBACK_ENABLED | + set_flags = AuthorizationFlag.AUTHORIZATION_CLAWBACK_ENABLED | AuthorizationFlag.AUTHORIZATION_REVOCABLE ) ] ) return server.submit_transaction(tx) - + # Establishes a trustline for `recipient` for ASSET (from above). -def establishTrustLine(recipient, key): +def establishTrustline(recipient, key): tx = buildTx( source = recipient, signer = key, @@ -187,17 +187,17 @@ def getAccounts(): server.load_account(A.public_key), server.load_account(B.public_key), server.load_account(C.public_key) - ) + ) # Enables clawback on A, and establishes trustlines from C, B -> A. def preamble(): accountA, accountB, accountC = getAccounts() return( enableClawback(accountA, A), - establishTrustLine(accountB, B), - establishTrustLine(accountC, C) + establishTrustline(accountB, B), + establishTrustline(accountC, C) ) - + # Helps simplify creating & signing a transaction. def buildTx(source, signer, ops): tx = ( @@ -214,7 +214,7 @@ def buildTx(source, signer, ops): return tx # Prints the balances of a list of accounts. -def showBalance(accounts): +def showBalances(accounts): for acc in accounts: print(acc.account_id()[0:5] + ": " + getBalance(acc)) ``` @@ -275,7 +275,7 @@ def makePayment(toAccount, fromAccount, fromKey, amount): signer = fromKey, ops = [ Payment( - destination = toAccount.account_id, + destination = toAccount.account_id(), asset = ASSET, amount = amount ) @@ -291,20 +291,20 @@ def doClawback(byAccount, byKey, fromAccount, amount): ops = [ Clawback( asset = ASSET, - from_ = fromAccount.account_id, + from_ = fromAccount.account_id(), amount = amount ) ] ) return server.submit_transaction(tx) -# Retrieves the balance of ASSET in `account`. -def getBalance(account): + # Retrieves the balance of ASSET in `account`. +def getBalance(account): balances = server \ .accounts() \ .account_id(account.account_id()) \ .call()["balances"] - + for i in range(len(balances)): if balances[i].get("asset_code") == ASSET.code and balances[i].get("asset_issuer") == ASSET.issuer: return balances[i]["balance"] @@ -345,7 +345,7 @@ if __name__ == "__main__": preamble() examplePaymentClawback() showBalances(getAccounts()) -``` + ```