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
const validatorAddr = 'OPDM7ACAW64Q4VBWAL77Z5SHSJVZZ44V3BAN7W44U43SUXEOUENZMZYOQU'
const validatorMnemo = '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 otherAddr = 'DMTBK62XZ6KNI7L5E6TRBTPB4B3YNVB4WYGSWR42SEV4XKV4LYHGBW4O34'
const otherMnemo = '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 symbol = 'BTC/USD '
const signatures = { }
signatures [ validatorAddr ] = algosdk . mnemonicToSecretKey ( validatorMnemo )
signatures [ otherAddr ] = algosdk . mnemonicToSecretKey ( otherMnemo )
function signCallback ( sender , tx ) {
const txSigned = tx . signTxn ( signatures [ sender ] . sk )
return txSigned
}
describe ( 'Price-Keeper contract tests' , function ( ) {
let pclib
let algodClient
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...' )
const appsTo = await tools . readCreatedApps ( algodClient , validatorAddr )
for ( let i = 0 ; i < appsTo . length ; i ++ ) {
console . log ( 'Clearing ' + appsTo [ i ] . id )
try {
const txId = await pclib . deleteApp ( validatorAddr , signCallback , appsTo [ i ] . id )
await pclib . waitForConfirmation ( txId )
} catch ( e ) {
console . error ( 'Could not delete application! Reason: ' + e )
}
}
console . log ( 'Creating new app...' )
const txId = await pclib . createApp ( validatorAddr , validatorAddr , symbol , signCallback )
const txResponse = await pclib . waitForTransactionResponse ( txId )
const 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-04 13:12:25 -07:00
it ( 'Must fail to create app with bad symbol' , async function ( ) {
await expect ( pclib . createApp ( validatorAddr , validatorAddr , 'XXXXX' , signCallback ) ) . to . be . rejectedWith ( 'Bad Request' )
} )
it ( 'Must accept valid message and store data' , async function ( ) {
const msgb64 = pclib . createMessage ( BigInt ( 1 ) , 'BTC/USD ' , 48526.50 , 8.99999967 , signatures [ validatorAddr ] . sk )
console . log ( msgb64 )
await expect ( pclib . submitMessage ( validatorAddr , msgb64 , signCallback ) ) . to . eventually . have . length ( 52 )
2021-10-01 11:06:33 -07:00
} )
} )