|
| 1 | +pragma cashscript ^0.10.0; |
| 2 | + |
| 3 | +contract PriceContract( |
| 4 | + bytes tokenIdOracleContract |
| 5 | + ) { |
| 6 | + // function updatePrice |
| 7 | + // Trigger oracle sequence update to avoid letting borrowers spawn coins using old price data |
| 8 | + // |
| 9 | + // Inputs: 00-pricecontract, 01-oraclecontract, ?02-feeBCH |
| 10 | + // Outputs: 00-pricecontract, ?01-changeBCH |
| 11 | + |
| 12 | + function updatePrice() { |
| 13 | + require(this.activeInputIndex == 0, "Parity contract must always be at input index 0"); |
| 14 | + |
| 15 | + require(tx.outputs[0].lockingBytecode == tx.inputs[0].lockingBytecode, "Recreate contract at output0 - invalid lockingBytecode"); |
| 16 | + require(tx.outputs[0].tokenCategory == tx.inputs[0].tokenCategory, "Recreate contract at output0 - invalid tokenCategory"); |
| 17 | + require(tx.outputs[0].value == 1000, "Recreate contract at output0 - needs to hold exactly 1000 sats"); |
| 18 | + require(tx.outputs[0].tokenAmount == tx.inputs[0].tokenAmount, "Invalid tokenAmount, needs to maintain atleast current tokenAmount"); |
| 19 | + |
| 20 | + bytes contractSeq = tx.inputs[0].nftCommitment.split(1)[1].split(6)[0]; |
| 21 | + |
| 22 | + require(tokenIdOracleContract == tx.inputs[1].tokenCategory); |
| 23 | + |
| 24 | + // Parse oracle data (Oracle price is denominated in USD cents/BCH) |
| 25 | + bytes oracleSeqBytes = tx.inputs[1].nftCommitment.split(6)[0]; |
| 26 | + int oracleSeq = int(oracleSeqBytes); |
| 27 | + |
| 28 | + // Verify oracle sequence |
| 29 | + require(oracleSeq > int(contractSeq), "Invalid oracle sequence, should be more recent than current contract sequence"); |
| 30 | + |
| 31 | + require(tx.outputs[0].nftCommitment == 0x00 + tx.inputs[1].nftCommitment, "Invalid nftCommitment, commitment should be the oracle sequence"); |
| 32 | + } |
| 33 | + |
| 34 | + // function sharePrice |
| 35 | + // Provides latest oracle price to various other contracts |
| 36 | + // |
| 37 | + // Inputs: 00-parity, ... |
| 38 | + // Outputs: 00-parity, ... |
| 39 | + |
| 40 | + function sharePrice() { |
| 41 | + require(tx.outputs[this.activeInputIndex].lockingBytecode == tx.inputs[this.activeInputIndex].lockingBytecode, "Recreate contract at output0 - invalid lockingBytecode"); |
| 42 | + require(tx.outputs[this.activeInputIndex].tokenCategory == tx.inputs[this.activeInputIndex].tokenCategory, "Recreate contract at output0 - invalid tokenCategory"); |
| 43 | + require(tx.outputs[this.activeInputIndex].value == 1000, "Recreate contract at output0 - needs to hold exactly 1000 sats"); |
| 44 | + // Allow giving the contract more tokens |
| 45 | + require(tx.outputs[this.activeInputIndex].tokenAmount >= tx.inputs[this.activeInputIndex].tokenAmount, "Invalid tokenAmount, needs to maintain atleast current tokenAmount"); |
| 46 | + // keep same contract state |
| 47 | + require(tx.outputs[this.activeInputIndex].nftCommitment == tx.inputs[this.activeInputIndex].nftCommitment, "Invalid nftCommitment, commitment should be replicated"); |
| 48 | + } |
| 49 | +} |
0 commit comments