@@ -3,6 +3,7 @@ package script
3
3
4
4
import (
5
5
"bytes"
6
+ _ "embed"
6
7
"text/template"
7
8
8
9
"github.com/onflow/rosetta/config"
@@ -15,248 +16,76 @@ import (
15
16
// Adapted from:
16
17
// https://github.com/onflow/flow-core-contracts/blob/master/transactions/flowToken/transfer_tokens.cdc
17
18
// Confirmed in use: https://www.flowdiver.io/tx/5316f7b228370d2571a3f2ec5b060a142d3261d8e05b80010c204915843d69e7?tab=script
18
- const BasicTransfer = `import FlowToken from 0x{{.Contracts.FlowToken}}
19
- import FungibleToken from 0x{{.Contracts.FungibleToken}}
20
-
21
- transaction(receiver: Address, amount: UFix64) {
22
-
23
- // The Vault resource that holds the tokens that are being transferred.
24
- let xfer: @{FungibleToken.Vault}
25
-
26
- prepare(sender: auth(BorrowValue) &Account) {
27
- // Get a reference to the sender's FlowToken.Vault.
28
- let vault = sender.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(from: /storage/flowTokenVault)
29
- ?? panic("Could not borrow a reference to the sender's vault")
30
-
31
- // Withdraw tokens from the sender's FlowToken.Vault.
32
- self.xfer <- vault.withdraw(amount: amount)
33
- }
34
-
35
- execute {
36
- // Get a reference to the receiver's default FungibleToken.Receiver
37
- // for FLOW tokens.
38
- let receiver = getAccount(receiver)
39
- .capabilities.borrow<&{FungibleToken.Receiver}>(/public/flowTokenReceiver)
40
- ?? panic("Could not borrow a reference to the receiver's vault")
41
-
42
- // Deposit the withdrawn tokens in the receiver's vault.
43
- receiver.deposit(from: <-self.xfer)
44
- }
45
- }
46
- `
19
+ //
20
+ //go:embed cadence/transactions/basic-transfer.cdc
21
+ var BasicTransfer string
47
22
48
23
// ComputeFees computes the transaction fees.
49
- const ComputeFees = `import FlowFees from 0x{{.Contracts.FlowFees}}
50
-
51
- access(all) fun main(inclusionEffort: UFix64, executionEffort: UFix64): UFix64 {
52
- return FlowFees.computeFees(inclusionEffort: inclusionEffort, executionEffort: executionEffort)
53
- }
54
- `
24
+ //
25
+ //go:embed cadence/scripts/compute-fees.cdc
26
+ var ComputeFees string
55
27
56
28
// CreateAccount defines the template for creating new Flow accounts.
57
29
// Confirmed in use: https://www.flowdiver.io/tx/ff0a8d816fe4f73edee665454f26b5fc06f5a39758cb90c313a9c3372f45f6c7?tab=script
58
- const CreateAccount = `transaction(publicKeys: [String]) {
59
- prepare(payer: auth(AddKey, BorrowValue) &Account) {
60
- for key in publicKeys {
61
- // Create an account and set the account public key.
62
- let acct = Account(payer: payer)
63
- let publicKey = PublicKey(
64
- publicKey: key.decodeHex(),
65
- signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1
66
- )
67
- acct.keys.add(
68
- publicKey: publicKey,
69
- hashAlgorithm: HashAlgorithm.SHA3_256,
70
- weight: 1000.0
71
- )
72
- }
73
- }
74
- }
75
- `
30
+ //
31
+ //go:embed cadence/transactions/create-account.cdc
32
+ var CreateAccount string
76
33
77
34
// CreateProxyAccount defines the template for creating a new Flow account with
78
35
// a FlowColdStorageProxy Vault.
79
- const CreateProxyAccount = `import FlowColdStorageProxy from 0x{{.Contracts.FlowColdStorageProxy}}
80
-
81
- transaction(publicKey: String) {
82
- prepare(payer: auth(BorrowValue) &Account) {
83
- // Create a new account with a FlowColdStorageProxy Vault.
84
- FlowColdStorageProxy.setup(payer: payer, publicKey: publicKey.decodeHex())
85
- }
86
- }
87
- `
36
+ //
37
+ //go:embed cadence/transactions/create-proxy-account.cdc
38
+ var CreateProxyAccount string
88
39
89
40
// GetBalances defines the template for the read-only transaction script that
90
41
// returns an account's balances.
91
42
//
92
43
// The returned balances include the value of the account's default FLOW vault,
93
44
// Jul 2024: The introduction of FlowColdStorageProxy contract was originally intended to be for the
94
- // Coinbase Institution investment featureset . However, this feature was never completed or released to MN
45
+ // Coinbase Institution investment feature set . However, this feature was never completed or released to MN
95
46
// The current version of Rosetta assumes that all CB accounts contain the contract, which is not the case for
96
47
// any account on MN.
97
48
// As a result if these scripts/transactions include the import they will error on execution in MN
98
- const GetBalances = `import FlowToken from 0x{{.Contracts.FlowToken}}
99
- import FungibleToken from 0x{{.Contracts.FungibleToken}}
100
-
101
- access(all) struct AccountBalances {
102
- access(all) let default_balance: UFix64
103
- access(all) let is_proxy: Bool
104
- access(all) let proxy_balance: UFix64
105
-
106
- init(default_balance: UFix64, is_proxy: Bool, proxy_balance: UFix64) {
107
- self.default_balance = default_balance
108
- self.is_proxy = is_proxy
109
- self.proxy_balance = proxy_balance
110
- }
111
- }
112
-
113
- access(all) fun main(addr: Address): AccountBalances {
114
- let acct = getAccount(addr)
115
- let balanceRef = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance)!
116
- var is_proxy = false
117
- var proxy_balance = 0.0
118
- // let ref = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath)
119
- // if let vault = ref {
120
- // is_proxy = true
121
- // proxy_balance = vault.getBalance()
122
- //}
123
- return AccountBalances(
124
- default_balance: balanceRef.balance,
125
- is_proxy: is_proxy,
126
- proxy_balance: proxy_balance
127
- )
128
- }
129
- `
49
+ //
50
+ //go:embed cadence/scripts/get-balances.cdc
51
+ var GetBalances string
130
52
131
53
// GetBalancesBasic defines the template for the read-only transaction script
132
54
// that returns the balance of an account's default FLOW vault.
133
- const GetBalancesBasic = `import FlowToken from 0x{{.Contracts.FlowToken}}
134
- import FungibleToken from 0x{{.Contracts.FungibleToken}}
135
-
136
- access(all) struct AccountBalances {
137
- access(all) let default_balance: UFix64
138
- access(all) let is_proxy: Bool
139
- access(all) let proxy_balance: UFix64
140
-
141
- init(default_balance: UFix64, is_proxy: Bool, proxy_balance: UFix64) {
142
- self.default_balance = default_balance
143
- self.is_proxy = is_proxy
144
- self.proxy_balance = proxy_balance
145
- }
146
- }
147
-
148
- access(all) fun main(addr: Address): AccountBalances {
149
- let acct = getAccount(addr)
150
- var balance = 0.0
151
- if let balanceRef = acct.capabilities.borrow<&{FungibleToken.Balance}>(/public/flowTokenBalance) {
152
- balance = balanceRef.balance
153
- }
154
- return AccountBalances(
155
- default_balance: balance,
156
- is_proxy: false,
157
- proxy_balance: 0.0
158
- )
159
- }
160
- `
55
+ //
56
+ //go:embed cadence/scripts/get-balances-basic.cdc
57
+ var GetBalancesBasic string
161
58
162
59
// GetProxyNonce defines the template for the read-only transaction script that
163
60
// returns a proxy account's sequence number, i.e. the next nonce value for its
164
61
// FlowColdStorageProxy Vault.
165
62
//
166
63
// If the account isn't a proxy account, i.e. doesn't have a
167
64
// FlowColdStorageProxy Vault, then it will return -1.
168
- const GetProxyNonce = `import FlowColdStorageProxy from 0x{{.Contracts.FlowColdStorageProxy}}
169
-
170
- access(all) fun main(addr: Address): Int64 {
171
- let acct = getAccount(addr)
172
- let ref = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath)
173
- if let vault = ref {
174
- return vault.lastNonce + 1
175
- }
176
- return -1
177
- }
178
- `
65
+ //
66
+ //go:embed cadence/scripts/get-proxy-nonce.cdc
67
+ var GetProxyNonce string
179
68
180
69
// GetProxyPublicKey defines the template for the read-only transaction script
181
70
// that returns a proxy account's public key.
182
71
//
183
72
// If the account isn't a proxy account, i.e. doesn't have a
184
73
// FlowColdStorageProxy Vault, then it will return the empty string.
185
- const GetProxyPublicKey = `import FlowColdStorageProxy from 0x{{.Contracts.FlowColdStorageProxy}}
186
-
187
- pub fun main(addr: Address): String {
188
- let acct = getAccount(addr)
189
- let ref = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath)
190
- if let vault = ref {
191
- return String.encodeHex(vault.getPublicKey())
192
- }
193
- return ""
194
- }
195
- `
74
+ //
75
+ //go:embed cadence/scripts/get-proxy-public-key.cdc
76
+ var GetProxyPublicKey string
196
77
197
78
// ProxyTransfer defines the template for doing a transfer of FLOW from a proxy
198
79
// account.
199
- const ProxyTransfer = `import FlowColdStorageProxy from 0x{{.Contracts.FlowColdStorageProxy}}
200
-
201
- transaction(sender: Address, receiver: Address, amount: UFix64, nonce: Int64, sig: String) {
202
- prepare(payer: auth(BorrowValue) &Account) {
203
- }
204
- execute {
205
- // Get a reference to the sender's FlowColdStorageProxy.Vault.
206
- let acct = getAccount(sender)
207
- let vault = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath)!
208
-
209
- // Transfer tokens to the receiver.
210
- vault.transfer(receiver: receiver, amount: amount, nonce: nonce, sig: sig.decodeHex())
211
- }
212
- }
213
- `
80
+ //
81
+ //go:embed cadence/transactions/proxy-transfer.cdc
82
+ var ProxyTransfer string
214
83
215
84
// SetContract deploys/updates a contract on an account, while also updating the
216
85
// account's signing key.
217
- const SetContract = `transaction(update: Bool, contractName: String, contractCode: String, prevKeyIndex: Int, newKey: String, keyMessage: String, keySignature: String, keyMetadata: String) {
218
- prepare(payer: auth(AddKey) AuthAccount) {
219
- let key = PublicKey(
220
- publicKey: newKey.decodeHex(),
221
- signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1
222
- )
223
- let verified = key.verify(
224
- signature: keySignature.decodeHex(),
225
- signedData: keyMessage.utf8,
226
- domainSeparationTag: "",
227
- hashAlgorithm: HashAlgorithm.SHA2_256
228
- )
229
- if !verified {
230
- panic("Key cannot be verified")
231
- }
232
- let prevKey = payer.keys.get(keyIndex: prevKeyIndex)
233
- if prevKey == nil {
234
- panic("Invalid prevKeyIndex, didn't find matching key")
235
- }
236
- let nextKey = payer.keys.get(keyIndex: prevKeyIndex + 1)
237
- if nextKey != nil {
238
- panic("Invalid prevKeyIndex, found key at next key index")
239
- }
240
- if update {
241
- payer.contracts.update__experimental(
242
- name: contractName,
243
- code: contractCode.decodeHex()
244
- )
245
- } else {
246
- payer.contracts.add(
247
- name: contractName,
248
- code: contractCode.decodeHex()
249
- )
250
- }
251
- payer.keys.add(
252
- publicKey: key,
253
- hashAlgorithm: HashAlgorithm.SHA3_256,
254
- weight: 1000.0
255
- )
256
- payer.keys.revoke(keyIndex: prevKeyIndex)
257
- }
258
- }
259
- `
86
+ //
87
+ //go:embed cadence/transactions/proxy-contract-update.cdc
88
+ var SetContract string
260
89
261
90
// Compile compiles the given script for a particular Flow chain.
262
91
func Compile (name string , src string , chain * config.Chain ) []byte {
0 commit comments