flyq
May 12, 2021, 10:20am
1
}
}
fmt.Println("My Address:", myAddress.Hex())
examples.FundAccountInEmulator(flowClient, myAddress, 100.0)
serviceAcctKey.SequenceNumber++
// Deploy the Great NFT contract
nftCode := examples.ReadFile(GreatTokenContractFile)
deployContractTx := templates.CreateAccount(nil,
[]templates.Contract{{
Name: "GreatToken",
Source: nftCode,
}}, serviceAcctAddr)
deployContractTx.SetProposalKey(
serviceAcctAddr,
serviceAcctKey.Index,
serviceAcctKey.SequenceNumber,
)
the examples is:
// Deploy the Great NFT contract
nftCode := examples.ReadFile(GreatTokenContractFile)
deployContractTx := templates.CreateAccount(nil,
[]templates.Contract{{
Name: "GreatToken",
Source: nftCode,
}}, serviceAcctAddr)
deployContractTx.SetProposalKey(
serviceAcctAddr,
serviceAcctKey.Index,
serviceAcctKey.SequenceNumber,
)
if the first argument of function CreateAccount(nil,...)
is nil, is the contract will be deployed the new account? if I just want to deploy the contract, not create any account, what should I do? is there any examples? Thanks
Contracts get deployed to accounts, so in order to deploy a contract, you have to either create a new account, or deploy it to an existing one that you control. Which one are you trying to do?
dh77
May 18, 2021, 10:39am
4
@flyq Here’s the full code for the *.go file
(You will also need Pinata Contract Code from here: Pinata Contract Code ):
package main
import (
“context”
“encoding/hex”
“fmt”
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/client"
"github.com/onflow/flow-go-sdk/crypto"
"github.com/onflow/flow-go-sdk/examples"
"google.golang.org/grpc"
)
const deployContractScript string = transaction(contractName: String, contractCode: String) { prepare(signer: AuthAccount) { signer.contracts.add(name: contractName, code: contractCode.decodeHex()) } }
func main() {
// Flow Test Net Node
node := “access.devnet.nodes.onflow.org:9000 ”
ctx := context.Background()
flowClient, err := client.New(node, grpc.WithInsecure())
examples.Handle(err)
// Flow Test Net Address & Private Key
serviceAddressHex := "SERVICE_ADDRESS_HEX"
servicePrivKeyHex := "SERVICE_PRIVATE_KEY"
senderAddress := flow.HexToAddress(serviceAddressHex)
sigAlgo := crypto.StringToSignatureAlgorithm("ECDSA_P256")
hashAlgoName := "SHA3_256"
senderPrivateKey, err := crypto.DecodePrivateKeyHex(sigAlgo, servicePrivKeyHex)
examples.Handle(err)
account, err := flowClient.GetAccount(context.Background(), senderAddress)
examples.Handle(err)
hashAlgo := crypto.StringToHashAlgorithm(hashAlgoName)
senderAccountKey := account.Keys[0]
signer := crypto.NewInMemorySigner(senderPrivateKey, hashAlgo)
referenceBlockID := examples.GetReferenceBlockId(flowClient)
tx := flow.NewTransaction().
SetScript([]byte(deployContractScript)).
SetGasLimit(100).
SetPayer(senderAddress).
AddAuthorizer(senderAddress).
SetReferenceBlockID(referenceBlockID).
SetProposalKey(senderAddress, senderAccountKey.Index, senderAccountKey.SequenceNumber)
contractString := examples.ReadFile("./PinataPartyContract.cdc")
contractName := cadence.NewString("PinataPartyContract")
contractCode := cadence.NewString(hex.EncodeToString([]byte(contractString)))
tx.AddRawArgument(jsoncdc.MustEncode(contractName))
tx.AddRawArgument(jsoncdc.MustEncode(contractCode))
err = tx.SignEnvelope(senderAddress, senderAccountKey.Index, signer)
examples.Handle(err)
err = flowClient.SendTransaction(ctx, *tx)
examples.Handle(err)
result := examples.WaitForSeal(ctx, flowClient, tx.ID())
examples.Handle(result.Error)
fmt.Println("Contract deployed succesfully")
}