Skip to main content

Create a Solver

Here is an example of how to create solver response for quote using TypeScript:

Steps:

  1. Build a message response based on the params
  2. Generate a nonce
  3. Serialize the intent
  4. Sign the message

Here params has same type as what you receive from a relay in the "quote" event:

params: {
defuse_asset_identifier_in: string;
defuse_asset_identifier_out: string;
exact_amount_in: string | undefined;
exact_amount_out: string | undefined;
min_deadline_ms: number;
},

Methods​

You can generate the nonce with the generateNonce() method:

const generateNonce = async (): Promise<string> => {
const randomArray = randomBytes(32);
return randomArray.toString('base64');
if (await this.isNonceUsed(nonceString)) { //this step can be skipped but if nonce is already used quote wont be taken into account
return this.generateNonce();
} else {
return nonceString;
}
}
const isNonceUsed = async (nonce: string) => {
const account = getAccount(); //function that will return Account instance(from "near-api-js") of solver Near account
return await account.viewFunction({
contractId: defuseContract,
methodName: 'is_nonce_used',
args: {
account_id: account.accountId,
nonce,
},
});
}

Example​

import bs58 from 'bs58';

const amount = "1000" //calculated amount solver want to propose
const standard = "nep413";
const message = {
signer_id: "...", //account id of solver account that will be used for signing
deadline: {
timestamp: 10000, //timestamp deadline in seconds
},
intents: [
{
intent: 'token_diff',
diff: {
[params.defuse_asset_identifier_in]: !!params.exact_amount_in
? params.exact_amount_in
: amount,
[params.defuse_asset_identifier_out]: `-${
!!params.exact_amount_out ? params.exact_amount_out : amount
}`,
},
},
],
};
const messageStr = JSON.stringify(message);
const nonce = await generateNonce();
const recipient = defuseContract; //for example "intents.near"
const quoteHash = serializeIntent(messageStr, recipient, nonce, standard);
const signature = signMessage(quoteHash);

const resp: IQuoteObject = {
quote_id,
quote_output: {},
signed_data: {
standard,
payload: {
message: messageStr,
nonce,
recipient,
},
signature: `ed25519:${bs58.encode(signature.signature)}`,
public_key: `ed25519:${bs58.encode(signature.publicKey.data)}`,
},
};
if (!params.exact_amount_in) {
resp.quote_output.amount_in = amount;
} else {
resp.quote_output.amount_out = amount;
}
Was this page helpful?