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