{"id":16039530,"url":"https://github.com/web3w/x2y2-js","last_synced_at":"2025-04-10T11:07:25.312Z","repository":{"id":45723518,"uuid":"514235271","full_name":"web3w/x2y2-js","owner":"web3w","description":"This SDK for X2Y2","archived":false,"fork":false,"pushed_at":"2022-08-02T12:03:56.000Z","size":1785,"stargazers_count":2,"open_issues_count":0,"forks_count":2,"subscribers_count":3,"default_branch":"main","last_synced_at":"2025-03-24T09:52:48.515Z","etag":null,"topics":["blockchain","ethereum","nft","sdk"],"latest_commit_sha":null,"homepage":"https://web3w.github.io/x2y2-js/","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/web3w.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2022-07-15T10:54:35.000Z","updated_at":"2023-05-12T05:00:12.000Z","dependencies_parsed_at":"2022-09-22T21:00:43.180Z","dependency_job_id":null,"html_url":"https://github.com/web3w/x2y2-js","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web3w%2Fx2y2-js","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web3w%2Fx2y2-js/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web3w%2Fx2y2-js/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/web3w%2Fx2y2-js/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/web3w","download_url":"https://codeload.github.com/web3w/x2y2-js/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247640507,"owners_count":20971564,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blockchain","ethereum","nft","sdk"],"created_at":"2024-10-08T23:05:14.390Z","updated_at":"2025-04-10T11:07:25.285Z","avatar_url":"https://github.com/web3w.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# x2y2-js\u003c!-- omit in toc --\u003e\n\nSDK for the X2Y2 protocol\n\nhttps://web3w.github.io/x2y2-js\n\n## Installation\n\nIn your project, run:\n\n```bash\nnpm i x2y2-js\n```\n\n## Getting Started\n\nTo get started, create a new OpenSeaJS client, called an using your chainId and address:\n\n```JavaScript\nimport {X2Y2SDK} from 'x2y2-js'\nimport {Web3Wallets} from 'web3-wallets'\n\nconst {chainId, address} = new Web3Wallets({name:'metamask'})\nconst x2y2 = new X2Y2SDK({chainId, address},{apiKey:\"xx-xx-xx\"})\n\n```\n\nIn the browser environment, only the chainId and address need to be configured，If you want to use the bash environment,\nconfigure the private key and RPC\n\n```ts\ntype WalletInfo = {\n    chainId: number;\n    address: string;\n    privateKeys?: string[];\n    rpcUrl?: RpcInfo; // User-defined RPC information of the provider\n}\n\ntype APIConfig = {\n    apiKey?: string; //opensea api key\n}\n``` \n\nAdvanced Settings\n\n```ts\ntype WalletInfo = {\n    offsetGasLimitRatio?: number; // Set the GAS limit offset of the wallet to be greater than 1 eg：1.2\n}\n# TODO\n```\n\n### Fetching Assets\n\nAssets are items on OpenSea. They can be non-fungible (conforming to standards like ERC721), semi-fungible (like ERC1155\nassets), and even fungible (ERC20).\n\nAssets are represented by the `Asset` type, defined in TypeScript:\n\n```TypeScript\n/**\n * Simple, unannotated non-fungible asset spec\n */\nexport interface Asset {\n    // The asset's token ID, or null if ERC-20\n    tokenId: string | undefined;\n    // The asset's contract address\n    tokenAddress: string;\n    // 'erc20' | 'erc721' | 'erc1155' | 'cryptokitties' | 'ensshortnameauction' | 'cryptopunks'\n    schemaName: string;\n    // Optional for fungible items\n    name?: string;\n    data?: string;\n    decimals?: number;\n    chainId?: number;\n    collection?: {\n        \"royaltyFeePoints\": 500,\n        \"royaltyFeeAddress\": \"0x9F7A946d935c8Efc7A8329C0d894A69bA241345A\"\n    };\n}\n\n```\n\nThe `Asset` type is the minimal type you need for most marketplace actions. `SchemaName` is optional. If omitted, most\nactions will assume you're referring to a non-fungible, ERC721 asset. Other options include 'ERC20' and 'ERC1155'.\n\nYou can fetch an asset using the `OpenSeaAPI`, which will return an `OpenSeaAsset` for you (`OpenSeaAsset`\nextends `Asset`):\n\n```TypeScript\n \nconst assetFee = await x2y2.getAssets([asset_contract_addresses])\n\n```\n\n#### Checking Balances and Ownerships\n\nThe nice thing about the `Asset` type is that it unifies logic between fungibles, non-fungibles, and semi-fungibles.\n\nOnce you have an `Asset`, you can see how many any account owns, regardless of whether it's an ERC-20 token or a\nnon-fungible good:\n\n```JavaScript\n\nconst asset = {\n    tokenId: '9',\n    tokenAddress: '0xb556f251eacbec4badbcddc4a146906f2c095bee',\n    schemaName: 'ERC721'\n}\n\nconst balance = await x2y2.userAccount.getAssetBalances(asset, accountAddress)\n\n```\n\nYou can use this same method for fungible ERC-20 tokens like wrapped ETH (WETH). As a convenience, you can use this\nfungible wrapper for checking fungible balances:\n\n```JavaScript\nconst balanceOfWETH = await x2y2.getTokenBalance({\n    accountAddress, // string\n    tokenAddress: \"0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2\"\n})\n```\n\n### Making Offers\n\nOnce you have your asset, you can do this to make an offer on it:\n\n```JavaScript\n// Token ID and smart contract address for a non-fungible token:\nconst {tokenId, tokenAddress} = YOUR_ASSET\n\nconst offer = await x2y2.createBuyOrder({\n    asset: {\n        tokenId,\n        tokenAddress,\n        schemaName // WyvernSchemaName. If omitted, defaults to 'ERC721'. Other options include 'ERC20' and 'ERC1155'\n    },\n    // Value of the offer, in units of the payment token (or wrapped ETH if none is specified):\n    startAmount: 1.2,\n})\n```\n\n### Making Listings / Selling Items\n\nTo sell an asset, call `createSellOrder`. You can do a fixed-price listing, where `startAmount` is equal to `endAmount`,\nor a declining [Dutch auction](https://en.wikipedia.org/wiki/Dutch_auction), where `endAmount` is lower and the price\ndeclines until `expirationTime` is hit:\n\n```JavaScript\n// Expire this auction one day from now.\n// Note that we convert from the JavaScript timestamp (milliseconds):\nconst expirationTime = Math.round(Date.now() / 1000 + 60 * 60 * 24)\n\nconst listing = await x2y2.createSellOrder({\n    asset: {\n        tokenId,\n        tokenAddress,\n        schemaName,\n        \"collection\": {\n            royaltyFeeAddress,\n            royaltyFeePoints\n        }\n    },\n    startAmount: 3,\n    // If `endAmount` is specified, the order will decline in value to that amount until `expirationTime`. Otherwise, it's a fixed-price order:\n    endAmount: 0.1,\n    expirationTime\n})\n```\n\n### Posting Order\n\n```ts\n// const orderStr = JSON.stringify(offer)\nconst orderStr = JSON.stringify(listing)\nconst order = await x2y2.postOrder(orderStr).catch((err: any) =\u003e {\n    throw err\n}) \n```\n\n### Fetching Orders\n\nTo retrieve a list of offers and auction on an asset, you can use an instance of the `OpenSeaAPI` exposed on the client.\nParameters passed into API filter objects are underscored instead of camel-cased, similar to the\nmain [OpenSea API parameters](https://docs.opensea.io/v1.0/reference):\n\n```JavaScript\nimport {OrderSide} from 'x2y2-js'\n\n// Get offers (bids), a.k.a. orders where `side == 0` \nconst query = {\n    asset_contract_address: tokenAddress, //\n    token_ids: [tokenId]\n}\nconst {orders, count} = await x2y2.api.getOrders(query)\n\n// Get page 2 of all auctions, a.k.a. orders where `side == 1`\nconst {orders, count} = await x2y2.api.getOrders({\n    asset_contract_address: tokenAddress,\n    token_ids: [tokenId],\n    side: OrderSide.Sell\n}, 2)\n\n// Get Owner Orders\nconst {orders, count} = await x2y2.getOwnerOrders()\n```\n\n\n### Buying Items\n\nTo buy an item , you need to **fulfill a sell order**. To do that, it's just one call:\n\n```JavaScript\nconst orders = await x2y2.api.getOrders({side: OrderSide.Sell, ...})\nconst tx = await x2y2.fulfillOrder(JSON.stringify(orders[0]))\nconsole.log(tx.hash)\nawait tx.wait()\n\nconst orderList = [{orderStr: JSON.stringify(order[0])}, {orderStr: JSON.stringify(order[0])}]\nconst res = await sdk.fulfillOrders({orderList})\nconsole.log(res.hash)\nawait res.wait()\n```\n\nNote that the `fulfillOrder` promise resolves when the transaction has been confirmed and mined to the blockchain. To\nget the transaction hash before this happens, add an event listener (see [Listening to Events](#listening-to-events))\nfor the `TransactionCreated` event.\n\nIf the order is a sell order (`order.side === OrderSide.Sell`), the taker is the *buyer* and this will prompt the buyer\nto pay for the item(s).\n\n### Accepting Offers\n\nSimilar to fulfilling sell orders above, you need to fulfill a buy order on an item you own to receive the tokens in the\noffer.\n\n```JavaScript\nconst orders = await x2y2.api.getOrders({side: OrderSide.Buy, ...})\nconst tx = await x2y2.fulfillOrder(JSON.stringify(orders[0]))\nconsole.log(tx.hash)\nawait tx.wait()\n```\n\nIf the order is a buy order (`order.side === OrderSide.Buy`), then the taker is the *owner* and this will prompt the\nowner to exchange their item(s) for whatever is being offered in return. See [Listening to Events](#listening-to-events)\nbelow to respond to the setup transactions that occur the first time a user accepts a bid.\n\n### Transferring Items or Coins (Gifting)\n\nA handy feature in OpenSea.js is the ability to transfer any supported asset (fungible or non-fungible tokens) in one\nline of JavaScript.\n\nTo transfer an ERC-721 asset or an ERC-1155 asset, it's just one call:\n\n```JavaScript\n\nconst transactionHash = await x2y2.userAccount.transfer({\n    asset: {tokenId, tokenAddress},\n    fromAddress, // Must own the asset\n    toAddress\n})\n```\n\nFor fungible ERC-1155 assets, you can set `schemaName` to \"ERC1155\" and pass a `quantity` in to transfer multiple at\nonce:\n\n```JavaScript\n\nconst transactionHash = await x2y2.userAccount.transfer({\n    asset: {\n        tokenId,\n        tokenAddress,\n        schemaName: \"ERC1155\"\n    },\n    toAddress,\n    quantity: 2,\n})\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweb3w%2Fx2y2-js","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fweb3w%2Fx2y2-js","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fweb3w%2Fx2y2-js/lists"}