github.com/Bytom/bytom@v1.1.2-0.20210127130405-ae40204c0b09/equity/compiler/equitytest/equitytest.go (about)

     1  package equitytest
     2  
     3  const TrivialLock = `
     4  contract TrivialLock() locks amount of asset {
     5    clause trivialUnlock() {
     6      unlock amount of asset
     7    }
     8  }
     9  `
    10  
    11  const LockWithPublicKey = `
    12  contract LockWithPublicKey(publicKey: PublicKey) locks amount of asset {
    13    clause unlockWithSig(sig: Signature) {
    14      verify checkTxSig(publicKey, sig)
    15      unlock amount of asset
    16    }
    17  }
    18  `
    19  
    20  const LockWithPKHash = `
    21  contract LockWithPublicKeyHash(pubKeyHash: Hash) locks amount of asset {
    22    clause spend(pubKey: PublicKey, sig: Signature) {
    23      verify sha3(pubKey) == pubKeyHash
    24      verify checkTxSig(pubKey, sig)
    25      unlock amount of asset
    26    }
    27  }
    28  `
    29  
    30  const LockWith2of3Keys = `
    31  contract LockWith3Keys(pubkey1, pubkey2, pubkey3: PublicKey) locks amount of asset {
    32    clause unlockWith2Sigs(sig1, sig2: Signature) {
    33      verify checkTxMultiSig([pubkey1, pubkey2, pubkey3], [sig1, sig2])
    34      unlock amount of asset
    35    }
    36  }
    37  `
    38  
    39  const LockToOutput = `
    40  contract LockToOutput(address: Program) locks amount of asset {
    41    clause relock() {
    42      lock amount of asset with address
    43    }
    44  }
    45  `
    46  
    47  const TradeOffer = `
    48  contract TradeOffer(requestedAsset: Asset, requestedAmount: Amount, sellerProgram: Program, sellerKey: PublicKey) locks amount of asset {
    49    clause trade() {
    50      lock requestedAmount of requestedAsset with sellerProgram
    51      unlock amount of asset
    52    }
    53    clause cancel(sellerSig: Signature) {
    54      verify checkTxSig(sellerKey, sellerSig)
    55      unlock amount of asset
    56    }
    57  }
    58  `
    59  
    60  const EscrowedTransfer = `
    61  contract EscrowedTransfer(agent: PublicKey, sender: Program, recipient: Program) locks amount of asset {
    62    clause approve(sig: Signature) {
    63      verify checkTxSig(agent, sig)
    64      lock amount of asset with recipient
    65    }
    66    clause reject(sig: Signature) {
    67      verify checkTxSig(agent, sig)
    68      lock amount of asset with sender
    69    }
    70  }
    71  `
    72  
    73  const RevealPreimage = `
    74  contract RevealPreimage(hash: Hash) locks amount of asset {
    75    clause reveal(string: String) {
    76      verify sha3(string) == hash
    77      unlock amount of asset
    78    }
    79  }
    80  `
    81  const PriceChanger = `
    82  contract PriceChanger(askAmount: Amount, askAsset: Asset, sellerKey: PublicKey, sellerProg: Program) locks valueAmount of valueAsset {
    83    clause changePrice(newAmount: Amount, newAsset: Asset, sig: Signature) {
    84      verify checkTxSig(sellerKey, sig)
    85      lock valueAmount of valueAsset with PriceChanger(newAmount, newAsset, sellerKey, sellerProg)
    86    }
    87    clause redeem() {
    88      lock askAmount of askAsset with sellerProg
    89      unlock valueAmount of valueAsset
    90    }
    91  }
    92  `
    93  
    94  const TestDefineVar = `
    95  contract TestDefineVar(result: Integer) locks valueAmount of valueAsset {
    96    clause LockWithMath(left: Integer, right: Integer) {
    97      define calculate: Integer = left + right
    98      verify left != calculate
    99      verify result == calculate
   100      unlock valueAmount of valueAsset
   101    }
   102  }
   103  `
   104  
   105  const TestAssignVar = `
   106  contract TestAssignVar(result: Integer) locks valueAmount of valueAsset {
   107    clause LockWithMath(first: Integer, second: Integer) {
   108      define calculate: Integer = first
   109      assign calculate = calculate + second
   110      verify result == calculate
   111      unlock valueAmount of valueAsset
   112    }
   113  }
   114  `
   115  
   116  const TestSigIf = `
   117  contract TestSigIf(a: Integer, count:Integer) locks valueAmount of valueAsset {
   118    clause check(b: Integer, c: Integer) {
   119      verify b != count
   120      if a > b {
   121          verify b > c
   122      } else {
   123          verify a > c
   124      }
   125      unlock valueAmount of valueAsset
   126    }
   127  }
   128  `
   129  const TestIfAndMultiClause = `
   130  contract TestIfAndMultiClause(a: Integer, cancelKey: PublicKey) locks valueAmount of valueAsset {
   131    clause check(b: Integer, c: Integer) {
   132      verify b != c
   133      if a > b {
   134          verify a > c
   135      }
   136      unlock valueAmount of valueAsset
   137    }
   138    clause cancel(sellerSig: Signature) {
   139      verify checkTxSig(cancelKey, sellerSig)
   140      unlock valueAmount of valueAsset
   141    }
   142  }
   143  `
   144  
   145  const TestIfNesting = `
   146  contract TestIfNesting(a: Integer, count:Integer) locks valueAmount of valueAsset {
   147    clause check(b: Integer, c: Integer, d: Integer) {
   148      verify b != count
   149      if a > b {
   150          if d > c {
   151             verify a > d
   152          }
   153          verify d != b
   154      } else {
   155          verify a > c
   156      }
   157      verify c != count
   158      unlock valueAmount of valueAsset
   159    }
   160    clause cancel(e: Integer, f: Integer) {
   161      verify a != e
   162      if a > f {
   163        verify e > count
   164      }
   165      verify f != count
   166      unlock valueAmount of valueAsset
   167    }
   168  }
   169  `
   170  const TestConstantMath = `
   171  contract TestConstantMath(result: Integer, hashByte: Hash, hashStr: Hash, outcome: Boolean) locks valueAmount of valueAsset {
   172    clause calculation(left: Integer, right: Integer, boolResult: Boolean) {
   173      verify result == left + right + 10
   174      verify hashByte == sha3(0x31323330)
   175      verify hashStr == sha3('string')
   176      verify !outcome
   177      verify boolResult && (result == left + 20)
   178      unlock valueAmount of valueAsset
   179    }
   180  }
   181  `