Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new script that checks if payer has balance to pay for tx #435

23 changes: 23 additions & 0 deletions lib/go/templates/internal/assets/assets.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions lib/go/templates/service_templates.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const (
setExecutionMemoryWeighs = "FlowServiceAccount/set_execution_memory_weights.cdc"
getExecutionMemoryLimit = "FlowServiceAccount/scripts/get_execution_memory_limit.cdc"
setExecutionMemoryLimit = "FlowServiceAccount/set_execution_memory_limit.cdc"

checkIfPayerHasSufficientBalance = "FlowServiceAccount/scripts/check_if_payer_has_sufficient_balance.cdc"
)

// FlowToken Templates
Expand Down Expand Up @@ -192,3 +194,9 @@ func GenerateGetExecutionMemoryLimit(env Environment) []byte {

return []byte(ReplaceAddresses(code, env))
}

func GenerateCheckIfPayerHasSufficientBalance(env Environment) []byte {
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
code := assets.MustAssetString(checkIfPayerHasSufficientBalance)

return []byte(ReplaceAddresses(code, env))
}
15 changes: 15 additions & 0 deletions lib/go/test/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,21 @@ func TestContracts(t *testing.T) {

})

t.Run("Should check if payer has sufficient balance to execute tx", func(t *testing.T) {
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
// TODO: Is it better to set up a mock tx and test against it?
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved

// TODO: What account should I use here?
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
account := cadence.NewAddress(flow.HexToAddress(env.FlowFeesAddress))
inclusionEffort := cadence.UFix64(1)
gasLimit := cadence.UFix64(99)

args := [][]byte{jsoncdc.MustEncode(account), jsoncdc.MustEncode(inclusionEffort), jsoncdc.MustEncode(gasLimit)}

result = executeScriptAndCheck(t, b, templates.GenerateCheckIfPayerHasSufficientBalance(env), args)
require.NotNil(t, result)
require.True(t, result.(cadence.Bool).ToGoValue().(bool))
})

// deploy the ServiceAccount contract
serviceAccountCode := contracts.FlowServiceAccount(
emulatorFTAddress,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import FlowFees from 0xFLOWFEESADDRESS

pub fun main(payerAcct: Address, inclusionEffort: UFix64, maxExecutionEffort: UFix64): Bool {
let authAcct = getAuthAccount(payerAcct)
return FlowFees.verifyPayersBalanceForTransactionExecution(authAcct,
illia-malachyn marked this conversation as resolved.
Show resolved Hide resolved
inclusionEffort: inclusionEffort,
maxExecutionEffort: maxExecutionEffort).canExecuteTransaction
}
Loading