-
Notifications
You must be signed in to change notification settings - Fork 178
/
token.go
76 lines (64 loc) · 2.37 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
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)
}
//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, flowToken flow.Address) *flow.TransactionBody {
contract := contracts.FlowToken(fungibleToken.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)
}