Exemple #1
0
// Parse parses the provided as a stellar "amount", i.e. A 64-bit signed integer
// that represents a decimal number with 7 digits of significance in the
// fractional portion of the number.
func Parse(v string) (xdr.Int64, error) {
	var f, o, r big.Rat

	_, ok := f.SetString(v)
	if !ok {
		return xdr.Int64(0), fmt.Errorf("cannot parse amount: %s", v)
	}

	o.SetInt64(One)
	r.Mul(&f, &o)

	is := r.FloatString(0)
	i, err := strconv.ParseInt(is, 10, 64)
	if err != nil {
		return xdr.Int64(0), err
	}
	return xdr.Int64(i), nil
}
			})

			It("failed", func() {
				Expect(subject.Err).To(HaveOccurred())
			})
		})
	})

	Describe("Limit", func() {
		Context("sets limit properly", func() {
			BeforeEach(func() {
				mut = Limit("20")
			})

			It("sets limit value properly", func() {
				Expect(subject.CT.Limit).To(Equal(xdr.Int64(200000000)))
			})
		})

		Context("sets max limit properly", func() {
			BeforeEach(func() {
				mut = MaxLimit
			})

			It("sets limit value properly", func() {
				Expect(subject.CT.Limit).To(Equal(xdr.Int64(9223372036854775807)))
			})
		})
	})
})
		Context("using a valid stellar address", func() {
			BeforeEach(func() { mut = SourceAccount{address} })

			It("succeeds", func() {
				Expect(subject.Err).NotTo(HaveOccurred())
			})

			It("sets the destination to the correct xdr.AccountId", func() {
				var aid xdr.AccountId
				aid.SetAddress(address)
				Expect(subject.O.SourceAccount.MustEd25519()).To(Equal(aid.MustEd25519()))
			})
		})

		Context("using an invalid value", func() {
			BeforeEach(func() { mut = SourceAccount{bad} })
			It("failed", func() { Expect(subject.Err).To(HaveOccurred()) })
		})
	})

	Describe("NativeAmount", func() {
		BeforeEach(func() { mut = NativeAmount{"101"} })
		It("sets the starting balance properly", func() {
			Expect(subject.CA.StartingBalance).To(Equal(xdr.Int64(1010000000)))
		})
		It("succeeds", func() {
			Expect(subject.Err).NotTo(HaveOccurred())
		})
	})
})
Exemple #4
0
			Expect(found).ToNot(BeNil())
			Expect(found.Data.Type).To(Equal(xdr.LedgerEntryTypeAccount))

			account := found.Data.MustAccount().AccountId
			Expect(account.Equals(masterAccount)).To(BeTrue())
		})
	})

	Describe("StateAfter", func() {
		It("returns newly created entries correctly", func() {
			state, err := createAccount.StateAfter(newAccount.LedgerKey(), 0)
			Expect(err).ToNot(HaveOccurred())
			Expect(state).ToNot(BeNil())

			account := state.Data.MustAccount()
			Expect(account.Balance).To(Equal(xdr.Int64(1000000000)))
		})
	})

	Describe("StateBefore", func() {
		Context("Accounts", func() {
			It("return nil when the account was created in the operation", func() {
				state, err := createAccount.StateBefore(newAccount.LedgerKey(), 0)
				Expect(err).ToNot(HaveOccurred())
				Expect(state).To(BeNil())
			})

			It("passes a sanity test", func() {
				before, err := createAccount.StateBefore(masterAccount.LedgerKey(), 0)
				Expect(err).ToNot(HaveOccurred())
				Expect(before).ToNot(BeNil())
				Buying:  NativeAsset(),
				Price:   Price("41.265"),
			}
		)

		JustBeforeEach(func() {
			subject = ManageOfferBuilder{}
			subject.Mutate(mut)
		})

		Describe("CreateOffer", func() {
			Context("creates offer properly", func() {
				It("sets values properly", func() {
					builder := CreateOffer(rate, "20")

					Expect(builder.MO.Amount).To(Equal(xdr.Int64(200000000)))

					Expect(builder.MO.Selling.Type).To(Equal(xdr.AssetTypeAssetTypeCreditAlphanum4))
					Expect(builder.MO.Selling.AlphaNum4.AssetCode).To(Equal([4]byte{'E', 'U', 'R', 0}))
					var aid xdr.AccountId
					aid.SetAddress(rate.Selling.Issuer)
					Expect(builder.MO.Selling.AlphaNum4.Issuer.MustEd25519()).To(Equal(aid.MustEd25519()))
					Expect(builder.MO.Selling.AlphaNum12).To(BeNil())

					Expect(builder.MO.Buying.Type).To(Equal(xdr.AssetTypeAssetTypeNative))
					Expect(builder.MO.Buying.AlphaNum4).To(BeNil())
					Expect(builder.MO.Buying.AlphaNum12).To(BeNil())

					Expect(builder.MO.Price.N).To(Equal(xdr.Int32(8253)))
					Expect(builder.MO.Price.D).To(Equal(xdr.Int32(200)))
		bad     = "foo"
	)

	Describe("Payment", func() {
		JustBeforeEach(func() {
			subject = PaymentBuilder{}
			subject.Mutate(mut)
		})

		Describe("CreditAmount", func() {
			Context("AlphaNum4", func() {
				BeforeEach(func() {
					mut = CreditAmount{"USD", address, "50.0"}
				})
				It("sets the asset properly", func() {
					Expect(subject.P.Amount).To(Equal(xdr.Int64(500000000)))
					Expect(subject.P.Asset.Type).To(Equal(xdr.AssetTypeAssetTypeCreditAlphanum4))
					Expect(subject.P.Asset.AlphaNum4.AssetCode).To(Equal([4]byte{'U', 'S', 'D', 0}))
					var aid xdr.AccountId
					aid.SetAddress(address)
					Expect(subject.P.Asset.AlphaNum4.Issuer.MustEd25519()).To(Equal(aid.MustEd25519()))
					Expect(subject.P.Asset.AlphaNum12).To(BeNil())
				})
				It("succeeds", func() {
					Expect(subject.Err).NotTo(HaveOccurred())
				})
			})

			Context("AlphaNum12", func() {
				BeforeEach(func() {
					mut = CreditAmount{"ABCDEF", address, "50.0"}