Last active
February 15, 2023 22:41
-
-
Save afsardo/d69d7eaa017fc42e5efa74a92908b677 to your computer and use it in GitHub Desktop.
Sign and Broadcast Tx w/ PrivateKey - Injective
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Mnemonic from "bitcore-mnemonic" | |
import { | |
BaseAccount, | |
ChainRestAuthApi, | |
createTransactionAndCosmosSignDoc, | |
PrivateKey, | |
TxRestApi, | |
InjectiveDirectEthSecp256k1Wallet, | |
hexToBuff, | |
createTxRawFromSigResponse, | |
MsgExecuteContractCompat, | |
DEFAULT_DERIVATION_PATH | |
} from '@injectivelabs/sdk-ts' | |
(async function() { | |
// Seed phrase - Mnemonic | |
const mnemonic = "[MNEMONIC]" | |
// 1. Derive private key hex from mnemonic | |
// const privateKeyHex = (new Mnemonic(mnemonic)).toHDPrivateKey().derive(DEFAULT_DERIVATION_PATH).privateKey.toBuffer().toString("hex") | |
// 2. Use private key hex directly | |
const privateKeyHex = "[PRIVATE_KEY_HEX]" | |
// Private key from hex | |
const privateKey = PrivateKey.fromHex(privateKeyHex) | |
const bech32Address = privateKey.toBech32() | |
console.log("INJ Signer: Executing tx with address", bech32Address) | |
// Astroport INJ-USDT pool injective testnet address | |
const INJ_USDT_POOL_ADDRESS = "inj13u7zxehmatgj4yhwgjcw5nf8hux067hx8wlx84"; | |
// USDT injective testnet address | |
const USDT_ADDRESS = "peggy0x87aB3B4C8661e07D6372361211B96ed4Dc36B1B5"; | |
// Create messages | |
const messages = [ | |
MsgExecuteContractCompat.fromJSON({ | |
sender: bech32Address, | |
contractAddress: INJ_USDT_POOL_ADDRESS, | |
msg: { | |
swap: { | |
offer_asset: { | |
amount: "100000000", // 100 USDT | |
info: { native_token: { denom: USDT_ADDRESS } }, | |
}, | |
max_spread: "0.1", | |
belief_price: "1185115746.301293090000000000", // Hardcoded from latest simulate query | |
}, | |
}, | |
funds: [{ denom: USDT_ADDRESS, amount: "100000000" }], // 100 USDT | |
}) | |
]; | |
// Injective testnet | |
const CHAIN_ID = "injective-888"; | |
// Injective testnet REST API URL | |
const INJECTIVE_REST_URL = "[INJECTIVE_REST_URL]"; | |
// Prepare tx | |
const chainRestAuthApi = new ChainRestAuthApi(INJECTIVE_REST_URL); | |
const accountDetailsResponse = await chainRestAuthApi.fetchAccount(bech32Address); | |
const baseAccount = BaseAccount.fromRestApi(accountDetailsResponse); | |
const preparedTx = await createTransactionAndCosmosSignDoc({ | |
pubKey: privateKey.toPublicKey().toBase64(), | |
chainId: CHAIN_ID, | |
message: messages.map((msg) => msg.toDirectSign()), | |
sequence: baseAccount.sequence, | |
accountNumber: baseAccount.accountNumber, | |
}); | |
// Setup tx rest api | |
const txRestApi = new TxRestApi(INJECTIVE_REST_URL); | |
// Simulate tx | |
try { | |
const txRaw = preparedTx.txRaw; | |
await txRestApi.simulate(txRaw); | |
} catch(e) { | |
console.log("INJ Signer: Simulate tx error"); | |
throw e; | |
} | |
try { | |
// Sign tx | |
const wallet = await InjectiveDirectEthSecp256k1Wallet.fromKey(hexToBuff(privateKeyHex)); | |
const directSignResponse = await wallet.signDirect(bech32Address, preparedTx.cosmosSignDoc); | |
const signing = createTxRawFromSigResponse(directSignResponse); | |
// Broadcast tx | |
const broadcast = await txRestApi.broadcast(signing); | |
if (broadcast.code !== 0) { | |
throw new Error(broadcast.rawLog); | |
} | |
// Fetch broadcasted tx | |
const response = await txRestApi.fetchTxPoll(broadcast.txHash, 15000); | |
console.log("INJ Signer: Tx response", response); | |
} catch(e) { | |
console.log("INJ Signer: Broadcast tx error"); | |
throw e; | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment