utils.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { LCDClient, MsgStoreCode, MsgInstantiateContract, MsgExecuteContract, MnemonicKey, isTxError } from '@terra-money/terra.js';
  2. import * as fs from 'fs';
  3. // test1 key from localterra accounts
  4. const mk = new MnemonicKey({
  5. mnemonic: 'notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius'
  6. })
  7. let terra: LCDClient;
  8. let wallet;
  9. export function init_lcd(host_name='http://localhost:1317') {
  10. // connect to localterra
  11. terra = new LCDClient({
  12. URL: host_name,
  13. chainID: 'localterra'
  14. });
  15. wallet = terra.wallet(mk);
  16. }
  17. function delay(ms: number) {
  18. return new Promise( resolve => setTimeout(resolve, ms) );
  19. }
  20. export async function deploy_contract(wasm_file) : Promise<number> {
  21. const storeCode = new MsgStoreCode(
  22. wallet.key.accAddress,
  23. fs.readFileSync(wasm_file).toString('base64')
  24. );
  25. let first_attempt = true;
  26. for (;;) {
  27. try {
  28. const storeCodeTx = await wallet.createAndSignTx({
  29. msgs: [storeCode],
  30. });
  31. const storeCodeTxResult = await terra.tx.broadcast(storeCodeTx);
  32. //console.log(storeCodeTxResult);
  33. if (isTxError(storeCodeTxResult)) {
  34. throw new Error(
  35. `store code failed. code: ${storeCodeTxResult.code}, codespace: ${storeCodeTxResult.codespace}, raw_log: ${storeCodeTxResult.raw_log}`
  36. );
  37. }
  38. const {
  39. store_code: { code_id },
  40. } = storeCodeTxResult.logs[0].eventsByType;
  41. return parseInt(code_id[0], 10);
  42. } catch (err) {
  43. // Only show error from the second time it shows to avoid spamming errors while initialization not yet finished
  44. if (!first_attempt) {
  45. console.log(`Error ${err}`);
  46. if (err.response) {
  47. console.log(err.response.data);
  48. }
  49. }
  50. first_attempt = false;
  51. await delay(5000);
  52. }
  53. }
  54. }
  55. export async function instantiate_contract(code_id: number, initMsg: object) : Promise<string> {
  56. try {
  57. const instantiate = new MsgInstantiateContract(
  58. wallet.key.accAddress,
  59. code_id,
  60. initMsg,
  61. {},
  62. false
  63. );
  64. const instantiateTx = await wallet.createAndSignTx({
  65. msgs: [instantiate],
  66. });
  67. const instantiateTxResult = await terra.tx.broadcast(instantiateTx);
  68. if (isTxError(instantiateTxResult)) {
  69. throw new Error(
  70. `instantiate failed. code: ${instantiateTxResult.code}, codespace: ${instantiateTxResult.codespace}, raw_log: ${instantiateTxResult.raw_log}`
  71. );
  72. return null;
  73. }
  74. const {
  75. instantiate_contract: { contract_address },
  76. } = instantiateTxResult.logs[0].eventsByType;
  77. return contract_address[0];
  78. } catch (err) {
  79. console.log(`Error ${err}`);
  80. if (err.response) {
  81. console.log(err.response.data);
  82. }
  83. return null;
  84. }
  85. }
  86. export async function execute_contract(contract_address: string, msg: object) : Promise<any> {
  87. try {
  88. const execute = new MsgExecuteContract(
  89. wallet.key.accAddress,
  90. contract_address,
  91. { ...msg }, { }
  92. );
  93. const executeTx = await wallet.createAndSignTx({
  94. msgs: [execute]
  95. });
  96. const result = await terra.tx.broadcast(executeTx);
  97. return result;
  98. } catch (err) {
  99. console.log(`Error ${err}`);
  100. if (err.response) {
  101. console.log(err.response.data);
  102. }
  103. return null;
  104. }
  105. }
  106. export async function query_contract(contract_address: string, query: object) : Promise<any> {
  107. const result = await terra.wasm.contractQuery(
  108. contract_address, query
  109. );
  110. return result;
  111. }