Skip to content

Commit

Permalink
feat (initia) add swap feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Widiskel committed May 27, 2024
1 parent 919f9e5 commit 4b1022f
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 6 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ import { account } from "./src/account.js";
} else {
await routine.sendOneInitToOther();
}
console.log("2. Claim EXP");
await routine.claimExp();

console.log("2. Swap 1 INIT to USDC");
await routine.swap();

// console.log("3. Claim EXP");
// await routine.claimExp();
});
} catch (error) {
console.log("Error During executing bot", error);
Expand Down
84 changes: 81 additions & 3 deletions src/initia.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as initia from "@initia/initia.js";
import * as initiaRepo from "./repository/initia_repo.js";
import { AppConstant } from "./utils/constant.js";

let lcd;
let chainId;
Expand Down Expand Up @@ -57,7 +58,7 @@ async function claimExp() {

const msg = new initia.MsgExecute();
msg.function_name = "claim_point";
msg.module_address = "0x9065fda28f52bb14ade545411f02e8e07a9cb4ba";
msg.module_address = AppConstant.CLAIMPOINTMODULEADDRESS;
msg.module_name = "initia_xp";
msg.sender = address;
msg.type_args = [];
Expand Down Expand Up @@ -89,7 +90,7 @@ async function sendToken() {
console.log();
const msg = new initia.MsgSend(
address, // sender address
"init1gadzrjcp3ef90yka3sz2r6tf4wrjdhe2qr0hyp", // recipient address
AppConstant.WIDISKELTESTNETADDRESS, // recipient address
"1000000uinit" // 1 Init
);

Expand All @@ -99,6 +100,83 @@ async function sendToken() {
}
}

async function swap() {
try {
// Args INITIA > USDC
var args = [
initia.bcs
.address()
.serialize(AppConstant.INITIALIQUIDITYADDRESS)
.toBase64(),
initia.bcs
.address()
.serialize(AppConstant.INITIAMETADATAADDRESS)
.toBase64(),
initia.bcs.u64().serialize(1000000).toBase64(), // 1 INITIA
];
const initToUsdcSimulation = await lcd.move.viewFunction(
"0x1",
"dex",
"get_swap_simulation",
[],
args
);
args.push(initia.bcs.u64().serialize(initToUsdcSimulation).toBase64());

const initiaToUsdcMsg = new initia.MsgExecute();
initiaToUsdcMsg.function_name = "swap_script";
initiaToUsdcMsg.module_address = "0x1";
initiaToUsdcMsg.module_name = "dex";
initiaToUsdcMsg.sender = address;
initiaToUsdcMsg.type_args = [];
initiaToUsdcMsg.args = args;
// console.log(initiaToUsdcMsg);
await signAndBroadcast(initiaToUsdcMsg);
console.log(
`Successfully Swap 1 Init To ${initToUsdcSimulation / 1000000} USDC`
);

// Args USDC > INIT
args = [
initia.bcs
.address()
.serialize(AppConstant.INITIALIQUIDITYADDRESS)
.toBase64(),
initia.bcs
.address()
.serialize(AppConstant.USDCMETADATAADDRESS)
.toBase64(),
initia.bcs.u64().serialize(initToUsdcSimulation).toBase64(), // SWAPPED USDC
];
const usdcToInitSimulation = await lcd.move.viewFunction(
"0x1",
"dex",
"get_swap_simulation",
[],
args
);
args.push(initia.bcs.u64().serialize(usdcToInitSimulation).toBase64());

const usdcToInitiaMsg = new initia.MsgExecute();
usdcToInitiaMsg.function_name = "swap_script";
usdcToInitiaMsg.module_address = "0x1";
usdcToInitiaMsg.module_name = "dex";
usdcToInitiaMsg.sender = address;
usdcToInitiaMsg.type_args = [];
usdcToInitiaMsg.args = args;
// console.log(usdcToInitiaMsg);

await signAndBroadcast(usdcToInitiaMsg);
console.log(
`Successfully Swap ${initToUsdcSimulation / 1000000} To ${
usdcToInitSimulation / 1000000
} INIT`
);
} catch (error) {
throw error;
}
}

async function signAndBroadcast(msg) {
try {
const signedTx = await wallet.createAndSignTx({
Expand All @@ -114,4 +192,4 @@ async function signAndBroadcast(msg) {
}
}

export { initiation, queryBalance, claimExp, sendToken };
export { initiation, queryBalance, claimExp, sendToken, swap };
9 changes: 8 additions & 1 deletion src/initia_routine.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,12 @@ async function claimExp() {
console.log("Error during claiming exp : ", error.response.data.message);
}
}
async function swap() {
try {
await initia.swap();
} catch (error) {
console.log("Error during swaping initia : ", error);
}
}

export { sendOneInitToOther, claimExp };
export { sendOneInitToOther, claimExp, swap };
13 changes: 13 additions & 0 deletions src/utils/constant.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class AppConstant {
static CLAIMPOINTMODULEADDRESS = "0x9065fda28f52bb14ade545411f02e8e07a9cb4ba";
static WIDISKELTESTNETADDRESS = "init1gadzrjcp3ef90yka3sz2r6tf4wrjdhe2qr0hyp";

static INITIALIQUIDITYADDRESS =
"0xdbf06c48af3984ec6d9ae8a9aa7dbb0bb1e784aa9b8c4a5681af660cf8558d7d";
static INITIAMETADATAADDRESS =
"0x8e4733bdabcf7d4afc3d14f0dd46c9bf52fb0fce9e4b996c939e195b8bc891d9";
static USDCMETADATAADDRESS =
"0x29824d952e035490fae7567deea5f15b504a68fa73610063c160ab1fa87dd609";
}

export { AppConstant };

0 comments on commit 4b1022f

Please sign in to comment.