github.com/koko1123/flow-go-1@v0.29.6/engine/execution/testutil/fixtures_counter.go (about)

     1  package testutil
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/koko1123/flow-go-1/model/flow"
     7  )
     8  
     9  const CounterContract = `
    10  access(all) contract Container {
    11  	access(all) resource Counter {
    12  		pub var count: Int
    13  
    14  		init(_ v: Int) {
    15  			self.count = v
    16  		}
    17  		pub fun add(_ count: Int) {
    18  			self.count = self.count + count
    19  		}
    20  	}
    21  	pub fun createCounter(_ v: Int): @Counter {
    22  		return <-create Counter(v)
    23  	}
    24  }
    25  `
    26  
    27  const CounterContractV2 = `
    28  access(all) contract Container {
    29  	access(all) resource Counter {
    30  		pub var count: Int
    31  
    32  		init(_ v: Int) {
    33  			self.count = v
    34  		}
    35  		pub fun add(_ count: Int) {
    36  			self.count = self.count + count
    37  		}
    38  	}
    39  	pub fun createCounter(_ v: Int): @Counter {
    40  		return <-create Counter(v)
    41  	}
    42  
    43  	pub fun createCounter2(_ v: Int): @Counter {
    44  		return <-create Counter(v)
    45  	}
    46  }
    47  `
    48  
    49  func DeployCounterContractTransaction(authorizer flow.Address, chain flow.Chain) *flow.TransactionBody {
    50  	return CreateContractDeploymentTransaction("Container", CounterContract, authorizer, chain)
    51  }
    52  
    53  func DeployUnauthorizedCounterContractTransaction(authorizer flow.Address) *flow.TransactionBody {
    54  	return CreateUnauthorizedContractDeploymentTransaction("Container", CounterContract, authorizer)
    55  }
    56  
    57  func UpdateUnauthorizedCounterContractTransaction(authorizer flow.Address) *flow.TransactionBody {
    58  	return UpdateContractUnathorizedDeploymentTransaction("Container", CounterContractV2, authorizer)
    59  }
    60  
    61  func RemoveUnauthorizedCounterContractTransaction(authorizer flow.Address) *flow.TransactionBody {
    62  	return RemoveContractUnathorizedDeploymentTransaction("Container", authorizer)
    63  }
    64  
    65  func RemoveCounterContractTransaction(authorizer flow.Address, chain flow.Chain) *flow.TransactionBody {
    66  	return RemoveContractDeploymentTransaction("Container", authorizer, chain)
    67  }
    68  
    69  func CreateCounterTransaction(counter, signer flow.Address) *flow.TransactionBody {
    70  	return flow.NewTransactionBody().
    71  		SetScript([]byte(fmt.Sprintf(`
    72  			import 0x%s
    73  
    74  			transaction {
    75  				prepare(acc: AuthAccount) {
    76  					var maybeCounter <- acc.load<@Container.Counter>(from: /storage/counter)
    77  
    78  					if maybeCounter == nil {
    79  						maybeCounter <-! Container.createCounter(3)
    80  					}
    81  
    82  					acc.save(<-maybeCounter!, to: /storage/counter)
    83  				}
    84  			}`, counter)),
    85  		).
    86  		AddAuthorizer(signer)
    87  }
    88  
    89  // CreateCounterPanicTransaction returns a transaction that will manipulate state by writing a new counter into storage
    90  // and then panic. It can be used to test whether execution state stays untouched/will revert
    91  func CreateCounterPanicTransaction(counter, signer flow.Address) *flow.TransactionBody {
    92  	return &flow.TransactionBody{
    93  		Script: []byte(fmt.Sprintf(`
    94  			import 0x%s
    95  
    96  			transaction {
    97  				prepare(acc: AuthAccount) {
    98  					if let existing <- acc.load<@Container.Counter>(from: /storage/counter) {
    99  						destroy existing
   100              		}
   101  
   102  					panic("fail for testing purposes")
   103                	}
   104              }`, counter)),
   105  		Authorizers: []flow.Address{signer},
   106  	}
   107  }
   108  
   109  func AddToCounterTransaction(counter, signer flow.Address) *flow.TransactionBody {
   110  	return &flow.TransactionBody{
   111  		Script: []byte(fmt.Sprintf(`
   112  			import 0x%s
   113  
   114  			transaction {
   115  				prepare(acc: AuthAccount) {
   116  					let counter = acc.borrow<&Container.Counter>(from: /storage/counter)
   117  					counter?.add(2)
   118  				}
   119  			}`, counter)),
   120  		Authorizers: []flow.Address{signer},
   121  	}
   122  }