2021-09-30 11:46:23 -07:00
|
|
|
#pragma version 5
|
|
|
|
// ================================================================================================
|
2021-10-01 11:06:33 -07:00
|
|
|
// PriceKeeper Approval Program
|
2021-09-30 11:46:23 -07:00
|
|
|
// ================================================================================================
|
|
|
|
//
|
|
|
|
// App-globals:
|
2021-10-01 11:06:33 -07:00
|
|
|
// sym : byte[] Symbol to keep price for
|
|
|
|
// vaddr : byte[] Validator account
|
2021-10-06 09:05:27 -07:00
|
|
|
// price : uint64 current price
|
|
|
|
// stdev : uint64 current confidence (standard deviation)
|
|
|
|
// slot : uint64 slot of this onchain publication
|
|
|
|
// exp : uint64 exponent (fixed point position)
|
2021-10-06 12:39:50 -07:00
|
|
|
// ts : uint64 last timestamp
|
2021-09-30 11:46:23 -07:00
|
|
|
//
|
|
|
|
// Slots:
|
|
|
|
// 0 Input message block
|
|
|
|
// 1 SHA256-Hashed message
|
|
|
|
//
|
|
|
|
// The Message format must have the packed fields:
|
|
|
|
//
|
|
|
|
// Field size
|
2021-09-30 15:04:16 -07:00
|
|
|
// 9 header Literal "PRICEDATA"
|
2021-09-30 11:46:23 -07:00
|
|
|
// 1 version int8 (Must be 1)
|
|
|
|
// 8 dest This appId
|
|
|
|
// 16 symbol String filled with spaces e.g ("ALGO/USD ")
|
2021-10-06 09:05:27 -07:00
|
|
|
// 8 price Price. 64bit integer.
|
|
|
|
// 8 priceexp Price exponent (fixed point position).
|
|
|
|
// 8 conf Confidence (stdev). 64bit integer.
|
|
|
|
// 8 slot Valid-slot of this aggregate price.
|
|
|
|
// 8 ts timestamp of this price submitted by PriceFetcher service
|
2021-09-30 15:04:16 -07:00
|
|
|
// 32 s Signature s-component
|
|
|
|
// 32 r Signature r-component
|
2021-09-30 11:46:23 -07:00
|
|
|
//
|
2021-10-06 12:39:50 -07:00
|
|
|
// Size: 138 bytes.
|
2021-09-30 11:46:23 -07:00
|
|
|
//
|
|
|
|
// ------------------------------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
// Application creation.
|
|
|
|
int 0
|
|
|
|
txn ApplicationID
|
|
|
|
==
|
2021-10-01 11:06:33 -07:00
|
|
|
bnz handle_create
|
2021-09-30 11:46:23 -07:00
|
|
|
|
|
|
|
// Handle app call: send price message
|
|
|
|
txn OnCompletion
|
|
|
|
int NoOp
|
|
|
|
==
|
|
|
|
bnz handle_call
|
|
|
|
|
2021-10-01 11:06:33 -07:00
|
|
|
// Handle deletion.
|
|
|
|
txn OnCompletion
|
|
|
|
int DeleteApplication
|
|
|
|
==
|
|
|
|
bnz success
|
|
|
|
|
2021-09-30 11:46:23 -07:00
|
|
|
// Fail otherwise
|
|
|
|
err
|
|
|
|
|
2021-10-01 11:06:33 -07:00
|
|
|
handle_create:
|
|
|
|
// -----------------------------------------------------
|
|
|
|
// Handle creation
|
|
|
|
// Arg 0: Validator address
|
|
|
|
// Arg 1: Symbol to keep price data
|
|
|
|
// -----------------------------------------------------
|
|
|
|
|
|
|
|
byte "vaddr"
|
|
|
|
txn ApplicationArgs 0
|
|
|
|
app_global_put
|
|
|
|
|
|
|
|
byte "sym"
|
|
|
|
txn ApplicationArgs 1
|
|
|
|
dup
|
|
|
|
len
|
|
|
|
int 16
|
|
|
|
==
|
|
|
|
assert
|
|
|
|
app_global_put
|
|
|
|
|
2021-10-06 12:39:50 -07:00
|
|
|
byte "ts"
|
|
|
|
global LatestTimestamp
|
2021-10-04 13:12:25 -07:00
|
|
|
app_global_put
|
|
|
|
|
2021-10-01 11:06:33 -07:00
|
|
|
b success
|
|
|
|
|
2021-09-30 11:46:23 -07:00
|
|
|
// -----------------------------------------------------
|
2021-10-05 09:31:54 -07:00
|
|
|
// Handle app call
|
2021-09-30 11:46:23 -07:00
|
|
|
// -----------------------------------------------------
|
|
|
|
|
|
|
|
handle_call:
|
2021-10-06 09:05:27 -07:00
|
|
|
// Group size must be 4 to raise computational allowance to 2800
|
2021-10-05 09:31:54 -07:00
|
|
|
|
|
|
|
global GroupSize
|
2021-10-06 09:05:27 -07:00
|
|
|
int 4
|
2021-10-05 09:31:54 -07:00
|
|
|
==
|
|
|
|
assert
|
|
|
|
|
2021-10-06 12:39:50 -07:00
|
|
|
// if this is one of dummy transactions(0, 1 or 2), exit with success
|
2021-10-05 09:31:54 -07:00
|
|
|
txn GroupIndex
|
2021-10-06 09:05:27 -07:00
|
|
|
int 3
|
2021-10-05 09:31:54 -07:00
|
|
|
!=
|
|
|
|
bnz success
|
2021-09-30 11:46:23 -07:00
|
|
|
|
|
|
|
// Verify if sender is the data validator
|
|
|
|
txn Sender
|
2021-10-01 11:06:33 -07:00
|
|
|
byte "vaddr"
|
|
|
|
app_global_get
|
2021-09-30 11:46:23 -07:00
|
|
|
==
|
|
|
|
assert
|
|
|
|
|
|
|
|
// Retrieve message, store in slot 0
|
|
|
|
txn ApplicationArgs 0
|
|
|
|
store 0
|
|
|
|
|
|
|
|
// ------------------------------------------------------
|
|
|
|
// Validate message
|
|
|
|
// ------------------------------------------------------
|
|
|
|
|
|
|
|
// Check length
|
|
|
|
|
|
|
|
load 0
|
|
|
|
len
|
2021-10-06 12:39:50 -07:00
|
|
|
int 138
|
2021-09-30 11:46:23 -07:00
|
|
|
==
|
|
|
|
assert
|
|
|
|
|
|
|
|
// Check header
|
|
|
|
|
|
|
|
byte "PRICEDATA"
|
2021-10-01 13:31:10 -07:00
|
|
|
load 0
|
2021-09-30 11:46:23 -07:00
|
|
|
extract 0 9
|
|
|
|
==
|
|
|
|
assert
|
|
|
|
|
|
|
|
// Check version - must be 1.
|
|
|
|
|
|
|
|
load 0
|
|
|
|
extract 9 1
|
|
|
|
byte 0x01
|
|
|
|
==
|
|
|
|
assert
|
|
|
|
|
|
|
|
// Check destination - must be this appId.
|
|
|
|
|
|
|
|
load 0
|
|
|
|
extract 10 8
|
|
|
|
btoi
|
|
|
|
txn ApplicationID
|
|
|
|
==
|
|
|
|
assert
|
|
|
|
|
2021-10-06 09:05:27 -07:00
|
|
|
// Reject zero price
|
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 34 8
|
2021-10-06 09:05:27 -07:00
|
|
|
btoi
|
|
|
|
int 0
|
|
|
|
!=
|
|
|
|
assert
|
|
|
|
|
|
|
|
// Reject zero slot
|
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 58 8
|
2021-10-06 09:05:27 -07:00
|
|
|
btoi
|
|
|
|
int 0
|
|
|
|
!=
|
|
|
|
assert
|
|
|
|
|
|
|
|
// Reject out-of-range exponent
|
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 42 8
|
2021-10-06 09:05:27 -07:00
|
|
|
btoi
|
|
|
|
int 18
|
|
|
|
<=
|
|
|
|
assert
|
2021-09-30 11:46:23 -07:00
|
|
|
|
2021-10-06 12:39:50 -07:00
|
|
|
// Check timestamp:
|
|
|
|
// * must be higher than ts recorded in global state
|
|
|
|
// * must be lower than current block stamp + 10s
|
2021-10-06 09:05:27 -07:00
|
|
|
// (TODO: check this again)
|
2021-09-30 11:46:23 -07:00
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 66 8
|
2021-09-30 11:46:23 -07:00
|
|
|
btoi
|
2021-10-06 09:05:27 -07:00
|
|
|
dup
|
2021-10-06 12:39:50 -07:00
|
|
|
dup
|
2021-10-06 09:05:27 -07:00
|
|
|
global LatestTimestamp
|
|
|
|
int 10
|
|
|
|
+
|
2021-10-06 12:39:50 -07:00
|
|
|
<
|
|
|
|
assert
|
|
|
|
byte "ts"
|
|
|
|
app_global_get
|
2021-10-06 09:05:27 -07:00
|
|
|
>
|
2021-10-06 12:39:50 -07:00
|
|
|
assert
|
2021-10-06 09:05:27 -07:00
|
|
|
|
2021-09-30 11:46:23 -07:00
|
|
|
|
2021-10-04 13:12:25 -07:00
|
|
|
// ed25519verify args in stack:
|
2021-09-30 11:46:23 -07:00
|
|
|
|
2021-10-04 13:12:25 -07:00
|
|
|
// data (hash of message)
|
2021-09-30 11:46:23 -07:00
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 0 74
|
2021-09-30 11:46:23 -07:00
|
|
|
sha512_256
|
|
|
|
|
2021-10-04 13:12:25 -07:00
|
|
|
// (B) signature
|
2021-09-30 11:46:23 -07:00
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 74 64
|
2021-09-30 11:46:23 -07:00
|
|
|
|
2021-10-04 13:12:25 -07:00
|
|
|
// validator-address
|
2021-10-01 11:06:33 -07:00
|
|
|
byte "vaddr"
|
|
|
|
app_global_get
|
2021-09-30 11:46:23 -07:00
|
|
|
|
2021-10-04 13:12:25 -07:00
|
|
|
// Verify signature
|
|
|
|
ed25519verify
|
2021-09-30 11:46:23 -07:00
|
|
|
int 1
|
|
|
|
==
|
2021-10-06 12:39:50 -07:00
|
|
|
assert
|
2021-09-30 11:46:23 -07:00
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Verified. Store data to app globals.
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
byte "ts"
|
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 66 8
|
2021-09-30 11:46:23 -07:00
|
|
|
btoi
|
|
|
|
app_global_put
|
|
|
|
|
|
|
|
byte "price"
|
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 34 8
|
2021-10-06 09:05:27 -07:00
|
|
|
btoi
|
|
|
|
app_global_put
|
|
|
|
|
|
|
|
byte "exp"
|
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 42 8
|
2021-10-06 09:05:27 -07:00
|
|
|
btoi
|
2021-09-30 11:46:23 -07:00
|
|
|
app_global_put
|
|
|
|
|
2021-10-06 12:39:50 -07:00
|
|
|
byte "conf"
|
2021-09-30 11:46:23 -07:00
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 50 8
|
2021-10-06 09:05:27 -07:00
|
|
|
btoi
|
|
|
|
app_global_put
|
|
|
|
|
|
|
|
byte "slot"
|
|
|
|
load 0
|
2021-10-06 12:39:50 -07:00
|
|
|
extract 58 8
|
2021-10-06 09:05:27 -07:00
|
|
|
btoi
|
2021-09-30 11:46:23 -07:00
|
|
|
app_global_put
|
|
|
|
|
|
|
|
b success
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
fail:
|
|
|
|
int 0
|
|
|
|
return
|
|
|
|
|
|
|
|
success:
|
|
|
|
int 1
|
|
|
|
return
|
|
|
|
|
|
|
|
|
|
|
|
|