Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CUDOS-2109 nft marketplace auctions #30

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions generate-proto-types.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash
set -o errexit -o nounset -o pipefail
command -v shellcheck >/dev/null && shellcheck "$0"

read -p "Enter full path to your cudos-node root directory:" CUDOS_NODE_DIR
CUDOS_NODE_PROTO_DIR="$(realpath $CUDOS_NODE_DIR/proto)"
THIRD_PARTY_PROTO_DIR="$(realpath $CUDOS_NODE_DIR/third_party/proto)"
TS_PROTO_OPTS="esModuleInterop=true,forceLong=long,useOptionals=messages,useDate=false"

read -p "Enter output folder:" OUT_DIR
mkdir -p "$OUT_DIR"

protoc \
--plugin="$(yarn bin protoc-gen-ts_proto)" \
--ts_proto_out="$OUT_DIR" \
--proto_path="$CUDOS_NODE_PROTO_DIR" \
--proto_path="$THIRD_PARTY_PROTO_DIR" \
--ts_proto_opt="$TS_PROTO_OPTS" \
$CUDOS_NODE_PROTO_DIR/*/**.proto
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"test": "cd tests && chmod u+x run-tests.sh && ./run-tests.sh",
"build": "rm -rf ./build && tsc",
"build-or-skip": "[ -n \"$SKIP_BUILD\" ] || yarn build",
"pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js"
"pack-web": "yarn build-or-skip && webpack --mode development --config webpack.web.config.js",
"generate-proto-types": "./generate-proto-types.sh"
},
"files": [
"build/",
Expand Down Expand Up @@ -49,9 +50,10 @@
"ts-jest": "^28.0.5",
"ts-loader": "^9.2.6",
"ts-node": "^8",
"ts-proto": "^1.139.0",
"tslib": "^2.4.1",
"typescript": "^4.4.3",
"webpack": "^5.53.0",
"webpack-cli": "^4.8.0"
}
}
}
61 changes: 61 additions & 0 deletions src/stargate/cudos-signingstargateclient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ import { Int53 } from "../math";
import { MarketplaceModule } from "./modules/marketplace/module";
import { AddressbookModule } from "./modules/addressbook/module";
import { Royalty } from "./modules/marketplace/proto-types/royalty";
import { Duration } from "cosmjs-types/google/protobuf/duration";
import { DutchAuctionRequest, EnglishAuctionRequest } from "./modules/marketplace/auctions";

export class CudosSigningStargateClient extends SigningStargateClient {
private readonly directSigner: OfflineDirectSigner | undefined;
Expand Down Expand Up @@ -730,4 +732,63 @@ export class CudosSigningStargateClient extends SigningStargateClient {
);
return this.signAndBroadcast(creator, [msg], fee, memo);
}

public async marketplacePublishAuction(
creator: string,
tokenId: string,
denomId: string,
duration: Duration,
auction: EnglishAuctionRequest | DutchAuctionRequest,
gasPrice: GasPrice,
gasMultiplier = DEFAULT_GAS_MULTIPLIER,
memo = ""
): Promise<DeliverTxResponse> {
const { msg, fee } = await this.marketplaceModule.msgPublishAuction(
creator,
tokenId,
denomId,
duration,
auction,
gasPrice,
gasMultiplier,
memo
);
return this.signAndBroadcast(creator, [msg], fee, memo);
}

public async marketplacePlaceBid(
bidder: string,
auctionId: string,
amount: Coin,
gasPrice: GasPrice,
gasMultiplier = DEFAULT_GAS_MULTIPLIER,
memo = ""
): Promise<DeliverTxResponse> {
const { msg, fee } = await this.marketplaceModule.msgPlaceBid(
bidder,
auctionId,
amount,
gasPrice,
gasMultiplier,
memo
);
return this.signAndBroadcast(bidder, [msg], fee, memo);
}

public async marketplaceAcceptBid(
sender: string,
auctionId: string,
gasPrice: GasPrice,
gasMultiplier = DEFAULT_GAS_MULTIPLIER,
memo = ""
): Promise<DeliverTxResponse> {
const { msg, fee } = await this.marketplaceModule.msgAcceptBid(
sender,
auctionId,
gasPrice,
gasMultiplier,
memo
);
return this.signAndBroadcast(sender, [msg], fee, memo);
}
}
1 change: 1 addition & 0 deletions src/stargate/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export * as nftQueryProto from './nft/proto-types/query'

export * as marketplaceMsgProto from './marketplace/proto-types/tx'
export * as marketplaceQueryProto from './marketplace/proto-types/query'
export * as marketplaceAuctionProto from './marketplace/proto-types/auction'

export * as addressbookMsgProto from './addressbook/proto-types/tx'
export * as addressbookQueryProto from './addressbook/proto-types/query'
32 changes: 32 additions & 0 deletions src/stargate/modules/marketplace/auctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { Any } from "cosmjs-types/google/protobuf/any";
import { EnglishAuction, DutchAuction } from "./proto-types/auction";
import { Coin } from "./proto-types/coin";
import { dutchAuction, englishAuction } from "./types";

export type EnglishAuctionRequest = {
minPrice: Coin;
};

export type DutchAuctionRequest = {
startPrice: Coin;
minPrice: Coin;
};

export function encodeAuction(auctionReq: EnglishAuctionRequest | DutchAuctionRequest): Any {
if ("startPrice" in auctionReq) {
return {
typeUrl: dutchAuction.typeUrl,
value: DutchAuction.encode(DutchAuction.fromPartial({
minPrice: auctionReq.minPrice,
startPrice: auctionReq.startPrice,
})).finish()
};
} else {
return {
typeUrl: englishAuction.typeUrl,
value: EnglishAuction.encode(EnglishAuction.fromPartial({
minPrice: auctionReq.minPrice,
})).finish()
};
}
}
12 changes: 11 additions & 1 deletion src/stargate/modules/marketplace/clients/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import {
QueryGetNftResponse,
QueryAllNftResponse,
QueryParamsResponse,
QueryListAdminsResponse
QueryListAdminsResponse,
QueryAllAuctionResponse,
QueryGetAuctionResponse
} from '../proto-types/query';

import { PageRequest } from 'cosmjs-types/cosmos/base/query/v1beta1/pagination';
Expand Down Expand Up @@ -50,4 +52,12 @@ export class MarketplaceQueryClient {
public async getAdmins(): Promise<QueryListAdminsResponse> {
return this.queryClient.marketplace.admins();
}

public async getAuction(id: Long): Promise<QueryGetAuctionResponse> {
return this.queryClient.marketplace.auction(id);
}

public async getAllAuctions(pagination?: PageRequest): Promise<QueryAllAuctionResponse> {
return this.queryClient.marketplace.allAuctions(pagination);
}
}
89 changes: 86 additions & 3 deletions src/stargate/modules/marketplace/module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import { MsgCreateCollection, MsgPublishCollection, MsgPublishNft,
MsgMintNft, MsgAddAdmin, MsgRemoveAdmin } from './proto-types/tx';

import { Royalty } from './proto-types/royalty';

import { msgPublishAuction, msgPlaceBid, msgAcceptBid } from './types';
import { Duration } from 'cosmjs-types/google/protobuf/duration';
import { MsgPublishAuction, MsgPlaceBid, MsgAcceptBid } from './proto-types/tx';
import { encodeAuction, EnglishAuctionRequest, DutchAuctionRequest } from './auctions';

export class MarketplaceModule {
private readonly _client: ClientSimulateFn;
Expand All @@ -31,7 +34,10 @@ export class MarketplaceModule {
msgVerifyCollection,
msgUnverifyCollection,
msgAddAdmin,
msgRemoveAdmin
msgRemoveAdmin,
msgPublishAuction,
msgPlaceBid,
msgAcceptBid
]);
}

Expand Down Expand Up @@ -366,4 +372,81 @@ export class MarketplaceModule {
fee: fee
}
}
}

public async msgPublishAuction(
creator: string,
tokenId: string,
denomId: string,
duration: Duration,
auction: EnglishAuctionRequest | DutchAuctionRequest,
gasPrice: GasPrice,
gasMultiplier = DEFAULT_GAS_MULTIPLIER,
memo = ""
): Promise<{ msg: EncodeObject; fee: StdFee }> {
const msgEncoded = {
typeUrl: msgPublishAuction.typeUrl,
value: MsgPublishAuction.fromPartial({
creator: creator,
denomId: denomId,
tokenId: tokenId,
duration: duration,
auction: encodeAuction(auction)
})
};

const fee = await estimateFee(this._client, creator, [msgEncoded], gasPrice, gasMultiplier, memo);

return {
msg: msgEncoded,
fee: fee
};
}

public async msgPlaceBid(
bidder: string,
auctionId: string,
amount: Coin,
gasPrice: GasPrice,
gasMultiplier = DEFAULT_GAS_MULTIPLIER,
memo = ""
): Promise<{ msg: EncodeObject; fee: StdFee }> {
const msgEncoded = {
typeUrl: msgPlaceBid.typeUrl,
value: MsgPlaceBid.fromPartial({
bidder: bidder,
auctionId: auctionId,
amount: amount
})
};

const fee = await estimateFee(this._client, bidder, [msgEncoded], gasPrice, gasMultiplier, memo);

return {
msg: msgEncoded,
fee: fee
};
}

public async msgAcceptBid(
sender: string,
auctionId: string,
gasPrice: GasPrice,
gasMultiplier = DEFAULT_GAS_MULTIPLIER,
memo = ""
): Promise<{ msg: EncodeObject; fee: StdFee }> {
const msgEncoded = {
typeUrl: msgAcceptBid.typeUrl,
value: MsgAcceptBid.fromPartial({
sender: sender,
auctionId: auctionId
})
};

const fee = await estimateFee(this._client, sender, [msgEncoded], gasPrice, gasMultiplier, memo);

return {
msg: msgEncoded,
fee: fee,
};
}
}
Loading