Open Trade

This action opens a new trade for the user.

const openTradeParams = {
functionId: FunctionId.OPEN_LONG_ID,
exchangeAddress: 0x8fA7490cedB7207281a5ceabee12773046dE664E,
assetPriceBound: toBn("0"),
stablePriceBound: toBn(String(toWei("1"))),
userInteractionNumber: toBn(createUserInteractionNumberRandom()),
gasStableBound: toBn("0"),
minTransmitterGas: toBn("0"),
signatureTime: toBn(Math.floor(Date.now() / 1000))
collateral: toBn(String(toWei(String(collateral)))),
leverage: toBn(leverage),
tradeFeeBound: toBn("0"),
};

The next step is to sign the object as a message. To do so you’ll use the UserMessageEncoder.

const userMessageEncoder = new UserMessageEncoder();
const { packedMessage, signature } = await userMessageEncoder.encodeOpenTrade(
openTradeParams,
async array => await wallet.signMessage(array)
);

Note: That on userMessageEncoder you must encode with the corresponding action encode. Each type of action has a unique encode method. (List the encode methods on the npm package)

The final step is to set up the payload and send it to our endpoint.

const payload = JSON.stringify({
signature,
packedMessage,
functionId
});
const res = await axios.put(CONTRACT_CALL_ENDPOINT, payload);

Response

The response back from our system is the userInteractionNumber(also called tradeId) that was created for the object. Use this id to track the status of the transaction.

{
id: '0xffbb1b35d41ba17fd99eaaa000f82b666bf06b69522db30b2d38809759667201'
}