Skip to content

Commit

Permalink
Flow NFT support - read methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Samuel Sramko committed May 26, 2021
1 parent e04024e commit da43540
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 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/tatum",
"version": "1.14.1",
"version": "1.14.2",
"description": "",
"main": "dist/src/index.js",
"repository": "https://github.com/tatumio/tatum-js",
Expand Down
12 changes: 11 additions & 1 deletion src/transaction/flow.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Currency, TransferFlow} from '../model';
import {flowAddPublicKeyToAccount, flowCreateAccountFromPublicKey, flowSendTransaction} from './flow';
import {flowAddPublicKeyToAccount, flowCreateAccountFromPublicKey, flowSendTransaction, getFlowNftMetadata, getFlowNftTokenByAddress} from './flow';

describe('Flow tests', () => {

Expand Down Expand Up @@ -29,4 +29,14 @@ describe('Flow tests', () => {
const result = await flowSendTransaction(true, body);
expect(result.txId).toBeDefined();
});

it.skip('should get NFT token by address', async () => {
const result = await getFlowNftTokenByAddress(true, '0x21cbd745a4df66f1', '1');
expect(result).toBeDefined();
});

it.skip('should get NFT token metadata', async () => {
const result = await getFlowNftMetadata(true, '0x21cbd745a4df66f1', '1', '1');
expect(result).toBeDefined();
});
});
23 changes: 23 additions & 0 deletions src/transaction/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import {Currency, FlowBurnNft, FlowMintMultipleNft, FlowMintNft, FlowTransferNft
import {generatePrivateKeyFromMnemonic} from '../wallet';
import {
burnFlowNftTokenTxTemplate,
metadataFlowNftTokenScript,
mintFlowMultipleNftTokenTxTemplate,
mintFlowNftTokenTxTemplate,
prepareAddPublicKeyToAccountTxTemplate,
prepareCreateAccountWithFUSDFromPublicKeyTxTemplate,
prepareTransferFlowTxTemplate,
tokenByAddressFlowNftTokenScript,
transferFlowNftTokenTxTemplate
} from './flowTransaction';

Expand Down Expand Up @@ -129,6 +131,15 @@ const sendTransaction = async (testnet: boolean, {
};
};

const sendScript = async (testnet: boolean, code: string, args: Argument[]) => {
fcl.config().put('accessNode.api', testnet ? 'https://access-testnet.onflow.org' : 'https://access-mainnet-beta.onflow.org');
const response = await fcl.send([
fcl.script(code),
fcl.args(args.map(arg => fcl.arg(arg.value, types[arg.type]))),
]);
return fcl.decode(response);
};

export const flowSignKMSTransaction = async (tx: TransactionKMS, privateKeys: string[], testnet: boolean) => {
if (tx.chain !== Currency.FLOW) {
throw Error('Unsupported chain.');
Expand Down Expand Up @@ -188,6 +199,18 @@ export const flowAddPublicKeyToAccount = async (testnet: boolean, publicKey: str
return {txId: result.id, address: result.events[0].data.address};
};

export const getFlowNftMetadata = async (testnet: boolean, account: string, id: string, tokenType: string) => {
const code = metadataFlowNftTokenScript(testnet);
const args = [{type: 'Address', value: account}, {type: 'UInt64', value: id}, {type: 'String', value: tokenType}];
return await sendScript(testnet, code, args);
};

export const getFlowNftTokenByAddress = async (testnet: boolean, account: string, tokenType: string) => {
const code = tokenByAddressFlowNftTokenScript(testnet);
const args = [{type: 'Address', value: account}, {type: 'String', value: tokenType}];
return await sendScript(testnet, code, args);
};

export const sendFlowNftMintToken = async (testnet: boolean, body: FlowMintNft):
Promise<{ txId: string }> => {
await validateBody(body, FlowMintNft);
Expand Down
25 changes: 25 additions & 0 deletions src/transaction/flowTransaction/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,31 @@ transaction(type: String) {
}
}`;

export const metadataFlowNftTokenScript = (testnet: boolean) => `
import TatumMultiNFT from ${testnet ? FLOW_TESTNET_ADDRESSES.TatumMultiNFT : FLOW_MAINNET_ADDRESSES.TatumMultiNFT}
pub fun main(account: Address, id: UInt64, type: String): String {
let collectionRef = getAccount(account)
.getCapability(TatumMultiNFT.CollectionPublicPath)
.borrow<&{TatumMultiNFT.TatumMultiNftCollectionPublic}>()
?? panic("Could not borrow capability from public collection")
return collectionRef.getMetadata(id: id, type: type)
}`;


export const tokenByAddressFlowNftTokenScript = (testnet: boolean) => `
import TatumMultiNFT from ${testnet ? FLOW_TESTNET_ADDRESSES.TatumMultiNFT : FLOW_MAINNET_ADDRESSES.TatumMultiNFT}
pub fun main(address: Address, type: String): [UInt64] {
let collectionRef = getAccount(address)
.getCapability(TatumMultiNFT.CollectionPublicPath)
.borrow<&{TatumMultiNFT.TatumMultiNftCollectionPublic}>()
?? panic("Could not borrow capability from public collection")
return collectionRef.getIDs(type: type)
}`;

export const mintFlowNftTokenTxTemplate = (testnet: boolean) => `
import TatumMultiNFT from ${testnet ? FLOW_TESTNET_ADDRESSES.TatumMultiNFT : FLOW_MAINNET_ADDRESSES.TatumMultiNFT}
Expand Down

0 comments on commit da43540

Please sign in to comment.