instruction.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. import { ASSOCIATED_TOKEN_PROGRAM_ID, TOKEN_PROGRAM_ID } from '@solana/spl-token';
  2. import { PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js';
  3. import BN from 'bn.js';
  4. import * as borsh from 'borsh';
  5. class Assignable {
  6. constructor(properties) {
  7. for (const [key, value] of Object.entries(properties)) {
  8. this[key] = value;
  9. }
  10. }
  11. }
  12. enum EscrowInstruction {
  13. MakeOffer = 0,
  14. TakeOffer = 1,
  15. }
  16. class MakeOffer extends Assignable {
  17. toBuffer() {
  18. return Buffer.from(borsh.serialize(MakeOfferSchema, this));
  19. }
  20. }
  21. const MakeOfferSchema = new Map([
  22. [
  23. MakeOffer,
  24. {
  25. kind: 'struct',
  26. fields: [
  27. ['instruction', 'u8'],
  28. ['id', 'u64'],
  29. ['token_a_offered_amount', 'u64'],
  30. ['token_b_wanted_amount', 'u64'],
  31. ],
  32. },
  33. ],
  34. ]);
  35. class TakeOffer extends Assignable {
  36. toBuffer() {
  37. return Buffer.from(borsh.serialize(TakeOfferSchema, this));
  38. }
  39. }
  40. const TakeOfferSchema = new Map([
  41. [
  42. TakeOffer,
  43. {
  44. kind: 'struct',
  45. fields: [['instruction', 'u8']],
  46. },
  47. ],
  48. ]);
  49. export function buildMakeOffer(props: {
  50. id: BN;
  51. token_a_offered_amount: BN;
  52. token_b_wanted_amount: BN;
  53. offer: PublicKey;
  54. mint_a: PublicKey;
  55. mint_b: PublicKey;
  56. maker_token_a: PublicKey;
  57. vault: PublicKey;
  58. maker: PublicKey;
  59. payer: PublicKey;
  60. programId: PublicKey;
  61. }) {
  62. const ix = new MakeOffer({
  63. instruction: EscrowInstruction.MakeOffer,
  64. id: props.id,
  65. token_a_offered_amount: props.token_a_offered_amount,
  66. token_b_wanted_amount: props.token_b_wanted_amount,
  67. });
  68. return new TransactionInstruction({
  69. keys: [
  70. {
  71. pubkey: props.offer,
  72. isSigner: false,
  73. isWritable: true,
  74. },
  75. {
  76. pubkey: props.mint_a,
  77. isSigner: false,
  78. isWritable: false,
  79. },
  80. {
  81. pubkey: props.mint_b,
  82. isSigner: false,
  83. isWritable: false,
  84. },
  85. {
  86. pubkey: props.maker_token_a,
  87. isSigner: false,
  88. isWritable: true,
  89. },
  90. {
  91. pubkey: props.vault,
  92. isSigner: false,
  93. isWritable: true,
  94. },
  95. {
  96. pubkey: props.maker,
  97. isSigner: true,
  98. isWritable: true,
  99. },
  100. {
  101. pubkey: props.payer,
  102. isSigner: true,
  103. isWritable: true,
  104. },
  105. {
  106. pubkey: TOKEN_PROGRAM_ID,
  107. isSigner: false,
  108. isWritable: false,
  109. },
  110. {
  111. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  112. isSigner: false,
  113. isWritable: false,
  114. },
  115. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  116. ],
  117. programId: props.programId,
  118. data: ix.toBuffer(),
  119. });
  120. }
  121. export function buildTakeOffer(props: {
  122. offer: PublicKey;
  123. mint_a: PublicKey;
  124. mint_b: PublicKey;
  125. maker_token_b: PublicKey;
  126. taker_token_a: PublicKey;
  127. taker_token_b: PublicKey;
  128. vault: PublicKey;
  129. taker: PublicKey;
  130. maker: PublicKey;
  131. payer: PublicKey;
  132. programId: PublicKey;
  133. }) {
  134. const ix = new TakeOffer({
  135. instruction: EscrowInstruction.TakeOffer,
  136. });
  137. return new TransactionInstruction({
  138. keys: [
  139. {
  140. pubkey: props.offer,
  141. isSigner: false,
  142. isWritable: true,
  143. },
  144. {
  145. pubkey: props.mint_a,
  146. isSigner: false,
  147. isWritable: false,
  148. },
  149. {
  150. pubkey: props.mint_b,
  151. isSigner: false,
  152. isWritable: false,
  153. },
  154. {
  155. pubkey: props.maker_token_b,
  156. isSigner: false,
  157. isWritable: true,
  158. },
  159. {
  160. pubkey: props.taker_token_a,
  161. isSigner: false,
  162. isWritable: true,
  163. },
  164. {
  165. pubkey: props.taker_token_b,
  166. isSigner: false,
  167. isWritable: true,
  168. },
  169. {
  170. pubkey: props.vault,
  171. isSigner: false,
  172. isWritable: true,
  173. },
  174. {
  175. pubkey: props.maker,
  176. isSigner: false,
  177. isWritable: false,
  178. },
  179. {
  180. pubkey: props.taker,
  181. isSigner: true,
  182. isWritable: true,
  183. },
  184. {
  185. pubkey: props.payer,
  186. isSigner: true,
  187. isWritable: true,
  188. },
  189. {
  190. pubkey: TOKEN_PROGRAM_ID,
  191. isSigner: false,
  192. isWritable: false,
  193. },
  194. {
  195. pubkey: ASSOCIATED_TOKEN_PROGRAM_ID,
  196. isSigner: false,
  197. isWritable: false,
  198. },
  199. { pubkey: SystemProgram.programId, isSigner: false, isWritable: false },
  200. ],
  201. programId: props.programId,
  202. data: ix.toBuffer(),
  203. });
  204. }