diff --git a/tests/data/bid/auction_bid_example_v1.json b/tests/data/bid/auction_bid_example_v1.json new file mode 100644 index 0000000..f4cc080 --- /dev/null +++ b/tests/data/bid/auction_bid_example_v1.json @@ -0,0 +1,14 @@ +{ + "bonding_purse": "uref-b3c03358245a0d9514064b6d0c3dd90023d29a0fe137507d430a26990f5ce8e3-007", + "staked_amount": "900000000000", + "delegation_rate": 10, + "delegators": [ + { + "public_key": "01d829cbfb66b2b11ef8d8feb6d3f2155789fc22f407bb57f89b05f6ba4b9ae070", + "staked_amount": "555000000000", + "bonding_purse": "uref-41059a3aa5c5759c7f5e3c826cb47e0b21257ac57ee63a4c00dc305133e55d64-007", + "delegatee": "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61" + } + ], + "inactive": false +} \ No newline at end of file diff --git a/tests/data/bid/auction_bid_example_v2.json b/tests/data/bid/auction_bid_example_v2.json new file mode 100644 index 0000000..e5b3949 --- /dev/null +++ b/tests/data/bid/auction_bid_example_v2.json @@ -0,0 +1,20 @@ +{ + "validator_public_key": "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61", + "bonding_purse": "uref-fafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafafa-007", + "staked_amount": "900000000000", + "delegation_rate": 0, + "vesting_schedule": null, + "delegators": [ + { + "delegator_public_key": "014508a07aa941707f3eb2db94c8897a80b2c1197476b6de213ac273df7d86c4ff", + "delegator": { + "delegator_public_key": "01d829cbfb66b2b11ef8d8feb6d3f2155789fc22f407bb57f89b05f6ba4b9ae070", + "staked_amount": "10", + "bonding_purse": "uref-fbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfbfb-007", + "validator_public_key": "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61", + "vesting_schedule": null + } + } + ], + "inactive": false +} \ No newline at end of file diff --git a/tests/types/bid_test.go b/tests/types/bid_test.go new file mode 100644 index 0000000..d46e6b9 --- /dev/null +++ b/tests/types/bid_test.go @@ -0,0 +1,42 @@ +package types + +import ( + "encoding/json" + "os" + "testing" + + "github.com/stretchr/testify/require" + + "github.com/make-software/casper-go-sdk/v2/types" +) + +func Test_Bid_MarshalUnmarshal(t *testing.T) { + tests := []struct { + name string + fixturePath string + }{ + { + "Auction Bid V1", + "../data/bid/auction_bid_example_v1.json", + }, + { + "Auction Bid V2", + "../data/bid/auction_bid_example_v2.json", + }, + } + for _, test := range tests { + t.Run(test.name, func(t *testing.T) { + data, err := os.ReadFile(test.fixturePath) + require.NoError(t, err) + + var bid types.Bid + err = json.Unmarshal(data, &bid) + require.NoError(t, err) + + require.Equal(t, bid.StakedAmount.String(), "900000000000") + require.Equal(t, 1, len(bid.Delegators)) + require.Equal(t, bid.Delegators[0].DelegatorPublicKey.ToHex(), "01d829cbfb66b2b11ef8d8feb6d3f2155789fc22f407bb57f89b05f6ba4b9ae070") + require.Equal(t, bid.Delegators[0].ValidatorPublicKey.ToHex(), "01197f6b23e16c8532c6abc838facd5ea789be0c76b2920334039bfa8b3d368d61") + }) + } +} diff --git a/types/bid.go b/types/bid.go index 49effd6..2987a53 100644 --- a/types/bid.go +++ b/types/bid.go @@ -54,11 +54,12 @@ func (d *Delegators) UnmarshalJSON(data []byte) error { } publicKeyAndDelegators := make([]struct { - DelegatorPublicKey keypair.PublicKey `json:"delegator_public_key"` - Delegator Delegator `json:"delegator"` + DelegatorPublicKey *keypair.PublicKey `json:"delegator_public_key"` + Delegator Delegator `json:"delegator"` }, 0) - if err := json.Unmarshal(data, &publicKeyAndDelegators); err == nil && len(publicKeyAndDelegators) > 0 { + err := json.Unmarshal(data, &publicKeyAndDelegators) + if err == nil && len(publicKeyAndDelegators) > 0 && publicKeyAndDelegators[0].DelegatorPublicKey != nil { delegators := make(Delegators, 0, len(publicKeyAndDelegators)) for _, item := range publicKeyAndDelegators { delegators = append(delegators, item.Delegator) @@ -68,11 +69,16 @@ func (d *Delegators) UnmarshalJSON(data []byte) error { return nil } - delegators := make([]Delegator, 0) - if err := json.Unmarshal(data, &publicKeyAndDelegators); err != nil { + delegatorsV1 := make([]DelegatorV1, 0) + if err := json.Unmarshal(data, &delegatorsV1); err != nil { return err } + delegators := make(Delegators, 0, len(delegatorsV1)) + for _, item := range delegatorsV1 { + delegators = append(delegators, NewDelegatorFromDelegatorV1(item)) + } + *d = delegators return nil } @@ -91,6 +97,30 @@ type Delegator struct { VestingSchedule *VestingSchedule `json:"vesting_schedule"` } +// DelegatorV1 of version 1 which is associated with the given validator. +type DelegatorV1 struct { + // The purse that was used for delegating. + BondingPurse key.URef `json:"bonding_purse"` + // Amount of Casper token (in motes) delegated + StakedAmount clvalue.UInt512 `json:"staked_amount"` + // Public Key of the validator + Delegatee keypair.PublicKey `json:"delegatee"` + // Public key of the delegator + PublicKey keypair.PublicKey `json:"public_key"` + // Vesting schedule for a genesis validator. `None` if non-genesis validator. + VestingSchedule *VestingSchedule `json:"vesting_schedule"` +} + +func NewDelegatorFromDelegatorV1(v1 DelegatorV1) Delegator { + return Delegator{ + BondingPurse: v1.BondingPurse, + StakedAmount: v1.StakedAmount, + DelegatorPublicKey: v1.PublicKey, + ValidatorPublicKey: v1.Delegatee, + VestingSchedule: v1.VestingSchedule, + } +} + // Credit is a bridge record pointing to a new `ValidatorBid` after the public key was changed. type Credit struct { // The era id the credit was created.