Skip to content

Commit

Permalink
chore(fix): pinata uploading of casper metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
D4mph1r committed Nov 30, 2024
1 parent 4725d70 commit 4e7426a
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 11 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ node_modules/
.yarn
test/
test.*
yarn-error.log
yarn-error.log
.env
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
"axios": "^1.6.7",
"casper-cep78-js-client": "^1.4.0",
"casper-js-sdk": "^2.13.3",
"dotenv": "^16.4.5",
"ethers": "^6.10.0",
"hashconnect": "^0.2.9",
"near-api-js": "^5.0.0",
Expand Down
73 changes: 70 additions & 3 deletions src/handlers/casper/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
PurseIdentifier,
RuntimeArgs,
} from "casper-js-sdk";
import type { Any } from "../utils/any";
import { pinata } from "../utils/pinata";
import { CLAIM_WASM } from "./claim.wasm";
import { getDeploy } from "./get-deploy";
import { LOCK_WASM } from "./lock.wasm";
Expand Down Expand Up @@ -298,9 +300,13 @@ export function casperHandler({
destinationChain,
to,
tokenId,
metaDataUri,
_metaDataUri,
extraArgs,
) {
const cc = new CasperClient(rpc);
const nftContract = new Contracts.Contract(cc);
nftContract.setContractHash(sourceNft);
const metadata = await getMetaData(nftContract, tokenId);
const nft_storage_exists = await checkStorage(
bc,
sourceNft.replace("hash-", ""),
Expand All @@ -316,7 +322,7 @@ export function casperHandler({
source_nft_contract_address_arg: CLValueBuilder.byteArray(
convertHashStrToHashBuff(sourceNft),
),
metadata_arg: CLValueBuilder.string(metaDataUri),
metadata_arg: CLValueBuilder.string(metadata),
amount: CLValueBuilder.u512(extraArgs?.amount || "110000000000"),
});
const n = new Contracts.Contract(cc);
Expand Down Expand Up @@ -344,7 +350,7 @@ export function casperHandler({
destinationChain,
to,
tokenId,
metaDataUri,
metadata,
extraArgs,
);
}
Expand Down Expand Up @@ -510,3 +516,64 @@ async function checkStorage(bc: Contracts.Contract, sourceNft: string) {

return ret;
}

async function getMetaData(nftContract: Contracts.Contract, tokenId: string) {
// CEP78 = 0, --> metadata_cep78
// NFT721 = 1, --> metadata_nft721
// Raw = 2, --> metadata_raw
// CustomValidated = 3 --> metadata_custom_validated

let data: Any | undefined;

try {
data = (
await nftContract.queryContractDictionary("metadata_cep78", tokenId)
).toJSON();
if (data?.token_uri) {
data = data?.token_uri;
}
console.log("metadata_cep78", data);
} catch (ex) {
try {
data = (
await nftContract.queryContractDictionary("metadata_nft721", tokenId)
).toJSON();
if (data?.token_uri) {
data = data?.token_uri;
}
console.log("metadata_nft721", data.toJSON());
} catch (ex) {
try {
data = (
await nftContract.queryContractDictionary("metadata_raw", tokenId)
).toJSON();
if (data?.token_uri) {
data = data?.token_uri;
}
console.log("metadata_raw", data.toJSON());
} catch (ex) {
try {
data = (
await nftContract.queryContractDictionary(
"metadata_custom_validated",
tokenId,
)
).toJSON();
if (data?.token_uri) {
data = data?.token_uri;
}
console.log("metadata_custom_validated", data.toJSON());
} catch (ex) {}
}
}
}
if (data) {
if (typeof data === "object") {
const pinResponse = await pinata.upload.json(data.toJSON());
const metadata = `https://xpnetwork.infura-ipfs.io/ipfs/ ${pinResponse.IpfsHash}`;
console.log({ metadata });
return metadata;
}
return data;
}
}
15 changes: 9 additions & 6 deletions src/handlers/secret/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { StdSignature, toBase64, validateAddress } from "secretjs";
import { Metadata } from "secretjs/dist/extensions/snip1155/types";
import { Pubkey } from "secretjs/dist/wallet_amino";
import { Lock721, Lock1155 } from "../../contractsTypes/secret/secretBridge";
import { type StdSignature, toBase64, validateAddress } from "secretjs";
import type { Metadata } from "secretjs/dist/extensions/snip1155/types";
import type { Pubkey } from "secretjs/dist/wallet_amino";
import type {
Lock721,
Lock1155,
} from "../../contractsTypes/secret/secretBridge";
import { raise } from "../ton";
import { TokenInfo } from "../types";
import type { TokenInfo } from "../types";
import { pinata } from "../utils";
import {
import type {
GetOwnedTokensResponse,
TNftInfo,
TSecretHandler,
Expand Down
2 changes: 2 additions & 0 deletions src/handlers/utils/any.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
export type Any = any;
4 changes: 3 additions & 1 deletion src/handlers/utils/pinata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { config } from "dotenv";
import { PinataSDK } from "pinata-web3";

config();
export const pinata = new PinataSDK({
pinataJwt: `${""}`,
pinataJwt: `${process.env.PINATA_JWT}`,
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2155,6 +2155,11 @@ dns-packet@^5.6.1:
dependencies:
"@leichtgewicht/ip-codec" "^2.0.1"

dotenv@^16.4.5:
version "16.4.5"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-16.4.5.tgz#cdd3b3b604cb327e286b4762e13502f717cb099f"
integrity sha512-ZmdL2rui+eB2YwhsWzjInR8LldtZHGDoQ1ugH85ppHKwpUHL7j7rN0Ti9NCnGiQbhaZ11FpR+7ao1dNsmduNUg==

[email protected]:
version "1.1.2"
resolved "https://registry.yarnpkg.com/ed25519-hd-key/-/ed25519-hd-key-1.1.2.tgz#168dcf08419694be7bba3319e7d64e4a5cfe5d44"
Expand Down

0 comments on commit 4e7426a

Please sign in to comment.