@imtbl/x-provider 2.0.0-alpha.7 → 2.0.0-alpha.9
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/browser/index.js +4 -1525
- package/dist/node/index.cjs +27 -1548
- package/dist/node/index.js +4 -1525
- package/package.json +5 -5
package/dist/browser/index.js
CHANGED
|
@@ -2,1532 +2,11 @@ import { imx } from '@imtbl/generated-clients';
|
|
|
2
2
|
import { convertToSignableToken, signRaw, signMessage } from '@imtbl/toolkit';
|
|
3
3
|
import { ImxConfiguration, Contracts, EncodingApi, MintsApi, signRegisterEthAddress } from '@imtbl/x-client';
|
|
4
4
|
import { isAxiosError } from 'axios';
|
|
5
|
-
import * as
|
|
5
|
+
import * as N from 'enc-utils';
|
|
6
6
|
import { parseUnits, BrowserProvider } from 'ethers';
|
|
7
|
-
import
|
|
7
|
+
import ft from '@metamask/detect-provider';
|
|
8
8
|
import { Environment } from '@imtbl/config';
|
|
9
9
|
|
|
10
|
-
// src/signable-actions/transfer.ts
|
|
10
|
+
function Ue(t,e){return t===e.ethConfiguration.chainID}async function l(t,e){let r=(await t.provider?.getNetwork())?.chainId;if(!Ue(Number(r),e))throw new Error("The wallet used for this operation is not connected to the correct network.")}async function Y({signers:{ethSigner:t,starkSigner:e},request:r,config:n}){await l(t,n.immutableXConfig);let a=await t.getAddress(),i=new imx.TransfersApi(n.immutableXConfig.apiConfiguration),s=r.type==="ERC721"?"1":r.amount,o=await i.getSignableTransferV1({getSignableTransferRequest:{sender:a,token:convertToSignableToken(r),amount:s,receiver:r.receiver}}),{signable_message:d,payload_hash:p}=o.data,g=await signRaw(d,t),E=await e.signMessage(p),f={sender_stark_key:o.data.sender_stark_key,sender_vault_id:o.data.sender_vault_id,receiver_stark_key:o.data.receiver_stark_key,receiver_vault_id:o.data.receiver_vault_id,asset_id:o.data.asset_id,amount:o.data.amount,nonce:o.data.nonce,expiration_timestamp:o.data.expiration_timestamp,stark_signature:E},c=await i.createTransferV1({createTransferRequest:f,xImxEthAddress:a,xImxEthSignature:g});return {sent_signature:c?.data.sent_signature,status:c?.data.status?.toString(),time:c?.data.time,transfer_id:c?.data.transfer_id}}async function Q({signers:{ethSigner:t,starkSigner:e},request:r,config:n}){await l(t,n.immutableXConfig);let a=await t.getAddress(),i=new imx.TransfersApi(n.immutableXConfig.apiConfiguration),s=r.map(c=>({amount:"1",token:convertToSignableToken({type:"ERC721",tokenId:c.tokenId,tokenAddress:c.tokenAddress}),receiver:c.receiver})),o=await i.getSignableTransfer({getSignableTransferRequestV2:{sender_ether_key:a,signable_requests:s}}),d=o.data.signable_message;if(d===void 0)throw new Error("Invalid response from Signable registration offchain");let p=await signRaw(d,t),g=[];for(let c of o.data.signable_responses){let w=await e.signMessage(c.payload_hash),T={sender_vault_id:c.sender_vault_id,receiver_stark_key:c.receiver_stark_key,receiver_vault_id:c.receiver_vault_id,asset_id:c.asset_id,amount:c.amount,nonce:c.nonce,expiration_timestamp:c.expiration_timestamp,stark_signature:w};g.push(T);}let E={sender_stark_key:o.data.sender_stark_key,requests:g};return {transfer_ids:(await i.createTransfer({createTransferRequestV2:E,xImxEthAddress:a,xImxEthSignature:p}))?.data.transfer_ids}}async function j({signers:t,request:e,config:r}){await l(t.ethSigner,r.immutableXConfig);let n=await t.ethSigner.getAddress(),a=new imx.OrdersApi(r.immutableXConfig.apiConfiguration),i=e.sell.type==="ERC721"?"1":e.sell.amount,s=e.buy.type==="ERC721"?"1":e.buy.amount,o={user:n,amount_buy:s,token_buy:convertToSignableToken(e.buy),amount_sell:i,token_sell:convertToSignableToken(e.sell),fees:e.fees,expiration_timestamp:e.expiration_timestamp},d=await a.getSignableOrder({getSignableOrderRequestV3:o}),{signable_message:p,payload_hash:g}=d.data,E=await signRaw(p,t.ethSigner),f=await t.starkSigner.signMessage(g),c=d.data,w={createOrderRequest:{amount_buy:c.amount_buy,amount_sell:c.amount_sell,asset_id_buy:c.asset_id_buy,asset_id_sell:c.asset_id_sell,expiration_timestamp:c.expiration_timestamp,fees:e.fees,nonce:c.nonce,stark_key:c.stark_key,stark_signature:f,vault_id_buy:c.vault_id_buy,vault_id_sell:c.vault_id_sell},xImxEthAddress:n,xImxEthSignature:E};return {...(await a.createOrderV3(w)).data}}async function J({signers:t,request:e,config:r}){let n=new imx.OrdersApi(r.immutableXConfig.apiConfiguration),a=await n.getSignableCancelOrderV3({getSignableCancelOrderRequest:{order_id:e.order_id}}),{signable_message:i,payload_hash:s}=a.data,o=await signRaw(i,t.ethSigner),d=await t.starkSigner.signMessage(s),p=await t.ethSigner.getAddress(),g=await n.cancelOrderV3({id:e.order_id.toString(),cancelOrderRequest:{order_id:e.order_id,stark_signature:d},xImxEthAddress:p,xImxEthSignature:o});return {order_id:g.data.order_id,status:g.data.status}}async function ee(t,e){await l(t.ethSigner,e.immutableXConfig);let r=new imx.UsersApi(e.immutableXConfig.apiConfiguration),n=await t.ethSigner.getAddress(),a=await t.starkSigner.getAddress(),i=await r.getSignableRegistrationOffchain({getSignableRegistrationRequest:{ether_key:n,stark_key:a}}),{signable_message:s,payload_hash:o}=i.data,d=await signRaw(s,t.ethSigner),p=await t.starkSigner.signMessage(o);return (await r.registerUser({registerUserRequest:{eth_signature:d,ether_key:n,stark_signature:p,stark_key:a}})).data}async function te(t,e){try{let n=await new imx.UsersApi(e.immutableXConfig.apiConfiguration).getUsers({user:t}),{accounts:a}=n.data;return a?.length>0}catch(r){if(isAxiosError(r)&&r.response?.status===404)return !1;throw r}}async function _(t,e,r){await l(e,r.immutableXConfig);let n=r.immutableXConfig,a=Contracts.RegistrationV4.connect(n.ethConfiguration.registrationV4ContractAddress||n.ethConfiguration.registrationContractAddress,e);try{return await a.isRegistered(t)}catch(i){if(i.reason==="USER_UNREGISTERED")return !1;throw i}}var h=t=>{if(t!==void 0)return t;throw new Error("undefined field exception")};async function re(t,e){let{signers:{ethSigner:r,starkSigner:n}}=t;await l(r,t.config);let a=t.type==="ERC721"?"1":t.amount,i=await e.getSignableWithdrawalV2({getSignableWithdrawalRequest:{user:await r.getAddress(),token:convertToSignableToken(t),amount:a}}),{signable_message:s,payload_hash:o}=i.data,d=await n.signMessage(o),{ethAddress:p,ethSignature:g}=await signMessage(s,r);return (await e.createWithdrawalV2({createWithdrawalRequestV2:{sender_stark_key:h(i.data.sender_stark_key),sender_vault_id:h(i.data.sender_vault_id),receiver_stark_key:h(i.data.receiver_stark_key),receiver_vault_id:h(i.data.receiver_vault_id),amount:a,asset_id:h(i.data.asset_id),expiration_timestamp:h(i.data.expiration_timestamp),nonce:h(i.data.nonce),stark_signature:d},xImxEthAddress:p,xImxEthSignature:g})).data}async function C(t,e,r,n){return (await new imx.EncodingApi(r.apiConfiguration).encodeAsset({assetType:t,encodeAssetRequest:{token:{type:e,...n&&{data:n}}}})).data}async function v(t,e,r,n){return Contracts.CoreV4.connect(n.ethConfiguration.coreContractAddress,t).getWithdrawalBalance(e,r)}async function ze(t,e,r){let n=await C("asset","ETH",r);return await v(t,e,n.asset_id,r)}async function Ke(t,e,r,n){let a=await C("asset","ERC20",n,{token_address:r});return await v(t,e,a.asset_id,n)}async function je(t,e,r,n,a,i){try{let s=await a.getMintableTokenDetailsByClientTokenId({tokenAddress:r.tokenAddress,tokenId:r.tokenId}),o=await C("mintable-asset","ERC721",i,{id:r.tokenId,token_address:r.tokenAddress,...s.data.blueprint&&{blueprint:s.data.blueprint}});return await v(t,e,o.asset_id,i)}catch(s){if(s.response?.status===404){let o=await C("asset","ERC721",i,{token_id:r.tokenId,token_address:r.tokenAddress});return await v(t,e,o.asset_id,i)}throw s}}async function x(t,e,r,n,a,i){switch(r.type){case"ETH":return await ze(t,e,i);case"ERC20":return await Ke(t,e,r.tokenAddress,i);case"ERC721":return await je(t,e,r,n,a,i);default:throw new Error("Unsupported token type")}}async function I(t,e,r,n,a){let i=new EncodingApi(a.apiConfiguration),s=new MintsApi(a.apiConfiguration),o=await x(t,e,n,i,s,a),d=await x(t,r,n,i,s,a);return {v3Balance:o,v4Balance:d}}async function ae(t,e,r,n,a,i){let s=new EncodingApi(a.apiConfiguration),o=await x(t,e,n,s,i,a),d=await x(t,r,n,s,i,a);return {v3Balance:o,v4Balance:d}}var ie="ERC20";async function D(t,e,r,n,a){let i=await t.getAddress(),s=await signRegisterEthAddress(e,i,r),d=await Contracts.RegistrationV4.connect(a.ethConfiguration.registrationV4ContractAddress||a.ethConfiguration.registrationContractAddress,t).registerAndWithdrawAll.populateTransaction(i,r,s,n);return t.sendTransaction(d)}async function q(t,e,r,n){let i=await Contracts.RegistrationV4.connect(n.ethConfiguration.registrationV4ContractAddress||n.ethConfiguration.registrationContractAddress,t).withdrawAll.populateTransaction(await t.getAddress(),e,r);return t.sendTransaction(i)}async function L(t,e,r,n){let i=await Contracts.CoreV4.connect(n.ethConfiguration.coreContractAddress,t).withdraw.populateTransaction(await t.getAddress(),r);return t.sendTransaction(i)}async function se({ethSigner:t,starkSigner:e,starkPublicKey:r,token:n,config:a}){await l(t,a.immutableXConfig);let{v3Balance:i,v4Balance:s}=await I(t,r,await t.getAddress(),{type:ie,tokenAddress:n.tokenAddress},a.immutableXConfig),o=await C("asset",ie,a.immutableXConfig,{token_address:n.tokenAddress});if(i>0)return await _(r,t,a)?q(t,r,o.asset_type,a.immutableXConfig):D(t,e,r,o.asset_type,a.immutableXConfig);if(s>0)return L(t,r,o.asset_type,a.immutableXConfig);throw new Error("No balance to withdraw")}var A="ERC721";function ce(t){let{id:e}=t.data,r=t.data.blueprint||"";return N.sanitizeHex(N.utf8ToHex(`{${e}}:{${r}}`))}async function Ze(t,e,r,n){let a=await t.getAddress(),i=await e.getAddress(),s=await C("asset",A,n,{token_id:r.tokenId,token_address:r.tokenAddress}),o=await signRegisterEthAddress(e,a,i),p=await Contracts.RegistrationV4.connect(n.ethConfiguration.registrationV4ContractAddress||n.ethConfiguration.registrationContractAddress,t).registerAndWithdrawNft.populateTransaction(a,i,o,s.asset_type,r.tokenId);return t.sendTransaction(p)}async function et(t,e,r,n){let a=await t.getAddress(),i=await e.getAddress(),s=await C("mintable-asset",A,n,{id:r.data.id,token_address:r.data.tokenAddress,...r.data.blueprint&&{blueprint:r.data.blueprint}}),o=ce(r),d=await signRegisterEthAddress(e,a,i),g=await Contracts.RegistrationV4.connect(n.ethConfiguration.registrationV4ContractAddress||n.ethConfiguration.registrationContractAddress,t).registerWithdrawAndMint.populateTransaction(a,i,d,s.asset_type,o);return t.sendTransaction(g)}async function tt(t,e,r,n,a){return t.getMintableTokenDetailsByClientTokenId({tokenAddress:n.tokenAddress,tokenId:n.tokenId}).then(i=>et(e,r,{type:A,data:{id:n.tokenId,tokenAddress:n.tokenAddress,blueprint:i.data.blueprint}},a)).catch(i=>{if(i.response?.status===404)return Ze(e,r,n,a);throw i})}async function rt(t,e,r,n){let a=await C("mintable-asset",A,n,{id:r.data.id,token_address:r.data.tokenAddress,...r.data.blueprint&&{blueprint:r.data.blueprint}}),i=ce(r),o=await Contracts.CoreV4.connect(n.ethConfiguration.coreContractAddress,t).withdrawAndMint.populateTransaction(e,a.asset_type,i);return t.sendTransaction(o)}async function nt(t,e,r,n){let a=await C("asset",A,n,{token_id:r.tokenId,token_address:r.tokenAddress}),s=await Contracts.CoreV4.connect(n.ethConfiguration.coreContractAddress,t).withdrawNft.populateTransaction(e,a.asset_type,r.tokenId);return t.sendTransaction(s)}async function oe(t,e,r,n,a){return t.getMintableTokenDetailsByClientTokenId({tokenAddress:n.tokenAddress,tokenId:n.tokenId}).then(i=>rt(e,r,{type:A,data:{id:n.tokenId,tokenAddress:n.tokenAddress,blueprint:i.data.blueprint}},a)).catch(i=>{if(i.response?.status===404)return nt(e,r,n,a);throw i})}async function me({ethSigner:t,starkSigner:e,starkPublicKey:r,token:n,config:a},i){await l(t,a.immutableXConfig);let s=await t.getAddress(),{v3Balance:o,v4Balance:d}=await ae(t,r,s,{type:A,tokenAddress:n.tokenAddress,tokenId:n.tokenId},a.immutableXConfig,i);if(o>0)return await _(r,t,a)?oe(i,t,r,n,a.immutableXConfig):tt(i,t,e,n,a.immutableXConfig);if(d>0)return oe(i,t,s,n,a.immutableXConfig);throw new Error("No balance to withdraw")}var ue="ETH";async function ge({ethSigner:t,starkSigner:e,starkPublicKey:r,config:n}){await l(t,n.immutableXConfig);let{v3Balance:a,v4Balance:i}=await I(t,r,await t.getAddress(),{type:ue},n.immutableXConfig),s=await C("asset",ue,n.immutableXConfig);if(a>0)return await _(r,t,n)?q(t,r,s.asset_type,n.immutableXConfig):D(t,e,r,s.asset_type,n.immutableXConfig);if(i>0)return L(t,r,s.asset_type,n.immutableXConfig);throw new Error("No balance to withdraw")}async function le({signers:t,withdrawal:e,config:r}){let n=new imx.WithdrawalsApi(r.immutableXConfig.apiConfiguration);return re({signers:t,config:r.immutableXConfig,...e},n)}async function fe({signers:{ethSigner:t,starkSigner:e},starkPublicKey:r,token:n,config:a}){let i=new imx.MintsApi(a.immutableXConfig.apiConfiguration);switch(n.type){case"ETH":return ge({ethSigner:t,starkSigner:e,starkPublicKey:r,config:a});case"ERC20":return se({ethSigner:t,starkSigner:e,starkPublicKey:r,token:n,config:a});case"ERC721":return me({ethSigner:t,starkSigner:e,starkPublicKey:r,token:n,config:a},i)}}async function Ce({signers:{ethSigner:t,starkSigner:e},request:r,config:n}){await l(t,n.immutableXConfig);let a=await t.getAddress(),i=new imx.TradesApi(n.immutableXConfig.apiConfiguration),s=await i.getSignableTrade({getSignableTradeRequest:{user:a,order_id:r.order_id,fees:r.fees}}),{signable_message:o,payload_hash:d}=s.data,p=await signRaw(o,t),g=await e.signMessage(d);return (await i.createTradeV3({createTradeRequest:{amount_buy:s.data.amount_buy,amount_sell:s.data.amount_sell,asset_id_buy:s.data.asset_id_buy,asset_id_sell:s.data.asset_id_sell,expiration_timestamp:s.data.expiration_timestamp,fee_info:s.data.fee_info,fees:r.fees,include_fees:!0,nonce:s.data.nonce,order_id:r.order_id,stark_key:s.data.stark_key,vault_id_buy:s.data.vault_id_buy,vault_id_sell:s.data.vault_id_sell,stark_signature:g},xImxEthAddress:a,xImxEthSignature:p})).data}async function dt(t,e,r,n,a,i){let o=await Contracts.CoreV4.connect(i.ethConfiguration.coreContractAddress,t)["deposit(uint256,uint256,uint256)"].populateTransaction(n,r,a);return t.sendTransaction({...o,value:e})}async function Re({signers:{ethSigner:t},deposit:e,config:r}){await l(t,r.immutableXConfig);let n=await t.getAddress(),a={decimals:18},i=parseUnits(e.amount,"wei"),s=r.immutableXConfig,o=new imx.DepositsApi(s.apiConfiguration),d=new imx.EncodingApi(s.apiConfiguration),p={user:n,token:{type:e.type,data:a},amount:i.toString()},g=await o.getSignableDeposit({getSignableDepositRequest:p}),f=(await d.encodeAsset({assetType:"asset",encodeAssetRequest:{token:{type:e.type}}})).data.asset_type,c=g.data.stark_key,w=g.data.vault_id;return dt(t,i,f,c,w,s)}async function mt(t,e,r,n,a,i){let o=await Contracts.CoreV4.connect(i.coreContractAddress,t).depositERC20.populateTransaction(n,r,a,e);return t.sendTransaction(o)}async function _e({signers:{ethSigner:t},deposit:e,config:r}){await l(t,r.immutableXConfig);let{apiConfiguration:n,ethConfiguration:a}=r.immutableXConfig,i=await t.getAddress(),s=new imx.TokensApi(n),o=new imx.DepositsApi(n),d=new imx.EncodingApi(n),p=await s.getToken({address:e.tokenAddress}),E={decimals:parseInt(p.data.decimals),token_address:e.tokenAddress},f=parseUnits(e.amount,0),w=await Contracts.IERC20.connect(e.tokenAddress,t).approve.populateTransaction(a.coreContractAddress,f);await t.sendTransaction(w);let T={user:i,token:{type:e.type,data:E},amount:f.toString()},b=await o.getSignableDeposit({getSignableDepositRequest:T}),Xe=(await d.encodeAsset({assetType:"asset",encodeAssetRequest:{token:{type:e.type,data:{token_address:e.tokenAddress}}}})).data.asset_type,De=b.data.stark_key,qe=b.data.vault_id,Le=BigInt(b.data.amount);return mt(t,Le,Xe,De,qe,a)}async function ut(t,e,r,n,a,i){let o=await Contracts.CoreV4.connect(i.ethConfiguration.coreContractAddress,t).depositNft.populateTransaction(n,r,a,e);return t.sendTransaction(o)}async function Ae({signers:{ethSigner:t},deposit:e,config:r}){await l(t,r.immutableXConfig);let n=await t.getAddress(),{immutableXConfig:a}=r,i=new imx.DepositsApi(a.apiConfiguration),s=new imx.EncodingApi(a.apiConfiguration),o={token_address:e.tokenAddress,token_id:e.tokenId},p={user:n,token:{type:e.type,data:o},amount:"1".toString()},g=await i.getSignableDeposit({getSignableDepositRequest:p}),f=(await s.encodeAsset({assetType:"asset",encodeAssetRequest:{token:{type:e.type,data:{token_address:e.tokenAddress,token_id:e.tokenId}}}})).data.asset_type,c=g.data.stark_key,w=g.data.vault_id,T=Contracts.IERC721.connect(e.tokenAddress,t),b=a.ethConfiguration.coreContractAddress;return await T.isApprovedForAll(n,b)||await T.setApprovalForAll(b,!0),ut(t,e.tokenId,f,c,w,a)}async function be({signers:t,deposit:e,config:r}){switch(e.type){case"ETH":return Re({signers:t,deposit:e,config:r});case"ERC20":return _e({signers:t,deposit:e,config:r});case"ERC721":return Ae({signers:t,deposit:e,config:r})}}async function ye({signers:t,request:e,config:r}){await l(t.ethSigner,r.immutableXConfig);let n=new imx.ExchangesApi(r.immutableXConfig.apiConfiguration),a=await t.ethSigner.getAddress(),i=e.amount,s=await n.getExchangeSignableTransfer({id:e.transactionID,getSignableTransferRequest:{sender:a,token:convertToSignableToken(e),amount:i,receiver:e.receiver}}),{signable_message:o,payload_hash:d}=s.data,p=await signRaw(o,t.ethSigner),g=await t.starkSigner.signMessage(d),E={sender_stark_key:s.data.sender_stark_key,sender_vault_id:s.data.sender_vault_id,receiver_stark_key:s.data.receiver_stark_key,receiver_vault_id:s.data.receiver_vault_id,asset_id:s.data.asset_id,amount:s.data.amount,nonce:s.data.nonce,expiration_timestamp:s.data.expiration_timestamp,stark_signature:g},f=await n.createExchangeTransfer({id:e.transactionID,createTransferRequest:E,xImxEthAddress:a,xImxEthSignature:p});return {sent_signature:f?.data.sent_signature,status:f?.data.status?.toString(),time:f?.data.time,transfer_id:f?.data.transfer_id}}var P=class{config;signers;constructor(e,r,n){this.config=e,this.signers={ethSigner:r,starkSigner:n};}async getAddress(){return this.signers.ethSigner.getAddress()}async isRegisteredOffchain(){let e=await this.getAddress();return te(e,this.config)}registerOffchain(){return ee(this.signers,this.config)}batchNftTransfer(e){return Q({signers:this.signers,request:e,config:this.config})}cancelOrder(e){return J({signers:this.signers,request:e,config:this.config})}completeWithdrawal(e,r){return fe({config:this.config,signers:this.signers,token:r,starkPublicKey:e})}createOrder(e){return j({signers:this.signers,request:e,config:this.config})}createTrade(e){return Ce({signers:this.signers,request:e,config:this.config})}deposit(e){return be({signers:this.signers,deposit:e,config:this.config})}exchangeTransfer(e){return ye({signers:this.signers,request:e,config:this.config})}async isRegisteredOnchain(){let e=await this.signers.starkSigner.getAddress();return _(e,this.signers.ethSigner,this.config)}prepareWithdrawal(e){return le({signers:this.signers,withdrawal:e,config:this.config})}transfer(e){return Y({signers:this.signers,request:e,config:this.config})}};var Se={SWITCH_CHAIN:"wallet_switchEthereumChain",CONNECT:"eth_requestAccounts"};function ke(t){return !!t?.request}async function Pe(t,e){await t.request({method:Se.CONNECT}),e&&await t.request({method:Se.SWITCH_CHAIN,params:[{chainId:`0x${e.toString(16)}`}]});}var Et={PROVIDER_NOT_FOUND:"The Metamask provider was not found"};async function ve({chainID:t}){let e=await ft();if(!ke(e))throw new Error(Et.PROVIDER_NOT_FOUND);return await Pe(e,t),new BrowserProvider(e)}var R="message";function y(t,e){t&&t.contentWindow&&t.contentWindow.postMessage(e,new URL(t.src).origin);}function S(t,e,r,n){if(t&&e.source!==t.contentWindow)return;let a=e.data;a.type===r&&n(a.details);}var M=class{publicAddress;iframe;constructor(e,r){this.publicAddress=e,this.iframe=r;}getAddress(){return this.publicAddress}signMessage(e){return new Promise((r,n)=>{let a=i=>{S(this.iframe,i,"SIGN_MESSAGE_RESPONSE",s=>{window.removeEventListener(R,a),s.success||n(new Error(s.error?.message)),r(s.data.signedMessage);});};window.addEventListener(R,a),y(this.iframe,{type:"SIGN_MESSAGE_REQUEST",details:{starkPublicKey:this.publicAddress,message:e}});})}getIFrame(){return this.iframe}getYCoordinate(){return new Promise((e,r)=>{let n=a=>{S(this.iframe,a,"GET_Y_COORDINATE_RESPONSE",i=>{window.removeEventListener(R,n),i.success||r(new Error(i.error?.message)),e(i.data.yCoordinate);});};window.addEventListener(R,n),y(this.iframe,{type:"GET_Y_COORDINATE_REQUEST",details:{starkPublicKey:this.publicAddress}});})}};var Ie="imx-wallet-app",_t={[Environment.SANDBOX]:"https://wallets.sandbox.immutable.com",[Environment.PRODUCTION]:"https://wallets.immutable.com"},Tt="display: none;";function ht(){return document.querySelector(`iframe#${Ie}`)}async function At(t){return new Promise(e=>{let r=document.createElement("iframe");r.setAttribute("id",Ie),r.setAttribute("src",_t[t]),r.setAttribute("style",Tt),document.body.appendChild(r),r.onload=()=>e(r);})}async function Oe(t){let e=ht();return e||await At(t)}var bt="Only sign this request if you\u2019ve initiated an action with Immutable X.",yt="The L2 IMX Wallet connection has failed";async function Ne(t,e){let r=await t.getSigner(),n=await r.getAddress(),a=await r.signMessage(bt),i=await Oe(e);return new Promise((s,o)=>{let d=p=>{S(i,p,"CONNECT_WALLET_RESPONSE",g=>{window.removeEventListener(R,d),g.success||o(new Error(yt)),s(new M(g.data.starkPublicKey,i));});};window.addEventListener(R,d),y(i,{type:"CONNECT_WALLET_REQUEST",details:{ethAddress:n,signature:a}});})}async function Me(t){let e=t.getIFrame();return new Promise((r,n)=>{let a=i=>{S(e,i,"DISCONNECT_WALLET_RESPONSE",s=>{window.removeEventListener(R,a),!s.success&&s.error&&n(s.error),e.remove(),r();});};window.addEventListener(R,a),y(e,{type:"DISCONNECT_WALLET_REQUEST",details:{starkPublicKey:t.getAddress()}});})}var k=class extends Error{type;constructor(e,r){super(e),this.type=r;}},W=async(t,e)=>{try{return await t()}catch(r){let n=e.message||`${r.message}`||"UnknownError";throw new k(n,e.type)}};var V=class t extends P{static imxSigner;static async connect(e){return await W(async()=>{let r=await ve({chainID:e.immutableXConfig.ethConfiguration.chainID});return this.imxSigner=await Ne(r,e.baseConfig.environment),new t(e,await r.getSigner(),this.imxSigner)},{type:"WALLET_CONNECTION_ERROR"})}static async disconnect(){if(!this.imxSigner)throw new k("Attempted to disconnect from the MetaMask IMX provider without an established connection","PROVIDER_CONNECTION_ERROR");return W(async()=>{await Me(this.imxSigner);},{type:"PROVIDER_CONNECTION_ERROR"})}static async signMessage(e){if(!this.imxSigner)throw new k("Attempted to sign a message with the MetaMask IMX provider without an established connection","PROVIDER_CONNECTION_ERROR");return W(async()=>await this.imxSigner.signMessage(e),{type:"PROVIDER_CONNECTION_ERROR"})}};var B=class{immutableXConfig;baseConfig;constructor({baseConfig:e,overrides:r}){if(this.baseConfig=e,r)this.immutableXConfig=r.immutableXConfig;else {let n=new ImxConfiguration({baseConfig:e});this.immutableXConfig=n.immutableXConfig;}}};
|
|
11
11
|
|
|
12
|
-
|
|
13
|
-
function isChainValid(chainID, config) {
|
|
14
|
-
return chainID === config.ethConfiguration.chainID;
|
|
15
|
-
}
|
|
16
|
-
async function validateChain(signer, config) {
|
|
17
|
-
const chainID = (await signer.provider?.getNetwork())?.chainId;
|
|
18
|
-
if (!isChainValid(Number(chainID), config)) {
|
|
19
|
-
throw new Error(
|
|
20
|
-
"The wallet used for this operation is not connected to the correct network."
|
|
21
|
-
);
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
// src/signable-actions/transfer.ts
|
|
26
|
-
async function transfer({
|
|
27
|
-
signers: { ethSigner, starkSigner },
|
|
28
|
-
request,
|
|
29
|
-
config
|
|
30
|
-
}) {
|
|
31
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
32
|
-
const ethAddress = await ethSigner.getAddress();
|
|
33
|
-
const transfersApi = new imx.TransfersApi(
|
|
34
|
-
config.immutableXConfig.apiConfiguration
|
|
35
|
-
);
|
|
36
|
-
const transferAmount = request.type === "ERC721" ? "1" : request.amount;
|
|
37
|
-
const signableResult = await transfersApi.getSignableTransferV1({
|
|
38
|
-
getSignableTransferRequest: {
|
|
39
|
-
sender: ethAddress,
|
|
40
|
-
token: convertToSignableToken(request),
|
|
41
|
-
amount: transferAmount,
|
|
42
|
-
receiver: request.receiver
|
|
43
|
-
}
|
|
44
|
-
});
|
|
45
|
-
const { signable_message: signableMessage, payload_hash: payloadHash } = signableResult.data;
|
|
46
|
-
const ethSignature = await signRaw(signableMessage, ethSigner);
|
|
47
|
-
const starkSignature = await starkSigner.signMessage(payloadHash);
|
|
48
|
-
const transferSigningParams = {
|
|
49
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
50
|
-
sender_stark_key: signableResult.data.sender_stark_key,
|
|
51
|
-
sender_vault_id: signableResult.data.sender_vault_id,
|
|
52
|
-
receiver_stark_key: signableResult.data.receiver_stark_key,
|
|
53
|
-
receiver_vault_id: signableResult.data.receiver_vault_id,
|
|
54
|
-
asset_id: signableResult.data.asset_id,
|
|
55
|
-
amount: signableResult.data.amount,
|
|
56
|
-
nonce: signableResult.data.nonce,
|
|
57
|
-
expiration_timestamp: signableResult.data.expiration_timestamp,
|
|
58
|
-
stark_signature: starkSignature
|
|
59
|
-
};
|
|
60
|
-
const response = await transfersApi.createTransferV1({
|
|
61
|
-
createTransferRequest: transferSigningParams,
|
|
62
|
-
xImxEthAddress: ethAddress,
|
|
63
|
-
xImxEthSignature: ethSignature
|
|
64
|
-
});
|
|
65
|
-
return {
|
|
66
|
-
sent_signature: response?.data.sent_signature,
|
|
67
|
-
status: response?.data.status?.toString(),
|
|
68
|
-
time: response?.data.time,
|
|
69
|
-
transfer_id: response?.data.transfer_id
|
|
70
|
-
};
|
|
71
|
-
}
|
|
72
|
-
async function batchTransfer({
|
|
73
|
-
signers: { ethSigner, starkSigner },
|
|
74
|
-
request,
|
|
75
|
-
config
|
|
76
|
-
}) {
|
|
77
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
78
|
-
const ethAddress = await ethSigner.getAddress();
|
|
79
|
-
const transfersApi = new imx.TransfersApi(
|
|
80
|
-
config.immutableXConfig.apiConfiguration
|
|
81
|
-
);
|
|
82
|
-
const signableRequests = request.map((nftTransfer) => ({
|
|
83
|
-
amount: "1",
|
|
84
|
-
token: convertToSignableToken({
|
|
85
|
-
type: "ERC721",
|
|
86
|
-
tokenId: nftTransfer.tokenId,
|
|
87
|
-
tokenAddress: nftTransfer.tokenAddress
|
|
88
|
-
}),
|
|
89
|
-
receiver: nftTransfer.receiver
|
|
90
|
-
}));
|
|
91
|
-
const signableResult = await transfersApi.getSignableTransfer({
|
|
92
|
-
getSignableTransferRequestV2: {
|
|
93
|
-
sender_ether_key: ethAddress,
|
|
94
|
-
signable_requests: signableRequests
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
const signableMessage = signableResult.data.signable_message;
|
|
98
|
-
if (signableMessage === void 0) {
|
|
99
|
-
throw new Error("Invalid response from Signable registration offchain");
|
|
100
|
-
}
|
|
101
|
-
const ethSignature = await signRaw(signableMessage, ethSigner);
|
|
102
|
-
const requests = [];
|
|
103
|
-
for (const resp of signableResult.data.signable_responses) {
|
|
104
|
-
const starkSignature = await starkSigner.signMessage(resp.payload_hash);
|
|
105
|
-
const req = {
|
|
106
|
-
sender_vault_id: resp.sender_vault_id,
|
|
107
|
-
receiver_stark_key: resp.receiver_stark_key,
|
|
108
|
-
receiver_vault_id: resp.receiver_vault_id,
|
|
109
|
-
asset_id: resp.asset_id,
|
|
110
|
-
amount: resp.amount,
|
|
111
|
-
nonce: resp.nonce,
|
|
112
|
-
expiration_timestamp: resp.expiration_timestamp,
|
|
113
|
-
stark_signature: starkSignature
|
|
114
|
-
};
|
|
115
|
-
requests.push(req);
|
|
116
|
-
}
|
|
117
|
-
const transferSigningParams = {
|
|
118
|
-
sender_stark_key: signableResult.data.sender_stark_key,
|
|
119
|
-
requests
|
|
120
|
-
};
|
|
121
|
-
const response = await transfersApi.createTransfer({
|
|
122
|
-
createTransferRequestV2: transferSigningParams,
|
|
123
|
-
xImxEthAddress: ethAddress,
|
|
124
|
-
xImxEthSignature: ethSignature
|
|
125
|
-
});
|
|
126
|
-
return {
|
|
127
|
-
transfer_ids: response?.data.transfer_ids
|
|
128
|
-
};
|
|
129
|
-
}
|
|
130
|
-
async function createOrder({
|
|
131
|
-
signers,
|
|
132
|
-
request,
|
|
133
|
-
config
|
|
134
|
-
}) {
|
|
135
|
-
await validateChain(signers.ethSigner, config.immutableXConfig);
|
|
136
|
-
const ethAddress = await signers.ethSigner.getAddress();
|
|
137
|
-
const ordersApi = new imx.OrdersApi(config.immutableXConfig.apiConfiguration);
|
|
138
|
-
const amountSell = request.sell.type === "ERC721" ? "1" : request.sell.amount;
|
|
139
|
-
const amountBuy = request.buy.type === "ERC721" ? "1" : request.buy.amount;
|
|
140
|
-
const getSignableOrderRequest = {
|
|
141
|
-
user: ethAddress,
|
|
142
|
-
amount_buy: amountBuy,
|
|
143
|
-
token_buy: convertToSignableToken(request.buy),
|
|
144
|
-
amount_sell: amountSell,
|
|
145
|
-
token_sell: convertToSignableToken(request.sell),
|
|
146
|
-
fees: request.fees,
|
|
147
|
-
expiration_timestamp: request.expiration_timestamp
|
|
148
|
-
};
|
|
149
|
-
const getSignableOrderResponse = await ordersApi.getSignableOrder({
|
|
150
|
-
getSignableOrderRequestV3: getSignableOrderRequest
|
|
151
|
-
});
|
|
152
|
-
const { signable_message: signableMessage, payload_hash: payloadHash } = getSignableOrderResponse.data;
|
|
153
|
-
const ethSignature = await signRaw(signableMessage, signers.ethSigner);
|
|
154
|
-
const starkSignature = await signers.starkSigner.signMessage(payloadHash);
|
|
155
|
-
const resp = getSignableOrderResponse.data;
|
|
156
|
-
const orderParams = {
|
|
157
|
-
createOrderRequest: {
|
|
158
|
-
amount_buy: resp.amount_buy,
|
|
159
|
-
amount_sell: resp.amount_sell,
|
|
160
|
-
asset_id_buy: resp.asset_id_buy,
|
|
161
|
-
asset_id_sell: resp.asset_id_sell,
|
|
162
|
-
expiration_timestamp: resp.expiration_timestamp,
|
|
163
|
-
fees: request.fees,
|
|
164
|
-
nonce: resp.nonce,
|
|
165
|
-
stark_key: resp.stark_key,
|
|
166
|
-
stark_signature: starkSignature,
|
|
167
|
-
vault_id_buy: resp.vault_id_buy,
|
|
168
|
-
vault_id_sell: resp.vault_id_sell
|
|
169
|
-
},
|
|
170
|
-
xImxEthAddress: ethAddress,
|
|
171
|
-
xImxEthSignature: ethSignature
|
|
172
|
-
};
|
|
173
|
-
const createOrderResponse = await ordersApi.createOrderV3(orderParams);
|
|
174
|
-
return {
|
|
175
|
-
...createOrderResponse.data
|
|
176
|
-
};
|
|
177
|
-
}
|
|
178
|
-
async function cancelOrder({
|
|
179
|
-
signers,
|
|
180
|
-
request,
|
|
181
|
-
config
|
|
182
|
-
}) {
|
|
183
|
-
const ordersApi = new imx.OrdersApi(config.immutableXConfig.apiConfiguration);
|
|
184
|
-
const getSignableCancelOrderResponse = await ordersApi.getSignableCancelOrderV3(
|
|
185
|
-
{
|
|
186
|
-
getSignableCancelOrderRequest: {
|
|
187
|
-
order_id: request.order_id
|
|
188
|
-
}
|
|
189
|
-
}
|
|
190
|
-
);
|
|
191
|
-
const { signable_message: signableMessage, payload_hash: payloadHash } = getSignableCancelOrderResponse.data;
|
|
192
|
-
const ethSignature = await signRaw(signableMessage, signers.ethSigner);
|
|
193
|
-
const starkSignature = await signers.starkSigner.signMessage(payloadHash);
|
|
194
|
-
const ethAddress = await signers.ethSigner.getAddress();
|
|
195
|
-
const cancelOrderResponse = await ordersApi.cancelOrderV3({
|
|
196
|
-
id: request.order_id.toString(),
|
|
197
|
-
cancelOrderRequest: {
|
|
198
|
-
order_id: request.order_id,
|
|
199
|
-
stark_signature: starkSignature
|
|
200
|
-
},
|
|
201
|
-
xImxEthAddress: ethAddress,
|
|
202
|
-
xImxEthSignature: ethSignature
|
|
203
|
-
});
|
|
204
|
-
return {
|
|
205
|
-
order_id: cancelOrderResponse.data.order_id,
|
|
206
|
-
status: cancelOrderResponse.data.status
|
|
207
|
-
};
|
|
208
|
-
}
|
|
209
|
-
async function registerOffchain(signers, config) {
|
|
210
|
-
await validateChain(signers.ethSigner, config.immutableXConfig);
|
|
211
|
-
const usersApi = new imx.UsersApi(config.immutableXConfig.apiConfiguration);
|
|
212
|
-
const userAddress = await signers.ethSigner.getAddress();
|
|
213
|
-
const starkPublicKey = await signers.starkSigner.getAddress();
|
|
214
|
-
const signableResult = await usersApi.getSignableRegistrationOffchain({
|
|
215
|
-
getSignableRegistrationRequest: {
|
|
216
|
-
ether_key: userAddress,
|
|
217
|
-
stark_key: starkPublicKey
|
|
218
|
-
}
|
|
219
|
-
});
|
|
220
|
-
const { signable_message: signableMessage, payload_hash: payloadHash } = signableResult.data;
|
|
221
|
-
const ethSignature = await signRaw(signableMessage, signers.ethSigner);
|
|
222
|
-
const starkSignature = await signers.starkSigner.signMessage(payloadHash);
|
|
223
|
-
const registeredUser = await usersApi.registerUser({
|
|
224
|
-
registerUserRequest: {
|
|
225
|
-
eth_signature: ethSignature,
|
|
226
|
-
ether_key: userAddress,
|
|
227
|
-
stark_signature: starkSignature,
|
|
228
|
-
stark_key: starkPublicKey
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
return registeredUser.data;
|
|
232
|
-
}
|
|
233
|
-
async function isRegisteredOffchain(ethAddress, config) {
|
|
234
|
-
try {
|
|
235
|
-
const usersApi = new imx.UsersApi(config.immutableXConfig.apiConfiguration);
|
|
236
|
-
const getUsersResult = await usersApi.getUsers({
|
|
237
|
-
user: ethAddress
|
|
238
|
-
});
|
|
239
|
-
const { accounts } = getUsersResult.data;
|
|
240
|
-
return accounts?.length > 0;
|
|
241
|
-
} catch (ex) {
|
|
242
|
-
if (isAxiosError(ex) && ex.response?.status === 404) {
|
|
243
|
-
return false;
|
|
244
|
-
}
|
|
245
|
-
throw ex;
|
|
246
|
-
}
|
|
247
|
-
}
|
|
248
|
-
async function isRegisteredOnChain(starkPublicKey, ethSigner, config) {
|
|
249
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
250
|
-
const imxConfig = config.immutableXConfig;
|
|
251
|
-
const registrationContract = Contracts.RegistrationV4.connect(
|
|
252
|
-
imxConfig.ethConfiguration.registrationV4ContractAddress || imxConfig.ethConfiguration.registrationContractAddress,
|
|
253
|
-
ethSigner
|
|
254
|
-
);
|
|
255
|
-
try {
|
|
256
|
-
return await registrationContract.isRegistered(starkPublicKey);
|
|
257
|
-
} catch (ex) {
|
|
258
|
-
if (ex.reason === "USER_UNREGISTERED") {
|
|
259
|
-
return false;
|
|
260
|
-
}
|
|
261
|
-
throw ex;
|
|
262
|
-
}
|
|
263
|
-
}
|
|
264
|
-
var assertIsDefined = (value) => {
|
|
265
|
-
if (value !== void 0) return value;
|
|
266
|
-
throw new Error("undefined field exception");
|
|
267
|
-
};
|
|
268
|
-
async function prepareWithdrawalAction(params, withdrawalsApi) {
|
|
269
|
-
const {
|
|
270
|
-
signers: { ethSigner, starkSigner }
|
|
271
|
-
} = params;
|
|
272
|
-
await validateChain(ethSigner, params.config);
|
|
273
|
-
const withdrawalAmount = params.type === "ERC721" ? "1" : params.amount;
|
|
274
|
-
const signableWithdrawalResult = await withdrawalsApi.getSignableWithdrawalV2(
|
|
275
|
-
{
|
|
276
|
-
getSignableWithdrawalRequest: {
|
|
277
|
-
user: await ethSigner.getAddress(),
|
|
278
|
-
token: convertToSignableToken(params),
|
|
279
|
-
amount: withdrawalAmount
|
|
280
|
-
}
|
|
281
|
-
}
|
|
282
|
-
);
|
|
283
|
-
const { signable_message: signableMessage, payload_hash: payloadHash } = signableWithdrawalResult.data;
|
|
284
|
-
const starkSignature = await starkSigner.signMessage(payloadHash);
|
|
285
|
-
const { ethAddress, ethSignature } = await signMessage(
|
|
286
|
-
signableMessage,
|
|
287
|
-
ethSigner
|
|
288
|
-
);
|
|
289
|
-
const prepareWithdrawalResponse = await withdrawalsApi.createWithdrawalV2({
|
|
290
|
-
createWithdrawalRequestV2: {
|
|
291
|
-
sender_stark_key: assertIsDefined(
|
|
292
|
-
signableWithdrawalResult.data.sender_stark_key
|
|
293
|
-
),
|
|
294
|
-
sender_vault_id: assertIsDefined(
|
|
295
|
-
signableWithdrawalResult.data.sender_vault_id
|
|
296
|
-
),
|
|
297
|
-
receiver_stark_key: assertIsDefined(
|
|
298
|
-
signableWithdrawalResult.data.receiver_stark_key
|
|
299
|
-
),
|
|
300
|
-
receiver_vault_id: assertIsDefined(
|
|
301
|
-
signableWithdrawalResult.data.receiver_vault_id
|
|
302
|
-
),
|
|
303
|
-
amount: withdrawalAmount,
|
|
304
|
-
asset_id: assertIsDefined(signableWithdrawalResult.data.asset_id),
|
|
305
|
-
expiration_timestamp: assertIsDefined(
|
|
306
|
-
signableWithdrawalResult.data.expiration_timestamp
|
|
307
|
-
),
|
|
308
|
-
nonce: assertIsDefined(signableWithdrawalResult.data.nonce),
|
|
309
|
-
stark_signature: starkSignature
|
|
310
|
-
},
|
|
311
|
-
xImxEthAddress: ethAddress,
|
|
312
|
-
xImxEthSignature: ethSignature
|
|
313
|
-
});
|
|
314
|
-
return prepareWithdrawalResponse.data;
|
|
315
|
-
}
|
|
316
|
-
async function getEncodeAssetInfo(assetType, tokenType, config, tokenData) {
|
|
317
|
-
const encodingApi = new imx.EncodingApi(config.apiConfiguration);
|
|
318
|
-
const result = await encodingApi.encodeAsset({
|
|
319
|
-
assetType,
|
|
320
|
-
encodeAssetRequest: {
|
|
321
|
-
token: {
|
|
322
|
-
type: tokenType,
|
|
323
|
-
...tokenData && { data: tokenData }
|
|
324
|
-
}
|
|
325
|
-
}
|
|
326
|
-
});
|
|
327
|
-
return result.data;
|
|
328
|
-
}
|
|
329
|
-
async function getWithdrawalBalance(signer, ownerKey, assetId, config) {
|
|
330
|
-
const coreContract = Contracts.CoreV4.connect(
|
|
331
|
-
config.ethConfiguration.coreContractAddress,
|
|
332
|
-
signer
|
|
333
|
-
);
|
|
334
|
-
return coreContract.getWithdrawalBalance(ownerKey, assetId);
|
|
335
|
-
}
|
|
336
|
-
async function getETHWithdrawalBalance(signer, ownerKey, config) {
|
|
337
|
-
const assetType = await getEncodeAssetInfo("asset", "ETH", config);
|
|
338
|
-
return await getWithdrawalBalance(
|
|
339
|
-
signer,
|
|
340
|
-
ownerKey,
|
|
341
|
-
assetType.asset_id,
|
|
342
|
-
config
|
|
343
|
-
);
|
|
344
|
-
}
|
|
345
|
-
async function getERC20WithdrawalBalance(signer, ownerKey, tokenAddress, config) {
|
|
346
|
-
const assetType = await getEncodeAssetInfo("asset", "ERC20", config, {
|
|
347
|
-
token_address: tokenAddress
|
|
348
|
-
});
|
|
349
|
-
return await getWithdrawalBalance(
|
|
350
|
-
signer,
|
|
351
|
-
ownerKey,
|
|
352
|
-
assetType.asset_id,
|
|
353
|
-
config
|
|
354
|
-
);
|
|
355
|
-
}
|
|
356
|
-
async function getERC721WithdrawalBalance(signer, ownerKey, token, encodingApi, mintsApi, config) {
|
|
357
|
-
try {
|
|
358
|
-
const mintableToken = await mintsApi.getMintableTokenDetailsByClientTokenId({
|
|
359
|
-
tokenAddress: token.tokenAddress,
|
|
360
|
-
tokenId: token.tokenId
|
|
361
|
-
});
|
|
362
|
-
const assetType = await getEncodeAssetInfo(
|
|
363
|
-
"mintable-asset",
|
|
364
|
-
"ERC721",
|
|
365
|
-
config,
|
|
366
|
-
{
|
|
367
|
-
id: token.tokenId,
|
|
368
|
-
token_address: token.tokenAddress,
|
|
369
|
-
...mintableToken.data.blueprint && {
|
|
370
|
-
blueprint: mintableToken.data.blueprint
|
|
371
|
-
}
|
|
372
|
-
}
|
|
373
|
-
);
|
|
374
|
-
return await getWithdrawalBalance(
|
|
375
|
-
signer,
|
|
376
|
-
ownerKey,
|
|
377
|
-
assetType.asset_id,
|
|
378
|
-
config
|
|
379
|
-
);
|
|
380
|
-
} catch (error) {
|
|
381
|
-
if (error.response?.status === 404) {
|
|
382
|
-
const assetType = await getEncodeAssetInfo(
|
|
383
|
-
"asset",
|
|
384
|
-
"ERC721",
|
|
385
|
-
config,
|
|
386
|
-
{
|
|
387
|
-
token_id: token.tokenId,
|
|
388
|
-
token_address: token.tokenAddress
|
|
389
|
-
}
|
|
390
|
-
);
|
|
391
|
-
return await getWithdrawalBalance(
|
|
392
|
-
signer,
|
|
393
|
-
ownerKey,
|
|
394
|
-
assetType.asset_id,
|
|
395
|
-
config
|
|
396
|
-
);
|
|
397
|
-
}
|
|
398
|
-
throw error;
|
|
399
|
-
}
|
|
400
|
-
}
|
|
401
|
-
async function getWithdrawalBalanceWorkflow(signer, ownerKey, token, encodingApi, mintsApi, config) {
|
|
402
|
-
switch (token.type) {
|
|
403
|
-
case "ETH":
|
|
404
|
-
return await getETHWithdrawalBalance(
|
|
405
|
-
signer,
|
|
406
|
-
ownerKey,
|
|
407
|
-
config
|
|
408
|
-
);
|
|
409
|
-
case "ERC20":
|
|
410
|
-
return await getERC20WithdrawalBalance(
|
|
411
|
-
signer,
|
|
412
|
-
ownerKey,
|
|
413
|
-
token.tokenAddress,
|
|
414
|
-
config
|
|
415
|
-
);
|
|
416
|
-
case "ERC721":
|
|
417
|
-
return await getERC721WithdrawalBalance(
|
|
418
|
-
signer,
|
|
419
|
-
ownerKey,
|
|
420
|
-
token,
|
|
421
|
-
encodingApi,
|
|
422
|
-
mintsApi,
|
|
423
|
-
config
|
|
424
|
-
);
|
|
425
|
-
default:
|
|
426
|
-
throw new Error("Unsupported token type");
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
async function getWithdrawalBalances(signer, starkPublicKey, ethAddress, token, config) {
|
|
430
|
-
const encodingApi = new EncodingApi(config.apiConfiguration);
|
|
431
|
-
const mintsApi = new MintsApi(config.apiConfiguration);
|
|
432
|
-
const v3Balance = await getWithdrawalBalanceWorkflow(
|
|
433
|
-
signer,
|
|
434
|
-
starkPublicKey,
|
|
435
|
-
token,
|
|
436
|
-
encodingApi,
|
|
437
|
-
mintsApi,
|
|
438
|
-
config
|
|
439
|
-
);
|
|
440
|
-
const v4Balance = await getWithdrawalBalanceWorkflow(
|
|
441
|
-
signer,
|
|
442
|
-
ethAddress,
|
|
443
|
-
token,
|
|
444
|
-
encodingApi,
|
|
445
|
-
mintsApi,
|
|
446
|
-
config
|
|
447
|
-
);
|
|
448
|
-
return {
|
|
449
|
-
v3Balance,
|
|
450
|
-
v4Balance
|
|
451
|
-
};
|
|
452
|
-
}
|
|
453
|
-
async function getWithdrawalBalancesERC721(signer, starkPublicKey, ethAddress, token, config, mintsApi) {
|
|
454
|
-
const encodingApi = new EncodingApi(config.apiConfiguration);
|
|
455
|
-
const v3Balance = await getWithdrawalBalanceWorkflow(
|
|
456
|
-
signer,
|
|
457
|
-
starkPublicKey,
|
|
458
|
-
token,
|
|
459
|
-
encodingApi,
|
|
460
|
-
mintsApi,
|
|
461
|
-
config
|
|
462
|
-
);
|
|
463
|
-
const v4Balance = await getWithdrawalBalanceWorkflow(
|
|
464
|
-
signer,
|
|
465
|
-
ethAddress,
|
|
466
|
-
token,
|
|
467
|
-
encodingApi,
|
|
468
|
-
mintsApi,
|
|
469
|
-
config
|
|
470
|
-
);
|
|
471
|
-
return {
|
|
472
|
-
v3Balance,
|
|
473
|
-
v4Balance
|
|
474
|
-
};
|
|
475
|
-
}
|
|
476
|
-
|
|
477
|
-
// src/signable-actions/withdrawal-actions/completeERC20Withdrawal.ts
|
|
478
|
-
var ERC20TokenType = "ERC20";
|
|
479
|
-
async function executeRegisterAndWithdrawAllFungible(ethSigner, starkSigner, starkPublicKey, assetType, config) {
|
|
480
|
-
const etherKey = await ethSigner.getAddress();
|
|
481
|
-
const starkSignature = await signRegisterEthAddress(
|
|
482
|
-
starkSigner,
|
|
483
|
-
etherKey,
|
|
484
|
-
starkPublicKey
|
|
485
|
-
);
|
|
486
|
-
const contract = Contracts.RegistrationV4.connect(
|
|
487
|
-
config.ethConfiguration.registrationV4ContractAddress || config.ethConfiguration.registrationContractAddress,
|
|
488
|
-
ethSigner
|
|
489
|
-
);
|
|
490
|
-
const populatedTransaction = await contract.registerAndWithdrawAll.populateTransaction(
|
|
491
|
-
etherKey,
|
|
492
|
-
starkPublicKey,
|
|
493
|
-
starkSignature,
|
|
494
|
-
assetType
|
|
495
|
-
);
|
|
496
|
-
return ethSigner.sendTransaction(populatedTransaction);
|
|
497
|
-
}
|
|
498
|
-
async function executeWithdrawAllFungible(ethSigner, starkPublicKey, assetType, config) {
|
|
499
|
-
const contract = Contracts.RegistrationV4.connect(
|
|
500
|
-
config.ethConfiguration.registrationV4ContractAddress || config.ethConfiguration.registrationContractAddress,
|
|
501
|
-
ethSigner
|
|
502
|
-
);
|
|
503
|
-
const populatedTransaction = await contract.withdrawAll.populateTransaction(
|
|
504
|
-
await ethSigner.getAddress(),
|
|
505
|
-
starkPublicKey,
|
|
506
|
-
assetType
|
|
507
|
-
);
|
|
508
|
-
return ethSigner.sendTransaction(populatedTransaction);
|
|
509
|
-
}
|
|
510
|
-
async function executeWithdrawFungible(ethSigner, starkPublicKey, assetType, config) {
|
|
511
|
-
const contract = Contracts.CoreV4.connect(
|
|
512
|
-
config.ethConfiguration.coreContractAddress,
|
|
513
|
-
ethSigner
|
|
514
|
-
);
|
|
515
|
-
const populatedTransaction = await contract.withdraw.populateTransaction(
|
|
516
|
-
await ethSigner.getAddress(),
|
|
517
|
-
assetType
|
|
518
|
-
);
|
|
519
|
-
return ethSigner.sendTransaction(populatedTransaction);
|
|
520
|
-
}
|
|
521
|
-
async function completeERC20WithdrawalAction({
|
|
522
|
-
ethSigner,
|
|
523
|
-
starkSigner,
|
|
524
|
-
starkPublicKey,
|
|
525
|
-
token,
|
|
526
|
-
config
|
|
527
|
-
}) {
|
|
528
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
529
|
-
const {
|
|
530
|
-
v3Balance,
|
|
531
|
-
v4Balance
|
|
532
|
-
} = await getWithdrawalBalances(
|
|
533
|
-
ethSigner,
|
|
534
|
-
starkPublicKey,
|
|
535
|
-
await ethSigner.getAddress(),
|
|
536
|
-
{
|
|
537
|
-
type: ERC20TokenType,
|
|
538
|
-
tokenAddress: token.tokenAddress
|
|
539
|
-
},
|
|
540
|
-
config.immutableXConfig
|
|
541
|
-
);
|
|
542
|
-
const assetType = await getEncodeAssetInfo("asset", ERC20TokenType, config.immutableXConfig, {
|
|
543
|
-
token_address: token.tokenAddress
|
|
544
|
-
});
|
|
545
|
-
if (v3Balance > 0) {
|
|
546
|
-
const isRegistered = await isRegisteredOnChain(
|
|
547
|
-
starkPublicKey,
|
|
548
|
-
ethSigner,
|
|
549
|
-
config
|
|
550
|
-
);
|
|
551
|
-
if (isRegistered) {
|
|
552
|
-
return executeWithdrawAllFungible(ethSigner, starkPublicKey, assetType.asset_type, config.immutableXConfig);
|
|
553
|
-
}
|
|
554
|
-
return executeRegisterAndWithdrawAllFungible(
|
|
555
|
-
ethSigner,
|
|
556
|
-
starkSigner,
|
|
557
|
-
starkPublicKey,
|
|
558
|
-
assetType.asset_type,
|
|
559
|
-
config.immutableXConfig
|
|
560
|
-
);
|
|
561
|
-
}
|
|
562
|
-
if (v4Balance > 0) {
|
|
563
|
-
return executeWithdrawFungible(ethSigner, starkPublicKey, assetType.asset_type, config.immutableXConfig);
|
|
564
|
-
}
|
|
565
|
-
throw new Error("No balance to withdraw");
|
|
566
|
-
}
|
|
567
|
-
var ERC721TokenType = "ERC721";
|
|
568
|
-
function getMintingBlob(token) {
|
|
569
|
-
const { id } = token.data;
|
|
570
|
-
const blueprint = token.data.blueprint || "";
|
|
571
|
-
return encUtils.sanitizeHex(encUtils.utf8ToHex(`{${id}}:{${blueprint}}`));
|
|
572
|
-
}
|
|
573
|
-
async function executeERC721RegisterAndWithdraw(ethSigner, starkSigner, token, config) {
|
|
574
|
-
const etherKey = await ethSigner.getAddress();
|
|
575
|
-
const starkPublicKey = await starkSigner.getAddress();
|
|
576
|
-
const assetType = await getEncodeAssetInfo("asset", ERC721TokenType, config, {
|
|
577
|
-
token_id: token.tokenId,
|
|
578
|
-
token_address: token.tokenAddress
|
|
579
|
-
});
|
|
580
|
-
const registrationStarkSignature = await signRegisterEthAddress(
|
|
581
|
-
starkSigner,
|
|
582
|
-
etherKey,
|
|
583
|
-
starkPublicKey
|
|
584
|
-
);
|
|
585
|
-
const contract = Contracts.RegistrationV4.connect(
|
|
586
|
-
config.ethConfiguration.registrationV4ContractAddress || config.ethConfiguration.registrationContractAddress,
|
|
587
|
-
ethSigner
|
|
588
|
-
);
|
|
589
|
-
const populatedTransaction = await contract.registerAndWithdrawNft.populateTransaction(
|
|
590
|
-
etherKey,
|
|
591
|
-
starkPublicKey,
|
|
592
|
-
registrationStarkSignature,
|
|
593
|
-
assetType.asset_type,
|
|
594
|
-
token.tokenId
|
|
595
|
-
);
|
|
596
|
-
return ethSigner.sendTransaction(populatedTransaction);
|
|
597
|
-
}
|
|
598
|
-
async function executeMintableERC721RegisterAndWithdraw(ethSigner, starkSigner, token, config) {
|
|
599
|
-
const etherKey = await ethSigner.getAddress();
|
|
600
|
-
const starkPublicKey = await starkSigner.getAddress();
|
|
601
|
-
const assetType = await getEncodeAssetInfo(
|
|
602
|
-
"mintable-asset",
|
|
603
|
-
ERC721TokenType,
|
|
604
|
-
config,
|
|
605
|
-
{
|
|
606
|
-
id: token.data.id,
|
|
607
|
-
token_address: token.data.tokenAddress,
|
|
608
|
-
...token.data.blueprint && { blueprint: token.data.blueprint }
|
|
609
|
-
}
|
|
610
|
-
);
|
|
611
|
-
const mintingBlob = getMintingBlob(token);
|
|
612
|
-
const starkSignature = await signRegisterEthAddress(
|
|
613
|
-
starkSigner,
|
|
614
|
-
etherKey,
|
|
615
|
-
starkPublicKey
|
|
616
|
-
);
|
|
617
|
-
const contract = Contracts.RegistrationV4.connect(
|
|
618
|
-
config.ethConfiguration.registrationV4ContractAddress || config.ethConfiguration.registrationContractAddress,
|
|
619
|
-
ethSigner
|
|
620
|
-
);
|
|
621
|
-
const populatedTransaction = await contract.registerWithdrawAndMint.populateTransaction(
|
|
622
|
-
etherKey,
|
|
623
|
-
starkPublicKey,
|
|
624
|
-
starkSignature,
|
|
625
|
-
assetType.asset_type,
|
|
626
|
-
mintingBlob
|
|
627
|
-
);
|
|
628
|
-
return ethSigner.sendTransaction(populatedTransaction);
|
|
629
|
-
}
|
|
630
|
-
async function completeERC721RegisterAndWithdrawal(mintsApi, ethSigner, starkSigner, token, config) {
|
|
631
|
-
return mintsApi.getMintableTokenDetailsByClientTokenId({
|
|
632
|
-
tokenAddress: token.tokenAddress,
|
|
633
|
-
tokenId: token.tokenId
|
|
634
|
-
}).then((mintableToken) => executeMintableERC721RegisterAndWithdraw(ethSigner, starkSigner, {
|
|
635
|
-
type: ERC721TokenType,
|
|
636
|
-
data: {
|
|
637
|
-
id: token.tokenId,
|
|
638
|
-
tokenAddress: token.tokenAddress,
|
|
639
|
-
blueprint: mintableToken.data.blueprint
|
|
640
|
-
}
|
|
641
|
-
}, config)).catch((error) => {
|
|
642
|
-
if (error.response?.status === 404) {
|
|
643
|
-
return executeERC721RegisterAndWithdraw(ethSigner, starkSigner, token, config);
|
|
644
|
-
}
|
|
645
|
-
throw error;
|
|
646
|
-
});
|
|
647
|
-
}
|
|
648
|
-
async function executeMintableERC721Withdrawal(ethSigner, ownerKey, token, config) {
|
|
649
|
-
const assetType = await getEncodeAssetInfo(
|
|
650
|
-
"mintable-asset",
|
|
651
|
-
ERC721TokenType,
|
|
652
|
-
config,
|
|
653
|
-
{
|
|
654
|
-
id: token.data.id,
|
|
655
|
-
token_address: token.data.tokenAddress,
|
|
656
|
-
...token.data.blueprint && { blueprint: token.data.blueprint }
|
|
657
|
-
}
|
|
658
|
-
);
|
|
659
|
-
const mintingBlob = getMintingBlob(token);
|
|
660
|
-
const contract = Contracts.CoreV4.connect(
|
|
661
|
-
config.ethConfiguration.coreContractAddress,
|
|
662
|
-
ethSigner
|
|
663
|
-
);
|
|
664
|
-
const populatedTransaction = await contract.withdrawAndMint.populateTransaction(
|
|
665
|
-
ownerKey,
|
|
666
|
-
assetType.asset_type,
|
|
667
|
-
mintingBlob
|
|
668
|
-
);
|
|
669
|
-
return ethSigner.sendTransaction(populatedTransaction);
|
|
670
|
-
}
|
|
671
|
-
async function executeERC721Withdrawal(ethSigner, ownerKey, token, config) {
|
|
672
|
-
const assetType = await getEncodeAssetInfo("asset", ERC721TokenType, config, {
|
|
673
|
-
token_id: token.tokenId,
|
|
674
|
-
token_address: token.tokenAddress
|
|
675
|
-
});
|
|
676
|
-
const contract = Contracts.CoreV4.connect(
|
|
677
|
-
config.ethConfiguration.coreContractAddress,
|
|
678
|
-
ethSigner
|
|
679
|
-
);
|
|
680
|
-
const populatedTransaction = await contract.withdrawNft.populateTransaction(
|
|
681
|
-
ownerKey,
|
|
682
|
-
assetType.asset_type,
|
|
683
|
-
token.tokenId
|
|
684
|
-
);
|
|
685
|
-
return ethSigner.sendTransaction(populatedTransaction);
|
|
686
|
-
}
|
|
687
|
-
async function completeERC721Withdrawal(mintsApi, ethSigner, ownerKey, token, config) {
|
|
688
|
-
return mintsApi.getMintableTokenDetailsByClientTokenId({
|
|
689
|
-
tokenAddress: token.tokenAddress,
|
|
690
|
-
tokenId: token.tokenId
|
|
691
|
-
}).then((mintableToken) => executeMintableERC721Withdrawal(
|
|
692
|
-
ethSigner,
|
|
693
|
-
ownerKey,
|
|
694
|
-
{
|
|
695
|
-
type: ERC721TokenType,
|
|
696
|
-
data: {
|
|
697
|
-
id: token.tokenId,
|
|
698
|
-
tokenAddress: token.tokenAddress,
|
|
699
|
-
blueprint: mintableToken.data.blueprint
|
|
700
|
-
}
|
|
701
|
-
},
|
|
702
|
-
config
|
|
703
|
-
)).catch((error) => {
|
|
704
|
-
if (error.response?.status === 404) {
|
|
705
|
-
return executeERC721Withdrawal(
|
|
706
|
-
ethSigner,
|
|
707
|
-
ownerKey,
|
|
708
|
-
token,
|
|
709
|
-
config
|
|
710
|
-
);
|
|
711
|
-
}
|
|
712
|
-
throw error;
|
|
713
|
-
});
|
|
714
|
-
}
|
|
715
|
-
async function completeERC721WithdrawalAction({
|
|
716
|
-
ethSigner,
|
|
717
|
-
starkSigner,
|
|
718
|
-
starkPublicKey,
|
|
719
|
-
token,
|
|
720
|
-
config
|
|
721
|
-
}, mintsApi) {
|
|
722
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
723
|
-
const ethAddress = await ethSigner.getAddress();
|
|
724
|
-
const {
|
|
725
|
-
v3Balance,
|
|
726
|
-
v4Balance
|
|
727
|
-
} = await getWithdrawalBalancesERC721(
|
|
728
|
-
ethSigner,
|
|
729
|
-
starkPublicKey,
|
|
730
|
-
ethAddress,
|
|
731
|
-
{
|
|
732
|
-
type: ERC721TokenType,
|
|
733
|
-
tokenAddress: token.tokenAddress,
|
|
734
|
-
tokenId: token.tokenId
|
|
735
|
-
},
|
|
736
|
-
config.immutableXConfig,
|
|
737
|
-
mintsApi
|
|
738
|
-
);
|
|
739
|
-
if (v3Balance > 0) {
|
|
740
|
-
const isRegistered = await isRegisteredOnChain(
|
|
741
|
-
starkPublicKey,
|
|
742
|
-
ethSigner,
|
|
743
|
-
config
|
|
744
|
-
);
|
|
745
|
-
if (isRegistered) {
|
|
746
|
-
return completeERC721Withdrawal(mintsApi, ethSigner, starkPublicKey, token, config.immutableXConfig);
|
|
747
|
-
}
|
|
748
|
-
return completeERC721RegisterAndWithdrawal(
|
|
749
|
-
mintsApi,
|
|
750
|
-
ethSigner,
|
|
751
|
-
starkSigner,
|
|
752
|
-
token,
|
|
753
|
-
config.immutableXConfig
|
|
754
|
-
);
|
|
755
|
-
}
|
|
756
|
-
if (v4Balance > 0) {
|
|
757
|
-
return completeERC721Withdrawal(mintsApi, ethSigner, ethAddress, token, config.immutableXConfig);
|
|
758
|
-
}
|
|
759
|
-
throw new Error("No balance to withdraw");
|
|
760
|
-
}
|
|
761
|
-
|
|
762
|
-
// src/signable-actions/withdrawal-actions/completeEthWithdrawal.ts
|
|
763
|
-
var EthTokenType = "ETH";
|
|
764
|
-
async function completeEthWithdrawalAction({
|
|
765
|
-
ethSigner,
|
|
766
|
-
starkSigner,
|
|
767
|
-
starkPublicKey,
|
|
768
|
-
config
|
|
769
|
-
}) {
|
|
770
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
771
|
-
const {
|
|
772
|
-
v3Balance,
|
|
773
|
-
v4Balance
|
|
774
|
-
} = await getWithdrawalBalances(
|
|
775
|
-
ethSigner,
|
|
776
|
-
starkPublicKey,
|
|
777
|
-
await ethSigner.getAddress(),
|
|
778
|
-
{ type: EthTokenType },
|
|
779
|
-
config.immutableXConfig
|
|
780
|
-
);
|
|
781
|
-
const assetType = await getEncodeAssetInfo("asset", EthTokenType, config.immutableXConfig);
|
|
782
|
-
if (v3Balance > 0) {
|
|
783
|
-
const isRegistered = await isRegisteredOnChain(
|
|
784
|
-
starkPublicKey,
|
|
785
|
-
ethSigner,
|
|
786
|
-
config
|
|
787
|
-
);
|
|
788
|
-
if (isRegistered) {
|
|
789
|
-
return executeWithdrawAllFungible(ethSigner, starkPublicKey, assetType.asset_type, config.immutableXConfig);
|
|
790
|
-
}
|
|
791
|
-
return executeRegisterAndWithdrawAllFungible(
|
|
792
|
-
ethSigner,
|
|
793
|
-
starkSigner,
|
|
794
|
-
starkPublicKey,
|
|
795
|
-
assetType.asset_type,
|
|
796
|
-
config.immutableXConfig
|
|
797
|
-
);
|
|
798
|
-
}
|
|
799
|
-
if (v4Balance > 0) {
|
|
800
|
-
return executeWithdrawFungible(ethSigner, starkPublicKey, assetType.asset_type, config.immutableXConfig);
|
|
801
|
-
}
|
|
802
|
-
throw new Error("No balance to withdraw");
|
|
803
|
-
}
|
|
804
|
-
|
|
805
|
-
// src/signable-actions/withdrawal.ts
|
|
806
|
-
async function prepareWithdrawal({
|
|
807
|
-
signers,
|
|
808
|
-
withdrawal,
|
|
809
|
-
config
|
|
810
|
-
}) {
|
|
811
|
-
const withdrawalsApi = new imx.WithdrawalsApi(config.immutableXConfig.apiConfiguration);
|
|
812
|
-
return prepareWithdrawalAction({
|
|
813
|
-
signers,
|
|
814
|
-
config: config.immutableXConfig,
|
|
815
|
-
...withdrawal
|
|
816
|
-
}, withdrawalsApi);
|
|
817
|
-
}
|
|
818
|
-
async function completeWithdrawal({
|
|
819
|
-
signers: { ethSigner, starkSigner },
|
|
820
|
-
starkPublicKey,
|
|
821
|
-
token,
|
|
822
|
-
config
|
|
823
|
-
}) {
|
|
824
|
-
const mintsApi = new imx.MintsApi(config.immutableXConfig.apiConfiguration);
|
|
825
|
-
switch (token.type) {
|
|
826
|
-
case "ETH":
|
|
827
|
-
return completeEthWithdrawalAction({
|
|
828
|
-
ethSigner,
|
|
829
|
-
starkSigner,
|
|
830
|
-
starkPublicKey,
|
|
831
|
-
config
|
|
832
|
-
});
|
|
833
|
-
case "ERC20":
|
|
834
|
-
return completeERC20WithdrawalAction({
|
|
835
|
-
ethSigner,
|
|
836
|
-
starkSigner,
|
|
837
|
-
starkPublicKey,
|
|
838
|
-
token,
|
|
839
|
-
config
|
|
840
|
-
});
|
|
841
|
-
case "ERC721":
|
|
842
|
-
return completeERC721WithdrawalAction({
|
|
843
|
-
ethSigner,
|
|
844
|
-
starkSigner,
|
|
845
|
-
starkPublicKey,
|
|
846
|
-
token,
|
|
847
|
-
config
|
|
848
|
-
}, mintsApi);
|
|
849
|
-
}
|
|
850
|
-
}
|
|
851
|
-
async function createTrade({
|
|
852
|
-
signers: { ethSigner, starkSigner },
|
|
853
|
-
request,
|
|
854
|
-
config
|
|
855
|
-
}) {
|
|
856
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
857
|
-
const ethAddress = await ethSigner.getAddress();
|
|
858
|
-
const tradesApi = new imx.TradesApi(config.immutableXConfig.apiConfiguration);
|
|
859
|
-
const signableResult = await tradesApi.getSignableTrade({
|
|
860
|
-
getSignableTradeRequest: {
|
|
861
|
-
user: ethAddress,
|
|
862
|
-
order_id: request.order_id,
|
|
863
|
-
fees: request.fees
|
|
864
|
-
}
|
|
865
|
-
});
|
|
866
|
-
const { signable_message: signableMessage, payload_hash: payloadHash } = signableResult.data;
|
|
867
|
-
const ethSignature = await signRaw(signableMessage, ethSigner);
|
|
868
|
-
const starkSignature = await starkSigner.signMessage(payloadHash);
|
|
869
|
-
const createTradeResponse = await tradesApi.createTradeV3({
|
|
870
|
-
createTradeRequest: {
|
|
871
|
-
amount_buy: signableResult.data.amount_buy,
|
|
872
|
-
amount_sell: signableResult.data.amount_sell,
|
|
873
|
-
asset_id_buy: signableResult.data.asset_id_buy,
|
|
874
|
-
asset_id_sell: signableResult.data.asset_id_sell,
|
|
875
|
-
expiration_timestamp: signableResult.data.expiration_timestamp,
|
|
876
|
-
fee_info: signableResult.data.fee_info,
|
|
877
|
-
fees: request.fees,
|
|
878
|
-
include_fees: true,
|
|
879
|
-
nonce: signableResult.data.nonce,
|
|
880
|
-
order_id: request.order_id,
|
|
881
|
-
stark_key: signableResult.data.stark_key,
|
|
882
|
-
vault_id_buy: signableResult.data.vault_id_buy,
|
|
883
|
-
vault_id_sell: signableResult.data.vault_id_sell,
|
|
884
|
-
stark_signature: starkSignature
|
|
885
|
-
},
|
|
886
|
-
xImxEthAddress: ethAddress,
|
|
887
|
-
xImxEthSignature: ethSignature
|
|
888
|
-
});
|
|
889
|
-
return createTradeResponse.data;
|
|
890
|
-
}
|
|
891
|
-
async function executeDepositEth(ethSigner, amount, assetType, starkPublicKey, vaultId, config) {
|
|
892
|
-
const coreContract = Contracts.CoreV4.connect(
|
|
893
|
-
config.ethConfiguration.coreContractAddress,
|
|
894
|
-
ethSigner
|
|
895
|
-
);
|
|
896
|
-
const populatedTransaction = await coreContract["deposit(uint256,uint256,uint256)"].populateTransaction(
|
|
897
|
-
starkPublicKey,
|
|
898
|
-
assetType,
|
|
899
|
-
vaultId
|
|
900
|
-
);
|
|
901
|
-
return ethSigner.sendTransaction({ ...populatedTransaction, value: amount });
|
|
902
|
-
}
|
|
903
|
-
async function depositEth({
|
|
904
|
-
signers: { ethSigner },
|
|
905
|
-
deposit: deposit2,
|
|
906
|
-
config
|
|
907
|
-
}) {
|
|
908
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
909
|
-
const user = await ethSigner.getAddress();
|
|
910
|
-
const data = {
|
|
911
|
-
decimals: 18
|
|
912
|
-
};
|
|
913
|
-
const amount = parseUnits(deposit2.amount, "wei");
|
|
914
|
-
const imxConfig = config.immutableXConfig;
|
|
915
|
-
const depositsApi = new imx.DepositsApi(imxConfig.apiConfiguration);
|
|
916
|
-
const encodingApi = new imx.EncodingApi(imxConfig.apiConfiguration);
|
|
917
|
-
const getSignableDepositRequest = {
|
|
918
|
-
user,
|
|
919
|
-
token: {
|
|
920
|
-
type: deposit2.type,
|
|
921
|
-
data
|
|
922
|
-
},
|
|
923
|
-
amount: amount.toString()
|
|
924
|
-
};
|
|
925
|
-
const signableDepositResult = await depositsApi.getSignableDeposit({
|
|
926
|
-
getSignableDepositRequest
|
|
927
|
-
});
|
|
928
|
-
const encodingResult = await encodingApi.encodeAsset({
|
|
929
|
-
assetType: "asset",
|
|
930
|
-
encodeAssetRequest: {
|
|
931
|
-
token: {
|
|
932
|
-
type: deposit2.type
|
|
933
|
-
}
|
|
934
|
-
}
|
|
935
|
-
});
|
|
936
|
-
const assetType = encodingResult.data.asset_type;
|
|
937
|
-
const starkPublicKey = signableDepositResult.data.stark_key;
|
|
938
|
-
const vaultId = signableDepositResult.data.vault_id;
|
|
939
|
-
return executeDepositEth(
|
|
940
|
-
ethSigner,
|
|
941
|
-
amount,
|
|
942
|
-
assetType,
|
|
943
|
-
starkPublicKey,
|
|
944
|
-
vaultId,
|
|
945
|
-
imxConfig
|
|
946
|
-
);
|
|
947
|
-
}
|
|
948
|
-
async function executeDepositERC20(ethSigner, quantizedAmount, assetType, starkPublicKey, vaultId, config) {
|
|
949
|
-
const coreContract = Contracts.CoreV4.connect(
|
|
950
|
-
config.coreContractAddress,
|
|
951
|
-
ethSigner
|
|
952
|
-
);
|
|
953
|
-
const populatedTransaction = await coreContract.depositERC20.populateTransaction(
|
|
954
|
-
starkPublicKey,
|
|
955
|
-
assetType,
|
|
956
|
-
vaultId,
|
|
957
|
-
quantizedAmount
|
|
958
|
-
);
|
|
959
|
-
return ethSigner.sendTransaction(populatedTransaction);
|
|
960
|
-
}
|
|
961
|
-
async function depositERC20({
|
|
962
|
-
signers: { ethSigner },
|
|
963
|
-
deposit: deposit2,
|
|
964
|
-
config
|
|
965
|
-
}) {
|
|
966
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
967
|
-
const { apiConfiguration, ethConfiguration } = config.immutableXConfig;
|
|
968
|
-
const user = await ethSigner.getAddress();
|
|
969
|
-
const tokensApi = new imx.TokensApi(apiConfiguration);
|
|
970
|
-
const depositsApi = new imx.DepositsApi(apiConfiguration);
|
|
971
|
-
const encodingApi = new imx.EncodingApi(apiConfiguration);
|
|
972
|
-
const token = await tokensApi.getToken({ address: deposit2.tokenAddress });
|
|
973
|
-
const decimals = parseInt(token.data.decimals);
|
|
974
|
-
const data = {
|
|
975
|
-
decimals,
|
|
976
|
-
token_address: deposit2.tokenAddress
|
|
977
|
-
};
|
|
978
|
-
const amount = parseUnits(deposit2.amount, 0);
|
|
979
|
-
const tokenContract = Contracts.IERC20.connect(
|
|
980
|
-
deposit2.tokenAddress,
|
|
981
|
-
ethSigner
|
|
982
|
-
);
|
|
983
|
-
const approveTransaction = await tokenContract.approve.populateTransaction(
|
|
984
|
-
ethConfiguration.coreContractAddress,
|
|
985
|
-
amount
|
|
986
|
-
);
|
|
987
|
-
await ethSigner.sendTransaction(approveTransaction);
|
|
988
|
-
const getSignableDepositRequest = {
|
|
989
|
-
user,
|
|
990
|
-
token: {
|
|
991
|
-
type: deposit2.type,
|
|
992
|
-
data
|
|
993
|
-
},
|
|
994
|
-
amount: amount.toString()
|
|
995
|
-
};
|
|
996
|
-
const signableDepositResult = await depositsApi.getSignableDeposit({
|
|
997
|
-
getSignableDepositRequest
|
|
998
|
-
});
|
|
999
|
-
const encodingResult = await encodingApi.encodeAsset({
|
|
1000
|
-
assetType: "asset",
|
|
1001
|
-
encodeAssetRequest: {
|
|
1002
|
-
token: {
|
|
1003
|
-
type: deposit2.type,
|
|
1004
|
-
data: {
|
|
1005
|
-
token_address: deposit2.tokenAddress
|
|
1006
|
-
}
|
|
1007
|
-
}
|
|
1008
|
-
}
|
|
1009
|
-
});
|
|
1010
|
-
const assetType = encodingResult.data.asset_type;
|
|
1011
|
-
const starkPublicKey = signableDepositResult.data.stark_key;
|
|
1012
|
-
const vaultId = signableDepositResult.data.vault_id;
|
|
1013
|
-
const quantizedAmount = BigInt(signableDepositResult.data.amount);
|
|
1014
|
-
return executeDepositERC20(
|
|
1015
|
-
ethSigner,
|
|
1016
|
-
quantizedAmount,
|
|
1017
|
-
assetType,
|
|
1018
|
-
starkPublicKey,
|
|
1019
|
-
vaultId,
|
|
1020
|
-
ethConfiguration
|
|
1021
|
-
);
|
|
1022
|
-
}
|
|
1023
|
-
async function executeDepositERC721(ethSigner, tokenId, assetType, starkPublicKey, vaultId, config) {
|
|
1024
|
-
const coreContract = Contracts.CoreV4.connect(
|
|
1025
|
-
config.ethConfiguration.coreContractAddress,
|
|
1026
|
-
ethSigner
|
|
1027
|
-
);
|
|
1028
|
-
const populatedTransaction = await coreContract.depositNft.populateTransaction(
|
|
1029
|
-
starkPublicKey,
|
|
1030
|
-
assetType,
|
|
1031
|
-
vaultId,
|
|
1032
|
-
tokenId
|
|
1033
|
-
);
|
|
1034
|
-
return ethSigner.sendTransaction(populatedTransaction);
|
|
1035
|
-
}
|
|
1036
|
-
async function depositERC721({
|
|
1037
|
-
signers: { ethSigner },
|
|
1038
|
-
deposit: deposit2,
|
|
1039
|
-
config
|
|
1040
|
-
}) {
|
|
1041
|
-
await validateChain(ethSigner, config.immutableXConfig);
|
|
1042
|
-
const user = await ethSigner.getAddress();
|
|
1043
|
-
const { immutableXConfig } = config;
|
|
1044
|
-
const depositsApi = new imx.DepositsApi(immutableXConfig.apiConfiguration);
|
|
1045
|
-
const encodingApi = new imx.EncodingApi(immutableXConfig.apiConfiguration);
|
|
1046
|
-
const data = {
|
|
1047
|
-
token_address: deposit2.tokenAddress,
|
|
1048
|
-
token_id: deposit2.tokenId
|
|
1049
|
-
};
|
|
1050
|
-
const amount = "1";
|
|
1051
|
-
const getSignableDepositRequest = {
|
|
1052
|
-
user,
|
|
1053
|
-
token: {
|
|
1054
|
-
type: deposit2.type,
|
|
1055
|
-
data
|
|
1056
|
-
},
|
|
1057
|
-
amount: amount.toString()
|
|
1058
|
-
};
|
|
1059
|
-
const signableDepositResult = await depositsApi.getSignableDeposit({
|
|
1060
|
-
getSignableDepositRequest
|
|
1061
|
-
});
|
|
1062
|
-
const encodingResult = await encodingApi.encodeAsset({
|
|
1063
|
-
assetType: "asset",
|
|
1064
|
-
encodeAssetRequest: {
|
|
1065
|
-
token: {
|
|
1066
|
-
type: deposit2.type,
|
|
1067
|
-
data: {
|
|
1068
|
-
token_address: deposit2.tokenAddress,
|
|
1069
|
-
token_id: deposit2.tokenId
|
|
1070
|
-
}
|
|
1071
|
-
}
|
|
1072
|
-
}
|
|
1073
|
-
});
|
|
1074
|
-
const assetType = encodingResult.data.asset_type;
|
|
1075
|
-
const starkPublicKey = signableDepositResult.data.stark_key;
|
|
1076
|
-
const vaultId = signableDepositResult.data.vault_id;
|
|
1077
|
-
const tokenContract = Contracts.IERC721.connect(
|
|
1078
|
-
deposit2.tokenAddress,
|
|
1079
|
-
ethSigner
|
|
1080
|
-
);
|
|
1081
|
-
const operator = immutableXConfig.ethConfiguration.coreContractAddress;
|
|
1082
|
-
const isApprovedForAll = await tokenContract.isApprovedForAll(user, operator);
|
|
1083
|
-
if (!isApprovedForAll) {
|
|
1084
|
-
await tokenContract.setApprovalForAll(operator, true);
|
|
1085
|
-
}
|
|
1086
|
-
return executeDepositERC721(
|
|
1087
|
-
ethSigner,
|
|
1088
|
-
deposit2.tokenId,
|
|
1089
|
-
assetType,
|
|
1090
|
-
starkPublicKey,
|
|
1091
|
-
vaultId,
|
|
1092
|
-
immutableXConfig
|
|
1093
|
-
);
|
|
1094
|
-
}
|
|
1095
|
-
|
|
1096
|
-
// src/signable-actions/deposit.ts
|
|
1097
|
-
async function deposit({ signers, deposit: deposit2, config }) {
|
|
1098
|
-
switch (deposit2.type) {
|
|
1099
|
-
case "ETH":
|
|
1100
|
-
return depositEth({ signers, deposit: deposit2, config });
|
|
1101
|
-
case "ERC20":
|
|
1102
|
-
return depositERC20({ signers, deposit: deposit2, config });
|
|
1103
|
-
case "ERC721":
|
|
1104
|
-
return depositERC721({ signers, deposit: deposit2, config });
|
|
1105
|
-
}
|
|
1106
|
-
}
|
|
1107
|
-
async function exchangeTransfer({
|
|
1108
|
-
signers,
|
|
1109
|
-
request,
|
|
1110
|
-
config
|
|
1111
|
-
}) {
|
|
1112
|
-
await validateChain(signers.ethSigner, config.immutableXConfig);
|
|
1113
|
-
const exchangeApi = new imx.ExchangesApi(
|
|
1114
|
-
config.immutableXConfig.apiConfiguration
|
|
1115
|
-
);
|
|
1116
|
-
const ethAddress = await signers.ethSigner.getAddress();
|
|
1117
|
-
const transferAmount = request.amount;
|
|
1118
|
-
const signableResult = await exchangeApi.getExchangeSignableTransfer({
|
|
1119
|
-
id: request.transactionID,
|
|
1120
|
-
getSignableTransferRequest: {
|
|
1121
|
-
sender: ethAddress,
|
|
1122
|
-
token: convertToSignableToken(request),
|
|
1123
|
-
amount: transferAmount,
|
|
1124
|
-
receiver: request.receiver
|
|
1125
|
-
}
|
|
1126
|
-
});
|
|
1127
|
-
const { signable_message: signableMessage, payload_hash: payloadHash } = signableResult.data;
|
|
1128
|
-
const ethSignature = await signRaw(signableMessage, signers.ethSigner);
|
|
1129
|
-
const starkSignature = await signers.starkSigner.signMessage(payloadHash);
|
|
1130
|
-
const transferSigningParams = {
|
|
1131
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1132
|
-
sender_stark_key: signableResult.data.sender_stark_key,
|
|
1133
|
-
sender_vault_id: signableResult.data.sender_vault_id,
|
|
1134
|
-
receiver_stark_key: signableResult.data.receiver_stark_key,
|
|
1135
|
-
receiver_vault_id: signableResult.data.receiver_vault_id,
|
|
1136
|
-
asset_id: signableResult.data.asset_id,
|
|
1137
|
-
amount: signableResult.data.amount,
|
|
1138
|
-
nonce: signableResult.data.nonce,
|
|
1139
|
-
expiration_timestamp: signableResult.data.expiration_timestamp,
|
|
1140
|
-
stark_signature: starkSignature
|
|
1141
|
-
};
|
|
1142
|
-
const response = await exchangeApi.createExchangeTransfer({
|
|
1143
|
-
id: request.transactionID,
|
|
1144
|
-
createTransferRequest: transferSigningParams,
|
|
1145
|
-
xImxEthAddress: ethAddress,
|
|
1146
|
-
xImxEthSignature: ethSignature
|
|
1147
|
-
});
|
|
1148
|
-
return {
|
|
1149
|
-
sent_signature: response?.data.sent_signature,
|
|
1150
|
-
status: response?.data.status?.toString(),
|
|
1151
|
-
time: response?.data.time,
|
|
1152
|
-
transfer_id: response?.data.transfer_id
|
|
1153
|
-
};
|
|
1154
|
-
}
|
|
1155
|
-
|
|
1156
|
-
// src/genericImxProvider.ts
|
|
1157
|
-
var GenericIMXProvider = class {
|
|
1158
|
-
config;
|
|
1159
|
-
signers;
|
|
1160
|
-
constructor(config, ethSigner, starkSigner) {
|
|
1161
|
-
this.config = config;
|
|
1162
|
-
this.signers = { ethSigner, starkSigner };
|
|
1163
|
-
}
|
|
1164
|
-
async getAddress() {
|
|
1165
|
-
return this.signers.ethSigner.getAddress();
|
|
1166
|
-
}
|
|
1167
|
-
async isRegisteredOffchain() {
|
|
1168
|
-
const ethAddress = await this.getAddress();
|
|
1169
|
-
return isRegisteredOffchain(
|
|
1170
|
-
ethAddress,
|
|
1171
|
-
this.config
|
|
1172
|
-
);
|
|
1173
|
-
}
|
|
1174
|
-
registerOffchain() {
|
|
1175
|
-
return registerOffchain(this.signers, this.config);
|
|
1176
|
-
}
|
|
1177
|
-
batchNftTransfer(request) {
|
|
1178
|
-
return batchTransfer({
|
|
1179
|
-
signers: this.signers,
|
|
1180
|
-
request,
|
|
1181
|
-
config: this.config
|
|
1182
|
-
});
|
|
1183
|
-
}
|
|
1184
|
-
cancelOrder(request) {
|
|
1185
|
-
return cancelOrder({
|
|
1186
|
-
signers: this.signers,
|
|
1187
|
-
request,
|
|
1188
|
-
config: this.config
|
|
1189
|
-
});
|
|
1190
|
-
}
|
|
1191
|
-
completeWithdrawal(starkPublicKey, token) {
|
|
1192
|
-
return completeWithdrawal({
|
|
1193
|
-
config: this.config,
|
|
1194
|
-
signers: this.signers,
|
|
1195
|
-
token,
|
|
1196
|
-
starkPublicKey
|
|
1197
|
-
});
|
|
1198
|
-
}
|
|
1199
|
-
createOrder(request) {
|
|
1200
|
-
return createOrder({
|
|
1201
|
-
signers: this.signers,
|
|
1202
|
-
request,
|
|
1203
|
-
config: this.config
|
|
1204
|
-
});
|
|
1205
|
-
}
|
|
1206
|
-
createTrade(request) {
|
|
1207
|
-
return createTrade({
|
|
1208
|
-
signers: this.signers,
|
|
1209
|
-
request,
|
|
1210
|
-
config: this.config
|
|
1211
|
-
});
|
|
1212
|
-
}
|
|
1213
|
-
deposit(tokenAmount) {
|
|
1214
|
-
return deposit({
|
|
1215
|
-
signers: this.signers,
|
|
1216
|
-
deposit: tokenAmount,
|
|
1217
|
-
config: this.config
|
|
1218
|
-
});
|
|
1219
|
-
}
|
|
1220
|
-
exchangeTransfer(request) {
|
|
1221
|
-
return exchangeTransfer({
|
|
1222
|
-
signers: this.signers,
|
|
1223
|
-
request,
|
|
1224
|
-
config: this.config
|
|
1225
|
-
});
|
|
1226
|
-
}
|
|
1227
|
-
async isRegisteredOnchain() {
|
|
1228
|
-
const starkPublicKey = await this.signers.starkSigner.getAddress();
|
|
1229
|
-
return isRegisteredOnChain(
|
|
1230
|
-
starkPublicKey,
|
|
1231
|
-
this.signers.ethSigner,
|
|
1232
|
-
this.config
|
|
1233
|
-
);
|
|
1234
|
-
}
|
|
1235
|
-
prepareWithdrawal(request) {
|
|
1236
|
-
return prepareWithdrawal({
|
|
1237
|
-
signers: this.signers,
|
|
1238
|
-
withdrawal: request,
|
|
1239
|
-
config: this.config
|
|
1240
|
-
});
|
|
1241
|
-
}
|
|
1242
|
-
transfer(request) {
|
|
1243
|
-
return transfer({
|
|
1244
|
-
signers: this.signers,
|
|
1245
|
-
request,
|
|
1246
|
-
config: this.config
|
|
1247
|
-
});
|
|
1248
|
-
}
|
|
1249
|
-
};
|
|
1250
|
-
|
|
1251
|
-
// src/l1-providers/rpc.ts
|
|
1252
|
-
var WALLET_ACTION = {
|
|
1253
|
-
// TODO: remove once fixed - consider using an enum
|
|
1254
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1255
|
-
SWITCH_CHAIN: "wallet_switchEthereumChain",
|
|
1256
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1257
|
-
CONNECT: "eth_requestAccounts"
|
|
1258
|
-
};
|
|
1259
|
-
function isRequestableProvider(provider) {
|
|
1260
|
-
return !!provider?.request;
|
|
1261
|
-
}
|
|
1262
|
-
async function connectProvider(provider, chainID) {
|
|
1263
|
-
await provider.request({ method: WALLET_ACTION.CONNECT });
|
|
1264
|
-
if (chainID) {
|
|
1265
|
-
await provider.request({
|
|
1266
|
-
method: WALLET_ACTION.SWITCH_CHAIN,
|
|
1267
|
-
params: [{ chainId: `0x${chainID.toString(16)}` }]
|
|
1268
|
-
});
|
|
1269
|
-
}
|
|
1270
|
-
}
|
|
1271
|
-
|
|
1272
|
-
// src/l1-providers/metaMask.ts
|
|
1273
|
-
var ERRORS = {
|
|
1274
|
-
// TODO: remove once fixed - consider using something in line with the naming convention
|
|
1275
|
-
// eslint-disable-next-line @typescript-eslint/naming-convention
|
|
1276
|
-
PROVIDER_NOT_FOUND: "The Metamask provider was not found"
|
|
1277
|
-
};
|
|
1278
|
-
async function connect({
|
|
1279
|
-
chainID
|
|
1280
|
-
}) {
|
|
1281
|
-
const provider = await detectEthereumProvider();
|
|
1282
|
-
if (!isRequestableProvider(provider)) {
|
|
1283
|
-
throw new Error(ERRORS.PROVIDER_NOT_FOUND);
|
|
1284
|
-
}
|
|
1285
|
-
await connectProvider(provider, chainID);
|
|
1286
|
-
return new BrowserProvider(provider);
|
|
1287
|
-
}
|
|
1288
|
-
|
|
1289
|
-
// src/imx-wallet/events.ts
|
|
1290
|
-
var COMMUNICATION_TYPE = "message";
|
|
1291
|
-
|
|
1292
|
-
// src/imx-wallet/postRequestMessage.ts
|
|
1293
|
-
function postRequestMessage(iframe, payload) {
|
|
1294
|
-
if (iframe && iframe.contentWindow) {
|
|
1295
|
-
iframe.contentWindow.postMessage(payload, new URL(iframe.src).origin);
|
|
1296
|
-
}
|
|
1297
|
-
}
|
|
1298
|
-
|
|
1299
|
-
// src/imx-wallet/messageResponseListener.ts
|
|
1300
|
-
function messageResponseListener(iframe, event, eventType, callback) {
|
|
1301
|
-
if (iframe && event.source !== iframe.contentWindow) {
|
|
1302
|
-
return;
|
|
1303
|
-
}
|
|
1304
|
-
const l2WalletMessage = event.data;
|
|
1305
|
-
if (l2WalletMessage.type !== eventType) {
|
|
1306
|
-
return;
|
|
1307
|
-
}
|
|
1308
|
-
callback(l2WalletMessage.details);
|
|
1309
|
-
}
|
|
1310
|
-
|
|
1311
|
-
// src/imx-wallet/ImxSigner.ts
|
|
1312
|
-
var ImxSigner = class {
|
|
1313
|
-
publicAddress;
|
|
1314
|
-
iframe;
|
|
1315
|
-
constructor(publicAddress, iframe) {
|
|
1316
|
-
this.publicAddress = publicAddress;
|
|
1317
|
-
this.iframe = iframe;
|
|
1318
|
-
}
|
|
1319
|
-
getAddress() {
|
|
1320
|
-
return this.publicAddress;
|
|
1321
|
-
}
|
|
1322
|
-
signMessage(rawMessage) {
|
|
1323
|
-
return new Promise((resolve, reject) => {
|
|
1324
|
-
const listener = (event) => {
|
|
1325
|
-
messageResponseListener(
|
|
1326
|
-
this.iframe,
|
|
1327
|
-
event,
|
|
1328
|
-
"SIGN_MESSAGE_RESPONSE" /* SIGN_MESSAGE_RESPONSE */,
|
|
1329
|
-
(messageDetails) => {
|
|
1330
|
-
window.removeEventListener(COMMUNICATION_TYPE, listener);
|
|
1331
|
-
if (!messageDetails.success) {
|
|
1332
|
-
reject(new Error(messageDetails.error?.message));
|
|
1333
|
-
}
|
|
1334
|
-
resolve(messageDetails.data.signedMessage);
|
|
1335
|
-
}
|
|
1336
|
-
);
|
|
1337
|
-
};
|
|
1338
|
-
window.addEventListener(COMMUNICATION_TYPE, listener);
|
|
1339
|
-
postRequestMessage(this.iframe, {
|
|
1340
|
-
type: "SIGN_MESSAGE_REQUEST" /* SIGN_MESSAGE_REQUEST */,
|
|
1341
|
-
details: { starkPublicKey: this.publicAddress, message: rawMessage }
|
|
1342
|
-
});
|
|
1343
|
-
});
|
|
1344
|
-
}
|
|
1345
|
-
getIFrame() {
|
|
1346
|
-
return this.iframe;
|
|
1347
|
-
}
|
|
1348
|
-
getYCoordinate() {
|
|
1349
|
-
return new Promise((resolve, reject) => {
|
|
1350
|
-
const listener = (event) => {
|
|
1351
|
-
messageResponseListener(
|
|
1352
|
-
this.iframe,
|
|
1353
|
-
event,
|
|
1354
|
-
"GET_Y_COORDINATE_RESPONSE" /* GET_Y_COORDINATE_RESPONSE */,
|
|
1355
|
-
(messageDetails) => {
|
|
1356
|
-
window.removeEventListener(COMMUNICATION_TYPE, listener);
|
|
1357
|
-
if (!messageDetails.success) {
|
|
1358
|
-
reject(new Error(messageDetails.error?.message));
|
|
1359
|
-
}
|
|
1360
|
-
resolve(messageDetails.data.yCoordinate);
|
|
1361
|
-
}
|
|
1362
|
-
);
|
|
1363
|
-
};
|
|
1364
|
-
window.addEventListener(COMMUNICATION_TYPE, listener);
|
|
1365
|
-
postRequestMessage(this.iframe, {
|
|
1366
|
-
type: "GET_Y_COORDINATE_REQUEST" /* GET_Y_COORDINATE_REQUEST */,
|
|
1367
|
-
details: { starkPublicKey: this.publicAddress }
|
|
1368
|
-
});
|
|
1369
|
-
});
|
|
1370
|
-
}
|
|
1371
|
-
};
|
|
1372
|
-
var IMX_WALLET_IFRAME_ID = "imx-wallet-app";
|
|
1373
|
-
var IMX_WALLET_IFRAME_HOSTS = {
|
|
1374
|
-
[Environment.SANDBOX]: "https://wallets.sandbox.immutable.com",
|
|
1375
|
-
[Environment.PRODUCTION]: "https://wallets.immutable.com"
|
|
1376
|
-
};
|
|
1377
|
-
var IMX_WALLET_IFRAME_STYLE = "display: none;";
|
|
1378
|
-
function getIFrame() {
|
|
1379
|
-
return document.querySelector(`iframe#${IMX_WALLET_IFRAME_ID}`);
|
|
1380
|
-
}
|
|
1381
|
-
async function setupIFrame(env) {
|
|
1382
|
-
return new Promise((resolve) => {
|
|
1383
|
-
const iframe = document.createElement("iframe");
|
|
1384
|
-
iframe.setAttribute("id", IMX_WALLET_IFRAME_ID);
|
|
1385
|
-
iframe.setAttribute("src", IMX_WALLET_IFRAME_HOSTS[env]);
|
|
1386
|
-
iframe.setAttribute("style", IMX_WALLET_IFRAME_STYLE);
|
|
1387
|
-
document.body.appendChild(iframe);
|
|
1388
|
-
iframe.onload = () => resolve(iframe);
|
|
1389
|
-
});
|
|
1390
|
-
}
|
|
1391
|
-
async function getOrSetupIFrame(env) {
|
|
1392
|
-
const iframe = getIFrame();
|
|
1393
|
-
if (iframe) return iframe;
|
|
1394
|
-
return await setupIFrame(env);
|
|
1395
|
-
}
|
|
1396
|
-
|
|
1397
|
-
// src/imx-wallet/imxWallet.ts
|
|
1398
|
-
var DEFAULT_CONNECTION_MESSAGE = "Only sign this request if you\u2019ve initiated an action with Immutable X.";
|
|
1399
|
-
var CONNECTION_FAILED_ERROR = "The L2 IMX Wallet connection has failed";
|
|
1400
|
-
async function connect2(l1Provider, env) {
|
|
1401
|
-
const l1Signer = await l1Provider.getSigner();
|
|
1402
|
-
const address = await l1Signer.getAddress();
|
|
1403
|
-
const signature = await l1Signer.signMessage(DEFAULT_CONNECTION_MESSAGE);
|
|
1404
|
-
const iframe = await getOrSetupIFrame(env);
|
|
1405
|
-
return new Promise((resolve, reject) => {
|
|
1406
|
-
const listener = (event) => {
|
|
1407
|
-
messageResponseListener(
|
|
1408
|
-
iframe,
|
|
1409
|
-
event,
|
|
1410
|
-
"CONNECT_WALLET_RESPONSE" /* CONNECT_WALLET_RESPONSE */,
|
|
1411
|
-
(messageDetails) => {
|
|
1412
|
-
window.removeEventListener(COMMUNICATION_TYPE, listener);
|
|
1413
|
-
if (!messageDetails.success) {
|
|
1414
|
-
reject(new Error(CONNECTION_FAILED_ERROR));
|
|
1415
|
-
}
|
|
1416
|
-
resolve(new ImxSigner(messageDetails.data.starkPublicKey, iframe));
|
|
1417
|
-
}
|
|
1418
|
-
);
|
|
1419
|
-
};
|
|
1420
|
-
window.addEventListener(COMMUNICATION_TYPE, listener);
|
|
1421
|
-
postRequestMessage(iframe, {
|
|
1422
|
-
type: "CONNECT_WALLET_REQUEST" /* CONNECT_WALLET_REQUEST */,
|
|
1423
|
-
details: { ethAddress: address, signature }
|
|
1424
|
-
});
|
|
1425
|
-
});
|
|
1426
|
-
}
|
|
1427
|
-
async function disconnect(imxSigner) {
|
|
1428
|
-
const iframe = imxSigner.getIFrame();
|
|
1429
|
-
return new Promise((resolve, reject) => {
|
|
1430
|
-
const listener = (event) => {
|
|
1431
|
-
messageResponseListener(
|
|
1432
|
-
iframe,
|
|
1433
|
-
event,
|
|
1434
|
-
"DISCONNECT_WALLET_RESPONSE" /* DISCONNECT_WALLET_RESPONSE */,
|
|
1435
|
-
(messageDetails) => {
|
|
1436
|
-
window.removeEventListener(COMMUNICATION_TYPE, listener);
|
|
1437
|
-
if (!messageDetails.success && messageDetails.error) {
|
|
1438
|
-
reject(messageDetails.error);
|
|
1439
|
-
}
|
|
1440
|
-
iframe.remove();
|
|
1441
|
-
resolve();
|
|
1442
|
-
}
|
|
1443
|
-
);
|
|
1444
|
-
};
|
|
1445
|
-
window.addEventListener(COMMUNICATION_TYPE, listener);
|
|
1446
|
-
postRequestMessage(iframe, {
|
|
1447
|
-
type: "DISCONNECT_WALLET_REQUEST" /* DISCONNECT_WALLET_REQUEST */,
|
|
1448
|
-
details: { starkPublicKey: imxSigner.getAddress() }
|
|
1449
|
-
});
|
|
1450
|
-
});
|
|
1451
|
-
}
|
|
1452
|
-
|
|
1453
|
-
// src/errors/providerError.ts
|
|
1454
|
-
var ProviderError = class extends Error {
|
|
1455
|
-
type;
|
|
1456
|
-
constructor(message, type) {
|
|
1457
|
-
super(message);
|
|
1458
|
-
this.type = type;
|
|
1459
|
-
}
|
|
1460
|
-
};
|
|
1461
|
-
var withProviderError = async (fn, customError) => {
|
|
1462
|
-
try {
|
|
1463
|
-
return await fn();
|
|
1464
|
-
} catch (error) {
|
|
1465
|
-
const errorMessage = customError.message || `${error.message}` || "UnknownError";
|
|
1466
|
-
throw new ProviderError(errorMessage, customError.type);
|
|
1467
|
-
}
|
|
1468
|
-
};
|
|
1469
|
-
|
|
1470
|
-
// src/l1-providers/metaMaskWrapper.ts
|
|
1471
|
-
var MetaMaskIMXProvider = class _MetaMaskIMXProvider extends GenericIMXProvider {
|
|
1472
|
-
static imxSigner;
|
|
1473
|
-
static async connect(config) {
|
|
1474
|
-
return await withProviderError(
|
|
1475
|
-
async () => {
|
|
1476
|
-
const metaMaskProvider = await connect({
|
|
1477
|
-
chainID: config.immutableXConfig.ethConfiguration.chainID
|
|
1478
|
-
});
|
|
1479
|
-
this.imxSigner = await connect2(
|
|
1480
|
-
metaMaskProvider,
|
|
1481
|
-
config.baseConfig.environment
|
|
1482
|
-
);
|
|
1483
|
-
return new _MetaMaskIMXProvider(
|
|
1484
|
-
config,
|
|
1485
|
-
await metaMaskProvider.getSigner(),
|
|
1486
|
-
this.imxSigner
|
|
1487
|
-
);
|
|
1488
|
-
},
|
|
1489
|
-
{ type: "WALLET_CONNECTION_ERROR" /* WALLET_CONNECTION_ERROR */ }
|
|
1490
|
-
);
|
|
1491
|
-
}
|
|
1492
|
-
static async disconnect() {
|
|
1493
|
-
if (!this.imxSigner) {
|
|
1494
|
-
throw new ProviderError(
|
|
1495
|
-
"Attempted to disconnect from the MetaMask IMX provider without an established connection",
|
|
1496
|
-
"PROVIDER_CONNECTION_ERROR" /* PROVIDER_CONNECTION_ERROR */
|
|
1497
|
-
);
|
|
1498
|
-
}
|
|
1499
|
-
return withProviderError(
|
|
1500
|
-
async () => {
|
|
1501
|
-
await disconnect(this.imxSigner);
|
|
1502
|
-
},
|
|
1503
|
-
{ type: "PROVIDER_CONNECTION_ERROR" /* PROVIDER_CONNECTION_ERROR */ }
|
|
1504
|
-
);
|
|
1505
|
-
}
|
|
1506
|
-
static async signMessage(message) {
|
|
1507
|
-
if (!this.imxSigner) {
|
|
1508
|
-
throw new ProviderError(
|
|
1509
|
-
"Attempted to sign a message with the MetaMask IMX provider without an established connection",
|
|
1510
|
-
"PROVIDER_CONNECTION_ERROR" /* PROVIDER_CONNECTION_ERROR */
|
|
1511
|
-
);
|
|
1512
|
-
}
|
|
1513
|
-
return withProviderError(
|
|
1514
|
-
async () => await this.imxSigner.signMessage(message),
|
|
1515
|
-
{ type: "PROVIDER_CONNECTION_ERROR" /* PROVIDER_CONNECTION_ERROR */ }
|
|
1516
|
-
);
|
|
1517
|
-
}
|
|
1518
|
-
};
|
|
1519
|
-
var ProviderConfiguration = class {
|
|
1520
|
-
immutableXConfig;
|
|
1521
|
-
baseConfig;
|
|
1522
|
-
constructor({ baseConfig, overrides }) {
|
|
1523
|
-
this.baseConfig = baseConfig;
|
|
1524
|
-
if (overrides) {
|
|
1525
|
-
this.immutableXConfig = overrides.immutableXConfig;
|
|
1526
|
-
} else {
|
|
1527
|
-
const clientConfig = new ImxConfiguration({ baseConfig });
|
|
1528
|
-
this.immutableXConfig = clientConfig.immutableXConfig;
|
|
1529
|
-
}
|
|
1530
|
-
}
|
|
1531
|
-
};
|
|
1532
|
-
|
|
1533
|
-
export { GenericIMXProvider, MetaMaskIMXProvider, ProviderConfiguration };
|
|
12
|
+
export { P as GenericIMXProvider, V as MetaMaskIMXProvider, B as ProviderConfiguration };
|