-
Notifications
You must be signed in to change notification settings - Fork 132
Document new ABI features #400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
32 commits
Select commit
Hold shift + click to select a range
e5c3101
Initial commit
jasonpaulos d6e8501
Fix abi-docs tests (#402)
michaeldiamant 9e01761
Fix abi-docs Sphinx warnings (#401)
michaeldiamant a581965
Extend abi-docs with experimental design language (#403)
michaeldiamant a5af855
Add type fundamentals section
jasonpaulos 4f1ec48
Add basic type usage and some docstrings for referenced methods
jasonpaulos 9a04c10
Finish documenting set and fix overloaded method docs
jasonpaulos 7ef9537
Add docstrings for get and __getitem__
jasonpaulos 0c7fe6f
Add reference type docs
jasonpaulos 33da26c
Add txn type examples
jasonpaulos 6a77a65
Fix errors
jasonpaulos 5e61e18
Make ComputedValue parameter type covariant
jasonpaulos 41d8e85
ComputedValue and subroutine sections
jasonpaulos b5d00a0
...wasn't included in previous commit
jasonpaulos 35b60fe
Add bare app call and method registration examples
jasonpaulos 67edead
Add router e2e example and compilation explanation
jasonpaulos 6982f77
Merge branch 'feature/abi' into abi-docs
jasonpaulos cd5d212
Fix post-merge linter/test failures
jasonpaulos 96b2cbd
Add calling documentation
jasonpaulos d37cc0f
Merge branch 'feature/abi' into abi-docs
jasonpaulos e3c7312
Partially address feedback
jasonpaulos 3b42c1c
Merge branch 'feature/abi' into abi-docs
jasonpaulos fe5d68b
Respond to feedback
jasonpaulos 7aabc2e
Resolve TODOs
jasonpaulos 13a81ab
Add pragma references
jasonpaulos bf2c233
didn't make it into the previous commit
jasonpaulos 7eae32c
Fix Bool.__module__
jasonpaulos e10f84f
Mention stack size limit
jasonpaulos fdbbc96
More pragma documentation
jasonpaulos f49ec24
Address other feedback
jasonpaulos bf85959
Warn about reference type limits
jasonpaulos 44ea401
**cannot**
jasonpaulos File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| { | ||
| "name": "AlgoBank", | ||
| "methods": [ | ||
| { | ||
| "name": "deposit", | ||
| "args": [ | ||
| { | ||
| "type": "pay", | ||
| "name": "payment" | ||
| }, | ||
| { | ||
| "type": "account", | ||
| "name": "sender" | ||
| } | ||
| ], | ||
| "returns": { | ||
| "type": "void" | ||
| }, | ||
| "desc": "This method receives a payment from an account opted into this app and records it in their local state. The caller may opt into this app during this call." | ||
| }, | ||
| { | ||
| "name": "getBalance", | ||
| "args": [ | ||
| { | ||
| "type": "account", | ||
| "name": "user" | ||
| } | ||
| ], | ||
| "returns": { | ||
| "type": "uint64" | ||
| }, | ||
| "desc": "Lookup the balance of a user held by this app." | ||
| }, | ||
| { | ||
| "name": "withdraw", | ||
| "args": [ | ||
| { | ||
| "type": "uint64", | ||
| "name": "amount" | ||
| }, | ||
| { | ||
| "type": "account", | ||
| "name": "recipient" | ||
| } | ||
| ], | ||
| "returns": { | ||
| "type": "void" | ||
| }, | ||
| "desc": "Withdraw an amount of Algos held by this app. The sender of this method call will be the source of the Algos, and the destination will be the `recipient` argument. This may or may not be the same as the sender's address. This method will fail if the amount of Algos requested to be withdrawn exceeds the amount of Algos held by this app for the sender. The Algos will be transferred to the recipient using an inner transaction whose fee is set to 0, meaning the caller's transaction must include a surplus fee to cover the inner transaction." | ||
| } | ||
| ], | ||
| "desc": null, | ||
| "networks": {} | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,113 @@ | ||
| # This example is provided for informational purposes only and has not been audited for security. | ||
| from pyteal import * | ||
| import json | ||
|
|
||
|
|
||
| @Subroutine(TealType.none) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. really nice example!!! |
||
| def assert_sender_is_creator() -> Expr: | ||
| return Assert(Txn.sender() == Global.creator_address()) | ||
|
|
||
|
|
||
| # move any balance that the user has into the "lost" amount when they close out or clear state | ||
| transfer_balance_to_lost = App.globalPut( | ||
| Bytes("lost"), | ||
| App.globalGet(Bytes("lost")) + App.localGet(Txn.sender(), Bytes("balance")), | ||
| ) | ||
|
|
||
| router = Router( | ||
| name="AlgoBank", | ||
| bare_calls=BareCallActions( | ||
| # approve a creation no-op call | ||
| no_op=OnCompleteAction(action=Approve(), call_config=CallConfig.CREATE), | ||
| # approve opt-in calls during normal usage, and during creation as a convenience for the creator | ||
| opt_in=OnCompleteAction(action=Approve(), call_config=CallConfig.ALL), | ||
| # move any balance that the user has into the "lost" amount when they close out or clear state | ||
| close_out=OnCompleteAction( | ||
| action=transfer_balance_to_lost, call_config=CallConfig.CALL | ||
| ), | ||
| clear_state=OnCompleteAction( | ||
| action=transfer_balance_to_lost, call_config=CallConfig.CALL | ||
| ), | ||
| # only the creator can update or delete the app | ||
| update_application=OnCompleteAction( | ||
| action=assert_sender_is_creator, call_config=CallConfig.CALL | ||
| ), | ||
tzaffi marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| delete_application=OnCompleteAction( | ||
| action=assert_sender_is_creator, call_config=CallConfig.CALL | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| @router.method(no_op=CallConfig.CALL, opt_in=CallConfig.CALL) | ||
| def deposit(payment: abi.PaymentTransaction, sender: abi.Account) -> Expr: | ||
| """This method receives a payment from an account opted into this app and records it in | ||
| their local state. | ||
| The caller may opt into this app during this call. | ||
| """ | ||
| return Seq( | ||
| Assert(payment.get().sender() == sender.address()), | ||
| Assert(payment.get().receiver() == Global.current_application_address()), | ||
| App.localPut( | ||
| sender.address(), | ||
| Bytes("balance"), | ||
| App.localGet(sender.address(), Bytes("balance")) + payment.get().amount(), | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| @router.method | ||
| def getBalance(user: abi.Account, *, output: abi.Uint64) -> Expr: | ||
| """Lookup the balance of a user held by this app.""" | ||
| return output.set(App.localGet(user.address(), Bytes("balance"))) | ||
|
|
||
|
|
||
| @router.method | ||
| def withdraw(amount: abi.Uint64, recipient: abi.Account) -> Expr: | ||
| """Withdraw an amount of Algos held by this app. | ||
| The sender of this method call will be the source of the Algos, and the destination will be | ||
| the `recipient` argument. This may or may not be the same as the sender's address. | ||
| This method will fail if the amount of Algos requested to be withdrawn exceeds the amount of | ||
| Algos held by this app for the sender. | ||
| The Algos will be transferred to the recipient using an inner transaction whose fee is set | ||
| to 0, meaning the caller's transaction must include a surplus fee to cover the inner | ||
| transaction. | ||
| """ | ||
| return Seq( | ||
| # if amount is larger than App.localGet(Txn.sender(), Bytes("balance")), the subtraction | ||
| # will underflow and fail this method call | ||
| App.localPut( | ||
| Txn.sender(), | ||
| Bytes("balance"), | ||
| App.localGet(Txn.sender(), Bytes("balance")) - amount.get(), | ||
| ), | ||
| InnerTxnBuilder.Begin(), | ||
| InnerTxnBuilder.SetFields( | ||
| { | ||
| TxnField.type_enum: TxnType.Payment, | ||
| TxnField.receiver: recipient.address(), | ||
| TxnField.amount: amount.get(), | ||
| TxnField.fee: Int(0), | ||
| } | ||
| ), | ||
| InnerTxnBuilder.Submit(), | ||
| ) | ||
|
|
||
|
|
||
| approval_program, clear_state_program, contract = router.compile_program( | ||
| version=6, optimize=OptimizeOptions(scratch_slots=True) | ||
| ) | ||
|
|
||
| if __name__ == "__main__": | ||
| with open("algobank_approval.teal", "w") as f: | ||
| f.write(approval_program) | ||
|
|
||
| with open("algobank_clear_state.teal", "w") as f: | ||
| f.write(clear_state_program) | ||
|
|
||
| with open("algobank.json", "w") as f: | ||
| f.write(json.dumps(contract.dictify(), indent=4)) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.