Skip to content

Commit 77ba663

Browse files
Illia malachyn/embed scripts to rosetta (#66)
* embed scripts instead of copypasting * add comments, update spork info * update sporks to version 7
1 parent 41d1988 commit 77ba663

File tree

6 files changed

+70
-209
lines changed

6 files changed

+70
-209
lines changed
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import FlowFees from 0xe5a8b7f23e8b548f
2+
3+
access(all) fun main(inclusionEffort: UFix64, executionEffort: UFix64): UFix64 {
4+
return FlowFees.computeFees(inclusionEffort: inclusionEffort, executionEffort: executionEffort)
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import FlowColdStorageProxy from 0xProxy
2+
3+
access(all) fun main(addr: Address): String {
4+
let acct = getAccount(addr)
5+
let ref = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath)
6+
if let vault = ref {
7+
return String.encodeHex(vault.getPublicKey())
8+
}
9+
return ""
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
transaction(publicKeys: [String]) {
2+
prepare(payer: auth(AddKey, BorrowValue) &Account) {
3+
for key in publicKeys {
4+
// Create an account and set the account public key.
5+
let acct = Account(payer: payer)
6+
let publicKey = PublicKey(
7+
publicKey: key.decodeHex(),
8+
signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1
9+
)
10+
acct.keys.add(
11+
publicKey: publicKey,
12+
hashAlgorithm: HashAlgorithm.SHA3_256,
13+
weight: 1000.0
14+
)
15+
}
16+
}
17+
}

script/script.go

+32-203
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package script
33

44
import (
55
"bytes"
6+
_ "embed"
67
"text/template"
78

89
"github.com/onflow/rosetta/config"
@@ -15,248 +16,76 @@ import (
1516
// Adapted from:
1617
// https://github.com/onflow/flow-core-contracts/blob/master/transactions/flowToken/transfer_tokens.cdc
1718
// 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
4722

4823
// 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
5527

5628
// CreateAccount defines the template for creating new Flow accounts.
5729
// 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
7633

7734
// CreateProxyAccount defines the template for creating a new Flow account with
7835
// 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
8839

8940
// GetBalances defines the template for the read-only transaction script that
9041
// returns an account's balances.
9142
//
9243
// The returned balances include the value of the account's default FLOW vault,
9344
// 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
9546
// The current version of Rosetta assumes that all CB accounts contain the contract, which is not the case for
9647
// any account on MN.
9748
// 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
13052

13153
// GetBalancesBasic defines the template for the read-only transaction script
13254
// 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
16158

16259
// GetProxyNonce defines the template for the read-only transaction script that
16360
// returns a proxy account's sequence number, i.e. the next nonce value for its
16461
// FlowColdStorageProxy Vault.
16562
//
16663
// If the account isn't a proxy account, i.e. doesn't have a
16764
// 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
17968

18069
// GetProxyPublicKey defines the template for the read-only transaction script
18170
// that returns a proxy account's public key.
18271
//
18372
// If the account isn't a proxy account, i.e. doesn't have a
18473
// 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
19677

19778
// ProxyTransfer defines the template for doing a transfer of FLOW from a proxy
19879
// 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
21483

21584
// SetContract deploys/updates a contract on an account, while also updating the
21685
// 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
26089

26190
// Compile compiles the given script for a particular Flow chain.
26291
func Compile(name string, src string, chain *config.Chain) []byte {

testnet-clone.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@
1818
"port": 8080,
1919
"spork_seal_tolerance": 600,
2020
"sporks": {
21-
"50": {
21+
"51": {
2222
"access_nodes": [
2323
{
2424
"address": "access.devnet.nodes.onflow.org:9000"
2525
}
2626
],
27-
"root_block": 185185854,
28-
"version": 6
27+
"root_block": 211176670,
28+
"version": 7
2929
}
3030
}
3131
}

testnet.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,14 @@
2020
"port": 8080,
2121
"spork_seal_tolerance": 600,
2222
"sporks": {
23-
"50": {
23+
"51": {
2424
"access_nodes": [
2525
{
2626
"address": "access.testnet.nodes.onflow.org:9000"
2727
}
2828
],
29-
"root_block": 185185854,
30-
"version": 6
29+
"root_block": 211176670,
30+
"version": 7
3131
}
3232
}
3333
}

0 commit comments

Comments
 (0)