github.com/koko1123/flow-go-1@v0.29.6/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/koko1123/flow-go-1/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  //go:embed scripts/deployFlowTokenTransactionTemplate.cdc
    26  var deployFlowTokenTransactionTemplate string
    27  
    28  //go:embed scripts/createFlowTokenMinterTransactionTemplate.cdc
    29  var createFlowTokenMinterTransactionTemplate string
    30  
    31  //go:embed scripts/mintFlowTokenTransactionTemplate.cdc
    32  var mintFlowTokenTransactionTemplate string
    33  
    34  func DeployFlowTokenContractTransaction(service, fungibleToken, flowToken flow.Address) *flow.TransactionBody {
    35  	contract := contracts.FlowToken(fungibleToken.HexWithPrefix())
    36  
    37  	return flow.NewTransactionBody().
    38  		SetScript([]byte(deployFlowTokenTransactionTemplate)).
    39  		AddArgument(jsoncdc.MustEncode(cadence.String(hex.EncodeToString(contract)))).
    40  		AddAuthorizer(flowToken).
    41  		AddAuthorizer(service)
    42  }
    43  
    44  // CreateFlowTokenMinterTransaction returns a transaction which creates a Flow
    45  // token Minter resource and stores it in the service account. This Minter is
    46  // expected to be stored here by the epoch smart contracts.
    47  func CreateFlowTokenMinterTransaction(service, flowToken flow.Address) *flow.TransactionBody {
    48  	return flow.NewTransactionBody().
    49  		SetScript([]byte(templates.ReplaceAddresses(
    50  			createFlowTokenMinterTransactionTemplate,
    51  			templates.Environment{
    52  				FlowTokenAddress: flowToken.Hex(),
    53  			})),
    54  		).
    55  		AddAuthorizer(service)
    56  }
    57  
    58  func MintFlowTokenTransaction(
    59  	fungibleToken, flowToken, service flow.Address,
    60  	initialSupply cadence.UFix64,
    61  ) *flow.TransactionBody {
    62  	initialSupplyArg, err := jsoncdc.Encode(initialSupply)
    63  	if err != nil {
    64  		panic(fmt.Sprintf("failed to encode initial token supply: %s", err.Error()))
    65  	}
    66  
    67  	return flow.NewTransactionBody().
    68  		SetScript([]byte(templates.ReplaceAddresses(mintFlowTokenTransactionTemplate,
    69  			templates.Environment{
    70  				FlowTokenAddress:     flowToken.Hex(),
    71  				FungibleTokenAddress: fungibleToken.Hex(),
    72  			})),
    73  		).
    74  		AddArgument(initialSupplyArg).
    75  		AddAuthorizer(service)
    76  }