Skip to content

Commit

Permalink
TON v3: _getTokenInfo method
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyAdoniev committed Nov 30, 2023
1 parent df743ec commit 795ba40
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 35 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -792,3 +792,12 @@ P.S. The library is a work in progress. More features will be added soon.
console.log(`The estimated fee on Tezos is: ${feeEstimation} Algos`);
})();
```
## Enabling Bridge version 3 transfers (early beta)
To implement v3 transfers for chain:
1. Specify _v3_chainId_ for the chain in src/consts.ts
2. Specify _v3_bridge_ contract address for the chain params in src/factory/factories.ts
3. Implement _getTokenInfo, getClaimData, lockNFT, claimV3NFT_ methods for the chain helper (see examples - src/helpers/elrond/elrond.ts, src/helpers/evm/web3.ts)
4. In order to allow UI to identify NFT origin chain implement _getNftOrigin_ for the chain helper
3 changes: 3 additions & 0 deletions src/factory/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ export namespace ChainFactoryConfigs {
})
),
nonce: Chain.TON,
proxy: "https://sheltered-crag-76748.herokuapp.com/",
bridgeAddr: "kQBwUu-b4O6qDYq3iDRvsYUnTD6l3WCxLXkv0aH6ywAaPs3c",
burnerAddr: "kQCbH9gGgqJzXuusUVajW_40brrl2fxTYqMkk6HUhJnIgOQA",
xpnftAddr: "EQDji0YH-SNT-qi6o5dQQBLeWL0Xmm46fnqj34EYhOL34WDc",
Expand Down Expand Up @@ -562,6 +563,7 @@ export namespace ChainFactoryConfigs {
bridgeAddr: "kQAhrkiW7pA5eE_7vtz7_AQhHznfqR0VFyTGs4mgyaVLPgfG",
burnerAddr: "kQBo5aNuDXghpZ2u9yMdfaR9oVQEuRddNLCoNg8YgI_k2MOE",
notifier,
proxy: "https://sheltered-crag-76748.herokuapp.com/",
nonce: Chain.TON,
tonweb: new TonWeb(
new TonWeb.HttpProvider("https://toncenter.com/api/v2/jsonRPC", {
Expand Down Expand Up @@ -871,6 +873,7 @@ export namespace ChainFactoryConfigs {
bridgeAddr: "kQBpucKquLw9uwGfLe_KHt65YWnchjoY2VPSsKDTI7JqrcLm",
burnerAddr: "kQBR45AvvaO9a-rGfVkR0INx3JwoR-HngYA3tfctlGM1_Ml7",
notifier,
proxy: "https://sheltered-crag-76748.herokuapp.com/",
tonweb: new TonWeb(
new TonWeb.HttpProvider("https://toncenter.com/api/v2/jsonRPC", {
apiKey:
Expand Down
2 changes: 1 addition & 1 deletion src/helpers/elrond/elrond-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import BigNumber from "bignumber.js";
//import { inspect } from "node:util";
const f = Mnemonic.fromString(
`evidence liberty culture stuff canal minute toward trash boil cry verb recall during citizen social upper budget ranch distance business excite fox icon tool`
``
);
const proxyNetworkProvider = new ProxyNetworkProvider("https://devnet-gateway.multiversx.com");
const apiNetworkProvider = new ApiNetworkProvider("https://devnet2-api.multiversx.com");
Expand Down
5 changes: 0 additions & 5 deletions src/helpers/elrond/elrond.ts
Original file line number Diff line number Diff line change
Expand Up @@ -993,8 +993,6 @@ export async function elrondHelperFactory(
return "";
},
async getTokenInfo(depTrxData) {
console.log(depTrxData, "depTrxData");

const nftData = await multiversexApiService.getTokenInfo(
depTrxData.sourceNftContractAddress,
Number(depTrxData.tokenId).toString(16)
Expand All @@ -1004,8 +1002,6 @@ export async function elrondHelperFactory(
depTrxData.sourceNftContractAddress
);

console.log(collectionData, "collectionData");

return {
metadata: Base64.decode(nftData?.uris?.at(1) || ""),
name: collectionData.name,
Expand All @@ -1025,7 +1021,6 @@ export async function elrondHelperFactory(
if (!sourceNonce) {
throw new Error("Source chain is undefined");
}
console.log(sourceNonce, "sourceNonce in elrond");

const sourceChainHelper = helpers.get(sourceNonce as ChainNonce);

Expand Down
9 changes: 1 addition & 8 deletions src/helpers/evm/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1133,7 +1133,6 @@ export async function web3HelperFactory(
if (!sourceNonce) {
throw new Error("Source chain is undefined");
}
console.log(sourceNonce, "sourceNonce");

const sourceChain = helpers.get(sourceNonce as ChainNonce);

Expand Down Expand Up @@ -1189,11 +1188,7 @@ export async function web3HelperFactory(
console.log(e.message);
return [];
});
console.log(
signatures,
transactionHash,
CHAIN_INFO.get(from.getNonce())?.v3_chainId!
);

if (signatures.length < sigNumber) return getSignatures(tryNumber + 1);
return signatures;
};
Expand Down Expand Up @@ -1224,8 +1219,6 @@ export async function web3HelperFactory(
}
);

console.log(trx);

const tx = await signer.sendTransaction(trx);

await tx.wait();
Expand Down
91 changes: 70 additions & 21 deletions src/helpers/ton/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ import {
BalanceCheck,
GetExtraFees,
WhitelistCheck,
//GetClaimData,
GetClaimData,
LockNFT,
GetTokenInfo,
DepTrxData,
TokenInfo,
} from "../chain";

import { ChainNonce, PreTransfer, ClaimV3NFT } from "../..";
Expand All @@ -38,16 +41,15 @@ import base64url from "base64url";

import { TonClient, Address, beginCell, Dictionary } from "newton";

//import { sign } from "ton-crypto";
import {
ClaimData,
storeClaimData,
SignerAndSignature,
//ClaimNFT721,
storeClaimNFT721,
NftItem,
NftCollection,
} from "./v3types";

import { CHAIN_INFO } from "../../consts";

export type TonSigner = {
Expand Down Expand Up @@ -93,13 +95,16 @@ export type TonHelper = ChainNonceGet &
NftListUtils &
ScVerifyUtils &
ClaimV3NFT<TonSigner, string> &
GetTokenInfo;
GetTokenInfo &
GetClaimData &
LockNFT<TonSigner, TonNft, string>;

export type TonParams = {
tonweb: TonWeb;
notifier: EvNotifier;
nonce: ChainNonce;
bridgeAddr: string;
proxy: string;
burnerAddr: string;
xpnftAddr: string;
feeMargin: FeeMargins;
Expand Down Expand Up @@ -501,48 +506,92 @@ export async function tonHelper(args: TonParams): Promise<TonHelper> {
getScVerifyAddr(address) {
return address.replace(/[^a-zA-Z0-9]/g, "");
},
//TODO: complete getClaimData method for TON
async getClaimData(hash, helpers) {
hash;
//TODO: fetch and decode TON trx;
const decoded = {} as DepTrxData;

const sourceNonce = Array.from(CHAIN_INFO.values()).find(
(c) => c.v3_chainId === decoded.sourceChain
)?.nonce;

const sourceChainHelper = helpers.get(sourceNonce as ChainNonce);

const tokenInfo: TokenInfo = await (
sourceChainHelper as any
).getTokenInfo(decoded);

return {
...tokenInfo,
...decoded,
};
},
async getTokenInfo(depTrxData) {
console.log(args.tonweb.provider.host, "host");
const client = new TonClient({
endpoint: args.tonweb.provider.host,
apiKey: args.tonweb.provider.options.apiKey,
});
const addr = Address.parseFriendly(
depTrxData.sourceNftContractAddress
).address;
console.log(addr);

const nftItem = client.open(NftItem.fromAddress(addr));
const nftData = await nftItem.getGetNftData();
console.log(nftData, "nftData");

let metaDataURL: string = "";
let royalty: string = "0";

if (nftData.collection_address) {
const nftCollection = client.open(
NftCollection.fromAddress(nftData.collection_address)
);

const { collection_content } =
await nftCollection.getGetCollectionData();
const collectionContentSlice = collection_content.asSlice();
collectionContentSlice.loadUint(8);
metaDataURL = collectionContentSlice.loadStringTail();
console.log(metaDataURL, "metaDataURL");
const [collectionData, royaltyData] = await Promise.allSettled([
nftCollection.getGetCollectionData(),
nftCollection.getRoyaltyParams(),
]);

if (collectionData.status === "fulfilled") {
const { collection_content } = collectionData.value;
const collectionContentSlice = collection_content.asSlice();
collectionContentSlice.loadUint(8);
metaDataURL = collectionContentSlice.loadStringTail();
}

if (royaltyData.status === "fulfilled") {
const royaltyParams = royaltyData.value;
const royaltyInNum =
royaltyParams.numerator / royaltyParams.denominator;
const standardRoyalty = royaltyInNum * BigInt(10);
royalty = standardRoyalty.toString();
}
} else {
const individualContentSlice = nftData.individual_content.asSlice();
individualContentSlice.loadBits(8);
metaDataURL = individualContentSlice.loadStringTail();
}
const individualContentSlice = nftData.individual_content.asSlice();
individualContentSlice.loadBits(8);
metaDataURL = individualContentSlice.loadStringTail();

const metaData = (await axios.get(metaDataURL)).data;
const metaData = (
await axios.get(
typeof window !== "undefined" ? args.proxy + metaDataURL : metaDataURL
)
).data;

console.log(metaData);

return {
name: metaData.name || "",
symbol: "",
metadata: "",
royalty: "",
image: "",
metadata: metaDataURL,
royalty,
//image: "",
};
},
//TODO: lock trx in TON
async lockNFT(sender, toChain, id, receiver) {
console.log(sender, toChain, id, receiver);
return "";
},
async claimV3NFT(
sender,
helpers,
Expand Down

0 comments on commit 795ba40

Please sign in to comment.