Swapping bLuna to UST using terra.js

Hi, I’m trying to convert the bLuna CW20 token to the UST native token using TerraSwap and terra.js, I’m following examples online but not having any luck. I’m also looking at this example of a bLuna to UST transaction. This is the closest I think I’ve come:

  const terraSwap = new MsgExecuteContract(
        "terra1qxv6uyan8u0a88zemfcqffdtfqq9fxy575cwgq",
        "terra1qpd9n7afwf45rkjlpujrrdfh83pldec8rpujgn",
        { 
            swap : {
                offer_asset: { 
                    amount: "4000", 
                    info: { 
                      token: { 
                        contract_addr: "terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp" 
                      } 
                    } 
                },
            } 
         },
         { terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp: "4000" }
    );

This returns the error:
'failed to execute message; message index: 0: 0terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp is smaller than 4000terra1kc87mu460fwkqte29rquh4hc20m54fxwtsx7gp: insufficient funds: invalid request'

I was wondering if anyone can see where I’m going wrong here. I have 58894 bLuna in my wallet and can print this out right before executing the transaction.

Seems to be still something wrong with the message because when I run this instead:

const terraSwap = new MsgExecuteContract(
        "terra1qxv6uyan8u0a88zemfcqffdtfqq9fxy575cwgq",
        "terra1qpd9n7afwf45rkjlpujrrdfh83pldec8rpujgn",
        { 
            swap : {
                offer_asset: { 
                    amount: "10", 
                    info: { 
                      token: { 
                        contract_addr: "...." 
                      } 
                    } 
                },
            } 
         },
         { "blaaaah123": "10" }
    );

It will give the error: 'failed to execute message; message index: 0: 0blaaaah123 is smaller than 10blaaaah123: insufficient funds: invalid request'

You’re attempting to use ExecuteMsg::Swap, which is intended for swapping native coins (i.e. Luna and Terra stablecoins.)

For CW20 tokens such as bLuna, use Cw20ExecuteMsg::Send.

Try this:

use { LCDClient, Wallet, MsgExecuteContract } from "@terra-money/terra.js";

function encodeBase64(obj: any): string {
  return Buffer.from(JSON.stringify(obj)).toString("base64");
}

const msg = new MsgExecuteContract(senderAddress, bLunaTokenAddress, {
  send: {
    amount: "12345",
    contract: terraswapOrAstroportPairAddress,
    msg: encodeBase64({
      swap: {}
    })
  }
});
2 Likes

this is a scam from “dev”

thanks, that is helped. where can i see documentation about it.

This is the github

1 Like