Skip to content

Commit

Permalink
fix api to be consistent and include fees
Browse files Browse the repository at this point in the history
* trying to fix emulator with fees

* fix all tests except fill up storage that does not work that well currently

* fixes #51

* fix spelling error

* debugging

* add name to transaction and fix output

* use name in scripts

* format name so that filename is preserved

* made the output even better, almost no need to have flowkit log on now

* fix better api for setup

* refactor old setup functions

* added back tests for panics again

* fixed free up space

* fixed output of gas from testnet/mainnet

* more renaming options

* use newst flowkit
  • Loading branch information
bjartek authored Jul 25, 2022
1 parent 53764a5 commit 24cea60
Show file tree
Hide file tree
Showing 30 changed files with 763 additions and 507 deletions.
10 changes: 5 additions & 5 deletions argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func (a *FlowArgumentsBuilder) Argument(value cadence.Value) *FlowArgumentsBuild
func (a *FlowArgumentsBuilder) StringMap(input map[string]string) *FlowArgumentsBuilder {
array := []cadence.KeyValuePair{}
for key, val := range input {
array = append(array, cadence.KeyValuePair{Key: CadenceString(key), Value: CadenceString(val)})
array = append(array, cadence.KeyValuePair{Key: cadenceString(key), Value: cadenceString(val)})
}
a.Arguments = append(a.Arguments, cadence.NewDictionary(array))
return a
Expand All @@ -300,7 +300,7 @@ func (a *FlowArgumentsBuilder) ScalarMap(input map[string]string) *FlowArguments
a.Error = err
return a
}
array = append(array, cadence.KeyValuePair{Key: CadenceString(key), Value: UFix64Val})
array = append(array, cadence.KeyValuePair{Key: cadenceString(key), Value: UFix64Val})
}
a.Arguments = append(a.Arguments, cadence.NewDictionary(array))
return a
Expand All @@ -312,7 +312,7 @@ func (a *FlowArgumentsBuilder) ScalarMap(input map[string]string) *FlowArguments
func (a *FlowArgumentsBuilder) StringArray(value ...string) *FlowArgumentsBuilder {
array := []cadence.Value{}
for _, val := range value {
array = append(array, CadenceString(val))
array = append(array, cadenceString(val))
}
a.Arguments = append(a.Arguments, cadence.NewArray(array))
return a
Expand All @@ -326,7 +326,7 @@ func (a *FlowArgumentsBuilder) StringMapArray(value ...map[string]string) *FlowA
for _, vals := range value {
dict := []cadence.KeyValuePair{}
for key, val := range vals {
dict = append(dict, cadence.KeyValuePair{Key: CadenceString(key), Value: CadenceString(val)})
dict = append(dict, cadence.KeyValuePair{Key: cadenceString(key), Value: cadenceString(val)})
}
array = append(array, cadence.NewDictionary(dict))
}
Expand All @@ -347,7 +347,7 @@ func (a *FlowArgumentsBuilder) ScalarMapArray(value ...map[string]string) *FlowA
a.Error = err
return a
}
dict = append(dict, cadence.KeyValuePair{Key: CadenceString(key), Value: UFix64Val})
dict = append(dict, cadence.KeyValuePair{Key: cadenceString(key), Value: UFix64Val})
}
array = append(array, cadence.NewDictionary(dict))
}
Expand Down
4 changes: 2 additions & 2 deletions argument_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,8 @@ transaction(names: [String]) {
})

arrayValues := []cadence.Value{
CadenceString("Bjarte"),
CadenceString("Karlsen"),
cadenceString("Bjarte"),
cadenceString("Karlsen"),
}
assert.Contains(t, builder.Arguments, cadence.NewArray(arrayValues))

Expand Down
2 changes: 2 additions & 0 deletions cadence.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ func CadenceValueToInterface(field cadence.Value) interface{} {
return value

case cadence.UFix64:
//fmt.Println("is ufix64 ", field.ToGoValue(), " ", field.String())

float, _ := strconv.ParseFloat(field.String(), 64)
return float
case cadence.Fix64:
Expand Down
10 changes: 5 additions & 5 deletions cadence_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ type Cadencetest struct {

func TestCadenceValueToInterface(t *testing.T) {

foo := CadenceString("foo")
bar := CadenceString("bar")
emptyString := CadenceString("")
foo := cadenceString("foo")
bar := cadenceString("bar")
emptyString := cadenceString("")

emptyStrct := cadence.Struct{
Fields: []cadence.Value{emptyString},
Expand Down Expand Up @@ -46,7 +46,7 @@ func TestCadenceValueToInterface(t *testing.T) {
}
dict := cadence.NewDictionary([]cadence.KeyValuePair{{Key: foo, Value: bar}})

emoji := CadenceString("😁")
emoji := cadenceString("😁")
emojiDict := cadence.NewDictionary([]cadence.KeyValuePair{{Key: emoji, Value: emoji}})

cadenceAddress1 := cadence.BytesToAddress(address1.Bytes())
Expand All @@ -58,7 +58,7 @@ func TestCadenceValueToInterface(t *testing.T) {
path := cadence.Path{Domain: "storage", Identifier: "foo"}

testCases := []Cadencetest{
{autogold.Want("EmptyString", nil), CadenceString("")},
{autogold.Want("EmptyString", nil), cadenceString("")},
{autogold.Want("nil", nil), nil},
{autogold.Want("None", nil), cadence.NewOptional(nil)},
{autogold.Want("Some(string)", "foo"), cadence.NewOptional(foo)},
Expand Down
112 changes: 61 additions & 51 deletions doc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,44 @@ package overflow_test

// importing overflow using "." will yield a cleaner DSL
import (
"fmt"
"time"

. "github.com/bjartek/overflow"
)

var docOptions = WithGlobalPrintOptions(WithoutId())

func Example() {

//in order to start overflow use the Overflow function
//it can be customized with lots of OverflowOption
o := Overflow(
StopOnError(),
PrintInteractionResults(),
)
fmt.Println(o)
//the result of the Overflow function is an OverflowState object
Overflow()
//Output:
//📜 deploy contracts NonFungibleToken, Debug
//🧑 Created account: emulator-first with address: 01cf0e2f2f715450 with flow: 10.00
//🧑 Created account: emulator-second with address: 179b6b1cb6755e31 with flow: 10.00
}

func ExampleOverflowState_Tx() {
o := Overflow(StopOnError(), PrintInteractionResults())
o := Overflow(docOptions)

// start the Tx DSL with the name of the transactions file, by default this
// is in the `transactions` folder in your root dit
o.Tx("arguments",
//Customize the Transaction by sending in more InteractionOptions,
//at minimum you need to set Signer and Args if any
SignProposeAndPayAs("first"),
WithSigner("first"),
//Arguments are always passed by name in the DSL builder, order does not matter
Arg("test", "overflow ftw!"),
WithArg("test", "overflow ftw!"),
)
//Output:
//📜 deploy contracts NonFungibleToken, Debug
//🧑 Created account: emulator-first with address: 01cf0e2f2f715450 with flow: 10.00
//🧑 Created account: emulator-second with address: 179b6b1cb6755e31 with flow: 10.00
//👌 Tx:arguments fee:0.00000244 gas:29
//
}

func ExampleOverflowState_Tx_inline() {
o := Overflow(StopOnError(), PrintInteractionResults())
o := Overflow(docOptions)

//The Tx dsl can also contain an inline transaction
o.Tx(`
Expand All @@ -45,30 +49,50 @@ func ExampleOverflowState_Tx_inline() {
Debug.log(message)
}
}`,
SignProposeAndPayAs("first"),
Arg("message", "overflow ftw!"),
WithSigner("first"),
WithArg("message", "overflow ftw!"),
)
//Output:
//📜 deploy contracts NonFungibleToken, Debug
//🧑 Created account: emulator-first with address: 01cf0e2f2f715450 with flow: 10.00
//🧑 Created account: emulator-second with address: 179b6b1cb6755e31 with flow: 10.00
//👌 Tx: fee:0.00000284 gas:37
//=== Events ===
//A.f8d6e0586b0a20c7.Debug.Log
// msg -> overflow ftw!
}

func ExampleOverflowState_Tx_multisign() {
o := Overflow(StopOnError(), PrintInteractionResults())
o := Overflow(docOptions)

//The Tx dsl can also contain an inline transaction
//The Tx dsl supports multiple signers, note that the mainSigner is the last account
o.Tx(`
import Debug from "../contracts/Debug.cdc"
transaction {
prepare(acct: AuthAccount, acct2: AuthAccount) {
//aact here is first
//acct2 here is second
Debug.log("acct:".concat(acct.address.toString()))
Debug.log("acct2:".concat(acct2.address.toString()))
}
}`,
SignProposeAndPayAs("first"),
PayloadSigner("second"),
WithSigner("first"),
WithPayloadSigner("second"),
)

//Output:
//📜 deploy contracts NonFungibleToken, Debug
//🧑 Created account: emulator-first with address: 01cf0e2f2f715450 with flow: 10.00
//🧑 Created account: emulator-second with address: 179b6b1cb6755e31 with flow: 10.00
//👌 Tx: fee:0.00000284 gas:37
//=== Events ===
//A.f8d6e0586b0a20c7.Debug.Log
// msg -> acct:0x179b6b1cb6755e31
//A.f8d6e0586b0a20c7.Debug.Log
// msg -> acct2:0x01cf0e2f2f715450
//
}

func ExampleOverflowState_Script() {
o := Overflow(StopOnError(), PrintInteractionResults())
o := Overflow(docOptions)

// the other major interaction you can run on Flow is a script, it uses the script DSL.
// Start it by specifying the script name from `scripts` folder
Expand All @@ -78,44 +102,30 @@ func ExampleOverflowState_Script() {
// `emulator-first` so it will insert that address as the argument.
// If you change the network to testnet/mainnet later and name your stakholders
// accordingly it will just work
Arg("account", "first"),
WithArg("account", "first"),
)
//Output:
//📜 deploy contracts NonFungibleToken, Debug
//🧑 Created account: emulator-first with address: 01cf0e2f2f715450 with flow: 10.00
//🧑 Created account: emulator-second with address: 179b6b1cb6755e31 with flow: 10.00
//⭐ Script test run result:"0x01cf0e2f2f715450"
}

func ExampleOverflowState_Script_inline() {
o := Overflow(StopOnError(), PrintInteractionResults())

o := Overflow(docOptions)

//Script can be run inline
o.Script(`
pub fun main(account: Address): String {
return getAccount(account).address.toString()
}`,
Arg("account", "first"),
WithArg("account", "first"),
WithName("get_address"),
)
}

func ExampleOverflowState_FetchEvents() {
o := Overflow(
StopOnError(),
PrintInteractionResults(),
// here you can send in more options to customize the way Overflow is started
)

for {
events, err := o.FetchEvents(
TrackProgressIn("minted_tokens"),
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
)
if err != nil {
panic(err)
}

if len(events) == 0 {
//here you can specify how long you will wait between polls
time.Sleep(10 * time.Second)
}

// do something with events, like sending them to discord/twitter or index in a database
fmt.Println(events)
}
//Output:
//📜 deploy contracts NonFungibleToken, Debug
//🧑 Created account: emulator-first with address: 01cf0e2f2f715450 with flow: 10.00
//🧑 Created account: emulator-second with address: 179b6b1cb6755e31 with flow: 10.00
//⭐ Script get_address run result:"0x01cf0e2f2f715450"
}
2 changes: 1 addition & 1 deletion event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (o OverflowEvent) ExistIn(events []OverflowEvent) bool {
}

//Parse raw flow events into a list of events and a fee event
func ParseEvents(events []flow.Event) (OverflowEvents, OverflowEvent) {
func parseEvents(events []flow.Event) (OverflowEvents, OverflowEvent) {
overflowEvents := OverflowEvents{}
fee := OverflowEvent{}
for _, event := range events {
Expand Down
8 changes: 4 additions & 4 deletions event_fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (o *OverflowState) FetchEvents(opts ...EventFetcherOption) ([]OverflowPastE

formatedEvents := []OverflowPastEvent{}
for _, blockEvent := range blockEvents {
events, _ := ParseEvents(blockEvent.Events)
events, _ := parseEvents(blockEvent.Events)
for name, eventList := range events {
for _, instance := range eventList {
formatedEvents = append(formatedEvents, OverflowPastEvent{
Expand Down Expand Up @@ -186,23 +186,23 @@ func WithLastBlocks(number uint64) EventFetcherOption {
}

// fetch events until theg given height alias to WithEndHeight
func UntilBlock(blockHeight uint64) EventFetcherOption {
func WithUntilBlock(blockHeight uint64) EventFetcherOption {
return func(e *EventFetcherBuilder) {
e.EndIndex = blockHeight
e.EndAtCurrentHeight = false
}
}

// set the end index to the current height
func UntilCurrentBlock() EventFetcherOption {
func WithUntilCurrentBlock() EventFetcherOption {
return func(e *EventFetcherBuilder) {
e.EndAtCurrentHeight = true
e.EndIndex = 0
}
}

// track what block we have read since last run in a file
func TrackProgressIn(fileName string) EventFetcherOption {
func WithTrackProgressIn(fileName string) EventFetcherOption {
return func(e *EventFetcherBuilder) {
e.ProgressFile = fileName
e.EndIndex = 0
Expand Down
10 changes: 5 additions & 5 deletions event_fetcher_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func startOverflowAndMintTokens(t *testing.T) *OverflowState {
t.Helper()
o := NewTestingEmulator().Start()
result := o.Tx("mint_tokens", SignProposeAndPayAsServiceAccount(), Arg("recipient", "first"), Arg("amount", 100.0))
result := o.Tx("mint_tokens", WithSignerServiceAccount(), WithArg("recipient", "first"), WithArg("amount", 100.0))
assert.NoError(t, result.Err)
return o

Expand Down Expand Up @@ -40,7 +40,7 @@ func TestIntegrationEventFetcher(t *testing.T) {

t.Run("Fetch last events and sort them ", func(t *testing.T) {
o := startOverflowAndMintTokens(t)
result := o.Tx("mint_tokens", SignProposeAndPayAsServiceAccount(), Arg("recipient", "first"), Arg("amount", "100.0"))
result := o.Tx("mint_tokens", WithSignerServiceAccount(), WithArg("recipient", "first"), WithArg("amount", "100.0"))
assert.NoError(t, result.Err)
ev, err := o.FetchEvents(
WithLastBlocks(3),
Expand All @@ -54,7 +54,7 @@ func TestIntegrationEventFetcher(t *testing.T) {
t.Run("Fetch last write progress file", func(t *testing.T) {
ev, err := startOverflowAndMintTokens(t).FetchEvents(
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
TrackProgressIn("progress"),
WithTrackProgressIn("progress"),
)
defer os.Remove("progress")
assert.NoError(t, err)
Expand All @@ -67,7 +67,7 @@ func TestIntegrationEventFetcher(t *testing.T) {

_, err = startOverflowAndMintTokens(t).FetchEvents(
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
TrackProgressIn("progress"),
WithTrackProgressIn("progress"),
)
defer os.Remove("progress")
assert.Error(t, err)
Expand All @@ -81,7 +81,7 @@ func TestIntegrationEventFetcher(t *testing.T) {

ev, err := startOverflowAndMintTokens(t).FetchEvents(
WithEvent("A.0ae53cb6e3f42a79.FlowToken.TokensMinted"),
TrackProgressIn("progress"),
WithTrackProgressIn("progress"),
)
defer os.Remove("progress")
assert.NoError(t, err)
Expand Down
2 changes: 0 additions & 2 deletions event_fetcher_old.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +285,7 @@ type FormatedEvent struct {

// Deprecated: Deprecated in favor of FetchEvent with builder
func (o FormatedEvent) ExistIn(events []*FormatedEvent) bool {
// litter.Dump(o)
for _, ev := range events {
//; litter.Dump(*ev)
result := reflect.DeepEqual(o, *ev)
if result {
return true
Expand Down
4 changes: 2 additions & 2 deletions event_fetcher_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func TestEventFetcher(t *testing.T) {
})

t.Run("Until argument", func(t *testing.T) {
ef := g.buildEventInteraction(UntilBlock(100))
ef := g.buildEventInteraction(WithUntilBlock(100))
assert.Equal(t, ef.EndIndex, uint64(100))
assert.False(t, ef.EndAtCurrentHeight)
})

t.Run("Until current argument", func(t *testing.T) {
ef := g.buildEventInteraction(UntilCurrentBlock())
ef := g.buildEventInteraction(WithUntilCurrentBlock())
assert.Equal(t, ef.EndIndex, uint64(0))
assert.True(t, ef.EndAtCurrentHeight)
})
Expand Down
Loading

0 comments on commit 24cea60

Please sign in to comment.