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 @@ -41,6 +41,8 @@ const (
getExecutionMemoryLimit = "FlowServiceAccount/scripts/get_execution_memory_limit.cdc"
setExecutionMemoryLimit = "FlowServiceAccount/set_execution_memory_limit.cdc"

verifyPayerBalanceForTxExecution = "FlowServiceAccount/scripts/verify_payer_balance_for_tx_execution.cdc"

// Account templates
createAccountFilename = "accounts/create_new_account.cdc"
addKeyFilename = "accounts/add_key.cdc"
Expand Down Expand Up @@ -243,3 +245,9 @@ func GenerateGetExecutionMemoryLimit(env Environment) []byte {

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

func GenerateVerifyPayerBalanceForTxExecution(env Environment) []byte {
code := assets.MustAssetString(verifyPayerBalanceForTxExecution)

return []byte(ReplaceAddresses(code, env))
}
70 changes: 70 additions & 0 deletions lib/go/test/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,76 @@ 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: it doesn't work, does it?
tx := createTxWithTemplateAndAuthorizer(b,
templates.GenerateChangeStorageFeeParametersScript(env),
storageFeesAddress)

err = tx.AddArgument(CadenceUFix64("1.0"))
require.NoError(t, err)
err = tx.AddArgument(CadenceUFix64("4.0"))
require.NoError(t, err)

signAndSubmit(
t, b, tx,
[]flow.Address{storageFeesAddress},
[]crypto.Signer{storageFeesSigner},
false,
)

assert.NoError(t, err)
_, err = b.CommitBlock()
assert.NoError(t, err)

// create SmallBalanceContract contract
code := []byte(`
access(all) contract SmallBalanceContract {
access(all) let value: Int32

init() {
self.value = 42
}
}
`)
keys := test.AccountKeyGenerator()
key, _ := keys.NewWithSigner()
address, err := adapter.CreateAccount(context.Background(), []*flow.AccountKey{key}, []sdktemplates.Contract{
{
Name: "SmallBalanceContract",
Source: string(code),
},
})

assert.NoError(t, err)
_, err = b.CommitBlock()
assert.NoError(t, err)

// mint some flows to the account
mintTokensForAccount(t, b, env, address, "15.0")

//TODO: remove this. this is for debug only
acc, err := adapter.GetAccount(context.Background(), address)
require.NotNil(t, acc)
require.NoError(t, err)

// set up args
cadenceAddress := cadence.NewAddress(address)
inclusionEffort := cadence.UFix64(500)
gasLimit := cadence.UFix64(1)

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

result = executeScriptAndCheck(t, b, templates.GenerateVerifyPayerBalanceForTxExecution(env), args)
require.NotNil(t, result)

// parse VerifyPayerBalanceResult.canExecuteTransaction
resultStruct := result.(cadence.Struct)
fields := cadence.FieldsMappedByName(resultStruct)
canExecuteTransaction := bool(fields["canExecuteTransaction"].(cadence.Bool))
require.True(t, canExecuteTransaction)
})

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

access(all) fun main(payerAcct: Address, inclusionEffort: UFix64, maxExecutionEffort: UFix64): FlowFees.VerifyPayerBalanceResult {
let authAcct = getAuthAccount<auth(BorrowValue) &Account>(payerAcct)
return FlowFees.verifyPayersBalanceForTransactionExecution(authAcct, inclusionEffort: inclusionEffort,
maxExecutionEffort: maxExecutionEffort)
}
Loading