wormhole/teal/pricekeeper.teal

231 lines
3.8 KiB
Plaintext
Raw Normal View History

2021-09-30 11:46:23 -07:00
#pragma version 5
// ================================================================================================
// PriceKeeper Approval Program
2021-09-30 11:46:23 -07:00
// ================================================================================================
//
// App-globals:
// sym : byte[] Symbol to keep price for
// vaddr : byte[] Validator account
2021-09-30 15:04:16 -07:00
// nonce : uint64 last sequence ID
// price : byte[] current price
// stdev : byte[] current confidence (standard deviation)
// ts : uint64 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
// 8 nonce uint64 Sequence identifier
// 16 symbol String filled with spaces e.g ("ALGO/USD ")
2021-09-30 15:04:16 -07:00
// 8 price Price. 64bit float encoded as big-endian.
// 8 conf Confidence (standard-deviation?). 64bit float encoded as big-endian.
2021-09-30 11:46:23 -07:00
// 8 ts timestamp of this price
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-04 13:12:25 -07:00
// Size: 130 bytes.
2021-09-30 11:46:23 -07:00
//
// ------------------------------------------------------------------------------------------------
// Application creation.
int 0
txn ApplicationID
==
bnz handle_create
2021-09-30 11:46:23 -07:00
// Handle app call: send price message
txn OnCompletion
int NoOp
==
bnz handle_call
// Handle deletion.
txn OnCompletion
int DeleteApplication
==
bnz success
2021-09-30 11:46:23 -07:00
// Fail otherwise
err
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-04 13:12:25 -07:00
byte "nonce"
int 0
app_global_put
b success
2021-09-30 11:46:23 -07:00
// -----------------------------------------------------
// Handle app call
2021-09-30 11:46:23 -07:00
// -----------------------------------------------------
handle_call:
// Group size must be 3 to raise computational allowance to 2100
global GroupSize
int 3
==
assert
// if this is one of dummy transactions(0 or 1), exit with success
txn GroupIndex
int 2
!=
bnz success
2021-09-30 11:46:23 -07:00
// Verify if sender is the data validator
txn Sender
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-04 13:12:25 -07:00
int 130
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
// Check nonce
load 0
extract 18 8
btoi
byte "nonce"
app_global_get
>
assert
// Check timestamp order
load 0
extract 58 8
btoi
global LatestTimestamp
<=
assert
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
extract 0 65
sha512_256
2021-10-04 13:12:25 -07:00
// (B) signature
2021-09-30 11:46:23 -07:00
load 0
2021-10-04 13:12:25 -07:00
extract 66 64
2021-09-30 11:46:23 -07:00
2021-10-04 13:12:25 -07:00
// validator-address
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
==
assert
// ----------------------------------------------------------------------------
// Verified. Store data to app globals.
// ----------------------------------------------------------------------------
byte "nonce"
load 0
extract 18 8
2021-10-05 06:52:32 -07:00
btoi
2021-09-30 11:46:23 -07:00
app_global_put
byte "ts"
load 0
extract 58 8
btoi
app_global_put
byte "price"
load 0
extract 42 8
app_global_put
byte "stdev"
load 0
extract 50 8
app_global_put
b success
// ----------------------------------------------------------------------------
fail:
int 0
return
success:
int 1
return