From 77ba663b0993457720ed46b21ffb1d96cb935e19 Mon Sep 17 00:00:00 2001 From: Illia <166405601+illia-malachyn@users.noreply.github.com> Date: Thu, 12 Sep 2024 21:14:46 +0300 Subject: [PATCH] Illia malachyn/embed scripts to rosetta (#66) * embed scripts instead of copypasting * add comments, update spork info * update sporks to version 7 --- script/cadence/scripts/compute-fees.cdc | 5 + .../cadence/scripts/get-proxy-public-key.cdc | 10 + .../cadence/transactions/create-account.cdc | 17 ++ script/script.go | 235 +++--------------- testnet-clone.json | 6 +- testnet.json | 6 +- 6 files changed, 70 insertions(+), 209 deletions(-) create mode 100644 script/cadence/scripts/compute-fees.cdc create mode 100644 script/cadence/scripts/get-proxy-public-key.cdc create mode 100644 script/cadence/transactions/create-account.cdc diff --git a/script/cadence/scripts/compute-fees.cdc b/script/cadence/scripts/compute-fees.cdc new file mode 100644 index 0000000..a1461e3 --- /dev/null +++ b/script/cadence/scripts/compute-fees.cdc @@ -0,0 +1,5 @@ +import FlowFees from 0xe5a8b7f23e8b548f + +access(all) fun main(inclusionEffort: UFix64, executionEffort: UFix64): UFix64 { + return FlowFees.computeFees(inclusionEffort: inclusionEffort, executionEffort: executionEffort) +} \ No newline at end of file diff --git a/script/cadence/scripts/get-proxy-public-key.cdc b/script/cadence/scripts/get-proxy-public-key.cdc new file mode 100644 index 0000000..71c4679 --- /dev/null +++ b/script/cadence/scripts/get-proxy-public-key.cdc @@ -0,0 +1,10 @@ +import FlowColdStorageProxy from 0xProxy + +access(all) fun main(addr: Address): String { + let acct = getAccount(addr) + let ref = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath) + if let vault = ref { + return String.encodeHex(vault.getPublicKey()) + } + return "" +} \ No newline at end of file diff --git a/script/cadence/transactions/create-account.cdc b/script/cadence/transactions/create-account.cdc new file mode 100644 index 0000000..c209dbc --- /dev/null +++ b/script/cadence/transactions/create-account.cdc @@ -0,0 +1,17 @@ +transaction(publicKeys: [String]) { + prepare(payer: auth(AddKey, BorrowValue) &Account) { + for key in publicKeys { + // Create an account and set the account public key. + let acct = Account(payer: payer) + let publicKey = PublicKey( + publicKey: key.decodeHex(), + signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1 + ) + acct.keys.add( + publicKey: publicKey, + hashAlgorithm: HashAlgorithm.SHA3_256, + weight: 1000.0 + ) + } + } +} \ No newline at end of file diff --git a/script/script.go b/script/script.go index 99b4c44..be47b2d 100644 --- a/script/script.go +++ b/script/script.go @@ -3,6 +3,7 @@ package script import ( "bytes" + _ "embed" "text/template" "github.com/onflow/rosetta/config" @@ -15,149 +16,45 @@ import ( // Adapted from: // https://github.com/onflow/flow-core-contracts/blob/master/transactions/flowToken/transfer_tokens.cdc // Confirmed in use: https://www.flowdiver.io/tx/5316f7b228370d2571a3f2ec5b060a142d3261d8e05b80010c204915843d69e7?tab=script -const BasicTransfer = `import FlowToken from 0x{{.Contracts.FlowToken}} -import FungibleToken from 0x{{.Contracts.FungibleToken}} - -transaction(receiver: Address, amount: UFix64) { - - // The Vault resource that holds the tokens that are being transferred. - let xfer: @{FungibleToken.Vault} - - prepare(sender: auth(BorrowValue) &Account) { - // Get a reference to the sender's FlowToken.Vault. - let vault = sender.storage.borrow(from: /storage/flowTokenVault) - ?? panic("Could not borrow a reference to the sender's vault") - - // Withdraw tokens from the sender's FlowToken.Vault. - self.xfer <- vault.withdraw(amount: amount) - } - - execute { - // Get a reference to the receiver's default FungibleToken.Receiver - // for FLOW tokens. - let receiver = getAccount(receiver) - .capabilities.borrow<&{FungibleToken.Receiver}>(/public/flowTokenReceiver) - ?? panic("Could not borrow a reference to the receiver's vault") - - // Deposit the withdrawn tokens in the receiver's vault. - receiver.deposit(from: <-self.xfer) - } -} -` +// +//go:embed cadence/transactions/basic-transfer.cdc +var BasicTransfer string // ComputeFees computes the transaction fees. -const ComputeFees = `import FlowFees from 0x{{.Contracts.FlowFees}} - -access(all) fun main(inclusionEffort: UFix64, executionEffort: UFix64): UFix64 { - return FlowFees.computeFees(inclusionEffort: inclusionEffort, executionEffort: executionEffort) -} -` +// +//go:embed cadence/scripts/compute-fees.cdc +var ComputeFees string // CreateAccount defines the template for creating new Flow accounts. // Confirmed in use: https://www.flowdiver.io/tx/ff0a8d816fe4f73edee665454f26b5fc06f5a39758cb90c313a9c3372f45f6c7?tab=script -const CreateAccount = `transaction(publicKeys: [String]) { - prepare(payer: auth(AddKey, BorrowValue) &Account) { - for key in publicKeys { - // Create an account and set the account public key. - let acct = Account(payer: payer) - let publicKey = PublicKey( - publicKey: key.decodeHex(), - signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1 - ) - acct.keys.add( - publicKey: publicKey, - hashAlgorithm: HashAlgorithm.SHA3_256, - weight: 1000.0 - ) - } - } -} -` +// +//go:embed cadence/transactions/create-account.cdc +var CreateAccount string // CreateProxyAccount defines the template for creating a new Flow account with // a FlowColdStorageProxy Vault. -const CreateProxyAccount = `import FlowColdStorageProxy from 0x{{.Contracts.FlowColdStorageProxy}} - -transaction(publicKey: String) { - prepare(payer: auth(BorrowValue) &Account) { - // Create a new account with a FlowColdStorageProxy Vault. - FlowColdStorageProxy.setup(payer: payer, publicKey: publicKey.decodeHex()) - } -} -` +// +//go:embed cadence/transactions/create-proxy-account.cdc +var CreateProxyAccount string // GetBalances defines the template for the read-only transaction script that // returns an account's balances. // // The returned balances include the value of the account's default FLOW vault, // Jul 2024: The introduction of FlowColdStorageProxy contract was originally intended to be for the -// Coinbase Institution investment featureset. However, this feature was never completed or released to MN +// Coinbase Institution investment feature set. However, this feature was never completed or released to MN // The current version of Rosetta assumes that all CB accounts contain the contract, which is not the case for // any account on MN. // As a result if these scripts/transactions include the import they will error on execution in MN -const GetBalances = `import FlowToken from 0x{{.Contracts.FlowToken}} -import FungibleToken from 0x{{.Contracts.FungibleToken}} - -access(all) struct AccountBalances { - access(all) let default_balance: UFix64 - access(all) let is_proxy: Bool - access(all) let proxy_balance: UFix64 - - init(default_balance: UFix64, is_proxy: Bool, proxy_balance: UFix64) { - self.default_balance = default_balance - self.is_proxy = is_proxy - self.proxy_balance = proxy_balance - } -} - -access(all) fun main(addr: Address): AccountBalances { - let acct = getAccount(addr) - let balanceRef = acct.capabilities.borrow<&FlowToken.Vault>(/public/flowTokenBalance)! - var is_proxy = false - var proxy_balance = 0.0 - // let ref = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath) - // if let vault = ref { - // is_proxy = true - // proxy_balance = vault.getBalance() - //} - return AccountBalances( - default_balance: balanceRef.balance, - is_proxy: is_proxy, - proxy_balance: proxy_balance - ) -} -` +// +//go:embed cadence/scripts/get-balances.cdc +var GetBalances string // GetBalancesBasic defines the template for the read-only transaction script // that returns the balance of an account's default FLOW vault. -const GetBalancesBasic = `import FlowToken from 0x{{.Contracts.FlowToken}} -import FungibleToken from 0x{{.Contracts.FungibleToken}} - -access(all) struct AccountBalances { - access(all) let default_balance: UFix64 - access(all) let is_proxy: Bool - access(all) let proxy_balance: UFix64 - - init(default_balance: UFix64, is_proxy: Bool, proxy_balance: UFix64) { - self.default_balance = default_balance - self.is_proxy = is_proxy - self.proxy_balance = proxy_balance - } -} - -access(all) fun main(addr: Address): AccountBalances { - let acct = getAccount(addr) - var balance = 0.0 - if let balanceRef = acct.capabilities.borrow<&{FungibleToken.Balance}>(/public/flowTokenBalance) { - balance = balanceRef.balance - } - return AccountBalances( - default_balance: balance, - is_proxy: false, - proxy_balance: 0.0 - ) -} -` +// +//go:embed cadence/scripts/get-balances-basic.cdc +var GetBalancesBasic string // GetProxyNonce defines the template for the read-only transaction script that // returns a proxy account's sequence number, i.e. the next nonce value for its @@ -165,98 +62,30 @@ access(all) fun main(addr: Address): AccountBalances { // // If the account isn't a proxy account, i.e. doesn't have a // FlowColdStorageProxy Vault, then it will return -1. -const GetProxyNonce = `import FlowColdStorageProxy from 0x{{.Contracts.FlowColdStorageProxy}} - -access(all) fun main(addr: Address): Int64 { - let acct = getAccount(addr) - let ref = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath) - if let vault = ref { - return vault.lastNonce + 1 - } - return -1 -} -` +// +//go:embed cadence/scripts/get-proxy-nonce.cdc +var GetProxyNonce string // GetProxyPublicKey defines the template for the read-only transaction script // that returns a proxy account's public key. // // If the account isn't a proxy account, i.e. doesn't have a // FlowColdStorageProxy Vault, then it will return the empty string. -const GetProxyPublicKey = `import FlowColdStorageProxy from 0x{{.Contracts.FlowColdStorageProxy}} - -pub fun main(addr: Address): String { - let acct = getAccount(addr) - let ref = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath) - if let vault = ref { - return String.encodeHex(vault.getPublicKey()) - } - return "" -} -` +// +//go:embed cadence/scripts/get-proxy-public-key.cdc +var GetProxyPublicKey string // ProxyTransfer defines the template for doing a transfer of FLOW from a proxy // account. -const ProxyTransfer = `import FlowColdStorageProxy from 0x{{.Contracts.FlowColdStorageProxy}} - -transaction(sender: Address, receiver: Address, amount: UFix64, nonce: Int64, sig: String) { - prepare(payer: auth(BorrowValue) &Account) { - } - execute { - // Get a reference to the sender's FlowColdStorageProxy.Vault. - let acct = getAccount(sender) - let vault = acct.capabilities.borrow<&FlowColdStorageProxy.Vault>(FlowColdStorageProxy.VaultCapabilityPublicPath)! - - // Transfer tokens to the receiver. - vault.transfer(receiver: receiver, amount: amount, nonce: nonce, sig: sig.decodeHex()) - } -} -` +// +//go:embed cadence/transactions/proxy-transfer.cdc +var ProxyTransfer string // SetContract deploys/updates a contract on an account, while also updating the // account's signing key. -const SetContract = `transaction(update: Bool, contractName: String, contractCode: String, prevKeyIndex: Int, newKey: String, keyMessage: String, keySignature: String, keyMetadata: String) { - prepare(payer: auth(AddKey) AuthAccount) { - let key = PublicKey( - publicKey: newKey.decodeHex(), - signatureAlgorithm: SignatureAlgorithm.ECDSA_secp256k1 - ) - let verified = key.verify( - signature: keySignature.decodeHex(), - signedData: keyMessage.utf8, - domainSeparationTag: "", - hashAlgorithm: HashAlgorithm.SHA2_256 - ) - if !verified { - panic("Key cannot be verified") - } - let prevKey = payer.keys.get(keyIndex: prevKeyIndex) - if prevKey == nil { - panic("Invalid prevKeyIndex, didn't find matching key") - } - let nextKey = payer.keys.get(keyIndex: prevKeyIndex + 1) - if nextKey != nil { - panic("Invalid prevKeyIndex, found key at next key index") - } - if update { - payer.contracts.update__experimental( - name: contractName, - code: contractCode.decodeHex() - ) - } else { - payer.contracts.add( - name: contractName, - code: contractCode.decodeHex() - ) - } - payer.keys.add( - publicKey: key, - hashAlgorithm: HashAlgorithm.SHA3_256, - weight: 1000.0 - ) - payer.keys.revoke(keyIndex: prevKeyIndex) - } -} -` +// +//go:embed cadence/transactions/proxy-contract-update.cdc +var SetContract string // Compile compiles the given script for a particular Flow chain. func Compile(name string, src string, chain *config.Chain) []byte { diff --git a/testnet-clone.json b/testnet-clone.json index c5e5968..ddbed5c 100644 --- a/testnet-clone.json +++ b/testnet-clone.json @@ -18,14 +18,14 @@ "port": 8080, "spork_seal_tolerance": 600, "sporks": { - "50": { + "51": { "access_nodes": [ { "address": "access.devnet.nodes.onflow.org:9000" } ], - "root_block": 185185854, - "version": 6 + "root_block": 211176670, + "version": 7 } } } \ No newline at end of file diff --git a/testnet.json b/testnet.json index ec62d19..2e949df 100644 --- a/testnet.json +++ b/testnet.json @@ -20,14 +20,14 @@ "port": 8080, "spork_seal_tolerance": 600, "sporks": { - "50": { + "51": { "access_nodes": [ { "address": "access.testnet.nodes.onflow.org:9000" } ], - "root_block": 185185854, - "version": 6 + "root_block": 211176670, + "version": 7 } } } \ No newline at end of file