Skip to content

Commit

Permalink
sdk/js: Injective refactor
Browse files Browse the repository at this point in the history
Updated injectivelabs packages, refactored code, added utility functions
  • Loading branch information
kev1n-peters authored and evan-gray committed Nov 9, 2022
1 parent 51eac51 commit d00c8ba
Show file tree
Hide file tree
Showing 14 changed files with 843 additions and 252 deletions.
8 changes: 8 additions & 0 deletions sdk/js/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## 0.9.1

### Added

queryExternalIdInjective

parseSmartContractStateResponse

## 0.9.0

### Added
Expand Down
849 changes: 721 additions & 128 deletions sdk/js/package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions sdk/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@certusone/wormhole-sdk",
"version": "0.9.0",
"version": "0.9.1",
"description": "SDK for interacting with Wormhole",
"homepage": "https://wormhole.com",
"main": "./lib/cjs/index.js",
Expand Down Expand Up @@ -40,8 +40,8 @@
"license": "Apache-2.0",
"devDependencies": {
"@improbable-eng/grpc-web-node-http-transport": "^0.15.0",
"@injectivelabs/networks": "^1.0.12",
"@injectivelabs/tx-ts": "^1.0.22",
"@injectivelabs/networks": "^1.0.37",
"@injectivelabs/tx-ts": "^1.0.43",
"@openzeppelin/contracts": "^4.2.0",
"@typechain/ethers-v5": "^7.0.1",
"@types/elliptic": "^6.4.14",
Expand All @@ -63,7 +63,7 @@
"dependencies": {
"@certusone/wormhole-sdk-proto-web": "^0.0.5",
"@certusone/wormhole-sdk-wasm": "^0.0.1",
"@injectivelabs/sdk-ts": "^1.0.75",
"@injectivelabs/sdk-ts": "^1.0.211",
"@project-serum/anchor": "^0.25.0",
"@solana/spl-token": "^0.3.5",
"@solana/web3.js": "^1.66.2",
Expand Down
80 changes: 31 additions & 49 deletions sdk/js/src/cosmwasm/query.testnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@ import {
privateKeyToPublicKeyBase64,
} from "@injectivelabs/sdk-ts";
import { createTransaction, MsgArg, TxGrpcClient } from "@injectivelabs/tx-ts";
import { PrivateKey } from "@injectivelabs/sdk-ts/dist/local";
import { PrivateKey } from "@injectivelabs/sdk-ts";
import { expect, test } from "@jest/globals";
import {
attestFromAlgorand,
attestFromInjective,
CHAIN_ID_ALGORAND,
CHAIN_ID_BSC,
CHAIN_ID_INJECTIVE,
coalesceChainId,
CONTRACTS,
Expand Down Expand Up @@ -121,11 +122,11 @@ test.skip("testnet - injective query token bridge", async () => {
Buffer.from(
JSON.stringify({
wrapped_registry: {
chain: coalesceChainId("injective"),
address: tryNativeToHexString(
"inj10jc4vr9vfq0ykkmfvfgz430w8z6hwdlqhdw9l8",
"injective"
),
chain: CHAIN_ID_BSC,
address: Buffer.from(
"000000000000000000000000ae13d989dac2f0debff460ac112a837c89baa7cd",
"hex"
).toString("base64"),
},
})
).toString("base64")
Expand Down Expand Up @@ -179,22 +180,19 @@ test.skip("testnet - injective attest native asset", async () => {

/** Append Signatures */
txRaw.setSignaturesList([signature]);
const txService = new TxGrpcClient({
txRaw,
endpoint: network.sentryGrpcApi,
});
const txService = new TxGrpcClient(network.sentryGrpcApi);

console.log("Simulating transaction...");
/** Simulate transaction */
const simulationResponse = await txService.simulate();
const simulationResponse = await txService.simulate(txRaw);
console.log(
`Transaction simulation response: ${JSON.stringify(
simulationResponse.gasInfo
)}`
);

/** Broadcast transaction */
const txResponse = await txService.broadcast();
const txResponse = await txService.broadcast(txRaw);
console.log(
`Broadcasted transaction hash: ${JSON.stringify(txResponse.txhash)}`
);
Expand Down Expand Up @@ -237,7 +235,7 @@ test.skip("testnet - injective attest native asset", async () => {
});
test.skip("testnet - injective attest foreign asset", async () => {
const tba = CONTRACTS.TESTNET.injective.token_bridge;
const wallet = "inj180rl9ezc4389t72pc3vvlkxxs5d9jx60w9eeu3";
const wallet = "inj13un2qqjaenrvlsr605u82c5q5y8zjkkhdgcetq";
const foreignAssetAddress = "inj13772jvadyx4j0hrlfh4jzk0v39k8uyfxrfs540";
const result = await attestFromInjective(tba, wallet, foreignAssetAddress);
console.log("token", JSON.stringify(result.params.msg));
Expand Down Expand Up @@ -270,21 +268,18 @@ test.skip("testnet - injective attest foreign asset", async () => {

/** Append Signatures */
txRaw.setSignaturesList([signature]);
const txService = new TxGrpcClient({
txRaw,
endpoint: network.sentryGrpcApi,
});
const txService = new TxGrpcClient(network.sentryGrpcApi);

/** Simulate transaction */
const simulationResponse = await txService.simulate();
const simulationResponse = await txService.simulate(txRaw);
console.log(
`Transaction simulation response: ${JSON.stringify(
simulationResponse.gasInfo
)}`
);

/** Broadcast transaction */
const txResponse = await txService.broadcast();
const txResponse = await txService.broadcast(txRaw);
console.log(
`Broadcasted transaction hash: ${JSON.stringify(txResponse.txhash)}`
);
Expand All @@ -297,14 +292,13 @@ test.skip("testnet - injective get foreign asset", async () => {
const network = getNetworkInfo(getEndPoint());
const client = new ChainGrpcWasmApi(network.sentryGrpcApi);
// const foreignAssetAddress = "inj10jc4vr9vfq0ykkmfvfgz430w8z6hwdlqhdw9l8";
const foreignAssetAddress = "inj13772jvadyx4j0hrlfh4jzk0v39k8uyfxrfs540";
const ateArray = tryNativeToUint8Array(foreignAssetAddress, "injective");
console.log("wormhole address:", ateArray);
const foreignAssetAddress =
"000000000000000000000000ae13d989dac2f0debff460ac112a837c89baa7cd";
const result = await getForeignAssetInjective(
tba,
client,
CHAIN_ID_INJECTIVE,
ateArray
CHAIN_ID_BSC,
hexToUint8Array(foreignAssetAddress)
);
console.log("result", result);

Expand Down Expand Up @@ -436,14 +430,11 @@ test.skip("testnet - injective submit a vaa", async () => {
/** Append Signatures */
txRaw.setSignaturesList([signature]);

const txService = new TxGrpcClient({
txRaw,
endpoint: network.sentryGrpcApi,
});
const txService = new TxGrpcClient(network.sentryGrpcApi);

console.log("simulate transaction...");
/** Simulate transaction */
const simulationResponse = await txService.simulate();
const simulationResponse = await txService.simulate(txRaw);
console.log(
`Transaction simulation response: ${JSON.stringify(
simulationResponse.gasInfo
Expand All @@ -452,7 +443,7 @@ test.skip("testnet - injective submit a vaa", async () => {

console.log("broadcast transaction...");
/** Broadcast transaction */
const txResponse = await txService.broadcast();
const txResponse = await txService.broadcast(txRaw);
console.log("txResponse", txResponse);

if (txResponse.code !== 0) {
Expand Down Expand Up @@ -534,14 +525,11 @@ test.skip("testnet - injective submit a vaa", async () => {
/** Append Signatures */
txRaw.setSignaturesList([sig]);

const txService = new TxGrpcClient({
txRaw,
endpoint: network.sentryGrpcApi,
});
const txService = new TxGrpcClient(network.sentryGrpcApi);

console.log("simulate transaction...");
/** Simulate transaction */
const simulationResponse = await txService.simulate();
const simulationResponse = await txService.simulate(txRaw);
console.log(
`Transaction simulation response: ${JSON.stringify(
simulationResponse.gasInfo
Expand All @@ -550,7 +538,7 @@ test.skip("testnet - injective submit a vaa", async () => {

console.log("broadcast transaction...");
/** Broadcast transaction */
const txResponse = await txService.broadcast();
const txResponse = await txService.broadcast(txRaw);
console.log("txResponse", txResponse);

if (txResponse.code !== 0) {
Expand Down Expand Up @@ -629,7 +617,7 @@ test.skip("testnet - injective submit a vaa", async () => {

test.skip("Attest and transfer token from Injective to Algorand", async () => {
const Asset: string = "inj";
const walletPKHash: string = process.env.INJ_PK_HASH || "";
const walletPKHash: string = process.env.ETH_KEY || "";
const walletPK = PrivateKey.fromPrivateKey(walletPKHash);
const walletInjAddr = walletPK.toBech32();
const walletPublicKey = privateKeyToPublicKeyBase64(
Expand Down Expand Up @@ -671,14 +659,11 @@ test.skip("Attest and transfer token from Injective to Algorand", async () => {
/** Append Signatures */
txRaw.setSignaturesList([signedMsg]);

const txService = new TxGrpcClient({
txRaw,
endpoint: network.sentryGrpcApi,
});
const txService = new TxGrpcClient(network.sentryGrpcApi);

console.log("simulate transaction...");
/** Simulate transaction */
const simulationResponse = await txService.simulate();
const simulationResponse = await txService.simulate(txRaw);
console.log(
`Transaction simulation response: ${JSON.stringify(
simulationResponse.gasInfo
Expand All @@ -687,7 +672,7 @@ test.skip("Attest and transfer token from Injective to Algorand", async () => {

console.log("broadcast transaction...");
/** Broadcast transaction */
const txResponse = await txService.broadcast();
const txResponse = await txService.broadcast(txRaw);
console.log("txResponse", txResponse);

if (txResponse.code !== 0) {
Expand Down Expand Up @@ -800,22 +785,19 @@ test.skip("Attest and transfer token from Injective to Algorand", async () => {
/** Append Signatures */
txRaw.setSignaturesList([signedMsg]);

const txService = new TxGrpcClient({
txRaw,
endpoint: network.sentryGrpcApi,
});
const txService = new TxGrpcClient(network.sentryGrpcApi);

console.log("simulate transaction...");
/** Simulate transaction */
const simulationResponse = await txService.simulate();
const simulationResponse = await txService.simulate(txRaw);
console.log(
`Transaction simulation response: ${JSON.stringify(
simulationResponse.gasInfo
)}`
);
console.log("broadcast transaction...");
/** Broadcast transaction */
const txResponse = await txService.broadcast();
const txResponse = await txService.broadcast(txRaw);
console.log("txResponse", txResponse);

if (txResponse.code !== 0) {
Expand Down
11 changes: 4 additions & 7 deletions sdk/js/src/token_bridge/attest.testnet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import {
ChainRestAuthApi,
DEFAULT_STD_FEE,
MsgExecuteContract,
PrivateKey,
privateKeyToPublicKeyBase64,
} from "@injectivelabs/sdk-ts";
import {
createTransaction,
TxClient,
TxGrpcClient,
} from "@injectivelabs/tx-ts";
import { PrivateKey } from "@injectivelabs/sdk-ts/dist/local";
import { test } from "@jest/globals";
import { CONTRACTS } from "..";

Expand Down Expand Up @@ -83,14 +83,11 @@ test.skip("testnet - injective attest native token", async () => {
console.log("Calculate hash");
console.log(`Transaction Hash: ${await TxClient.hash(txRaw)}`);

const txService = new TxGrpcClient({
txRaw,
endpoint: network.sentryGrpcApi,
});
const txService = new TxGrpcClient(network.sentryGrpcApi);

/** Simulate transaction */
console.log("Simulate transaction");
const simulationResponse = await txService.simulate();
const simulationResponse = await txService.simulate(txRaw);
console.log(
`Transaction simulation response: ${JSON.stringify(
simulationResponse.gasInfo
Expand All @@ -99,7 +96,7 @@ test.skip("testnet - injective attest native token", async () => {

/** Broadcast transaction */
console.log("Broadcast transaction");
const txResponse = await txService.broadcast();
const txResponse = await txService.broadcast(txRaw);
console.log(
`Broadcasted transaction hash: ${JSON.stringify(txResponse.txhash)}`
);
Expand Down
1 change: 0 additions & 1 deletion sdk/js/src/token_bridge/attest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
OnApplicationComplete,
SuggestedParams,
} from "algosdk";
import { Account as nearAccount } from "near-api-js";
import BN from "bn.js";
import { ethers, PayableOverrides } from "ethers";
import { isNativeDenom } from "../terra";
Expand Down
10 changes: 3 additions & 7 deletions sdk/js/src/token_bridge/getForeignAsset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
coalesceChainId,
getAssetFullyQualifiedType,
coalesceModuleAddress,
parseSmartContractStateResponse,
} from "../utils";
import { Provider } from "near-api-js/lib/providers";
import { LCDClient as XplaLCDClient } from "@xpla/xpla.js";
Expand Down Expand Up @@ -97,13 +98,8 @@ export async function getForeignAssetInjective(
})
).toString("base64")
);
let result: any = null;
if (typeof queryResult.data === "string") {
result = JSON.parse(
Buffer.from(queryResult.data, "base64").toString("utf-8")
);
}
return result.address;
const parsed = parseSmartContractStateResponse(queryResult);
return parsed.address;
} catch (e) {
return null;
}
Expand Down
15 changes: 7 additions & 8 deletions sdk/js/src/token_bridge/getIsTransferCompleted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import axios from "axios";
import { ethers } from "ethers";
import { fromUint8Array } from "js-base64";
import { redeemOnTerra } from ".";
import { ensureHexPrefix, TERRA_REDEEMED_CHECK_WALLET_ADDRESS } from "..";
import {
ensureHexPrefix,
parseSmartContractStateResponse,
TERRA_REDEEMED_CHECK_WALLET_ADDRESS,
} from "..";
import { getClaim } from "../solana/wormhole";
import {
BITS_PER_KEY,
Expand Down Expand Up @@ -125,13 +129,8 @@ export async function getIsTransferCompletedInjective(
})
).toString("base64")
);
if (typeof queryResult.data === "string") {
const result = JSON.parse(
Buffer.from(queryResult.data, "base64").toString("utf-8")
);
return result.is_redeemed;
}
return false;
const parsed = parseSmartContractStateResponse(queryResult);
return parsed.is_redeemed;
}

export async function getIsTransferCompletedXpla(
Expand Down
Loading

0 comments on commit d00c8ba

Please sign in to comment.