-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract receipts writing into own package (#29)
- Loading branch information
Showing
3 changed files
with
71 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package receipts | ||
|
||
import ( | ||
"bytes" | ||
"encoding/csv" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/shopspring/decimal" | ||
"storj.io/crypto-batch-payment/pkg/payer" | ||
) | ||
|
||
type Buffer struct { | ||
buf bytes.Buffer | ||
csv *csv.Writer | ||
} | ||
|
||
func (b *Buffer) Emit(wallet common.Address, amount decimal.Decimal, txHash string, mechanism payer.Type) { | ||
b.init() | ||
b.write(wallet.String(), amount.String(), txHash, mechanism.String()) | ||
} | ||
|
||
func (b *Buffer) Finalize() []byte { | ||
b.csv.Flush() | ||
return b.buf.Bytes() | ||
} | ||
|
||
func (b *Buffer) init() { | ||
if b.csv == nil { | ||
b.csv = csv.NewWriter(&b.buf) | ||
b.write("wallet", "amount", "txhash", "mechanism") | ||
} | ||
} | ||
|
||
func (b *Buffer) write(c1, c2, c3, c4 string) { | ||
_ = b.csv.Write([]string{c1, c2, c3, c4}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package receipts_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
"github.com/ethereum/go-ethereum/common" | ||
"github.com/shopspring/decimal" | ||
"github.com/stretchr/testify/require" | ||
"storj.io/crypto-batch-payment/pkg/payer" | ||
"storj.io/crypto-batch-payment/pkg/receipts" | ||
) | ||
|
||
func TestBuffer(t *testing.T) { | ||
address1 := common.BytesToAddress(bytes.Repeat([]byte{1}, common.AddressLength)) | ||
address2 := common.BytesToAddress(bytes.Repeat([]byte{2}, common.AddressLength)) | ||
address3 := common.BytesToAddress(bytes.Repeat([]byte{3}, common.AddressLength)) | ||
|
||
var b receipts.Buffer | ||
b.Emit(address1, decimal.NewFromInt(1), "hash1", payer.Eth) | ||
b.Emit(address2, decimal.NewFromInt(2), "hash2", payer.ZkSync) | ||
b.Emit(address3, decimal.NewFromInt(3), "hash3", payer.ZkSyncEra) | ||
receipts := b.Finalize() | ||
require.Equal(t, `wallet,amount,txhash,mechanism | ||
0x0101010101010101010101010101010101010101,1,hash1,eth | ||
0x0202020202020202020202020202020202020202,2,hash2,zksync | ||
0x0303030303030303030303030303030303030303,3,hash3,zksync-era | ||
`, string(receipts)) | ||
} |