sdk/js: add flexible typing for getForeignAssetAptos tokenId param

This commit is contained in:
heyitaki 2023-02-07 12:20:14 +00:00 committed by Evan Gray
parent dc924313b9
commit 0e8f8fa771
2 changed files with 19 additions and 12 deletions

View File

@ -22,7 +22,6 @@ import {
deriveTokenHashFromTokenId,
ensureHexPrefix,
generateSignAndSubmitEntryFunction,
hexToUint8Array,
tryNativeToHexString,
tryNativeToUint8Array,
} from "../../utils";
@ -147,7 +146,7 @@ describe("Aptos NFT SDK tests", () => {
APTOS_NFT_BRIDGE_ADDRESS,
CHAIN_ID_ETH,
tryNativeToUint8Array(ethNft.address, CHAIN_ID_ETH),
hexToUint8Array(BigInt(10).toString(16).padStart(64, "0"))
10n
);
assertIsNotNull(tokenId);
expect(
@ -405,7 +404,8 @@ describe("Aptos NFT SDK tests", () => {
aptosClient,
APTOS_NFT_BRIDGE_ADDRESS,
CHAIN_ID_SOLANA,
new Uint8Array(solanaTransferVaaParsed.tokenAddress)
new Uint8Array(solanaTransferVaaParsed.tokenAddress),
solanaTransferVaaParsed.tokenId
);
assertIsNotNull(tokenData);
expect(tokenData.token_data_id.collection).toBe(
@ -425,7 +425,7 @@ describe("Aptos NFT SDK tests", () => {
);
});
test.only("Transfer multiple tokens from same collection from Ethereum to Aptos", async () => {
test("Transfer multiple tokens from same collection from Ethereum to Aptos", async () => {
const ETH_COLLECTION_NAME = "Test APE 🐒";
// create NFTs on Ethereum
@ -489,14 +489,14 @@ describe("Aptos NFT SDK tests", () => {
APTOS_NFT_BRIDGE_ADDRESS,
CHAIN_ID_ETH,
tryNativeToUint8Array(ethNfts.address, CHAIN_ID_ETH),
hexToUint8Array(BigInt(0).toString(16).padStart(64, "0"))
0n
);
const tokenId2 = await getForeignAssetAptos(
aptosClient,
APTOS_NFT_BRIDGE_ADDRESS,
CHAIN_ID_ETH,
tryNativeToUint8Array(ethNfts.address, CHAIN_ID_ETH),
hexToUint8Array(BigInt(1).toString(16).padStart(64, "0"))
1n
);
assertIsNotNull(tokenId1);
assertIsNotNull(tokenId2);

View File

@ -16,6 +16,7 @@ import {
coalesceChainId,
deriveResourceAccountAddress,
ensureHexPrefix,
hexToUint8Array,
} from "../utils";
/**
@ -130,7 +131,7 @@ export async function getForeignAssetAptos(
nftBridgeAddress: string,
originChain: ChainId | ChainName,
originAddress: Uint8Array,
tokenId?: Uint8Array
tokenId?: Uint8Array | Buffer | bigint
): Promise<TokenTypes.TokenId | null> {
const originChainId = coalesceChainId(originChain);
if (originChainId === CHAIN_ID_APTOS) {
@ -154,10 +155,6 @@ export async function getForeignAssetAptos(
return { token_data_id, property_version };
}
if (!tokenId) {
throw new Error("Invalid token ID");
}
const creatorAddress = await deriveResourceAccountAddress(
nftBridgeAddress,
originChainId,
@ -167,6 +164,16 @@ export async function getForeignAssetAptos(
throw new Error("Could not derive creator account address");
}
if (typeof tokenId === "bigint") {
tokenId = hexToUint8Array(BigInt(tokenId).toString(16).padStart(64, "0"));
}
if (!tokenId) {
throw new Error("Invalid token ID");
}
const tokenIdAsUint8Array = new Uint8Array(tokenId);
// Each creator account should contain a single collection that contains the
// token creation event with the token id that we're looking for.
const PAGE_SIZE = 25;
@ -183,7 +190,7 @@ export async function getForeignAssetAptos(
event = events.find(
(e) =>
ensureHexPrefix((e as CreateTokenDataEvent).data.id.name) ===
HexString.fromUint8Array(tokenId).hex()
HexString.fromUint8Array(tokenIdAsUint8Array).hex()
);
numEvents = events.length;
curr += numEvents;