service backend
All checks were successful
ci/woodpecker/push/db Pipeline was successful
ci/woodpecker/push/nats Pipeline was successful

This commit is contained in:
Stephan D
2025-11-07 18:35:26 +01:00
parent 20e8f9acc4
commit 62a6631b9a
537 changed files with 48453 additions and 0 deletions

2
api/fx/storage/.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
internal/generated
.gocache

32
api/fx/storage/go.mod Normal file
View File

@@ -0,0 +1,32 @@
module github.com/tech/sendico/fx/storage
go 1.25.3
replace github.com/tech/sendico/pkg => ../../pkg
require (
github.com/tech/sendico/pkg v0.1.0
go.mongodb.org/mongo-driver v1.17.6
go.uber.org/zap v1.27.0
)
require (
github.com/bmatcuk/doublestar/v4 v4.9.1 // indirect
github.com/casbin/casbin/v2 v2.128.0 // indirect
github.com/casbin/govaluate v1.10.0 // indirect
github.com/casbin/mongodb-adapter/v3 v3.7.0 // indirect
github.com/golang/snappy v1.0.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/klauspost/compress v1.18.1 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/montanaflynn/stats v0.7.1 // indirect
github.com/xdg-go/pbkdf2 v1.0.0 // indirect
github.com/xdg-go/scram v1.1.2 // indirect
github.com/xdg-go/stringprep v1.0.4 // indirect
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.43.0 // indirect
golang.org/x/sync v0.17.0 // indirect
golang.org/x/text v0.30.0 // indirect
google.golang.org/protobuf v1.36.10 // indirect
)

177
api/fx/storage/go.sum Normal file
View File

@@ -0,0 +1,177 @@
dario.cat/mergo v1.0.1 h1:Ra4+bf83h2ztPIQYNP99R6m+Y7KfnARDfID+a+vLl4s=
dario.cat/mergo v1.0.1/go.mod h1:uNxQE+84aUszobStD9th8a29P2fMDhsBdgRYvZOxGmk=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161 h1:L/gRVlceqvL25UVaW/CKtUDjefjrs0SPonmDGUVOYP0=
github.com/Azure/go-ansiterm v0.0.0-20230124172434-306776ec8161/go.mod h1:xomTg63KZ2rFqZQzSB4Vz2SUXa1BpHTVz9L5PTmPC4E=
github.com/Microsoft/go-winio v0.6.2 h1:F2VQgta7ecxGYO8k3ZZz3RS8fVIXVxONVUPlNERoyfY=
github.com/Microsoft/go-winio v0.6.2/go.mod h1:yd8OoFMLzJbo9gZq8j5qaps8bJ9aShtEA8Ipt1oGCvU=
github.com/bmatcuk/doublestar/v4 v4.6.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/bmatcuk/doublestar/v4 v4.9.1 h1:X8jg9rRZmJd4yRy7ZeNDRnM+T3ZfHv15JiBJ/avrEXE=
github.com/bmatcuk/doublestar/v4 v4.9.1/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/casbin/casbin/v2 v2.128.0 h1:761dLmXLy/ZNSckAITvpUZ8VdrxARyIlwmdafHzRb7Y=
github.com/casbin/casbin/v2 v2.128.0/go.mod h1:iAwqzcYzJtAK5QWGT2uRl9WfRxXyKFBG1AZuhk2NAQg=
github.com/casbin/govaluate v1.3.0/go.mod h1:G/UnbIjZk/0uMNaLwZZmFQrR72tYRZWQkO70si/iR7A=
github.com/casbin/govaluate v1.10.0 h1:ffGw51/hYH3w3rZcxO/KcaUIDOLP84w7nsidMVgaDG0=
github.com/casbin/govaluate v1.10.0/go.mod h1:G/UnbIjZk/0uMNaLwZZmFQrR72tYRZWQkO70si/iR7A=
github.com/casbin/mongodb-adapter/v3 v3.7.0 h1:w9c3bea1BGK4eZTAmk17JkY52yv/xSZDSHKji8q+z6E=
github.com/casbin/mongodb-adapter/v3 v3.7.0/go.mod h1:F1mu4ojoJVE/8VhIMxMedhjfwRDdIXgANYs6Sd0MgVA=
github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8=
github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE=
github.com/containerd/log v0.1.0 h1:TCJt7ioM2cr/tfR8GPbGf9/VRAX8D2B4PjzCpfX540I=
github.com/containerd/log v0.1.0/go.mod h1:VRRf09a7mHDIRezVKTRCrOq78v577GXq3bSa3EhrzVo=
github.com/containerd/platforms v0.2.1 h1:zvwtM3rz2YHPQsF2CHYM8+KtB5dvhISiXh5ZpSBQv6A=
github.com/containerd/platforms v0.2.1/go.mod h1:XHCb+2/hzowdiut9rkudds9bE5yJ7npe7dG/wG+uFPw=
github.com/cpuguy83/dockercfg v0.3.2 h1:DlJTyZGBDlXqUZ2Dk2Q3xHs/FtnooJJVaad2S9GKorA=
github.com/cpuguy83/dockercfg v0.3.2/go.mod h1:sugsbF4//dDlL/i+S+rtpIWp+5h0BHJHfjj5/jFyUJc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/distribution/reference v0.6.0 h1:0IXCQ5g4/QMHHkarYzh5l+u8T3t73zM5QvfrDyIgxBk=
github.com/distribution/reference v0.6.0/go.mod h1:BbU0aIcezP1/5jX/8MP0YiH4SdvB5Y4f/wlDRiLyi3E=
github.com/docker/docker v27.3.1+incompatible h1:KttF0XoteNTicmUtBO0L2tP+J7FGRFTjaEF4k6WdhfI=
github.com/docker/docker v27.3.1+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/go-connections v0.5.0 h1:USnMq7hx7gwdVZq1L49hLXaFtUdTADjXGp+uj1Br63c=
github.com/docker/go-connections v0.5.0/go.mod h1:ov60Kzw0kKElRwhNs9UlUHAE/F9Fe6GLaXnqyDdmEXc=
github.com/docker/go-units v0.5.0 h1:69rxXcBk27SvSaaxTtLh/8llcHD8vYHT7WSdRZ/jvr4=
github.com/docker/go-units v0.5.0/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/felixge/httpsnoop v1.0.4 h1:NFTV2Zj1bL4mc9sqWACXbQFVBBg2W3GPvqp8/ESS2Wg=
github.com/felixge/httpsnoop v1.0.4/go.mod h1:m8KPJKqk1gH5J9DgRY2ASl2lWCfGKXixSwevea8zH2U=
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-ole/go-ole v1.3.0 h1:Dt6ye7+vXGIKZ7Xtk4s6/xVdGDQynvom7xCFEdWr6uE=
github.com/go-ole/go-ole v1.3.0/go.mod h1:5LS6F96DhAwUc7C+1HLexzMXY1xGRSryjyPPKW6zv78=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/snappy v1.0.0 h1:Oy607GVXHs7RtbggtPBnr2RmDArIsAefDwvrdWvRhGs=
github.com/golang/snappy v1.0.0/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co=
github.com/klauspost/compress v1.18.1/go.mod h1:ZQFFVG+MdnR0P+l6wpXgIL4NTtwiKIdBnrBd8Nrxr+0=
github.com/lufia/plan9stats v0.0.0-20250827001030-24949be3fa54 h1:mFWunSatvkQQDhpdyuFAYwyAan3hzCuma+Pz8sqvOfg=
github.com/lufia/plan9stats v0.0.0-20250827001030-24949be3fa54/go.mod h1:autxFIvghDt3jPTLoqZ9OZ7s9qTGNAWmYCjVFWPX/zg=
github.com/magiconair/properties v1.8.7 h1:IeQXZAiQcpL9mgcAe1Nu6cX9LLw6ExEHKjN0VQdvPDY=
github.com/magiconair/properties v1.8.7/go.mod h1:Dhd985XPs7jluiymwWYZ0G4Z61jb3vdS329zhj2hYo0=
github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE=
github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo=
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
github.com/moby/patternmatcher v0.6.0 h1:GmP9lR19aU5GqSSFko+5pRqHi+Ohk1O69aFiKkVGiPk=
github.com/moby/patternmatcher v0.6.0/go.mod h1:hDPoyOpDY7OrrMDLaYoY3hf52gNCR/YOUYxkhApJIxc=
github.com/moby/sys/sequential v0.6.0 h1:qrx7XFUd/5DxtqcoH1h438hF5TmOvzC/lspjy7zgvCU=
github.com/moby/sys/sequential v0.6.0/go.mod h1:uyv8EUTrca5PnDsdMGXhZe6CCe8U/UiTWd+lL+7b/Ko=
github.com/moby/sys/user v0.3.0 h1:9ni5DlcW5an3SvRSx4MouotOygvzaXbaSrc/wGDFWPo=
github.com/moby/sys/user v0.3.0/go.mod h1:bG+tYYYJgaMtRKgEmuueC0hJEAZWwtIbZTB+85uoHjs=
github.com/moby/sys/userns v0.1.0 h1:tVLXkFOxVu9A64/yh59slHVv9ahO9UIev4JZusOLG/g=
github.com/moby/sys/userns v0.1.0/go.mod h1:IHUYgu/kao6N8YZlp9Cf444ySSvCmDlmzUcYfDHOl28=
github.com/moby/term v0.5.0 h1:xt8Q1nalod/v7BqbG21f8mQPqH+xAaC9C3N3wfWbVP0=
github.com/moby/term v0.5.0/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y=
github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE=
github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow=
github.com/morikuni/aec v1.0.0 h1:nP9CBfwrvYnBRgY6qfDQkygYDmYwOilePFkwzv4dU8A=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8Oi/yOhh5U=
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55 h1:o4JXh1EVt9k/+g42oCprj/FisM4qX9L3sZB3upGN2ZU=
github.com/power-devops/perfstat v0.0.0-20240221224432-82ca36839d55/go.mod h1:OmDBASR4679mdNQnz2pUhc2G8CO2JrUAVFDRBDP/hJE=
github.com/shirou/gopsutil/v3 v3.24.5 h1:i0t8kL+kQTvpAYToeuiVk3TgDeKOFioZO3Ztz/iZ9pI=
github.com/shirou/gopsutil/v3 v3.24.5/go.mod h1:bsoOS1aStSs9ErQ1WWfxllSeS1K5D+U30r2NfcubMVk=
github.com/shoenig/go-m1cpu v0.1.6 h1:nxdKQNcEB6vzgA2E2bvzKIYRuNj7XNJ4S/aRSwKzFtM=
github.com/shoenig/go-m1cpu v0.1.6/go.mod h1:1JJMcUBvfNwpq05QDQVAnx3gUHr9IYF7GNg9SUEw2VQ=
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
github.com/sirupsen/logrus v1.9.3/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U=
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
github.com/testcontainers/testcontainers-go v0.33.0 h1:zJS9PfXYT5O0ZFXM2xxXfk4J5UMw/kRiISng037Gxdw=
github.com/testcontainers/testcontainers-go v0.33.0/go.mod h1:W80YpTa8D5C3Yy16icheD01UTDu+LmXIA2Keo+jWtT8=
github.com/testcontainers/testcontainers-go/modules/mongodb v0.33.0 h1:iXVA84s5hKMS5gn01GWOYHE3ymy/2b+0YkpFeTxB2XY=
github.com/testcontainers/testcontainers-go/modules/mongodb v0.33.0/go.mod h1:R6tMjTojRiaoo89fh/hf7tOmfzohdqSU17R9DwSVSog=
github.com/tklauser/go-sysconf v0.3.15 h1:VE89k0criAymJ/Os65CSn1IXaol+1wrsFHEB8Ol49K4=
github.com/tklauser/go-sysconf v0.3.15/go.mod h1:Dmjwr6tYFIseJw7a3dRLJfsHAMXZ3nEnL/aZY+0IuI4=
github.com/tklauser/numcpus v0.10.0 h1:18njr6LDBk1zuna922MgdjQuJFjrdppsZG60sHGfjso=
github.com/tklauser/numcpus v0.10.0/go.mod h1:BiTKazU708GQTYF4mB+cmlpT2Is1gLk7XVuEeem8LsQ=
github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c=
github.com/xdg-go/pbkdf2 v1.0.0/go.mod h1:jrpuAogTd400dnrH08LKmI/xc1MbPOebTwRqcT5RDeI=
github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY=
github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4=
github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8=
github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=
github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
github.com/yusufpapurcu/wmi v1.2.4 h1:zFUKzehAFReQwLys1b/iSMl+JQGSCSjtVqQn9bBrPo0=
github.com/yusufpapurcu/wmi v1.2.4/go.mod h1:SBZ9tNy3G9/m5Oi98Zks0QjeHVDvuK0qfxQmPyzfmi0=
go.mongodb.org/mongo-driver v1.17.4 h1:jUorfmVzljjr0FLzYQsGP8cgN/qzzxlY9Vh0C9KFXVw=
go.mongodb.org/mongo-driver v1.17.4/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
go.mongodb.org/mongo-driver v1.17.6 h1:87JUG1wZfWsr6rIz3ZmpH90rL5tea7O3IHuSwHUpsss=
go.mongodb.org/mongo-driver v1.17.6/go.mod h1:Hy04i7O2kC4RS06ZrhPRqj/u4DTYkFDAAccj+rVKqgQ=
go.opentelemetry.io/auto/sdk v1.1.0 h1:cH53jehLUN6UFLY71z+NDOiNJqDdPRaXzTel0sJySYA=
go.opentelemetry.io/auto/sdk v1.1.0/go.mod h1:3wSPjt5PWp2RhlCcmmOial7AvC4DQqZb7a7wCow3W8A=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0 h1:UP6IpuHFkUgOQL9FFQFrZ+5LiwhhYRbi7VZSIx6Nj5s=
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.56.0/go.mod h1:qxuZLtbq5QDtdeSHsS7bcf6EH6uO6jUAgk764zd3rhM=
go.opentelemetry.io/otel v1.37.0 h1:9zhNfelUvx0KBfu/gb+ZgeAfAgtWrfHJZcAqFC228wQ=
go.opentelemetry.io/otel v1.37.0/go.mod h1:ehE/umFRLnuLa/vSccNq9oS1ErUlkkK71gMcN34UG8I=
go.opentelemetry.io/otel/metric v1.37.0 h1:mvwbQS5m0tbmqML4NqK+e3aDiO02vsf/WgbsdpcPoZE=
go.opentelemetry.io/otel/metric v1.37.0/go.mod h1:04wGrZurHYKOc+RKeye86GwKiTb9FKm1WHtO+4EVr2E=
go.opentelemetry.io/otel/trace v1.37.0 h1:HLdcFNbRQBE2imdSEgm/kwqmQj1Or1l/7bW6mxVK7z4=
go.opentelemetry.io/otel/trace v1.37.0/go.mod h1:TlgrlQ+PtQO5XFerSPUYG0JSgGyryXewPGyayAWSBS0=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/crypto v0.43.0 h1:dduJYIi3A3KOfdGOHX8AVZ/jGiyPa3IbBozJ5kNuE04=
golang.org/x/crypto v0.43.0/go.mod h1:BFbav4mRNlXJL4wNeejLpWxB7wMbc79PdRGhWKncxR0=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.17.0 h1:l60nONMj9l5drqw6jlhIELNv9I0A4OFgRsG9k2oT9Ug=
golang.org/x/sync v0.17.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.37.0 h1:fdNQudmxPjkdUTPnLn5mdQv7Zwvbvpaxqs831goi9kQ=
golang.org/x/sys v0.37.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ=
golang.org/x/text v0.30.0 h1:yznKA/E9zq54KzlzBEAWn1NXSQ8DIp/NYMy88xJjl4k=
golang.org/x/text v0.30.0/go.mod h1:yDdHFIX9t+tORqspjENWgzaCVXgk0yYnYuSZ8UzzBVM=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190425150028-36563e24a262/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@@ -0,0 +1,18 @@
package model
// CrossRateConfig describes how to synthetically derive a currency pair using
// two other pairs connected by a pivot currency.
type CrossRateConfig struct {
Enabled bool `bson:"enabled" json:"enabled"`
PivotCurrency string `bson:"pivotCurrency,omitempty" json:"pivotCurrency,omitempty"`
BaseLeg CrossRateLeg `bson:"baseLeg" json:"baseLeg"`
QuoteLeg CrossRateLeg `bson:"quoteLeg" json:"quoteLeg"`
}
// CrossRateLeg identifies a supporting currency pair and optional overrides to
// fetch or orient its pricing data for cross-rate calculations.
type CrossRateLeg struct {
Pair CurrencyPair `bson:"pair" json:"pair"`
Invert bool `bson:"invert,omitempty" json:"invert,omitempty"`
Provider string `bson:"provider,omitempty" json:"provider,omitempty"`
}

View File

@@ -0,0 +1,27 @@
package model
import "github.com/tech/sendico/pkg/db/storable"
// Currency captures rounding metadata for a given currency code.
type Currency struct {
storable.Base `bson:",inline" json:",inline"`
Code string `bson:"code" json:"code"`
Decimals uint32 `bson:"decimals" json:"decimals"`
Rounding RoundingMode `bson:"rounding" json:"rounding"`
DisplayName string `bson:"displayName,omitempty" json:"displayName,omitempty"`
Symbol string `bson:"symbol,omitempty" json:"symbol,omitempty"`
MinUnit string `bson:"minUnit,omitempty" json:"minUnit,omitempty"`
}
// Collection implements storable.Storable.
func (*Currency) Collection() string {
return CurrenciesCollection
}
// CurrencySettings embeds precision details inside a Pair document.
type CurrencySettings struct {
Code string `bson:"code" json:"code"`
Decimals uint32 `bson:"decimals" json:"decimals"`
Rounding RoundingMode `bson:"rounding" json:"rounding"`
}

View File

@@ -0,0 +1,26 @@
package model
import "github.com/tech/sendico/pkg/db/storable"
// Pair describes a supported FX currency pair and related metadata.
type Pair struct {
storable.Base `bson:",inline" json:",inline"`
Pair CurrencyPair `bson:"pair" json:"pair"`
BaseMeta CurrencySettings `bson:"baseMeta" json:"baseMeta"`
QuoteMeta CurrencySettings `bson:"quoteMeta" json:"quoteMeta"`
Providers []string `bson:"providers,omitempty" json:"providers,omitempty"`
IsEnabled bool `bson:"isEnabled" json:"isEnabled"`
TenantRef string `bson:"tenantRef,omitempty" json:"tenantRef,omitempty"`
DefaultProvider string `bson:"defaultProvider,omitempty" json:"defaultProvider,omitempty"`
Attributes map[string]any `bson:"attributes,omitempty" json:"attributes,omitempty"`
SupportedSides []QuoteSide `bson:"supportedSides,omitempty" json:"supportedSides,omitempty"`
FallbackProviders []string `bson:"fallbackProviders,omitempty" json:"fallbackProviders,omitempty"`
Tags []string `bson:"tags,omitempty" json:"tags,omitempty"`
Cross *CrossRateConfig `bson:"cross,omitempty" json:"cross,omitempty"`
}
// Collection implements storable.Storable.
func (*Pair) Collection() string {
return PairsCollection
}

View File

@@ -0,0 +1,63 @@
package model
import (
"time"
"github.com/tech/sendico/pkg/db/storable"
)
// Quote represents a firm or indicative quote persisted by the oracle.
type Quote struct {
storable.Base `bson:",inline" json:",inline"`
QuoteRef string `bson:"quoteRef" json:"quoteRef"`
Firm bool `bson:"firm" json:"firm"`
Status QuoteStatus `bson:"status" json:"status"`
Pair CurrencyPair `bson:"pair" json:"pair"`
Side QuoteSide `bson:"side" json:"side"`
Price string `bson:"price" json:"price"`
BaseAmount Money `bson:"baseAmount" json:"baseAmount"`
QuoteAmount Money `bson:"quoteAmount" json:"quoteAmount"`
AmountType QuoteAmountType `bson:"amountType" json:"amountType"`
ExpiresAtUnixMs int64 `bson:"expiresAtUnixMs" json:"expiresAtUnixMs"`
ExpiresAt *time.Time `bson:"expiresAt,omitempty" json:"expiresAt,omitempty"`
RateRef string `bson:"rateRef" json:"rateRef"`
Provider string `bson:"provider" json:"provider"`
PreferredProvider string `bson:"preferredProvider,omitempty" json:"preferredProvider,omitempty"`
RequestedTTLMs int64 `bson:"requestedTtlMs,omitempty" json:"requestedTtlMs,omitempty"`
MaxAgeToleranceMs int64 `bson:"maxAgeToleranceMs,omitempty" json:"maxAgeToleranceMs,omitempty"`
ConsumedByLedgerTxnRef string `bson:"consumedByLedgerTxnRef,omitempty" json:"consumedByLedgerTxnRef,omitempty"`
ConsumedAtUnixMs *int64 `bson:"consumedAtUnixMs,omitempty" json:"consumedAtUnixMs,omitempty"`
Meta *QuoteMeta `bson:"meta,omitempty" json:"meta,omitempty"`
}
// Collection implements storable.Storable.
func (*Quote) Collection() string {
return QuotesCollection
}
// MarkConsumed switches the quote to consumed status and links it to a ledger transaction.
func (q *Quote) MarkConsumed(ledgerTxnRef string, consumedAt time.Time) {
if ledgerTxnRef == "" {
return
}
q.Status = QuoteStatusConsumed
q.ConsumedByLedgerTxnRef = ledgerTxnRef
ts := consumedAt.UnixMilli()
q.ConsumedAtUnixMs = &ts
q.Base.Update()
}
// MarkExpired marks the quote as expired.
func (q *Quote) MarkExpired() {
q.Status = QuoteStatusExpired
q.Base.Update()
}
// IsExpired reports whether the quote has passed its expiration instant.
func (q *Quote) IsExpired(now time.Time) bool {
if q.ExpiresAtUnixMs == 0 {
return false
}
return now.UnixMilli() >= q.ExpiresAtUnixMs
}

View File

@@ -0,0 +1,34 @@
package model
import (
"time"
"github.com/tech/sendico/pkg/db/storable"
)
// RateSnapshot stores a normalized FX rate observation.
type RateSnapshot struct {
storable.Base `bson:",inline" json:",inline"`
RateRef string `bson:"rateRef" json:"rateRef"`
Pair CurrencyPair `bson:"pair" json:"pair"`
Provider string `bson:"provider" json:"provider"`
Mid string `bson:"mid,omitempty" json:"mid,omitempty"`
Bid string `bson:"bid,omitempty" json:"bid,omitempty"`
Ask string `bson:"ask,omitempty" json:"ask,omitempty"`
SpreadBps string `bson:"spreadBps,omitempty" json:"spreadBps,omitempty"`
AsOfUnixMs int64 `bson:"asOfUnixMs" json:"asOfUnixMs"`
AsOf *time.Time `bson:"asOf,omitempty" json:"asOf,omitempty"`
Source string `bson:"source,omitempty" json:"source,omitempty"`
ProviderRef string `bson:"providerRef,omitempty" json:"providerRef,omitempty"`
}
// Collection implements storable.Storable.
func (*RateSnapshot) Collection() string {
return RatesCollection
}
// AsOfTime converts the stored millisecond timestamp to time.Time.
func (r *RateSnapshot) AsOfTime() time.Time {
return time.UnixMilli(r.AsOfUnixMs)
}

View File

@@ -0,0 +1,68 @@
package model
import "github.com/tech/sendico/pkg/model"
// Collection names used by the FX oracle persistence layer.
const (
RatesCollection = "rates"
QuotesCollection = "quotes"
CurrenciesCollection = "currencies"
PairsCollection = "pairs"
)
// QuoteStatus tracks the lifecycle state of a quote.
type QuoteStatus string
const (
QuoteStatusIssued QuoteStatus = "issued"
QuoteStatusConsumed QuoteStatus = "consumed"
QuoteStatusExpired QuoteStatus = "expired"
)
// QuoteSide expresses the trade direction for the requested quote.
type QuoteSide string
const (
QuoteSideBuyBaseSellQuote QuoteSide = "buy_base_sell_quote"
QuoteSideSellBaseBuyQuote QuoteSide = "sell_base_buy_quote"
)
// QuoteAmountType indicates which leg amount was provided by the caller.
type QuoteAmountType string
const (
QuoteAmountTypeBase QuoteAmountType = "base"
QuoteAmountTypeQuote QuoteAmountType = "quote"
)
// RoundingMode describes how rounding should be applied for a currency.
type RoundingMode string
const (
RoundingModeUnspecified RoundingMode = "unspecified"
RoundingModeHalfEven RoundingMode = "half_even"
RoundingModeHalfUp RoundingMode = "half_up"
RoundingModeDown RoundingMode = "down"
)
// CurrencyPair identifies an FX pair.
type CurrencyPair struct {
Base string `bson:"base" json:"base"`
Quote string `bson:"quote" json:"quote"`
}
// Money represents an exact decimal amount with its currency.
type Money struct {
Currency string `bson:"currency" json:"currency"`
Amount string `bson:"amount" json:"amount"`
}
// QuoteMeta carries request-scoped metadata associated with a quote.
type QuoteMeta struct {
model.OrganizationBoundBase `bson:",inline" json:",inline"`
RequestRef string `bson:"requestRef,omitempty" json:"requestRef,omitempty"`
TenantRef string `bson:"tenantRef,omitempty" json:"tenantRef,omitempty"`
TraceRef string `bson:"traceRef,omitempty" json:"traceRef,omitempty"`
IdempotencyKey string `bson:"idempotencyKey,omitempty" json:"idempotencyKey,omitempty"`
}

View File

@@ -0,0 +1,115 @@
package mongo
import (
"context"
"time"
"github.com/tech/sendico/fx/storage"
"github.com/tech/sendico/fx/storage/mongo/store"
"github.com/tech/sendico/pkg/db"
"github.com/tech/sendico/pkg/db/transaction"
"github.com/tech/sendico/pkg/merrors"
"github.com/tech/sendico/pkg/mlogger"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)
type Store struct {
logger mlogger.Logger
conn *db.MongoConnection
db *mongo.Database
txFactory transaction.Factory
rates storage.RatesStore
quotes storage.QuotesStore
pairs storage.PairStore
currencies storage.CurrencyStore
}
func New(logger mlogger.Logger, conn *db.MongoConnection) (*Store, error) {
if conn == nil {
return nil, merrors.InvalidArgument("mongo connection is nil")
}
client := conn.Client()
if client == nil {
return nil, merrors.Internal("mongo client not initialised")
}
db := conn.Database()
txFactory := newMongoTransactionFactory(client)
s := &Store{
logger: logger.Named("storage").Named("mongo"),
conn: conn,
db: db,
txFactory: txFactory,
}
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
if err := s.Ping(ctx); err != nil {
s.logger.Error("mongo ping failed during store init", zap.Error(err))
return nil, err
}
ratesStore, err := store.NewRates(s.logger, db)
if err != nil {
s.logger.Error("failed to initialize rates store", zap.Error(err))
return nil, err
}
quotesStore, err := store.NewQuotes(s.logger, db, txFactory)
if err != nil {
s.logger.Error("failed to initialize quotes store", zap.Error(err))
return nil, err
}
pairsStore, err := store.NewPair(s.logger, db)
if err != nil {
s.logger.Error("failed to initialize pair store", zap.Error(err))
return nil, err
}
currencyStore, err := store.NewCurrency(s.logger, db)
if err != nil {
s.logger.Error("failed to initialize currency store", zap.Error(err))
return nil, err
}
s.rates = ratesStore
s.quotes = quotesStore
s.pairs = pairsStore
s.currencies = currencyStore
s.logger.Info("mongo storage ready")
return s, nil
}
func (s *Store) Ping(ctx context.Context) error {
return s.conn.Ping(ctx)
}
func (s *Store) Rates() storage.RatesStore {
return s.rates
}
func (s *Store) Quotes() storage.QuotesStore {
return s.quotes
}
func (s *Store) Pairs() storage.PairStore {
return s.pairs
}
func (s *Store) Currencies() storage.CurrencyStore {
return s.currencies
}
func (s *Store) Database() *mongo.Database {
return s.db
}
func (s *Store) TransactionFactory() transaction.Factory {
return s.txFactory
}
var _ storage.Repository = (*Store)(nil)

View File

@@ -0,0 +1,113 @@
package store
import (
"context"
"errors"
"github.com/tech/sendico/fx/storage"
"github.com/tech/sendico/fx/storage/model"
"github.com/tech/sendico/pkg/db/repository"
ri "github.com/tech/sendico/pkg/db/repository/index"
"github.com/tech/sendico/pkg/merrors"
"github.com/tech/sendico/pkg/mlogger"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)
type currencyStore struct {
logger mlogger.Logger
repo repository.Repository
}
func NewCurrency(logger mlogger.Logger, db *mongo.Database) (storage.CurrencyStore, error) {
repo := repository.CreateMongoRepository(db, model.CurrenciesCollection)
index := &ri.Definition{
Keys: []ri.Key{
{Field: "code", Sort: ri.Asc},
},
Unique: true,
}
if err := repo.CreateIndex(index); err != nil {
logger.Error("failed to ensure currencies index", zap.Error(err))
return nil, err
}
childLogger := logger.Named(model.CurrenciesCollection)
childLogger.Debug("currency store initialised", zap.String("collection", model.CurrenciesCollection))
return &currencyStore{
logger: childLogger,
repo: repo,
}, nil
}
func (c *currencyStore) Get(ctx context.Context, code string) (*model.Currency, error) {
if code == "" {
c.logger.Warn("attempt to fetch currency with empty code")
return nil, merrors.InvalidArgument("currencyStore: empty code")
}
result := &model.Currency{}
if err := c.repo.FindOneByFilter(ctx, repository.Filter("code", code), result); err != nil {
if errors.Is(err, merrors.ErrNoData) {
c.logger.Debug("currency not found", zap.String("code", code))
}
return nil, err
}
c.logger.Debug("currency loaded", zap.String("code", code))
return result, nil
}
func (c *currencyStore) List(ctx context.Context, codes ...string) ([]*model.Currency, error) {
query := repository.Query()
if len(codes) > 0 {
values := make([]any, len(codes))
for i, code := range codes {
values[i] = code
}
query = query.In(repository.Field("code"), values...)
}
currencies := make([]*model.Currency, 0)
err := c.repo.FindManyByFilter(ctx, query, func(cur *mongo.Cursor) error {
doc := &model.Currency{}
if err := cur.Decode(doc); err != nil {
return err
}
currencies = append(currencies, doc)
return nil
})
if err != nil {
c.logger.Error("failed to list currencies", zap.Error(err))
return nil, err
}
c.logger.Debug("listed currencies", zap.Int("count", len(currencies)))
return currencies, nil
}
func (c *currencyStore) Upsert(ctx context.Context, currency *model.Currency) error {
if currency == nil {
c.logger.Warn("attempt to upsert nil currency")
return merrors.InvalidArgument("currencyStore: nil currency")
}
if currency.Code == "" {
c.logger.Warn("attempt to upsert currency with empty code")
return merrors.InvalidArgument("currencyStore: empty code")
}
existing := &model.Currency{}
filter := repository.Filter("code", currency.Code)
if err := c.repo.FindOneByFilter(ctx, filter, existing); err != nil {
if errors.Is(err, merrors.ErrNoData) {
c.logger.Debug("inserting new currency", zap.String("code", currency.Code))
return c.repo.Insert(ctx, currency, filter)
}
c.logger.Error("failed to fetch currency", zap.Error(err), zap.String("code", currency.Code))
return err
}
if existing.GetID() != nil {
currency.SetID(*existing.GetID())
}
c.logger.Debug("updating currency", zap.String("code", currency.Code))
return c.repo.Update(ctx, currency)
}

View File

@@ -0,0 +1,104 @@
package store
import (
"context"
"errors"
"testing"
"github.com/tech/sendico/fx/storage/model"
"github.com/tech/sendico/pkg/db/repository/builder"
rd "github.com/tech/sendico/pkg/db/repository/decoder"
"github.com/tech/sendico/pkg/db/storable"
"github.com/tech/sendico/pkg/merrors"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
)
func TestCurrencyStoreGet(t *testing.T) {
repo := &repoStub{
findOneFn: func(_ context.Context, _ builder.Query, result storable.Storable) error {
currency := result.(*model.Currency)
currency.Code = "USD"
return nil
},
}
store := &currencyStore{logger: zap.NewNop(), repo: repo}
res, err := store.Get(context.Background(), "USD")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.Code != "USD" {
t.Fatalf("unexpected code: %s", res.Code)
}
}
func TestCurrencyStoreList(t *testing.T) {
repo := &repoStub{
findManyFn: func(_ context.Context, _ builder.Query, decode rd.DecodingFunc) error {
return runDecoderWithDocs(t, decode, &model.Currency{Code: "USD"})
},
}
store := &currencyStore{logger: zap.NewNop(), repo: repo}
currencies, err := store.List(context.Background(), "USD")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(currencies) != 1 || currencies[0].Code != "USD" {
t.Fatalf("unexpected list result: %+v", currencies)
}
}
func TestCurrencyStoreUpsertInsert(t *testing.T) {
inserted := false
repo := &repoStub{
findOneFn: func(context.Context, builder.Query, storable.Storable) error {
return merrors.ErrNoData
},
insertFn: func(_ context.Context, obj storable.Storable, _ builder.Query) error {
_ = cloneCurrency(t, obj)
inserted = true
return nil
},
}
store := &currencyStore{logger: zap.NewNop(), repo: repo}
if err := store.Upsert(context.Background(), &model.Currency{Code: "USD"}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if !inserted {
t.Fatalf("expected insert to be called")
}
}
func TestCurrencyStoreGetInvalid(t *testing.T) {
store := &currencyStore{logger: zap.NewNop(), repo: &repoStub{}}
if _, err := store.Get(context.Background(), ""); !errors.Is(err, merrors.ErrInvalidArg) {
t.Fatalf("expected invalid argument error")
}
}
func TestCurrencyStoreUpsertUpdate(t *testing.T) {
var updated *model.Currency
repo := &repoStub{
findOneFn: func(_ context.Context, _ builder.Query, result storable.Storable) error {
currency := result.(*model.Currency)
currency.SetID(primitive.NewObjectID())
currency.Code = "USD"
return nil
},
updateFn: func(_ context.Context, obj storable.Storable) error {
updated = cloneCurrency(t, obj)
return nil
},
}
store := &currencyStore{logger: zap.NewNop(), repo: repo}
if err := store.Upsert(context.Background(), &model.Currency{Code: "USD"}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if updated == nil || updated.GetID() == nil {
t.Fatalf("expected update to preserve ID")
}
}

View File

@@ -0,0 +1,111 @@
package store
import (
"context"
"errors"
"github.com/tech/sendico/fx/storage"
"github.com/tech/sendico/fx/storage/model"
"github.com/tech/sendico/pkg/db/repository"
ri "github.com/tech/sendico/pkg/db/repository/index"
"github.com/tech/sendico/pkg/merrors"
"github.com/tech/sendico/pkg/mlogger"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)
type pairStore struct {
logger mlogger.Logger
repo repository.Repository
}
func NewPair(logger mlogger.Logger, db *mongo.Database) (storage.PairStore, error) {
repo := repository.CreateMongoRepository(db, model.PairsCollection)
index := &ri.Definition{
Keys: []ri.Key{
{Field: "pair.base", Sort: ri.Asc},
{Field: "pair.quote", Sort: ri.Asc},
},
Unique: true,
}
if err := repo.CreateIndex(index); err != nil {
logger.Error("failed to ensure pairs index", zap.Error(err))
return nil, err
}
logger.Debug("pair store initialised", zap.String("collection", model.PairsCollection))
return &pairStore{
logger: logger.Named(model.PairsCollection),
repo: repo,
}, nil
}
func (p *pairStore) ListEnabled(ctx context.Context) ([]*model.Pair, error) {
filter := repository.Query().Filter(repository.Field("isEnabled"), true)
pairs := make([]*model.Pair, 0)
err := p.repo.FindManyByFilter(ctx, filter, func(cur *mongo.Cursor) error {
doc := &model.Pair{}
if err := cur.Decode(doc); err != nil {
return err
}
pairs = append(pairs, doc)
return nil
})
if err != nil {
p.logger.Error("failed to list enabled pairs", zap.Error(err))
return nil, err
}
p.logger.Debug("listed enabled pairs", zap.Int("count", len(pairs)))
return pairs, nil
}
func (p *pairStore) Get(ctx context.Context, pair model.CurrencyPair) (*model.Pair, error) {
if pair.Base == "" || pair.Quote == "" {
p.logger.Warn("attempt to fetch pair with empty currency", zap.String("base", pair.Base), zap.String("quote", pair.Quote))
return nil, merrors.InvalidArgument("pairStore: incomplete pair")
}
result := &model.Pair{}
query := repository.Query().
Filter(repository.Field("pair").Dot("base"), pair.Base).
Filter(repository.Field("pair").Dot("quote"), pair.Quote)
if err := p.repo.FindOneByFilter(ctx, query, result); err != nil {
if errors.Is(err, merrors.ErrNoData) {
p.logger.Debug("pair not found", zap.String("base", pair.Base), zap.String("quote", pair.Quote))
}
return nil, err
}
p.logger.Debug("pair loaded", zap.String("base", pair.Base), zap.String("quote", pair.Quote))
return result, nil
}
func (p *pairStore) Upsert(ctx context.Context, pair *model.Pair) error {
if pair == nil {
p.logger.Warn("attempt to upsert nil pair")
return merrors.InvalidArgument("pairStore: nil pair")
}
if pair.Pair.Base == "" || pair.Pair.Quote == "" {
p.logger.Warn("attempt to upsert pair with empty currency", zap.String("base", pair.Pair.Base), zap.String("quote", pair.Pair.Quote))
return merrors.InvalidArgument("pairStore: incomplete pair")
}
existing := &model.Pair{}
query := repository.Query().
Filter(repository.Field("pair").Dot("base"), pair.Pair.Base).
Filter(repository.Field("pair").Dot("quote"), pair.Pair.Quote)
err := p.repo.FindOneByFilter(ctx, query, existing)
if err != nil {
if errors.Is(err, merrors.ErrNoData) {
p.logger.Debug("inserting new pair", zap.String("base", pair.Pair.Base), zap.String("quote", pair.Pair.Quote))
return p.repo.Insert(ctx, pair, query)
}
p.logger.Error("failed to fetch pair", zap.Error(err), zap.String("base", pair.Pair.Base), zap.String("quote", pair.Pair.Quote))
return err
}
if existing.GetID() != nil {
pair.SetID(*existing.GetID())
}
p.logger.Debug("updating pair", zap.String("base", pair.Pair.Base), zap.String("quote", pair.Pair.Quote))
return p.repo.Update(ctx, pair)
}

View File

@@ -0,0 +1,101 @@
package store
import (
"context"
"errors"
"testing"
"github.com/tech/sendico/fx/storage/model"
"github.com/tech/sendico/pkg/db/repository/builder"
rd "github.com/tech/sendico/pkg/db/repository/decoder"
"github.com/tech/sendico/pkg/db/storable"
"github.com/tech/sendico/pkg/merrors"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
)
func TestPairStoreListEnabled(t *testing.T) {
repo := &repoStub{
findManyFn: func(_ context.Context, _ builder.Query, decode rd.DecodingFunc) error {
docs := []interface{}{
&model.Pair{Pair: model.CurrencyPair{Base: "USD", Quote: "EUR"}},
}
return runDecoderWithDocs(t, decode, docs...)
},
}
store := &pairStore{logger: zap.NewNop(), repo: repo}
pairs, err := store.ListEnabled(context.Background())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if len(pairs) != 1 || pairs[0].Pair.Base != "USD" {
t.Fatalf("unexpected pairs result: %+v", pairs)
}
}
func TestPairStoreGetInvalid(t *testing.T) {
store := &pairStore{logger: zap.NewNop(), repo: &repoStub{}}
if _, err := store.Get(context.Background(), model.CurrencyPair{}); !errors.Is(err, merrors.ErrInvalidArg) {
t.Fatalf("expected invalid argument error")
}
}
func TestPairStoreGetNotFound(t *testing.T) {
repo := &repoStub{
findOneFn: func(context.Context, builder.Query, storable.Storable) error {
return merrors.ErrNoData
},
}
store := &pairStore{logger: zap.NewNop(), repo: repo}
if _, err := store.Get(context.Background(), model.CurrencyPair{Base: "USD", Quote: "EUR"}); !errors.Is(err, merrors.ErrNoData) {
t.Fatalf("expected ErrNoData, got %v", err)
}
}
func TestPairStoreUpsertInsert(t *testing.T) {
ctx := context.Background()
var inserted *model.Pair
repo := &repoStub{
findOneFn: func(context.Context, builder.Query, storable.Storable) error {
return merrors.ErrNoData
},
insertFn: func(_ context.Context, obj storable.Storable, _ builder.Query) error {
inserted = clonePair(t, obj)
return nil
},
}
store := &pairStore{logger: zap.NewNop(), repo: repo}
pair := &model.Pair{Pair: model.CurrencyPair{Base: "USD", Quote: "EUR"}}
if err := store.Upsert(ctx, pair); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if inserted == nil {
t.Fatalf("expected insert to be called")
}
}
func TestPairStoreUpsertUpdate(t *testing.T) {
ctx := context.Background()
var updated *model.Pair
repo := &repoStub{
findOneFn: func(_ context.Context, _ builder.Query, result storable.Storable) error {
pair := result.(*model.Pair)
pair.SetID(primitive.NewObjectID())
return nil
},
updateFn: func(_ context.Context, obj storable.Storable) error {
updated = clonePair(t, obj)
return nil
},
}
store := &pairStore{logger: zap.NewNop(), repo: repo}
if err := store.Upsert(ctx, &model.Pair{Pair: model.CurrencyPair{Base: "USD", Quote: "EUR"}}); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if updated == nil || updated.GetID() == nil {
t.Fatalf("expected update to preserve existing ID")
}
}

View File

@@ -0,0 +1,198 @@
package store
import (
"context"
"errors"
"time"
"github.com/tech/sendico/fx/storage"
"github.com/tech/sendico/fx/storage/model"
"github.com/tech/sendico/pkg/db/repository"
"github.com/tech/sendico/pkg/db/repository/builder"
ri "github.com/tech/sendico/pkg/db/repository/index"
"github.com/tech/sendico/pkg/db/transaction"
"github.com/tech/sendico/pkg/merrors"
"github.com/tech/sendico/pkg/mlogger"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)
type quotesStore struct {
logger mlogger.Logger
repo repository.Repository
txFactory transaction.Factory
}
func NewQuotes(logger mlogger.Logger, db *mongo.Database, txFactory transaction.Factory) (storage.QuotesStore, error) {
repo := repository.CreateMongoRepository(db, model.QuotesCollection)
indexes := []*ri.Definition{
{
Keys: []ri.Key{
{Field: "quoteRef", Sort: ri.Asc},
},
Unique: true,
},
{
Keys: []ri.Key{
{Field: "status", Sort: ri.Asc},
{Field: "expiresAtUnixMs", Sort: ri.Asc},
},
},
{
Keys: []ri.Key{
{Field: "consumedByLedgerTxnRef", Sort: ri.Asc},
},
},
}
ttlSeconds := int32(0)
indexes = append(indexes, &ri.Definition{
Keys: []ri.Key{
{Field: "expiresAt", Sort: ri.Asc},
},
TTL: &ttlSeconds,
Name: "quotes_expires_at_ttl",
})
for _, def := range indexes {
if err := repo.CreateIndex(def); err != nil {
logger.Error("failed to ensure quotes index", zap.Error(err))
return nil, err
}
}
childLogger := logger.Named(model.QuotesCollection)
childLogger.Debug("quotes store initialised", zap.String("collection", model.QuotesCollection))
return &quotesStore{
logger: childLogger,
repo: repo,
txFactory: txFactory,
}, nil
}
func (q *quotesStore) Issue(ctx context.Context, quote *model.Quote) error {
if quote == nil {
q.logger.Warn("attempt to issue nil quote")
return merrors.InvalidArgument("quotesStore: nil quote")
}
if quote.QuoteRef == "" {
q.logger.Warn("attempt to issue quote with empty ref")
return merrors.InvalidArgument("quotesStore: empty quoteRef")
}
if quote.ExpiresAtUnixMs > 0 && quote.ExpiresAt == nil {
expiry := time.UnixMilli(quote.ExpiresAtUnixMs)
quote.ExpiresAt = &expiry
}
quote.Status = model.QuoteStatusIssued
quote.ConsumedByLedgerTxnRef = ""
quote.ConsumedAtUnixMs = nil
if err := q.repo.Insert(ctx, quote, repository.Filter("quoteRef", quote.QuoteRef)); err != nil {
q.logger.Error("failed to insert quote", zap.Error(err), zap.String("quote_ref", quote.QuoteRef))
return err
}
q.logger.Debug("quote issued", zap.String("quote_ref", quote.QuoteRef), zap.Bool("firm", quote.Firm))
return nil
}
func (q *quotesStore) GetByRef(ctx context.Context, quoteRef string) (*model.Quote, error) {
if quoteRef == "" {
q.logger.Warn("attempt to fetch quote with empty ref")
return nil, merrors.InvalidArgument("quotesStore: empty quoteRef")
}
quote := &model.Quote{}
if err := q.repo.FindOneByFilter(ctx, repository.Filter("quoteRef", quoteRef), quote); err != nil {
if errors.Is(err, merrors.ErrNoData) {
q.logger.Debug("quote not found", zap.String("quote_ref", quoteRef))
}
return nil, err
}
q.logger.Debug("quote loaded", zap.String("quote_ref", quoteRef), zap.String("status", string(quote.Status)))
return quote, nil
}
func (q *quotesStore) Consume(ctx context.Context, quoteRef, ledgerTxnRef string, when time.Time) (*model.Quote, error) {
if quoteRef == "" || ledgerTxnRef == "" {
q.logger.Warn("attempt to consume quote with missing identifiers", zap.String("quote_ref", quoteRef), zap.String("ledger_ref", ledgerTxnRef))
return nil, merrors.InvalidArgument("quotesStore: missing identifiers")
}
if when.IsZero() {
when = time.Now()
}
q.logger.Debug("consuming quote", zap.String("quote_ref", quoteRef), zap.String("ledger_ref", ledgerTxnRef))
txn := q.txFactory.CreateTransaction()
result, err := txn.Execute(ctx, func(txCtx context.Context) (any, error) {
quote := &model.Quote{}
if err := q.repo.FindOneByFilter(txCtx, repository.Filter("quoteRef", quoteRef), quote); err != nil {
return nil, err
}
if !quote.Firm {
q.logger.Warn("quote not firm", zap.String("quote_ref", quoteRef))
return nil, storage.ErrQuoteNotFirm
}
if quote.Status == model.QuoteStatusExpired || quote.IsExpired(when) {
quote.MarkExpired()
if err := q.repo.Update(txCtx, quote); err != nil {
return nil, err
}
q.logger.Info("quote expired during consume", zap.String("quote_ref", quoteRef))
return nil, storage.ErrQuoteExpired
}
if quote.Status == model.QuoteStatusConsumed {
if quote.ConsumedByLedgerTxnRef == ledgerTxnRef {
q.logger.Debug("quote already consumed by ledger", zap.String("quote_ref", quoteRef), zap.String("ledger_ref", ledgerTxnRef))
return quote, nil
}
q.logger.Warn("quote consumed by different ledger", zap.String("quote_ref", quoteRef), zap.String("existing_ledger_ref", quote.ConsumedByLedgerTxnRef))
return nil, storage.ErrQuoteConsumed
}
quote.MarkConsumed(ledgerTxnRef, when)
if err := q.repo.Update(txCtx, quote); err != nil {
return nil, err
}
q.logger.Info("quote consumed", zap.String("quote_ref", quoteRef), zap.String("ledger_ref", ledgerTxnRef))
return quote, nil
})
if err != nil {
q.logger.Error("quote consumption failed", zap.Error(err), zap.String("quote_ref", quoteRef), zap.String("ledger_ref", ledgerTxnRef))
return nil, err
}
quote, _ := result.(*model.Quote)
if quote == nil {
return nil, merrors.Internal("quotesStore: transaction returned nil quote")
}
return quote, nil
}
func (q *quotesStore) ExpireIssuedBefore(ctx context.Context, cutoff time.Time) (int, error) {
if cutoff.IsZero() {
q.logger.Warn("attempt to expire quotes with zero cutoff")
return 0, merrors.InvalidArgument("quotesStore: cutoff time is zero")
}
filter := repository.Query().
Filter(repository.Field("status"), model.QuoteStatusIssued).
Comparison(repository.Field("expiresAtUnixMs"), builder.Lt, cutoff.UnixMilli())
patch := repository.Patch().
Set(repository.Field("status"), model.QuoteStatusExpired).
Unset(repository.Field("consumedByLedgerTxnRef")).
Unset(repository.Field("consumedAtUnixMs"))
updated, err := q.repo.PatchMany(ctx, filter, patch)
if err != nil {
q.logger.Error("failed to expire quotes", zap.Error(err))
return 0, err
}
if updated > 0 {
q.logger.Info("quotes expired", zap.Int("count", updated))
}
return updated, nil
}

View File

@@ -0,0 +1,184 @@
package store
import (
"context"
"errors"
"testing"
"time"
"github.com/tech/sendico/fx/storage"
"github.com/tech/sendico/fx/storage/model"
"github.com/tech/sendico/pkg/db/repository/builder"
"github.com/tech/sendico/pkg/db/storable"
"github.com/tech/sendico/pkg/merrors"
"go.uber.org/zap"
)
func TestQuotesStoreIssue(t *testing.T) {
ctx := context.Background()
var inserted *model.Quote
repo := &repoStub{
insertFn: func(_ context.Context, obj storable.Storable, _ builder.Query) error {
inserted = cloneQuote(t, obj)
return nil
},
}
store := &quotesStore{logger: zap.NewNop(), repo: repo, txFactory: &txFactoryStub{}}
quote := &model.Quote{QuoteRef: "q1"}
if err := store.Issue(ctx, quote); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if inserted == nil || inserted.Status != model.QuoteStatusIssued {
t.Fatalf("expected issued quote to be inserted")
}
}
func TestQuotesStoreIssueSetsExpiryDate(t *testing.T) {
ctx := context.Background()
var inserted *model.Quote
repo := &repoStub{
insertFn: func(_ context.Context, obj storable.Storable, _ builder.Query) error {
inserted = cloneQuote(t, obj)
return nil
},
}
store := &quotesStore{logger: zap.NewNop(), repo: repo, txFactory: &txFactoryStub{}}
expiry := time.Now().Add(2 * time.Minute).UnixMilli()
quote := &model.Quote{
QuoteRef: "q1",
ExpiresAtUnixMs: expiry,
}
if err := store.Issue(ctx, quote); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if inserted == nil || inserted.ExpiresAt == nil {
t.Fatalf("expected expiry timestamp to be populated")
}
if inserted.ExpiresAt.UnixMilli() != expiry {
t.Fatalf("expected expiry to equal %d, got %d", expiry, inserted.ExpiresAt.UnixMilli())
}
}
func TestQuotesStoreIssueInvalidInput(t *testing.T) {
store := &quotesStore{logger: zap.NewNop(), repo: &repoStub{}, txFactory: &txFactoryStub{}}
if err := store.Issue(context.Background(), nil); !errors.Is(err, merrors.ErrInvalidArg) {
t.Fatalf("expected invalid argument error, got %v", err)
}
}
func TestQuotesStoreConsumeSuccess(t *testing.T) {
ctx := context.Background()
now := time.Now()
ledgerRef := "ledger-1"
stored := &model.Quote{
QuoteRef: "q1",
Firm: true,
Status: model.QuoteStatusIssued,
ExpiresAtUnixMs: now.Add(5 * time.Minute).UnixMilli(),
}
var updated *model.Quote
repo := &repoStub{
findOneFn: func(_ context.Context, _ builder.Query, result storable.Storable) error {
quote := result.(*model.Quote)
*quote = *stored
return nil
},
updateFn: func(_ context.Context, obj storable.Storable) error {
updated = cloneQuote(t, obj)
return nil
},
}
factory := &txFactoryStub{}
store := &quotesStore{logger: zap.NewNop(), repo: repo, txFactory: factory}
res, err := store.Consume(ctx, "q1", ledgerRef, now)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res == nil || res.Status != model.QuoteStatusConsumed {
t.Fatalf("expected consumed quote")
}
if updated == nil || updated.ConsumedByLedgerTxnRef != ledgerRef {
t.Fatalf("expected update with ledger ref")
}
}
func TestQuotesStoreConsumeExpired(t *testing.T) {
ctx := context.Background()
stored := &model.Quote{
QuoteRef: "q1",
Firm: true,
Status: model.QuoteStatusIssued,
ExpiresAtUnixMs: time.Now().Add(-time.Minute).UnixMilli(),
}
var updated *model.Quote
repo := &repoStub{
findOneFn: func(_ context.Context, _ builder.Query, result storable.Storable) error {
quote := result.(*model.Quote)
*quote = *stored
return nil
},
updateFn: func(_ context.Context, obj storable.Storable) error {
updated = cloneQuote(t, obj)
return nil
},
}
factory := &txFactoryStub{}
store := &quotesStore{logger: zap.NewNop(), repo: repo, txFactory: factory}
_, err := store.Consume(ctx, "q1", "ledger", time.Now())
if !errors.Is(err, storage.ErrQuoteExpired) {
t.Fatalf("expected ErrQuoteExpired, got %v", err)
}
if updated == nil || updated.Status != model.QuoteStatusExpired {
t.Fatalf("expected quote marked expired")
}
}
func TestQuotesStoreExpireIssuedBefore(t *testing.T) {
repo := &repoStub{
patchManyFn: func(context.Context, builder.Query, builder.Patch) (int, error) {
return 3, nil
},
}
store := &quotesStore{logger: zap.NewNop(), repo: repo, txFactory: &txFactoryStub{}}
count, err := store.ExpireIssuedBefore(context.Background(), time.Now())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if count != 3 {
t.Fatalf("expected 3 expired quotes, got %d", count)
}
}
func TestQuotesStoreExpireZeroCutoff(t *testing.T) {
store := &quotesStore{logger: zap.NewNop(), repo: &repoStub{}, txFactory: &txFactoryStub{}}
if _, err := store.ExpireIssuedBefore(context.Background(), time.Time{}); !errors.Is(err, merrors.ErrInvalidArg) {
t.Fatalf("expected invalid argument error")
}
}
func TestQuotesStoreGetByRefNotFound(t *testing.T) {
repo := &repoStub{
findOneFn: func(context.Context, builder.Query, storable.Storable) error {
return merrors.ErrNoData
},
}
store := &quotesStore{logger: zap.NewNop(), repo: repo, txFactory: &txFactoryStub{}}
if _, err := store.GetByRef(context.Background(), "missing"); !errors.Is(err, merrors.ErrNoData) {
t.Fatalf("expected ErrNoData, got %v", err)
}
}
func TestQuotesStoreGetByRefInvalid(t *testing.T) {
store := &quotesStore{logger: zap.NewNop(), repo: &repoStub{}, txFactory: &txFactoryStub{}}
if _, err := store.GetByRef(context.Background(), ""); !errors.Is(err, merrors.ErrInvalidArg) {
t.Fatalf("expected invalid argument error")
}
}

View File

@@ -0,0 +1,127 @@
package store
import (
"context"
"errors"
"time"
"github.com/tech/sendico/fx/storage"
"github.com/tech/sendico/fx/storage/model"
"github.com/tech/sendico/pkg/db/repository"
ri "github.com/tech/sendico/pkg/db/repository/index"
"github.com/tech/sendico/pkg/merrors"
"github.com/tech/sendico/pkg/mlogger"
"go.mongodb.org/mongo-driver/mongo"
"go.uber.org/zap"
)
type ratesStore struct {
logger mlogger.Logger
repo repository.Repository
}
func NewRates(logger mlogger.Logger, db *mongo.Database) (storage.RatesStore, error) {
repo := repository.CreateMongoRepository(db, model.RatesCollection)
indexes := []*ri.Definition{
{
Keys: []ri.Key{
{Field: "pair.base", Sort: ri.Asc},
{Field: "pair.quote", Sort: ri.Asc},
{Field: "provider", Sort: ri.Asc},
{Field: "asOfUnixMs", Sort: ri.Desc},
},
},
{
Keys: []ri.Key{
{Field: "rateRef", Sort: ri.Asc},
},
Unique: true,
},
}
ttlSeconds := int32(24 * 60 * 60)
indexes = append(indexes, &ri.Definition{
Keys: []ri.Key{
{Field: "asOf", Sort: ri.Asc},
},
TTL: &ttlSeconds,
Name: "rates_as_of_ttl",
})
for _, def := range indexes {
if err := repo.CreateIndex(def); err != nil {
logger.Error("failed to ensure rates index", zap.Error(err))
return nil, err
}
}
logger.Debug("rates store initialised", zap.String("collection", model.RatesCollection))
return &ratesStore{
logger: logger.Named(model.RatesCollection),
repo: repo,
}, nil
}
func (r *ratesStore) UpsertSnapshot(ctx context.Context, snapshot *model.RateSnapshot) error {
if snapshot == nil {
r.logger.Warn("attempt to upsert nil snapshot")
return merrors.InvalidArgument("ratesStore: nil snapshot")
}
if snapshot.RateRef == "" {
r.logger.Warn("attempt to upsert snapshot with empty rate_ref")
return merrors.InvalidArgument("ratesStore: empty rateRef")
}
if snapshot.AsOfUnixMs > 0 && snapshot.AsOf == nil {
asOf := time.UnixMilli(snapshot.AsOfUnixMs).UTC()
snapshot.AsOf = &asOf
}
existing := &model.RateSnapshot{}
filter := repository.Filter("rateRef", snapshot.RateRef)
err := r.repo.FindOneByFilter(ctx, filter, existing)
if err != nil {
if errors.Is(err, merrors.ErrNoData) {
r.logger.Debug("inserting new rate snapshot", zap.String("rate_ref", snapshot.RateRef))
return r.repo.Insert(ctx, snapshot, filter)
}
r.logger.Error("failed to query rate snapshot", zap.Error(err), zap.String("rate_ref", snapshot.RateRef))
return err
}
if existing.GetID() != nil {
snapshot.SetID(*existing.GetID())
}
r.logger.Debug("updating rate snapshot", zap.String("rate_ref", snapshot.RateRef))
return r.repo.Update(ctx, snapshot)
}
func (r *ratesStore) LatestSnapshot(ctx context.Context, pair model.CurrencyPair, provider string) (*model.RateSnapshot, error) {
query := repository.Query().
Filter(repository.Field("pair").Dot("base"), pair.Base).
Filter(repository.Field("pair").Dot("quote"), pair.Quote)
if provider != "" {
query = query.Filter(repository.Field("provider"), provider)
}
limit := int64(1)
query = query.Sort(repository.Field("asOfUnixMs"), false).Limit(&limit)
var result *model.RateSnapshot
err := r.repo.FindManyByFilter(ctx, query, func(cur *mongo.Cursor) error {
doc := &model.RateSnapshot{}
if err := cur.Decode(doc); err != nil {
return err
}
result = doc
return nil
})
if err != nil {
return nil, err
}
if result == nil {
return nil, merrors.ErrNoData
}
return result, nil
}

View File

@@ -0,0 +1,87 @@
package store
import (
"context"
"testing"
"time"
"github.com/tech/sendico/fx/storage/model"
"github.com/tech/sendico/pkg/db/repository/builder"
rd "github.com/tech/sendico/pkg/db/repository/decoder"
"github.com/tech/sendico/pkg/db/storable"
"github.com/tech/sendico/pkg/merrors"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.uber.org/zap"
)
func TestRatesStoreUpsertInsert(t *testing.T) {
ctx := context.Background()
var inserted *model.RateSnapshot
repo := &repoStub{
findOneFn: func(context.Context, builder.Query, storable.Storable) error {
return merrors.ErrNoData
},
insertFn: func(_ context.Context, obj storable.Storable, _ builder.Query) error {
inserted = cloneRate(t, obj)
return nil
},
}
store := &ratesStore{logger: zap.NewNop(), repo: repo}
snapshot := &model.RateSnapshot{RateRef: "r1"}
if err := store.UpsertSnapshot(ctx, snapshot); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if inserted == nil || inserted.RateRef != "r1" {
t.Fatalf("expected snapshot to be inserted")
}
}
func TestRatesStoreUpsertUpdate(t *testing.T) {
ctx := context.Background()
existingID := primitive.NewObjectID()
var updated *model.RateSnapshot
repo := &repoStub{
findOneFn: func(_ context.Context, _ builder.Query, result storable.Storable) error {
snap := result.(*model.RateSnapshot)
snap.SetID(existingID)
snap.RateRef = "existing"
return nil
},
updateFn: func(_ context.Context, obj storable.Storable) error {
snap := obj.(*model.RateSnapshot)
updated = snap
return nil
},
}
store := &ratesStore{logger: zap.NewNop(), repo: repo}
toUpdate := &model.RateSnapshot{RateRef: "existing"}
if err := store.UpsertSnapshot(ctx, toUpdate); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if updated == nil || updated.GetID() == nil || *updated.GetID() != existingID {
t.Fatalf("expected update to preserve ID")
}
}
func TestRatesStoreLatestSnapshot(t *testing.T) {
now := time.Now().UnixMilli()
repo := &repoStub{
findManyFn: func(_ context.Context, _ builder.Query, decode rd.DecodingFunc) error {
doc := &model.RateSnapshot{RateRef: "latest", AsOfUnixMs: now}
return runDecoderWithDocs(t, decode, doc)
},
}
store := &ratesStore{logger: zap.NewNop(), repo: repo}
res, err := store.LatestSnapshot(context.Background(), model.CurrencyPair{Base: "USD", Quote: "EUR"}, "")
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if res.RateRef != "latest" || res.AsOfUnixMs != now {
t.Fatalf("unexpected snapshot returned: %+v", res)
}
}

View File

@@ -0,0 +1,189 @@
package store
import (
"context"
"testing"
"github.com/tech/sendico/fx/storage/model"
"github.com/tech/sendico/pkg/db/repository/builder"
rd "github.com/tech/sendico/pkg/db/repository/decoder"
ri "github.com/tech/sendico/pkg/db/repository/index"
"github.com/tech/sendico/pkg/db/storable"
"github.com/tech/sendico/pkg/db/transaction"
"github.com/tech/sendico/pkg/merrors"
pmodel "github.com/tech/sendico/pkg/model"
"go.mongodb.org/mongo-driver/bson/primitive"
"go.mongodb.org/mongo-driver/mongo"
)
type repoStub struct {
insertFn func(ctx context.Context, obj storable.Storable, filter builder.Query) error
insertManyFn func(ctx context.Context, objects []storable.Storable) error
findOneFn func(ctx context.Context, query builder.Query, result storable.Storable) error
findManyFn func(ctx context.Context, query builder.Query, decoder rd.DecodingFunc) error
updateFn func(ctx context.Context, obj storable.Storable) error
patchManyFn func(ctx context.Context, filter builder.Query, patch builder.Patch) (int, error)
createIdxFn func(def *ri.Definition) error
}
func (r *repoStub) Aggregate(ctx context.Context, b builder.Pipeline, decoder rd.DecodingFunc) error {
return merrors.NotImplemented("Aggregate not used")
}
func (r *repoStub) Insert(ctx context.Context, obj storable.Storable, filter builder.Query) error {
if r.insertFn != nil {
return r.insertFn(ctx, obj, filter)
}
return nil
}
func (r *repoStub) InsertMany(ctx context.Context, objects []storable.Storable) error {
if r.insertManyFn != nil {
return r.insertManyFn(ctx, objects)
}
return nil
}
func (r *repoStub) Get(ctx context.Context, id primitive.ObjectID, result storable.Storable) error {
return merrors.NotImplemented("Get not used")
}
func (r *repoStub) FindOneByFilter(ctx context.Context, query builder.Query, result storable.Storable) error {
if r.findOneFn != nil {
return r.findOneFn(ctx, query, result)
}
return nil
}
func (r *repoStub) FindManyByFilter(ctx context.Context, query builder.Query, decoder rd.DecodingFunc) error {
if r.findManyFn != nil {
return r.findManyFn(ctx, query, decoder)
}
return nil
}
func (r *repoStub) Update(ctx context.Context, obj storable.Storable) error {
if r.updateFn != nil {
return r.updateFn(ctx, obj)
}
return nil
}
func (r *repoStub) Patch(ctx context.Context, id primitive.ObjectID, patch builder.Patch) error {
return merrors.NotImplemented("Patch not used")
}
func (r *repoStub) PatchMany(ctx context.Context, filter builder.Query, patch builder.Patch) (int, error) {
if r.patchManyFn != nil {
return r.patchManyFn(ctx, filter, patch)
}
return 0, nil
}
func (r *repoStub) Delete(ctx context.Context, id primitive.ObjectID) error {
return merrors.NotImplemented("Delete not used")
}
func (r *repoStub) DeleteMany(ctx context.Context, query builder.Query) error {
return merrors.NotImplemented("DeleteMany not used")
}
func (r *repoStub) CreateIndex(def *ri.Definition) error {
if r.createIdxFn != nil {
return r.createIdxFn(def)
}
return nil
}
func (r *repoStub) ListIDs(ctx context.Context, query builder.Query) ([]primitive.ObjectID, error) {
return nil, merrors.NotImplemented("ListIDs not used")
}
func (r *repoStub) ListPermissionBound(ctx context.Context, query builder.Query) ([]pmodel.PermissionBoundStorable, error) {
return nil, merrors.NotImplemented("ListPermissionBound not used")
}
func (r *repoStub) ListAccountBound(ctx context.Context, query builder.Query) ([]pmodel.AccountBoundStorable, error) {
return nil, merrors.NotImplemented("ListAccountBound not used")
}
func (r *repoStub) Collection() string { return "test" }
type txFactoryStub struct {
executeFn func(ctx context.Context, cb transaction.Callback) (any, error)
}
func (f *txFactoryStub) CreateTransaction() transaction.Transaction {
return &txStub{executeFn: f.executeFn}
}
type txStub struct {
executeFn func(ctx context.Context, cb transaction.Callback) (any, error)
}
func (t *txStub) Execute(ctx context.Context, cb transaction.Callback) (any, error) {
if t.executeFn != nil {
return t.executeFn(ctx, cb)
}
return cb(ctx)
}
func cloneRate(t *testing.T, obj storable.Storable) *model.RateSnapshot {
t.Helper()
rate, ok := obj.(*model.RateSnapshot)
if !ok {
t.Fatalf("expected *model.RateSnapshot, got %T", obj)
}
copy := *rate
return &copy
}
func cloneQuote(t *testing.T, obj storable.Storable) *model.Quote {
t.Helper()
quote, ok := obj.(*model.Quote)
if !ok {
t.Fatalf("expected *model.Quote, got %T", obj)
}
copy := *quote
return &copy
}
func clonePair(t *testing.T, obj storable.Storable) *model.Pair {
t.Helper()
pair, ok := obj.(*model.Pair)
if !ok {
t.Fatalf("expected *model.Pair, got %T", obj)
}
copy := *pair
return &copy
}
func cloneCurrency(t *testing.T, obj storable.Storable) *model.Currency {
t.Helper()
currency, ok := obj.(*model.Currency)
if !ok {
t.Fatalf("expected *model.Currency, got %T", obj)
}
copy := *currency
return &copy
}
func runDecoderWithDocs(t *testing.T, decode rd.DecodingFunc, docs ...interface{}) error {
t.Helper()
cur, err := mongo.NewCursorFromDocuments(docs, nil, nil)
if err != nil {
t.Fatalf("failed to create cursor: %v", err)
}
defer cur.Close(context.Background())
if len(docs) > 0 {
if !cur.Next(context.Background()) {
return cur.Err()
}
}
if err := decode(cur); err != nil {
return err
}
return cur.Err()
}

View File

@@ -0,0 +1,38 @@
package mongo
import (
"context"
"github.com/tech/sendico/pkg/db/transaction"
"go.mongodb.org/mongo-driver/mongo"
)
type mongoTransactionFactory struct {
client *mongo.Client
}
func (f *mongoTransactionFactory) CreateTransaction() transaction.Transaction {
return &mongoTransaction{client: f.client}
}
type mongoTransaction struct {
client *mongo.Client
}
func (t *mongoTransaction) Execute(ctx context.Context, cb transaction.Callback) (any, error) {
session, err := t.client.StartSession()
if err != nil {
return nil, err
}
defer session.EndSession(ctx)
run := func(sessCtx mongo.SessionContext) (any, error) {
return cb(sessCtx)
}
return session.WithTransaction(ctx, run)
}
func newMongoTransactionFactory(client *mongo.Client) transaction.Factory {
return &mongoTransactionFactory{client: client}
}

53
api/fx/storage/storage.go Normal file
View File

@@ -0,0 +1,53 @@
package storage
import (
"context"
"time"
"github.com/tech/sendico/fx/storage/model"
)
type storageError string
func (e storageError) Error() string {
return string(e)
}
var (
ErrQuoteExpired = storageError("fx.storage: quote expired")
ErrQuoteConsumed = storageError("fx.storage: quote consumed")
ErrQuoteNotFirm = storageError("fx.storage: quote is not firm")
ErrQuoteConsumptionRace = storageError("fx.storage: quote consumption collision")
)
type Repository interface {
Ping(ctx context.Context) error
Rates() RatesStore
Quotes() QuotesStore
Pairs() PairStore
Currencies() CurrencyStore
}
type RatesStore interface {
UpsertSnapshot(ctx context.Context, snapshot *model.RateSnapshot) error
LatestSnapshot(ctx context.Context, pair model.CurrencyPair, provider string) (*model.RateSnapshot, error)
}
type QuotesStore interface {
Issue(ctx context.Context, quote *model.Quote) error
GetByRef(ctx context.Context, quoteRef string) (*model.Quote, error)
Consume(ctx context.Context, quoteRef, ledgerTxnRef string, when time.Time) (*model.Quote, error)
ExpireIssuedBefore(ctx context.Context, cutoff time.Time) (int, error)
}
type PairStore interface {
ListEnabled(ctx context.Context) ([]*model.Pair, error)
Get(ctx context.Context, pair model.CurrencyPair) (*model.Pair, error)
Upsert(ctx context.Context, p *model.Pair) error
}
type CurrencyStore interface {
Get(ctx context.Context, code string) (*model.Currency, error)
List(ctx context.Context, codes ...string) ([]*model.Currency, error)
Upsert(ctx context.Context, currency *model.Currency) error
}