-
Notifications
You must be signed in to change notification settings - Fork 178
/
account.go
113 lines (91 loc) · 2.99 KB
/
account.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
113
package fvm
import (
"fmt"
"github.com/onflow/cadence"
jsoncdc "github.com/onflow/cadence/encoding/json"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/flow-go/fvm/programs"
"github.com/onflow/flow-go/fvm/state"
"github.com/onflow/flow-go/model/flow"
)
func getAccount(
vm *VirtualMachine,
ctx Context,
sth *state.StateHolder,
programs *programs.Programs,
address flow.Address,
) (*flow.Account, error) {
accounts := state.NewAccounts(sth)
account, err := accounts.Get(address)
if err != nil {
return nil, err
}
if ctx.ServiceAccountEnabled {
env := newEnvironment(ctx, vm, sth, programs)
balance, err := env.GetAccountBalance(common.BytesToAddress(address.Bytes()))
if err != nil {
return nil, err
}
account.Balance = balance
}
return account, nil
}
const initAccountTransactionTemplate = `
import FlowServiceAccount from 0x%s
transaction(restrictedAccountCreationEnabled: Bool) {
prepare(newAccount: AuthAccount, payerAccount: AuthAccount) {
if restrictedAccountCreationEnabled && !FlowServiceAccount.isAccountCreator(payerAccount.address) {
panic("Account not authorized to create accounts")
}
FlowServiceAccount.setupNewAccount(newAccount: newAccount, payer: payerAccount)
}
}
`
const getFlowTokenBalanceScriptTemplate = `
import FlowServiceAccount from 0x%s
pub fun main(): UFix64 {
let acct = getAccount(0x%s)
return FlowServiceAccount.defaultTokenBalance(acct)
}
`
const getFlowTokenAvailableBalanceScriptTemplate = `
import FlowStorageFees from 0x%s
pub fun main(): UFix64 {
return FlowStorageFees.defaultTokenAvailableBalance(0x%s)
}
`
const getStorageCapacityScriptTemplate = `
import FlowStorageFees from 0x%s
pub fun main(): UFix64 {
return FlowStorageFees.calculateAccountCapacity(0x%s)
}
`
func initAccountTransaction(
payerAddress flow.Address,
accountAddress flow.Address,
serviceAddress flow.Address,
restrictedAccountCreationEnabled bool,
) *TransactionProcedure {
arg, err := jsoncdc.Encode(cadence.NewBool(restrictedAccountCreationEnabled))
if err != nil {
// this should not fail! It simply encodes a boolean
panic(fmt.Errorf("cannot json encode cadence boolean argument: %w", err))
}
return Transaction(
flow.NewTransactionBody().
SetScript([]byte(fmt.Sprintf(initAccountTransactionTemplate, serviceAddress))).
AddAuthorizer(accountAddress).
AddAuthorizer(payerAddress).
AddArgument(arg),
0,
)
}
func getFlowTokenBalanceScript(accountAddress, serviceAddress flow.Address) *ScriptProcedure {
return Script([]byte(fmt.Sprintf(getFlowTokenBalanceScriptTemplate, serviceAddress, accountAddress)))
}
func getFlowTokenAvailableBalanceScript(accountAddress, serviceAddress flow.Address) *ScriptProcedure {
return Script([]byte(fmt.Sprintf(getFlowTokenAvailableBalanceScriptTemplate, serviceAddress, accountAddress)))
}
func getStorageCapacityScript(accountAddress, serviceAddress flow.Address) *ScriptProcedure {
return Script([]byte(fmt.Sprintf(getStorageCapacityScriptTemplate, serviceAddress, accountAddress)))
}