Skip to content

Commit

Permalink
multivers v3: fix encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexeyAdoniev committed Nov 26, 2023
1 parent 5f298cf commit 2e761f1
Show file tree
Hide file tree
Showing 14 changed files with 324 additions and 146 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ src/test.ts
.env*
test*
*.DS_Store
src/helpers/elrond/elrond-test.ts
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
"@make-software/ces-js-parser": "^1.3.1",
"@multiversx/sdk-core": "^12.9.0",
"@multiversx/sdk-extension-provider": "^3.0.0",
"@multiversx/sdk-network-providers": "^2.1.0",
"@multiversx/sdk-network-providers": "^2.2.0",
"@multiversx/sdk-wallet": "^4.2.0",
"@project-serum/anchor": "^0.25.0-beta.1",
"@randlabs/myalgo-connect": "^1.1.1",
Expand All @@ -61,7 +61,7 @@
"@walletconnect/client": "^1.7.0",
"algosdk": "=2.2.0",
"aptos": "^1.6.0",
"axios": "^0.21.1",
"axios": "^1.6.2",
"base64url": "^3.0.1",
"bignumber.js": "=9.0.1",
"bn.js": "^5.2.1",
Expand Down
2 changes: 1 addition & 1 deletion src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export namespace AppConfigs {
heartbeatUri: "https://tools.xp.network/testnet-pinger/",
wrappedNftPrefix: "https://tools.xp.network/testnet-wnft/", //"https://bridge-wnftapi.herokuapp.com/",
scVerifyUri: "https://tools.xp.network/testnet-sc-verify/",
storageContract: "0x54194d68528B72EEb96d0a7ce7ffabaF14bD250D", //"0x8F1fd3a5DbBd5659579aE7d9b258CC6CbcB3e53d", //"0xDb2a42f40158B1Cb29703e2a95a6fa3094294f05",
storageContract: "0x0263A038014505881E33d4201950fa11c29793F3", //"0x8F1fd3a5DbBd5659579aE7d9b258CC6CbcB3e53d", //"0xDb2a42f40158B1Cb29703e2a95a6fa3094294f05",
storegeNetwork: "https://optimism-goerli.publicnode.com",
network: "testnet",
};
Expand Down
35 changes: 20 additions & 15 deletions src/consts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
ElrondParams,
ElrondHelper,
} from "./helpers/elrond/elrond";

import { tronHelperFactory, TronParams, TronHelper } from "./helpers/tron";
import { web3HelperFactory, Web3Params, Web3Helper } from "./helpers/evm/web3";
import { HederaHelperFactory } from "./helpers/hedera/hedera_refactor";
Expand Down Expand Up @@ -47,7 +48,7 @@ export enum TestNetRpcUri {
ELROND = "https://devnet-gateway.multiversx.com",
HECO = "https://http-testnet.hecochain.com",
BSC = "https://data-seed-prebsc-1-s1.binance.org:8545",
ROPSTEN = "https://ultra-light-patina.ethereum-sepolia.discover.quiknode.pro/6f98178f32c668af8ee4bb1cc4b8b9308e29367b/",
ROPSTEN = "https://ethereum-sepolia.blockpi.network/v1/rpc/public", //"https://ultra-light-patina.ethereum-sepolia.discover.quiknode.pro/6f98178f32c668af8ee4bb1cc4b8b9308e29367b/",
AVALANCHE = "https://api.avax-test.network/ext/bc/C/rpc",
POLYGON = "https://polygon-mumbai.blockpi.network/v1/rpc/public",
FANTOM = "https://rpc.testnet.fantom.network/",
Expand Down Expand Up @@ -287,20 +288,8 @@ export enum ChainType {
}

export const CHAIN_INFO: ChainInfo = new Map();
CHAIN_INFO.set(Chain.ELROND, {
name: "Elrond",
nonce: 2,
v3_chainId: v3_ChainId.ELROND,
decimals: Decimals.EGLD,
constructor: elrondHelperFactory,
blockExplorerUrl: "https://explorer.elrond.com/transactions/",
blockExplorerUrlAddr: "https://explorer.elrond.com/address/",
tnBlockExplorerUrl: "https://devnet-explorer.multiversx.com/transactions/",
tnBlockExplorerUrlAddr: "https://devnet-explorer.multiversx.com/accounts/",
currency: SupportedCurrency.EGLD,
currencySymbol: SupportedCurrencyName.EGLD,
type: ChainType.ELROND,
});
console.log(elrondHelperFactory, "elrondHelperFactory");

CHAIN_INFO.set(Chain.HECO, {
name: "HECO",
nonce: 3,
Expand Down Expand Up @@ -935,3 +924,19 @@ CHAIN_INFO.set(Chain.FINDORA, {
tnBlockExplorerUrlAddr: "https://testnet-anvil.evm.findorascan.io/address/",
tnChainId: 2153,
});
setTimeout(() => {
CHAIN_INFO.set(Chain.ELROND, {
name: "Elrond",
nonce: 2,
v3_chainId: v3_ChainId.ELROND,
decimals: Decimals.EGLD,
constructor: elrondHelperFactory,
blockExplorerUrl: "https://explorer.elrond.com/transactions/",
blockExplorerUrlAddr: "https://explorer.elrond.com/address/",
tnBlockExplorerUrl: "https://devnet-explorer.multiversx.com/transactions/",
tnBlockExplorerUrlAddr: "https://devnet-explorer.multiversx.com/accounts/",
currency: SupportedCurrency.EGLD,
currencySymbol: SupportedCurrencyName.EGLD,
type: ChainType.ELROND,
});
}, 300);
6 changes: 3 additions & 3 deletions src/factory/factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export namespace ChainFactoryConfigs {
elrondApi: "https://devnet-api.multiversx.com",
elrondIndex: "https://devnet-index.multiversx.com",
v3_bridge:
"erd1qqqqqqqqqqqqqpgqmzkpcys59zu6txccjkl7v3dusch5plc36n9sfla9y0", //"erd1qqqqqqqqqqqqqpgqthnm9plsj3he2jqky672vty4adg02hg26n9s3g8rdm", //"erd1qqqqqqqqqqqqqpgqghvly0npf6ewpmzh47ud76ssh6nppu4e7hnses7qlz",
"erd1qqqqqqqqqqqqqpgqtsw8s3evjjyqqa2j2tfn9yvufqskdv236n9s2a06h9", //"erd1qqqqqqqqqqqqqpgqthnm9plsj3he2jqky672vty4adg02hg26n9s3g8rdm", //"erd1qqqqqqqqqqqqqpgqghvly0npf6ewpmzh47ud76ssh6nppu4e7hnses7qlz",
notifier,
nonce: Chain.ELROND,
feeMargin,
Expand Down Expand Up @@ -286,7 +286,7 @@ export namespace ChainFactoryConfigs {
erc1155Minter: "0x5dA3b7431f4581a7d35aEc2f3429174DC0f2A2E1",
erc721Minter: "0x97CD6fD6cbFfaa24f5c858843955C2601cc7F2b9",
erc1155_addr: "0x22A23E09dBa96c8e10A57a53Abc1227fAe06b4D1", //"0xb5278A4808e2345A3B9d08bAc8909A121aFaEBB3",
v3_bridge: "0xF468FdB229acA4Df52e1A280b84221F7e1BE99bf", //"0x3fCA19cd73F774E13433a86C0E7996E3D291d29E", //"0x64F90C25A0802E64D0593518fa26FfBc9D78564F",
v3_bridge: "0xBBF8B4601ebE9d14aDa11220749F71030012A57a", //"0xF468FdB229acA4Df52e1A280b84221F7e1BE99bf", //"0x3fCA19cd73F774E13433a86C0E7996E3D291d29E", //"0x64F90C25A0802E64D0593518fa26FfBc9D78564F",
nonce: Chain.BSC,
feeMargin,
},
Expand Down Expand Up @@ -323,7 +323,7 @@ export namespace ChainFactoryConfigs {
erc1155Minter: "0x539daC37CF7d918fe93897D4C02277a220cE3a6e",
erc721Minter: "0x833A2efC56B3625829727bb9D15F70cd2971Cd84",
minter_addr: "0xDb199384BA327126653Ab8AcAB1fB3D5d88E2Ee2",
v3_bridge: "0xbc24902a64A50a047F621BB0Cf5efDFb0ae441E0",
v3_bridge: "0xbcA0587e714Ed9B312aec735AbE28C763BfC6442",
nonce: Chain.ETHEREUM,
feeMargin,
},
Expand Down
6 changes: 6 additions & 0 deletions src/factory/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { NftInfo, FullChain } from "..";
import { CHAIN_INFO, ChainType, Chain } from "../consts";
import axios from "axios";
import BigNumber from "bignumber.js";
import { V3_ChainId } from "../type-utils";

export const _headers = {
"Content-Type": "application/json",
Expand Down Expand Up @@ -167,3 +168,8 @@ export const decodeBase64Array = (encodedArray: string[]): string[] | null => {
return Buffer.from(encodedString, "base64").toString("utf-8");
});
};

export const v3BridgeIdToNonce = (id: V3_ChainId) =>
Array.from(CHAIN_INFO.values())
.find((c) => c.v3_chainId === id)!
.nonce.toString();
4 changes: 4 additions & 0 deletions src/helpers/chain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ export interface GetTokenInfo {
getTokenInfo(depTrxData: DepTrxData): Promise<TokenInfo>;
}

export interface GetNftOrigin {
getNftOrigin(address: string): Promise<{ origin: string; contract?: string }>;
}

export interface ClaimV3NFT<Signer, Resp> {
claimV3NFT(
sender: Signer,
Expand Down
173 changes: 141 additions & 32 deletions src/helpers/elrond/elrond-test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//import { Mnemonic } from "@elrondnetwork/erdjs";

/*import {
/*
import {
AbiRegistry,
SmartContract,
Address,
Expand All @@ -9,13 +9,26 @@
TransactionPayload,
ResultsParser,
BigUIntValue,
StructType,
BytesType,
FieldDefinition,
BigUIntType,
AddressType,
Field,
BytesValue,
Struct,
VariadicValue,
AddressValue,
} from "@multiversx/sdk-core";
import { ProxyNetworkProvider, ApiNetworkProvider } from "@multiversx/sdk-network-providers";
import { UserSigner, Mnemonic } from "@multiversx/sdk-wallet";
import abi from "./v3Bridge_abi.json";
import { decodeBase64Array } from "../../factory";
import axios from "axios";
import { Nonce } from "@multiversx/sdk-network-providers/out/primitives";
//import { decodeBase64Array } from "../../factory";
//import axios from "axios";
import BigNumber from "bignumber.js";
import { decodeBase64Array } from "../../factory";
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`
Expand All @@ -24,13 +37,13 @@ const proxyNetworkProvider = new ProxyNetworkProvider("https://devnet-gateway.mu
const apiNetworkProvider = new ApiNetworkProvider("https://devnet2-api.multiversx.com");
const signer = new UserSigner(f.deriveKey());
const bridgeAddress = new Address("erd1qqqqqqqqqqqqqpgqghvly0npf6ewpmzh47ud76ssh6nppu4e7hnses7qlz");
const bridgeAddress = new Address("erd1qqqqqqqqqqqqqpgqtsw8s3evjjyqqa2j2tfn9yvufqskdv236n9s2a06h9");
const abiRegistry = AbiRegistry.create(abi);
const bridgeContract = new SmartContract({
address: bridgeAddress,
abi: abiRegistry,
});
bridgeContract;
const collectionName = "Alex";
const collectionTicker = "ALX";
const collectionIdentifier = "ALX-afef0b";
Expand Down Expand Up @@ -188,6 +201,7 @@ async function transferToSc(
receiver: signer.getAddress(),
chainID: "D",
});
const nonce1 = account.getNonceThenIncrement();
tx3.setNonce(nonce1);
Expand All @@ -199,6 +213,98 @@ async function transferToSc(
console.log("Hash:", txHash);
}
async function claim(account: Account, signer: UserSigner) {
const structClaimData = new StructType("ClaimData", [
new FieldDefinition("token_id", "name of the nft", new BytesType()),
new FieldDefinition("source_chain", "attributes of the nft", new BytesType()),
new FieldDefinition("destination_chain", "attributes of the nft", new BytesType()),
new FieldDefinition("destination_user_address", "attributes of the nft", new AddressType()),
new FieldDefinition("source_nft_contract_address", "attributes of the nft", new BytesType()),
new FieldDefinition("name", "attributes of the nft", new BytesType()),
new FieldDefinition("symbol", "attributes of the nft", new BytesType()),
new FieldDefinition("royalty", "attributes of the nft", new BigUIntType()),
new FieldDefinition("royalty_receiver", "attributes of the nft", new AddressType()),
new FieldDefinition("attrs", "attributes of the nft", new BytesType()),
new FieldDefinition("transaction_hash", "attributes of the nft", new BytesType()),
new FieldDefinition("token_amount", "attributes of the nft", new BigUIntType()),
new FieldDefinition("nft_type", "attributes of the nft", new BytesType()),
new FieldDefinition("fee", "attributes of the nft", new BigUIntType()),
]);
//copied from validator logs
const claimData = {
tokenId: "33",
sourceChain: "BSC",
destinationChain: "MULTIVERSX",
destinationUserAddress: "erd1ymdj4ze52a0tmcjzeyhcntzaf5uxpn2d6t203yreh6qx6fqeftgqmz9ly6",
sourceNftContractAddress: "0xc679bdad7c2a34ca93552eae75e4bc03bf505adc",
name: "Istra",
symbol: "NSA",
royalty: "0",
royaltyReceiver: "9fb927c978225cb7a93b8b3cd8d8423e176e009dc284c536d9c4372bbe128487",
metadata: "https://meta.polkamon.com/meta?id=10002366748",
transactionHash: "0x4e7cc1b533361fc39f013391c6fead3f0a55a4656f8e7f1a84af7740ff5a2abf",
tokenAmount: "1",
nftType: "singular",
fee: "100000000000000",
};
//sig copied validator logs
const signature = {
sig: "0xdccd31367579bfd82e46881e28c2201f7e9e58327e1c4366e972c170ad9b3b1924861101446bdc8ea1c5b364361fa12cabcea32c473e2f759a01e699e24bdc07",
public_key: "9fb927c978225cb7a93b8b3cd8d8423e176e009dc284c536d9c4372bbe128487",
};
const claimDataArgs = new Struct(structClaimData, [
new Field(new BytesValue(Buffer.from(new Nonce(Number(claimData.tokenId)).hex(), "hex")), "token_id"),
new Field(new BytesValue(Buffer.from(claimData.sourceChain)), "source_chain"),
new Field(new BytesValue(Buffer.from(claimData.destinationChain)), "destination_chain"),
new Field(new AddressValue(new Address(claimData.destinationUserAddress)), "destination_user_address"),
new Field(new BytesValue(Buffer.from(claimData.sourceNftContractAddress)), "source_nft_contract_address"),
new Field(new BytesValue(Buffer.from(claimData.name)), "name"),
new Field(new BytesValue(Buffer.from("N" + claimData.sourceChain.toUpperCase())), "symbol"),
new Field(new BigUIntValue(new Nonce(Number(claimData.royalty)).hex()), "royalty"),
new Field(new AddressValue(new Address(claimData.royaltyReceiver)), "royalty_receiver"),
new Field(new BytesValue(Buffer.from(claimData.metadata)), "attrs"),
new Field(new BytesValue(Buffer.from(claimData.transactionHash)), "transaction_hash"),
new Field(new BigUIntValue(claimData.tokenAmount), "token_amount"),
new Field(new BytesValue(Buffer.from(claimData.nftType)), "nft_type"),
new Field(new BigUIntValue(claimData.fee), "fee"),
]);
const data = [
claimDataArgs,
[
{
public_key: new AddressValue(new Address(Buffer.from(signature.public_key, "hex"))),
sig: new BytesValue(Buffer.from(signature.sig.replace(/^0x/, ""), "hex")),
},
],
VariadicValue.fromItems(
new BytesValue(
Buffer.from(
"https://drive.polychainmonsters.com/ipfs/QmXBAiT4um5WSJZfoJ1arCbcJh89yBEYvi17bVM91rj3t4",
"utf-8"
)
),
new BytesValue(Buffer.from(claimData.metadata, "utf-8"))
),
];
const transaction = bridgeContract.methods
.claimNft721(data)
.withSender(signer.getAddress())
.withNonce(account.getNonceThenIncrement())
.withChainID("D")
.withGasLimit(6_000_000_00)
.withValue(new BigUIntValue(new BigNumber("50000000000000000")))
.buildTransaction();
transaction.applySignature(await signer.sign(transaction.serializeForSigning()));
const hash = await proxyNetworkProvider.sendTransaction(transaction);
console.log(hash);
}
const address = new Address("erd1ymdj4ze52a0tmcjzeyhcntzaf5uxpn2d6t203yreh6qx6fqeftgqmz9ly6");
const account = new Account(address);
Expand All @@ -215,36 +321,39 @@ const account = new Account(address);
setSpecialRoles;
createNft;
transferToSc;
false && (await claim(account, signer));
//transferToSc(bridgeAddress, account, signer, "ALX-afef0b-01", "01");
//await createNft("ALX-afef0b-07", address, account, signer);
//await createNft("ALX-afef0b-51", address, account, signer);
const data = {
headers: {
"Content-Type": "application/json",
},
data: {
_source: ["events"],
query: {
bool: {
should: [
{
terms: {
originalTxHash: ["6ae4c0fdb82785f7642975b0e1a21b5b3d441c7d74345e06667ea20dcadda150"],
},
},
],
},
},
},
};
const logs = (await axios.get(`https://devnet-index.multiversx.com/logs/_search`, data))?.data?.hits?.hits[0]
?._source?.events;
const query = bridgeContract.createQuery({
func: "originalToDuplicateMapping",
args: [],
//caller: signer.getAddress(),
});
const provider = new ProxyNetworkProvider("https://devnet-gateway.multiversx.com");
const queryResponse = await provider.queryContract(query);
const def = bridgeContract.getEndpoint("originalToDuplicateMapping");
const { firstValue } = new ResultsParser().parseQueryResponse(queryResponse, def);
const count = firstValue?.valueOf();
const decodedMapping = count.map((pair: any) => {
return pair.flatMap((item: any[]) => {
return Object.keys(item).map((key: any) => ({ [key]: Buffer.from(item[key]).toString() }));
});
});
const pair = decodedMapping.find((item: []) =>
item.find((obj) => Object.values(obj).find((val) => val === "NSA-42b661"))
);
console.log(logs);
const x = decodeBase64Array(logs[1].topics) as string[];
console.log(pair[0].field0, pair[1].field1);
console.log(x, "x");
signer;
//console.log(decodeBase64Array(res.returnData), "res");
//await createNft("ALX-afef0b-07", address, account, signer);
//await createNft("ALX-afef0b-08", address, account, signer);
//await createNft("ALX-afef0b-09", address, account, signer);
Expand Down
Loading

0 comments on commit 2e761f1

Please sign in to comment.