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