-
Notifications
You must be signed in to change notification settings - Fork 0
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
Update Executor Contracts #7
base: main
Are you sure you want to change the base?
Changes from 29 commits
7f10e0b
fe7afb7
ab56782
64f15a1
dee010a
092ca1b
2007c4f
f6515fb
6e20eed
6201f0d
fe72cd9
e25bcdd
1bb0053
dd5fb4c
5362922
e002106
260330c
099172a
9058eee
4217e5d
065e36c
d3ead63
f9bf5ac
3a48973
bba767c
84ef7af
08eec8d
abe561e
673877c
12e1377
1eb32cb
719d309
ca111c5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
#pragma version 9 | ||
#tealish version git+https://github.com/Hipo/tealish.git@c4f8c12b36dbcc8b63844df5f818164925735087 | ||
|
||
# Taken from Proposal Voting App | ||
# Size: 123 bytes | ||
struct Proposal: | ||
index: int | ||
creation_timestamp: int | ||
voting_start_timestamp: int | ||
voting_end_timestamp: int | ||
snapshot_total_voting_power: int | ||
vote_count: int | ||
quorum_threshold: int | ||
against_voting_power: int | ||
for_voting_power: int | ||
abstain_voting_power: int | ||
is_approved: bytes[1] | ||
is_cancelled: bytes[1] | ||
is_executed: bytes[1] | ||
is_quorum_reached: bytes[1] | ||
proposer: bytes[32] | ||
execution_hash: bytes[34] | ||
executor: bytes[32] | ||
end | ||
|
||
# 24 * 60 * 60 | ||
const int DAY = 86400 | ||
const bytes BYTES_FALSE = "\x00" | ||
const bytes BYTES_TRUE = "\x80" | ||
|
||
const bytes MANAGER_KEY = "manager" | ||
const bytes PROPOSAL_VOTING_APP_ID_KEY = "proposal_voting_app_id" | ||
|
||
# Proposal States | ||
const int PROPOSAL_STATE_WAITING_FOR_APPROVAL = 0 | ||
const int PROPOSAL_STATE_CANCELLED = 1 | ||
const int PROPOSAL_STATE_PENDING = 2 | ||
const int PROPOSAL_STATE_ACTIVE = 3 | ||
const int PROPOSAL_STATE_DEFEATED = 4 | ||
const int PROPOSAL_STATE_SUCCEEDED = 5 | ||
const int PROPOSAL_STATE_EXECUTED = 6 | ||
|
||
# Execution Hash Prefixes | ||
const bytes VALIDATE_TRANSACTION_HASH_PREFIX = "vt" | ||
const bytes VALIDATE_GROUP_HASH_PREFIX = "vg" | ||
|
||
router: | ||
create_application | ||
update_application | ||
validate_transaction | ||
validate_group | ||
end | ||
|
||
@public(OnCompletion=CreateApplication) | ||
func create_application(proposal_voting_app_id: int): | ||
app_global_put(PROPOSAL_VOTING_APP_ID_KEY, proposal_voting_app_id) | ||
|
||
app_global_put(MANAGER_KEY, Txn.Sender) | ||
|
||
return | ||
end | ||
|
||
@public(OnCompletion=UpdateApplication) | ||
func update_application(): | ||
bytes user_address = Txn.Sender | ||
assert(user_address == app_global_get(MANAGER_KEY)) | ||
|
||
return | ||
end | ||
|
||
@public() | ||
func validate_transaction(proposal_id: bytes): | ||
# Read proposal from proposal_voting app | ||
inner_txn: | ||
TypeEnum: Appl | ||
ApplicationID: app_global_get(PROPOSAL_VOTING_APP_ID_KEY) | ||
ApplicationArgs[0]: "get_proposal" | ||
ApplicationArgs[1]: proposal_id | ||
Fee: 0 | ||
end | ||
|
||
Proposal proposal = Cast(extract(4, 182, Itxn.LastLog), Proposal) | ||
int proposal_state = btoi(extract(186, 0, Itxn.LastLog)) | ||
|
||
# proposal checks | ||
assert(proposal_state == PROPOSAL_STATE_SUCCEEDED) | ||
|
||
# Assert Gtxn[1]'s transaction_id. | ||
bytes execution_hash = Concat(VALIDATE_TRANSACTION_HASH_PREFIX, Gtxn[1].TxID) | ||
assert(execution_hash == proposal.execution_hash) | ||
|
||
set_proposal_as_executed(proposal_id) | ||
return | ||
end | ||
|
||
@public() | ||
func validate_group(proposal_id: bytes): | ||
# 1. Read proposal from proposal_voting app | ||
inner_txn: | ||
TypeEnum: Appl | ||
ApplicationID: app_global_get(PROPOSAL_VOTING_APP_ID_KEY) | ||
ApplicationArgs[0]: "get_proposal" | ||
ApplicationArgs[1]: proposal_id | ||
Fee: 0 | ||
end | ||
|
||
Proposal proposal = Cast(extract(4, 182, Itxn.LastLog), Proposal) | ||
int proposal_state = btoi(extract(186, 0, Itxn.LastLog)) | ||
|
||
# 2 .Proposal checks | ||
assert(proposal_state == PROPOSAL_STATE_SUCCEEDED) | ||
|
||
bytes execution_hash = Concat(VALIDATE_GROUP_HASH_PREFIX, Global.GroupID) | ||
assert(execution_hash == proposal.execution_hash) | ||
|
||
set_proposal_as_executed(proposal_id) | ||
|
||
return | ||
end | ||
|
||
func set_proposal_as_executed(proposal_id: bytes): | ||
inner_txn: | ||
TypeEnum: Appl | ||
ApplicationID: app_global_get(PROPOSAL_VOTING_APP_ID_KEY) | ||
ApplicationArgs[0]: "execute_proposal" | ||
ApplicationArgs[1]: proposal_id | ||
Fee: 0 | ||
end | ||
return | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#pragma version 9 | ||
#tealish version git+https://github.com/Hipo/tealish.git@c4f8c12b36dbcc8b63844df5f818164925735087 | ||
|
||
exit(1) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma version 9 | ||
#tealish version git+https://github.com/Hipo/tealish.git@c4f8c12b36dbcc8b63844df5f818164925735087 | ||
|
||
# Assert that Gtxn[0] is an appcall to execution validator | ||
assert(Gtxn[0].ApplicationID == 10000) | ||
|
||
assert(Txn.GroupIndex == 1) | ||
|
||
if Gtxn[0].ApplicationArgs[0] == "validate_transaction": | ||
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. Move this logic to app 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. Assert that the Txn.Rekey == Zero Address |
||
assert(Global.GroupSize == 2) | ||
elif Gtxn[0].ApplicationArgs[0] == "validate_group": | ||
assert(Global.GroupSize >= 2) | ||
else: | ||
exit(0) | ||
end | ||
|
||
# Assert that there is no rekeying | ||
int start = 0 | ||
int end = Global.GroupSize | ||
for i in start:end: | ||
assert(Gtxn[i].RekeyTo == Global.ZeroAddress) | ||
end | ||
exit(1) |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,188 @@ | ||||||
#pragma version 8 | ||||||
#tealish version git+https://github.com/Hipo/tealish.git@e8d1b27620220bc4e520d7d3b6d62523e13a7723 | ||||||
|
||||||
# Taken from Proposal Voting App | ||||||
struct Proposal: | ||||||
index: int | ||||||
creation_timestamp: int | ||||||
voting_start_timestamp: int | ||||||
voting_end_timestamp: int | ||||||
snapshot_total_voting_power: int | ||||||
vote_count: int | ||||||
quorum_threshold: int | ||||||
against_voting_power: int | ||||||
for_voting_power: int | ||||||
abstain_voting_power: int | ||||||
is_approved: bytes[1] | ||||||
is_cancelled: bytes[1] | ||||||
is_executed: bytes[1] | ||||||
is_quorum_reached: bytes[1] | ||||||
proposer: bytes[32] | ||||||
execution_hash: bytes[34] | ||||||
executor: bytes[32] | ||||||
end | ||||||
|
||||||
# 24 * 60 * 60 | ||||||
const int DAY = 86400 | ||||||
const bytes BYTES_FALSE = "\x00" | ||||||
const bytes BYTES_TRUE = "\x80" | ||||||
|
||||||
const bytes MANAGER_KEY = "manager" | ||||||
const bytes AMM_APP_ID_KEY = "amm_app_id" | ||||||
const bytes PROPOSAL_VOTING_APP_ID_KEY = "proposal_voting_app_id" | ||||||
|
||||||
# Proposal States | ||||||
const int PROPOSAL_STATE_WAITING_FOR_APPROVAL = 0 | ||||||
const int PROPOSAL_STATE_CANCELLED = 1 | ||||||
const int PROPOSAL_STATE_PENDING = 2 | ||||||
const int PROPOSAL_STATE_ACTIVE = 3 | ||||||
const int PROPOSAL_STATE_DEFEATED = 4 | ||||||
const int PROPOSAL_STATE_SUCCEEDED = 5 | ||||||
const int PROPOSAL_STATE_EXECUTED = 6 | ||||||
|
||||||
# Execution Hash Prefixes | ||||||
|
||||||
const bytes SET_FEE_SETTER_HASH_PREFIX = "fs" | ||||||
const bytes SET_FEE_MANAGER_HASH_PREFIX = "fm" | ||||||
const bytes SET_FEE_COLLECTOR_HASH_PREFIX = "fc" | ||||||
const bytes SET_FEE_FOR_POOL_HASH_PREFIX = "sf" | ||||||
|
||||||
router: | ||||||
create_application | ||||||
update_application | ||||||
set_fee_setter | ||||||
set_fee_manager | ||||||
set_fee_collector | ||||||
set_fee_for_pool | ||||||
end | ||||||
|
||||||
@public(OnCompletion=CreateApplication) | ||||||
func create_application(amm_app_id: int, proposal_voting_app_id: int): | ||||||
app_global_put(AMM_APP_ID_KEY, amm_app_id) | ||||||
app_global_put(PROPOSAL_VOTING_APP_ID_KEY, proposal_voting_app_id) | ||||||
|
||||||
app_global_put(MANAGER_KEY, Txn.Sender) | ||||||
|
||||||
return | ||||||
end | ||||||
|
||||||
@public(OnCompletion=UpdateApplication) | ||||||
func update_application(): | ||||||
bytes user_address = Txn.Sender | ||||||
assert(user_address == app_global_get(MANAGER_KEY)) | ||||||
|
||||||
return | ||||||
end | ||||||
|
||||||
@public() | ||||||
func set_fee_setter(proposal_id: bytes, new_fee_setter: bytes): | ||||||
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.
Suggested change
|
||||||
Proposal proposal = check_and_get_proposal(proposal_id) | ||||||
|
||||||
# Assert proposal.execution_hash | ||||||
bytes execution_hash = Concat(SET_FEE_SETTER_HASH_PREFIX, sha256(Concat("set_fee_setter", new_fee_setter))) | ||||||
assert(proposal.execution_hash == execution_hash) | ||||||
|
||||||
inner_txn: | ||||||
TypeEnum: Appl | ||||||
ApplicationID: app_global_get(AMM_APP_ID_KEY) | ||||||
ApplicationArgs[0]: "set_fee_setter" | ||||||
Accounts[0]: new_fee_setter | ||||||
Fee: 0 | ||||||
end | ||||||
|
||||||
set_proposal_as_executed(proposal_id) | ||||||
return | ||||||
end | ||||||
|
||||||
@public() | ||||||
func set_fee_manager(proposal_id: bytes, new_fee_manager: bytes): | ||||||
Proposal proposal = check_and_get_proposal(proposal_id) | ||||||
|
||||||
# Assert proposal.execution_hash | ||||||
bytes execution_hash = Concat(SET_FEE_MANAGER_HASH_PREFIX, sha256(Concat("set_fee_manager", new_fee_manager))) | ||||||
assert(proposal.execution_hash == execution_hash) | ||||||
|
||||||
inner_txn: | ||||||
TypeEnum: Appl | ||||||
ApplicationID: app_global_get(AMM_APP_ID_KEY) | ||||||
ApplicationArgs[0]: "set_fee_manager" | ||||||
Accounts[0]: new_fee_manager | ||||||
Fee: 0 | ||||||
end | ||||||
|
||||||
set_proposal_as_executed(proposal_id) | ||||||
return | ||||||
end | ||||||
|
||||||
@public() | ||||||
func set_fee_collector(proposal_id: bytes, new_fee_collector: bytes): | ||||||
Proposal proposal = check_and_get_proposal(proposal_id) | ||||||
|
||||||
# Assert proposal.execution_hash | ||||||
bytes execution_hash = Concat(SET_FEE_COLLECTOR_HASH_PREFIX, sha256(Concat("set_fee_collector", new_fee_collector))) | ||||||
assert(proposal.execution_hash == execution_hash) | ||||||
|
||||||
inner_txn: | ||||||
TypeEnum: Appl | ||||||
ApplicationID: app_global_get(AMM_APP_ID_KEY) | ||||||
ApplicationArgs[0]: "set_fee_manager" | ||||||
Accounts[0]: new_fee_collector | ||||||
Fee: 0 | ||||||
end | ||||||
|
||||||
set_proposal_as_executed(proposal_id) | ||||||
return | ||||||
end | ||||||
|
||||||
@public() | ||||||
func set_fee_for_pool(proposal_id: bytes, pool_address: bytes, total_fee_share: bytes, protocol_fee_ratio: bytes): | ||||||
Proposal proposal = check_and_get_proposal(proposal_id) | ||||||
|
||||||
# Assert proposal.execution_hash | ||||||
# prefix + hash(method_name, pool_address: address, total_fee_share, protocol_fee_ratio) | ||||||
bytes execution_hash = Concat(SET_FEE_FOR_POOL_HASH_PREFIX, sha256(Concat("set_fee_for_pool", pool_address, total_fee_share, protocol_fee_ratio))) | ||||||
assert(proposal.execution_hash == execution_hash) | ||||||
|
||||||
inner_txn: | ||||||
TypeEnum: Appl | ||||||
ApplicationID: app_global_get(AMM_APP_ID_KEY) | ||||||
ApplicationArgs[0]: "set_fee" | ||||||
ApplicationArgs[1]: total_fee_share | ||||||
ApplicationArgs[2]: protocol_fee_ratio | ||||||
Accounts[0]: pool_address | ||||||
Fee: 0 | ||||||
end | ||||||
|
||||||
set_proposal_as_executed(proposal_id) | ||||||
return | ||||||
end | ||||||
|
||||||
func check_and_get_proposal(proposal_id: bytes) Proposal: | ||||||
# Read proposal from proposal_voting app | ||||||
inner_txn: | ||||||
TypeEnum: Appl | ||||||
ApplicationID: app_global_get(PROPOSAL_VOTING_APP_ID_KEY) | ||||||
ApplicationArgs[0]: "get_proposal" | ||||||
ApplicationArgs[1]: proposal_id | ||||||
Fee: 0 | ||||||
end | ||||||
|
||||||
Proposal proposal = Cast(extract(4, 182, Itxn.LastLog), Proposal) | ||||||
int proposal_state = btoi(extract(186, 0, Itxn.LastLog)) | ||||||
|
||||||
# Proposal checks | ||||||
assert(proposal_state == PROPOSAL_STATE_SUCCEEDED) | ||||||
|
||||||
return proposal | ||||||
end | ||||||
|
||||||
func set_proposal_as_executed(proposal_id: bytes): | ||||||
inner_txn: | ||||||
TypeEnum: Appl | ||||||
ApplicationID: app_global_get(PROPOSAL_VOTING_APP_ID_KEY) | ||||||
ApplicationArgs[0]: "execute_proposal" | ||||||
ApplicationArgs[1]: proposal_id | ||||||
Fee: 0 | ||||||
end | ||||||
return | ||||||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
#pragma version 8 | ||
#tealish version git+https://github.com/Hipo/tealish.git@e8d1b27620220bc4e520d7d3b6d62523e13a7723 | ||
|
||
exit(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is better to use Sizeof