Last active
November 22, 2024 20:34
-
-
Save tunnckoCore/0cd72f3a623b9c483af257700fff7e1b to your computer and use it in GitHub Desktop.
Ordex Bulk Withdrawal - Unescrow
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { http, createPublicClient, createWalletClient } from 'viem'; | |
import { privateKeyToAccount } from 'viem/accounts'; | |
import { mainnet } from 'viem/chains'; | |
export const publicClient = createPublicClient({ | |
chain: mainnet, | |
transport: http(), | |
}); | |
const ethscriptionIds = [ | |
'0x072ba19c7edec75edafd442dce2afab94d5de8a65d2ed77aa3c86d4426e3c28c', | |
]; | |
export const account = privateKeyToAccount( | |
'0x private key' | |
); | |
export const walletClient = createWalletClient({ | |
account, | |
chain: mainnet, | |
transport: http(), | |
}); | |
const abi = [ | |
{ | |
name: 'bulkWithdrawItems', | |
type: 'function', | |
stateMutability: 'nonpayable', | |
inputs: [ | |
{ | |
type: 'address', | |
}, | |
{ | |
type: 'address', | |
}, | |
{ | |
type: 'uint256[]', | |
}, | |
], | |
outputs: [ | |
{ | |
type: 'uint256', | |
}, | |
{ | |
type: 'uint8', | |
}, | |
{ | |
type: 'bytes32', | |
}, | |
{ | |
type: 'bytes32', | |
}, | |
], | |
}, | |
]; | |
function getMessage(ids, mode = 'bulk_withdraw') { | |
const i = new Date(); | |
const o = (Math.floor(i.getTime() / 6e5) + 2) * 6e5; | |
const expires = new Date(o).toISOString(); | |
let intent = ''; | |
if (mode === 'confirm') { | |
intent = 'confirm the following escrow'; | |
} else if (mode === 'bulk_withdaw') { | |
intent = 'withdraw the following item'; | |
} else if (mode === 'sell') { | |
intent = 'fulfill the following bid'; | |
} else if (mode === 'buy') { | |
intent = 'fulfill the following listing'; | |
} else if (mode === 'accept_bid') { | |
intent = 'accept the following bid'; | |
} else if (mode === 'cancel_listing') { | |
intent = 'cancel the following listing'; | |
} else if (mode === 'cancel_bid') { | |
intent = 'cancel the following bid'; | |
} else if (mode === 'cancel_order') { | |
intent = 'cancel the following order'; | |
} else { | |
throw new Error('unknown operation'); | |
} | |
console.log({ i, o, expires, intent }); | |
const itemsList = ids.join(', '); | |
const sigMsg = ` | |
Signing this message does not cost gas. | |
This signature expires at: ${expires}`; | |
const msg = `I would like to ${intent}${ids.length > 1 ? 's' : ''}: ${itemsList} ${sigMsg}`; | |
console.log({ msg }); | |
return msg; | |
} | |
async function main(ids) { | |
const itemIds = ids.map((x) => BigInt(x).toString()); | |
const signature = await walletClient.signMessage({ | |
message: getMessage(itemIds, 'bulk_withdaw'), | |
}); | |
const body = { | |
client: account.address, | |
itemIds, | |
clientSignature: signature, | |
}; | |
console.log({ signature, body }); | |
const resp = await fetch('https://api-next.ordex.io/signer/s/wc', { | |
method: 'POST', | |
body: JSON.stringify(body), | |
headers: { | |
'content-type': 'application/json', | |
}, | |
}); | |
if (resp.status !== 200) { | |
console.log(await resp.json()); | |
console.error('Failed to withdraw items'); | |
return; | |
} | |
const { confirmation, sig, statusCode, ...rest } = await resp.json(); | |
console.log({ confirmation, sig, statusCode, ...rest }); | |
if (statusCode === 400) { | |
console.log(rest); | |
console.error('Failed to withdraw items'); | |
return; | |
} | |
const { request } = await publicClient.simulateContract({ | |
account, | |
address: '0xc33f8610941be56fb0d84e25894c0d928cc97dde', | |
abi, | |
functionName: 'bulkWithdrawItems', | |
args: [confirmation, sig], | |
}); | |
console.log({ request }); | |
const hash = await walletClient.writeContract(request); | |
console.log({ hash }); | |
} | |
main(ethscriptionIds); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment