2021-10-01 11:06:33 -07:00
const PricecasterLib = require ( '../lib/pricecaster' )
const tools = require ( '../tools/app-tools' )
const algosdk = require ( 'algosdk' )
2021-10-04 13:12:25 -07:00
const { expect } = require ( 'chai' )
const chai = require ( 'chai' )
chai . use ( require ( 'chai-as-promised' ) )
2021-10-01 11:06:33 -07:00
// Test general configuration for Betanet
2021-10-06 12:39:50 -07:00
const VALIDATOR _ADDR = 'OPDM7ACAW64Q4VBWAL77Z5SHSJVZZ44V3BAN7W44U43SUXEOUENZMZYOQU'
const VALIDATOR _MNEMO = 'assault approve result rare float sugar power float soul kind galaxy edit unusual pretty tone tilt net range pelican avoid unhappy amused recycle abstract master'
const OTHER _ADDR = 'DMTBK62XZ6KNI7L5E6TRBTPB4B3YNVB4WYGSWR42SEV4XKV4LYHGBW4O34'
const OTHER _MNEMO = 'old agree harbor cost pink fog chunk hope vital used rural soccer model acquire clown host friend bring marriage surge dirt surge slab absent punch'
const SIGNATURES = { }
SIGNATURES [ VALIDATOR _ADDR ] = algosdk . mnemonicToSecretKey ( VALIDATOR _MNEMO )
SIGNATURES [ OTHER _ADDR ] = algosdk . mnemonicToSecretKey ( OTHER _MNEMO )
const VALID _SYMBOL = 'BTC/USD '
const VALID _PRICE = BigInt ( 485265555 )
const VALID _EXPONENT = BigInt ( 4 )
const VALID _CONF = BigInt ( 1400000 )
const VALID _SLOT = BigInt ( 104566700 )
2021-10-01 11:06:33 -07:00
function signCallback ( sender , tx ) {
2021-10-06 12:39:50 -07:00
const txSigned = tx . signTxn ( SIGNATURES [ sender ] . sk )
2021-10-01 11:06:33 -07:00
return txSigned
}
describe ( 'Price-Keeper contract tests' , function ( ) {
let pclib
let algodClient
2021-10-06 12:39:50 -07:00
let appId
let lastTs
2021-10-01 11:06:33 -07:00
before ( async function ( ) {
algodClient = new algosdk . Algodv2 ( '' , 'https://api.betanet.algoexplorer.io' , '' )
pclib = new PricecasterLib . PricecasterLib ( algodClient )
console . log ( 'Clearing accounts of all previous apps...' )
2021-10-06 12:39:50 -07:00
const appsTo = await tools . readCreatedApps ( algodClient , VALIDATOR _ADDR )
2021-10-01 11:06:33 -07:00
for ( let i = 0 ; i < appsTo . length ; i ++ ) {
console . log ( 'Clearing ' + appsTo [ i ] . id )
try {
2021-10-06 12:39:50 -07:00
const txId = await pclib . deleteApp ( VALIDATOR _ADDR , signCallback , appsTo [ i ] . id )
2021-10-01 11:06:33 -07:00
await pclib . waitForConfirmation ( txId )
} catch ( e ) {
console . error ( 'Could not delete application! Reason: ' + e )
}
}
console . log ( 'Creating new app...' )
2021-10-06 12:39:50 -07:00
const txId = await pclib . createApp ( VALIDATOR _ADDR , VALIDATOR _ADDR , VALID _SYMBOL , signCallback )
2021-10-01 11:06:33 -07:00
const txResponse = await pclib . waitForTransactionResponse ( txId )
2021-10-06 12:39:50 -07:00
appId = pclib . appIdFromCreateAppResponse ( txResponse )
2021-10-04 13:12:25 -07:00
pclib . setAppId ( appId )
2021-10-01 11:06:33 -07:00
console . log ( 'App Id: %d' , appId )
} )
2021-10-06 12:39:50 -07:00
it ( 'Must create app with proper initial global state' , async function ( ) {
const stVAddr = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'vaddr' )
const stSym = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'sym' )
expect ( Buffer . from ( stSym , 'base64' ) . toString ( ) ) . to . equal ( VALID _SYMBOL )
expect ( stVAddr . toString ( ) ) . to . equal ( VALIDATOR _ADDR )
} )
2021-10-04 13:12:25 -07:00
it ( 'Must accept valid message and store data' , async function ( ) {
2021-10-06 12:39:50 -07:00
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , VALID _EXPONENT , VALID _CONF , VALID _SLOT , SIGNATURES [ VALIDATOR _ADDR ] . sk )
const txid = await pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback )
expect ( txid ) . to . have . length ( 52 )
await pclib . waitForTransactionResponse ( txid )
2021-10-07 10:41:19 -07:00
// console.log(await tools.printAppGlobalState(algodClient, appId, VALIDATOR_ADDR))
2021-10-06 12:39:50 -07:00
const stPrice = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'price' )
const stExp = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'exp' )
const stConf = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'conf' )
const stSlot = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'slot' )
expect ( stPrice . toString ( ) ) . to . equal ( VALID _PRICE . toString ( ) )
2021-10-08 11:45:46 -07:00
expect ( ( Buffer . from ( stExp , 'base64' ) ) . readBigInt64BE ( ) ) . to . equal ( VALID _EXPONENT )
2021-10-06 12:39:50 -07:00
expect ( stSlot . toString ( ) ) . to . equal ( VALID _SLOT . toString ( ) )
expect ( stConf . toString ( ) ) . to . equal ( VALID _CONF . toString ( ) )
} )
it ( 'Must accept second message with different price' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE + BigInt ( 400 ) , VALID _EXPONENT + BigInt ( 3 ) , VALID _CONF + BigInt ( 2 ) , VALID _SLOT + BigInt ( 100 ) , SIGNATURES [ VALIDATOR _ADDR ] . sk )
const txid = await pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback )
expect ( txid ) . to . have . length ( 52 )
await pclib . waitForTransactionResponse ( txid )
const stPrice = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'price' )
const stExp = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'exp' )
const stConf = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'conf' )
const stSlot = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'slot' )
expect ( stPrice . toString ( ) ) . to . equal ( ( VALID _PRICE + BigInt ( 400 ) ) . toString ( ) )
2021-10-08 11:45:46 -07:00
expect ( ( Buffer . from ( stExp , 'base64' ) ) . readBigInt64BE ( ) ) . to . equal ( VALID _EXPONENT + BigInt ( 3 ) )
2021-10-06 12:39:50 -07:00
expect ( stSlot . toString ( ) ) . to . equal ( ( VALID _SLOT + BigInt ( 100 ) ) . toString ( ) )
expect ( stConf . toString ( ) ) . to . equal ( ( VALID _CONF + BigInt ( 2 ) ) . toString ( ) )
lastTs = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'ts' )
} )
2021-10-08 11:45:46 -07:00
it ( 'Must accept negative exponent, stored as 2-complement 64bit' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , BigInt ( - 9 ) , VALID _CONF , VALID _SLOT , SIGNATURES [ VALIDATOR _ADDR ] . sk )
const txid = await pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback )
expect ( txid ) . to . have . length ( 52 )
await pclib . waitForTransactionResponse ( txid )
const stExp = await tools . readAppGlobalStateByKey ( algodClient , appId , VALIDATOR _ADDR , 'exp' )
const bufExp = Buffer . from ( stExp , 'base64' )
const val = bufExp . readBigInt64BE ( )
expect ( val . toString ( ) ) . to . equal ( '-9' )
} )
2021-10-06 12:39:50 -07:00
it ( 'Must reject non-validator as signer' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , VALID _EXPONENT , VALID _CONF , VALID _SLOT , SIGNATURES [ OTHER _ADDR ] . sk )
await expect ( pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must reject non-validator as sender' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , VALID _EXPONENT , VALID _CONF , VALID _SLOT , SIGNATURES [ VALIDATOR _ADDR ] . sk )
await expect ( pclib . submitMessage ( OTHER _ADDR , msgBuffer , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must reject future timestamp' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , VALID _EXPONENT , VALID _CONF , VALID _SLOT , SIGNATURES [ VALIDATOR _ADDR ] . sk , undefined , undefined , undefined , BigInt ( lastTs + 200 ) )
await expect ( pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must reject old timestamp' , async function ( ) {
2021-10-08 11:45:46 -07:00
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , VALID _EXPONENT , VALID _CONF , VALID _SLOT , SIGNATURES [ VALIDATOR _ADDR ] . sk , undefined , undefined , undefined , BigInt ( lastTs - 999999 ) )
2021-10-06 12:39:50 -07:00
await expect ( pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must reject zero-priced message' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , BigInt ( 0 ) , VALID _EXPONENT , VALID _CONF , VALID _SLOT , SIGNATURES [ VALIDATOR _ADDR ] . sk )
await expect ( pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must reject zero slot' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , VALID _EXPONENT , VALID _CONF , BigInt ( 0 ) , SIGNATURES [ VALIDATOR _ADDR ] . sk )
await expect ( pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must reject bad header' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , VALID _EXPONENT , VALID _CONF , BigInt ( 0 ) , SIGNATURES [ VALIDATOR _ADDR ] . sk , 'BADHEADER' )
await expect ( pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must reject bad destination appId' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , VALID _EXPONENT , VALID _CONF , BigInt ( 0 ) , SIGNATURES [ VALIDATOR _ADDR ] . sk , undefined , BigInt ( 100 ) )
await expect ( pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must reject bad message version' , async function ( ) {
const msgBuffer = pclib . createMessage ( VALID _SYMBOL , VALID _PRICE , VALID _EXPONENT , VALID _CONF , BigInt ( 0 ) , SIGNATURES [ VALIDATOR _ADDR ] . sk , undefined , undefined , 0 )
await expect ( pclib . submitMessage ( VALIDATOR _ADDR , msgBuffer , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
2021-10-01 11:06:33 -07:00
} )
} )