-
Notifications
You must be signed in to change notification settings - Fork 175
/
fixtures_counter.go
88 lines (73 loc) · 2.28 KB
/
fixtures_counter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package testutil
import (
"fmt"
"github.com/onflow/flow-go/model/flow"
)
const CounterContract = `
access(all) contract Container {
access(all) resource Counter {
pub var count: Int
init(_ v: Int) {
self.count = v
}
pub fun add(_ count: Int) {
self.count = self.count + count
}
}
pub fun createCounter(_ v: Int): @Counter {
return <-create Counter(v)
}
}
`
func DeployCounterContractTransaction(authorizer flow.Address, chain flow.Chain) *flow.TransactionBody {
return CreateContractDeploymentTransaction("Container", CounterContract, authorizer, chain)
}
func DeployUnauthorizedCounterContractTransaction(authorizer flow.Address) *flow.TransactionBody {
return CreateUnauthorizedContractDeploymentTransaction("Container", CounterContract, authorizer)
}
func CreateCounterTransaction(counter, signer flow.Address) *flow.TransactionBody {
return flow.NewTransactionBody().
SetScript([]byte(fmt.Sprintf(`
import 0x%s
transaction {
prepare(acc: AuthAccount) {
var maybeCounter <- acc.load<@Container.Counter>(from: /storage/counter)
if maybeCounter == nil {
maybeCounter <-! Container.createCounter(3)
}
acc.save(<-maybeCounter!, to: /storage/counter)
}
}`, counter)),
).
AddAuthorizer(signer)
}
// CreateCounterPanicTransaction returns a transaction that will manipulate state by writing a new counter into storage
// and then panic. It can be used to test whether execution state stays untouched/will revert
func CreateCounterPanicTransaction(counter, signer flow.Address) *flow.TransactionBody {
return &flow.TransactionBody{
Script: []byte(fmt.Sprintf(`
import 0x%s
transaction {
prepare(acc: AuthAccount) {
if let existing <- acc.load<@Container.Counter>(from: /storage/counter) {
destroy existing
}
panic("fail for testing purposes")
}
}`, counter)),
Authorizers: []flow.Address{signer},
}
}
func AddToCounterTransaction(counter, signer flow.Address) *flow.TransactionBody {
return &flow.TransactionBody{
Script: []byte(fmt.Sprintf(`
import 0x%s
transaction {
prepare(acc: AuthAccount) {
let counter = acc.borrow<&Container.Counter>(from: /storage/counter)
counter?.add(2)
}
}`, counter)),
Authorizers: []flow.Address{signer},
}
}