Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unable to work with client in wasm (won't compile beacuse of github.com/lib/pq ) #3566

Closed
bukowa opened this issue Apr 23, 2021 · 5 comments
Closed
Labels

Comments

@bukowa
Copy link

bukowa commented Apr 23, 2021

What version are you using?

github.com/stellar/go v0.0.0-20210422144147-73974e979522 h1:5WPbBwBnFQWC1IGqRKHWqpNAtZPQy3vZl+rcRvVvu/M=
github.com/stellar/go v0.0.0-20210422144147-73974e979522/go.mod h1:m5Azd56FGZT/mnjlZE9i68+nxm/6b6YOg0kP99aBDi4=
github.com/stellar/go-xdr v0.0.0-20201028102745-f80a23dac78a h1:GnM0ArRp7EDbaTiFhSp/CLgyk2cacXxdUklqJmdJs1Q=
github.com/stellar/go-xdr v0.0.0-20201028102745-f80a23dac78a/go.mod h1:yoxyU/M8nl9LKeWIoBrbDPQ7Cy+4jxRcWcOayZ4BMps=
github.com/stellar/throttled v2.2.3-0.20190823235211-89d75816f59d+incompatible/go.mod h1:7CJ23pXirXBJq45DqvO6clzTEGM/l1SfKrgrzLry8b4=

What did you do?

GOARCH=wasm GOOS=js go build -o server/app/app.wasm

For more information see: lib/pq#987

What did you expect to see?

Everything works! We compiled for wasm!

What did you see instead?

GOARCH=wasm GOOS=js go build -o server/app/app.wasm
# github.com/lib/pq
vendor/github.com/lib/pq/connector.go:102:13: undefined: userCurrent

I want to use the client and transaction building from a wasm application, i guess it has nothing to do with postgresql.

package main

import (
	"errors"
	"fmt"
	"net/http"

	hz "github.com/stellar/go/clients/horizonclient"
	"github.com/stellar/go/keypair"
	"github.com/stellar/go/network"
	"github.com/stellar/go/protocols/horizon"
	"github.com/stellar/go/txnbuild"
)

var Client = hz.DefaultTestNetClient

func main() {
	var dist, issuer *keypair.Full
	var distAcc, issuerAcc horizon.Account

	var err error

	// create issuer and distributor accounts
	if issuer, err = keypair.Random(); err != nil {
		panic(err)
	}
	if dist, err = keypair.Random(); err != nil {
		panic(err)
	}

	// fund accounts with friendbot to make them active
	if err := fundAcc(*dist); err != nil {
		panic(err)
	}
	if err := fundAcc(*issuer); err != nil {
		panic(err)
	}

	// get account details
	if issuerAcc, err = Client.AccountDetail(hz.AccountRequest{
		AccountID: issuer.Address(),
	}); err != nil {
		panic(err)
	}

	if distAcc, err = Client.AccountDetail(hz.AccountRequest{
		AccountID: dist.Address(),
	}); err != nil {
		panic(err)
	}

	// create trust transaction
	tr, err := txnbuild.NewTransaction(txnbuild.TransactionParams{
		IncrementSequenceNum: true,
		Operations: []txnbuild.Operation{&txnbuild.ChangeTrust{
			Line: txnbuild.CreditAsset{Code: "XXX", Issuer: issuer.Address()},
		}},
		BaseFee: txnbuild.MinBaseFee,
		Timebounds: txnbuild.NewTimeout(3000),
		SourceAccount: &distAcc,
	})
	if err != nil {
		panic(err)
	}

	// sign transaction
	tr, err = tr.SignWithKeyString(network.TestNetworkPassphrase, dist.Seed())
	if err != nil {
		panic(err)
	}

	// submit transaction
	_, err = Client.SubmitTransaction(tr)
	if err != nil {
		panic(err)
	}

	// create payment transaction from issuer to distributor
	tr, err = txnbuild.NewTransaction(txnbuild.TransactionParams{
		IncrementSequenceNum: true,
		Operations: []txnbuild.Operation{
			&txnbuild.Payment{
				Destination: dist.Address(),
				Amount: "10000",
				Asset: txnbuild.CreditAsset{Code: "XXX", Issuer: issuer.Address()},
			},
		},
		Timebounds: txnbuild.NewTimeout(3000),
		SourceAccount: &issuerAcc,
		BaseFee: txnbuild.MinBaseFee,
	})
	if err != nil {
		panic(err)
	}

	// sign transaction
	tr, err = tr.Sign(network.TestNetworkPassphrase, issuer)
	if err != nil {
		panic(err)
	}

	// submit transaction
	_, err = Client.SubmitTransaction(tr)
	if err != nil {
		panic(err)
	}
}

func fundAcc(kp keypair.Full) error {
	fmt.Println(kp.Address())
	r, err := http.DefaultClient.Get(fmt.Sprintf("https://horizon-testnet.stellar.org/friendbot?addr=%s", kp.Address()))
	if err != nil {
		return err
	}
	if r.StatusCode != 200 {
		return errors.New(fmt.Sprint(kp.Address()))
	}
	return nil
}
@bukowa bukowa added the bug label Apr 23, 2021
@leighmcculloch
Copy link
Member

leighmcculloch commented Apr 23, 2021

I tried to do the same thing a few days ago – use the Stellar Go SDK with wasm – and ran into the same issue. It looks like the change required in pq is really quite small, as proposed in lib/pq#987 (comment).

I think #3152 is the most relevant thing we can do independent of pq. The xdr package has gained a dependency on pq which means the txnbuild package is loading pq and trying to compile it. We really need to remove the dependency on pq from xdr.


Ultimately, this would be one significant advantage of implementing multiple modules inside this repository that would scope dependencies like pq to only specific modules within the repo. We have this issue for that work still in the backlog #1237. If we put the effort in to doing this it will become clear when a package like pq becomes a dependency of a package like xdr.

@bukowa
Copy link
Author

bukowa commented Apr 24, 2021

Keep in mind https://github.com/lib/pq#status

This package is effectively in maintenance mode and is not actively developed. 
Small patches and features are only rarely reviewed and merged. 
We recommend using pgx which is actively maintained.

@bukowa
Copy link
Author

bukowa commented May 13, 2021

This was merged i don't have a chance to test it: lib/pq#1036 (comment)

@leighmcculloch
Copy link
Member

The change that got merged looks like it would address this problem. You'd just need to choose that version of lib/pq in your applications go.mod file. I think #3152 is still worth doing though. The fact that lib/pq is imported into the xdr package is far from ideal.

@leighmcculloch
Copy link
Member

I'm closing this issue because I tested the example code in the issue description and it compiles successfully with the latest version of lib/pq.

$ go get -u github.com/lib/pq@master      
go: downloading github.com/lib/pq v1.10.2-0.20210513161931-b2cfb1abfd0b
go get: upgraded github.com/lib/pq v1.2.0 => v1.10.2-0.20210513161931-b2cfb1abfd0b

$ GOARCH=wasm GOOS=js go build -o out.wasm

$ echo $?
0

Note that the version of lib/pq in stellar/go is v1.2.0, but that doesn't stop an application importing stellar/go from specifying a newer version. Stellar/go specifies only the minimum version it needs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants