Skip to content
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

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 29 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
7f10e0b
init arbitrary executor
etzellux Nov 15, 2023
fe7afb7
fix cast error in arbitrary executor
etzellux Nov 15, 2023
ab56782
add arbitrary_executor_clear_state
etzellux Nov 16, 2023
64f15a1
add initial test
etzellux Nov 16, 2023
dee010a
add test_execute_proposal
etzellux Nov 17, 2023
092ca1b
update test_execute_proposal
etzellux Nov 20, 2023
2007c4f
add executor logic signature
etzellux Nov 21, 2023
f6515fb
add fee_management_executor contracts
etzellux Nov 23, 2023
6e20eed
add fee_management_executor tests
etzellux Nov 27, 2023
6201f0d
update test_set_fee_for_pool_execution
etzellux Nov 28, 2023
fe72cd9
add treasury_management_executor contracts
etzellux Nov 28, 2023
e25bcdd
add TreasuryManagementExecutorTestCase
etzellux Nov 29, 2023
1bb0053
update test and executors
etzellux Nov 30, 2023
dd5fb4c
remove sha256 from arbitrary methods
etzellux Dec 1, 2023
5362922
update fee_management_executor and tests
etzellux Dec 1, 2023
e002106
update treasury_management_executor and tests
etzellux Dec 1, 2023
260330c
update arbitrary_executor_logic_signature
etzellux Dec 1, 2023
099172a
add set_proposal_as_executed to executors
etzellux Dec 5, 2023
9058eee
update tests with sdk functions
etzellux Dec 7, 2023
4217e5d
minor updates on contracts and tests
etzellux Jan 31, 2024
065e36c
update execution_hash field in proposal_voting
etzellux Feb 2, 2024
d3ead63
Merge branch 'main' into update-proposal-voting
etzellux Feb 2, 2024
f9bf5ac
update proposal_voting and tests
etzellux Feb 5, 2024
3a48973
Merge branch 'executor-contract' into use-tealish-router-executors
etzellux Feb 5, 2024
bba767c
update arbitrary_executor_approval
etzellux Feb 6, 2024
84ef7af
Merge branch 'main' into use-tealish-router-executors
etzellux Feb 6, 2024
08eec8d
Merge branch 'update-proposal-voting' into use-tealish-router-executors
etzellux Feb 6, 2024
abe561e
update fee_management_executor_approval
etzellux Feb 6, 2024
673877c
update treasury_management_executor_approval
etzellux Feb 6, 2024
12e1377
update arbitrary_executor approval and logic_signature
etzellux Feb 9, 2024
1eb32cb
add asset_optin method to arbitrary and fee management
etzellux Feb 9, 2024
719d309
add test_asset_optin
etzellux Feb 19, 2024
ca111c5
add length constraint to address parameters in executor contracts
etzellux Feb 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions contracts/arbitrary_executor/arbitrary_executor_approval.tl
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)
Copy link
Collaborator Author

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

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)
23 changes: 23 additions & 0 deletions contracts/arbitrary_executor/arbitrary_executor_logic_signature.tl
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":
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this logic to app

Copy link
Collaborator Author

Choose a reason for hiding this comment

The 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)
188 changes: 188 additions & 0 deletions contracts/fee_management_executor/fee_management_executor_approval.tl
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):
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
func set_fee_setter(proposal_id: bytes, new_fee_setter: bytes):
func set_fee_setter(proposal_id: bytes, new_fee_setter: bytes[32]):

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)
Loading