Skip to content

Commit

Permalink
feat(x/nft): draft for NftKit (WiP)
Browse files Browse the repository at this point in the history
Ref: #33

Note: update /packages/cosmic-proto/dist/helpers.d.ts to the following:

export declare const toRequestQueryJson: (msg: QueryBalanceRequestProtoMsg | QueryNFTsRequestProtoMsg, opts?: RequestQueryOpts) => JsonSafe<RequestQuery>;
  • Loading branch information
Jorge-Lopes committed Jun 5, 2024
1 parent f7470aa commit 7e4a3c3
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 0 deletions.
83 changes: 83 additions & 0 deletions packages/orchestration/src/exos/nftKit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import { M } from '@endo/patterns';
import { E } from '@endo/far';
import { TopicsRecordShape } from '@agoric/zoe/src/contractSupport/index.js';
import { ChainAddressShape, NftShape } from '../typeGuards.js';
import {
QueryNFTsRequest,
QueryNFTsResponse,
} from '@agoric/cosmic-proto/cosmos/nft/v1beta1/query.js';
import { toRequestQueryJson } from '@agoric/cosmic-proto';
import { Fail } from '@agoric/assert';
import { decodeBase64 } from '@endo/base64';

/**
* @param {import('@agoric/zone').Zone} zone
* @param {ZCF} zcf
*/
export const prepareNftKit = (zone, zcf) => {
const makeNftKit = zone.exoClassKit(
'Nft Account Holder',
{
helper: M.interface('helper', {
toNftDenomAmount: M.call(M.string()).returns(M.any()),
}),
invitationMakers: M.interface('invitationMakers', {
send: M.call().returns(M.any()),
}),
holder: M.interface('IcaAccountHolder', {
getPublicTopics: M.call().returns(TopicsRecordShape),
getAddress: M.call().returns(ChainAddressShape),
getBalance: M.call(M.string()).returns(NftShape),
getBalances: M.call().returns(M.arrayOf(NftShape)),
}),
},
/**
* @param {import('../types.js').ChainAddress} chainAddress
* @param {import('../types.js').IcaAccount} account
* @param {import('../types.js').ICQConnection} icqConnection
* @param {string} nftDenom
* @param {StorageNode} storageNode
*/
(chainAddress, account, icqConnection, nftDenom, storageNode) => {
return { chainAddress, account, icqConnection, nftDenom };
},
{
helper: {
toNftDenomAmount(nft) {},
},

invitationMakers: {
async send(nftAmount, address) {},
},

holder: {
getAddress() {
return this.state.chainAddress;
},
async getBalance(denom) {
const { toNftDenomAmount } = this.facets.helper;
const { chainAddress, icqConnection, nftDenom } = this.state;
denom ||= nftDenom;
assert.typeof(denom, 'string');

const [result] = await E(icqConnection).query([
toRequestQueryJson(
QueryNFTsRequest.toProtoMsg({
classId: denom,
owner: chainAddress.address,
}),
),
]);

if (!result?.key) throw Fail`Error parsing result ${result}`;
const { nfts } = QueryNFTsResponse.decode(decodeBase64(result.key));
if (!nfts) throw Fail`Result lacked balance key: ${result}`;

return harden(toNftDenomAmount(nfts));
},
async getBalances() {},
},
},
);
return harden({ makeNftKit });
};
4 changes: 4 additions & 0 deletions packages/orchestration/src/typeGuards.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,7 @@ export const IBCTransferOptionsShape = M.splitRecord(
memo: M.string(),
},
);

export const NftShape = { nftDenom: M.string(), value: M.record() };

export const NftAmountShape = harden({ nftDenom: M.string(), value: M.bag() });

0 comments on commit 7e4a3c3

Please sign in to comment.