I am developing an extension for Go-ethereum using xk6.
Connect method works, but other methods throws error when called from K6.
K6 throws error: TypeError: Object has no member ‘getLatestBlock’
The extension gets compiled successfully. The function in extension also works fine independently, when called from another go function. Call from K6 causes this error.
Appreciate any solution.
Extension Code below:
func init() {
modules.Register("k6/x/ethers", new(Ethers))
}
// Ethers is the k6 extension for a Ethers client.
type Ethers struct {
}
// Client is the Ether client wrapper.
type Client struct {
ctx context.Context
client *ethclient.Client
}
func (r *Ethers) Connect(infuraurl string) *Client {
ctx := context.Background()
client, err := ethclient.DialContext(ctx, infuraurl)
if err != nil {log.Print("There was an error connecting the Infura URL", err)}
return &Client{ctx:ctx, client: client}
}
func (c *Client) getLatestBlock() *types.Block {
block, err := c.client.BlockByNumber(c.ctx, nil)
if err != nil {log.Println(err)}
return block
}
I’m executing it from K6 script with custom k6 build with the extension:
import ethers from 'k6/x/ethers';
import { check, fail } from 'k6';
export default function etherconnect() {
const infuraurl = "https://eth-goerli.g.alchemy.com/v2/FoMafzy7HtXrDnkkdGlGeGG-xhdJ3PNh"
let client = ethers.connect(infuraurl);`
if(!check(client, {
'[ethers] successful connection': (obj) => obj,
})
){
fail('[ethers] connection failed');
}
let block = client.getLatestBlock()
console.log(`Block Number: ${block.Number}`)
}