Skip to content

Commit

Permalink
ALL-1479-tezos-estimate (#901)
Browse files Browse the repository at this point in the history
Co-authored-by: oliverjantar <jiri.andras@protonmail.com>
  • Loading branch information
oliverjantar and oliverjantar authored Aug 30, 2023
1 parent 4bd006e commit ad3ed51
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 20 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "tatumio",
"version": "2.2.28",
"version": "2.2.29",
"license": "MIT",
"repository": "https://github.com/tatumio/tatum-js",
"scripts": {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { TatumTezosSDK } from '../tezos.sdk'
import {
BurnTezosToken,
DeployTezosNft,
MintTezosToken,
Token,
TransferTezosToken,
Expand All @@ -26,14 +25,17 @@ describe.skip('TatumTezosSDK - blockchain', () => {
const sdk = TatumTezosSDK()
const tezosWeb = sdk.tezosWeb

const body: DeployTezosNft = {
const body = {
privateKey,
owner: addr1,
metadata: JSON.stringify({ name: 'contract name', symbol: 'TZ-gold' }),
metadata: JSON.stringify({
name: 'contract name',
symbol: 'TZ-gold',
version: '1.26.0',
}),
}

const opHash = await sdk.nft({ tezosWeb }).deploy.tzip12(body, testnet)

console.log(opHash)
})

Expand All @@ -42,24 +44,22 @@ describe.skip('TatumTezosSDK - blockchain', () => {
const tezosWeb = sdk.tezosWeb

const token: Token = {
id: '6',
id: '1',
ipfs: 'ipfs://Qmcz7iquheYehi4rmA2v9ZWakxMJCJusC5K7Harz8WNdza',
}

const token2: Token = {
id: '7',
id: '2',
ipfs: 'ipfs://QmS7W7vtC6f3kSPUUkapVtW5bAXQvtn2tDDBDiBH1R3g2h',
}

const body: MintTezosToken = {
contractAddress,
owner: addr1,
owner: addr2,
nfts: [token, token2],
privateKey,
}

const tx = await sdk.nft({ tezosWeb }).mintTzip12(body, testnet)

console.log(tx)
})

Expand All @@ -70,14 +70,14 @@ describe.skip('TatumTezosSDK - blockchain', () => {
const body: BurnTezosToken = {
contractAddress,
owner: addr1,
tokens: ['6'],
privateKey,
tokens: ['1'],
privateKey: privateKey,
}

const tx = await sdk.nft({ tezosWeb }).burnTzip12(body, testnet)

console.log(tx)
})

it('Should add an operator to a token on testnet', async () => {
const sdk = TatumTezosSDK()
const tezosWeb = sdk.tezosWeb
Expand All @@ -100,16 +100,76 @@ describe.skip('TatumTezosSDK - blockchain', () => {

const body: TransferTezosToken = {
contractAddress,
from: addr1,
from: addr2,
to: addr3,
tokenId: '7',
tokenId: '1',
amount: '1',
privateKey: privateKey2,
}

const tx = await sdk.nft({ tezosWeb }).transferTzip12(body, testnet)

console.log(tx)
})

it('Should estimate contract origination on testnet', async () => {
const sdk = TatumTezosSDK()
const tezosWeb = sdk.tezosWeb

const body = {
privateKey,
owner: addr1,
metadata: JSON.stringify({
name: 'contract name',
symbol: 'TZ-gold',
version: '1.26.0',
}),
}

const totalCost = await sdk.nft({ tezosWeb }).estimate.contractDeploy(body, testnet)
console.log(totalCost)
})

it('Should estimate a token mint on testnet', async () => {
const sdk = TatumTezosSDK()
const tezosWeb = sdk.tezosWeb

const token: Token = {
id: '1',
ipfs: 'ipfs://Qmcz7iquheYehi4rmA2v9ZWakxMJCJusC5K7Harz8WNdza',
}

const token2: Token = {
id: '2',
ipfs: 'ipfs://QmS7W7vtC6f3kSPUUkapVtW5bAXQvtn2tDDBDiBH1R3g2h',
}

const body: MintTezosToken = {
contractAddress,
owner: addr1,
nfts: [token, token2],
privateKey,
}

const totalCost = await sdk.nft({ tezosWeb }).estimate.tokenMint(body, testnet)

console.log(totalCost)
})

it('Should estimate a token transfer on testnet', async () => {
const sdk = TatumTezosSDK()
const tezosWeb = sdk.tezosWeb
const body: TransferTezosToken = {
contractAddress,
from: addr2,
to: addr3,
tokenId: '1',
amount: '1',
privateKey: privateKey2,
}

const totalCost = await sdk.nft({ tezosWeb }).estimate.tokenTransfer(body, testnet)

console.log(totalCost)
})
})
})
109 changes: 105 additions & 4 deletions packages/blockchain/tezos/src/lib/services/tezos.tzip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,11 @@ import { char2Bytes } from '@taquito/utils'
import * as fa2 from '@oxheadalpha/fa2-interfaces'
import { MichelsonMap } from '@taquito/taquito'

export type DeployTezosNft = {
export type DeployContractData = {
privateKey: string
owner: string
metadata: string
}

export type MintTezosToken = {
privateKey: string
contractAddress: string
Expand Down Expand Up @@ -45,7 +44,11 @@ export type UpdateOperatorTezosToken = {
operator: string
}

const deployTzip12 = async (body: DeployTezosNft, tronWeb: ITezosWeb, provider?: string): Promise<string> => {
const deployTzip12 = async (
body: DeployContractData,
tronWeb: ITezosWeb,
provider?: string,
): Promise<string> => {
const client = tronWeb.getClient(provider)
const { privateKey, owner, metadata } = body

Expand All @@ -62,6 +65,7 @@ const deployTzip12 = async (body: DeployTezosNft, tronWeb: ITezosWeb, provider?:

return hash
}

const mintToken = async (body: MintTezosToken, tronWeb: ITezosWeb, provider?: string): Promise<string> => {
const { contractAddress, nfts, owner, privateKey } = body

Expand Down Expand Up @@ -172,6 +176,94 @@ const updateOperator = async (
return opHash
}

const estimateContractDeploy = async (
body: DeployContractData,
tronWeb: ITezosWeb,
provider?: string,
): Promise<number> => {
const client = tronWeb.getClient(provider)
const { privateKey, owner, metadata } = body

const memorySigner = new InMemorySigner(privateKey)

client.setSignerProvider(memorySigner)

const storage = createStorage({ metadata, owner })

const { burnFeeMutez, suggestedFeeMutez } = await client.estimate.originate({
code: Tezos_TZIP_12.michelson,
storage,
})

return burnFeeMutez + suggestedFeeMutez
}

const estimateTokenMint = async (
body: MintTezosToken,
tronWeb: ITezosWeb,
provider?: string,
): Promise<number> => {
const { contractAddress, nfts, owner, privateKey } = body

const client = tronWeb.getClient(provider)
const memorySigner = new InMemorySigner(privateKey)

client.setSignerProvider(memorySigner)

const contract = await client.contract.at(contractAddress)
const tokens = nfts.map(({ id, ipfs }) => {
const tokenInfoMap = new MichelsonMap<string, string>()

tokenInfoMap.set('', char2Bytes(ipfs))
return { token_id: id, token_info: tokenInfoMap }
})

const mintParams = [
{
owner,
tokens,
},
]

const op = contract.methods.mint(mintParams)

const { burnFeeMutez, suggestedFeeMutez } = await client.estimate.contractCall(op)
return burnFeeMutez + suggestedFeeMutez
}

const estimateTransferToken = async (
body: TransferTezosToken,
tronWeb: ITezosWeb,
provider?: string,
): Promise<number> => {
const { contractAddress, privateKey, from, to, tokenId, amount } = body

const client = tronWeb.getClient(provider)
const memorySigner = new InMemorySigner(privateKey)

client.setSignerProvider(memorySigner)

const contract = await client.contract.at(contractAddress)

const transferParams = [
{
from_: from,
txs: [
{
to_: to,
token_id: tokenId,
amount: amount,
},
],
},
]

const op = contract.methods.transfer(transferParams)

const { burnFeeMutez, suggestedFeeMutez } = await client.estimate.contractCall(op)
return burnFeeMutez + suggestedFeeMutez
}

export const tezosTzip = (args: { tezosWeb: ITezosWeb }) => ({
deploy: {
/**
Expand All @@ -180,7 +272,16 @@ export const tezosTzip = (args: { tezosWeb: ITezosWeb }) => ({
* @param provider
* @returns The hash (ID) of the transaction
*/
tzip12: async (body: DeployTezosNft, provider?: string) => deployTzip12(body, args.tezosWeb, provider),
tzip12: async (body: DeployContractData, provider?: string) =>
deployTzip12(body, args.tezosWeb, provider),
},
estimate: {
contractDeploy: async (body: DeployContractData, provider?: string) =>
estimateContractDeploy(body, args.tezosWeb, provider),
tokenMint: async (body: MintTezosToken, provider?: string) =>
estimateTokenMint(body, args.tezosWeb, provider),
tokenTransfer: async (body: TransferTezosToken, provider?: string) =>
estimateTransferToken(body, args.tezosWeb, provider),
},
/**
* Mint a new NFT collection
Expand Down

0 comments on commit ad3ed51

Please sign in to comment.