github.com/onflow/flow-go@v0.35.7-crescendo-preview.23-atree-inlining/fvm/blueprints/token.go (about)

     1  package blueprints
     2  
     3  import (
     4  	_ "embed"
     5  	"encoding/hex"
     6  	"fmt"
     7  
     8  	"github.com/onflow/cadence"
     9  	jsoncdc "github.com/onflow/cadence/encoding/json"
    10  
    11  	"github.com/onflow/flow-core-contracts/lib/go/contracts"
    12  	"github.com/onflow/flow-core-contracts/lib/go/templates"
    13  
    14  	"github.com/onflow/flow-go/model/flow"
    15  )
    16  
    17  func DeployFungibleTokenContractTransaction(fungibleToken flow.Address, contract []byte) *flow.TransactionBody {
    18  	contractName := "FungibleToken"
    19  	return DeployContractTransaction(
    20  		fungibleToken,
    21  		contract,
    22  		contractName,
    23  	)
    24  }
    25  
    26  func DeployNonFungibleTokenContractTransaction(nonFungibleToken flow.Address, contract []byte) *flow.TransactionBody {
    27  	contractName := "NonFungibleToken"
    28  	return DeployContractTransaction(
    29  		nonFungibleToken,
    30  		contract,
    31  		contractName,
    32  	)
    33  }
    34  
    35  func DeployMetadataViewsContractTransaction(nonFungibleToken flow.Address, contract []byte) *flow.TransactionBody {
    36  	contractName := "MetadataViews"
    37  	return DeployContractTransaction(
    38  		nonFungibleToken,
    39  		contract,
    40  		contractName,
    41  	)
    42  }
    43  
    44  func DeployViewResolverContractTransaction(nonFungibleToken flow.Address) *flow.TransactionBody {
    45  	contract := contracts.ViewResolver()
    46  	contractName := "ViewResolver"
    47  	return DeployContractTransaction(
    48  		nonFungibleToken,
    49  		contract,
    50  		contractName,
    51  	)
    52  }
    53  
    54  func DeployBurnerContractTransaction(fungibleToken flow.Address) *flow.TransactionBody {
    55  	contract := contracts.Burner()
    56  	contractName := "Burner"
    57  	return DeployContractTransaction(
    58  		fungibleToken,
    59  		contract,
    60  		contractName,
    61  	)
    62  }
    63  
    64  func DeployFungibleTokenMetadataViewsContractTransaction(fungibleToken flow.Address, contract []byte) *flow.TransactionBody {
    65  
    66  	contractName := "FungibleTokenMetadataViews"
    67  	return DeployContractTransaction(
    68  		fungibleToken,
    69  		contract,
    70  		contractName,
    71  	)
    72  }
    73  
    74  func DeployFungibleTokenSwitchboardContractTransaction(fungibleToken flow.Address, contract []byte) *flow.TransactionBody {
    75  
    76  	contractName := "FungibleTokenSwitchboard"
    77  	return DeployContractTransaction(
    78  		fungibleToken,
    79  		contract,
    80  		contractName,
    81  	)
    82  }
    83  
    84  //go:embed scripts/deployFlowTokenTransactionTemplate.cdc
    85  var deployFlowTokenTransactionTemplate string
    86  
    87  //go:embed scripts/createFlowTokenMinterTransactionTemplate.cdc
    88  var createFlowTokenMinterTransactionTemplate string
    89  
    90  //go:embed scripts/mintFlowTokenTransactionTemplate.cdc
    91  var mintFlowTokenTransactionTemplate string
    92  
    93  func DeployFlowTokenContractTransaction(service, flowToken flow.Address, contract []byte) *flow.TransactionBody {
    94  
    95  	return flow.NewTransactionBody().
    96  		SetScript([]byte(deployFlowTokenTransactionTemplate)).
    97  		AddArgument(jsoncdc.MustEncode(cadence.String(hex.EncodeToString(contract)))).
    98  		AddAuthorizer(flowToken).
    99  		AddAuthorizer(service)
   100  }
   101  
   102  // CreateFlowTokenMinterTransaction returns a transaction which creates a Flow
   103  // token Minter resource and stores it in the service account. This Minter is
   104  // expected to be stored here by the epoch smart contracts.
   105  func CreateFlowTokenMinterTransaction(service, flowToken flow.Address) *flow.TransactionBody {
   106  	return flow.NewTransactionBody().
   107  		SetScript([]byte(templates.ReplaceAddresses(
   108  			createFlowTokenMinterTransactionTemplate,
   109  			templates.Environment{
   110  				FlowTokenAddress: flowToken.Hex(),
   111  			})),
   112  		).
   113  		AddAuthorizer(service)
   114  }
   115  
   116  func MintFlowTokenTransaction(
   117  	fungibleToken, flowToken, service flow.Address,
   118  	initialSupply cadence.UFix64,
   119  ) *flow.TransactionBody {
   120  	initialSupplyArg, err := jsoncdc.Encode(initialSupply)
   121  	if err != nil {
   122  		panic(fmt.Sprintf("failed to encode initial token supply: %s", err.Error()))
   123  	}
   124  
   125  	return flow.NewTransactionBody().
   126  		SetScript([]byte(templates.ReplaceAddresses(mintFlowTokenTransactionTemplate,
   127  			templates.Environment{
   128  				FlowTokenAddress:     flowToken.Hex(),
   129  				FungibleTokenAddress: fungibleToken.Hex(),
   130  			})),
   131  		).
   132  		AddArgument(initialSupplyArg).
   133  		AddAuthorizer(service)
   134  }