Skip to content

Commit

Permalink
Introduce ClaimItems action
Browse files Browse the repository at this point in the history
  • Loading branch information
ipdae committed Apr 29, 2024
1 parent dca11fd commit 4f21a19
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 1 deletion.
52 changes: 52 additions & 0 deletions @planetarium/lib9c/src/actions/claim_items.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import type { Address } from "@planetarium/account";
import {
BencodexDictionary,
type Dictionary,
type Key,
type Value,
} from "@planetarium/bencodex";
import {
type FungibleAssetValue,
encodeFungibleAssetValue,
} from "@planetarium/tx";
import { GameAction } from "./common.js";

export class ClaimItems extends GameAction {
protected readonly type_id: string = "claim_items";

public readonly claimData: [Address, FungibleAssetValue[]][];
public readonly memo: string | null;

constructor({
claimData,
memo,
id,
}: {
claimData: [Address, FungibleAssetValue[]][];
memo?: string;
id?: Uint8Array;
}) {
super(id);

this.claimData = claimData;
this.memo = memo || null;
}

protected plain_value_internal(): Dictionary {
const params: [Key, Value][] = [
[
"cd",
this.claimData.map(([address, fungibleAssetValues]) => [
address.toBytes(),
fungibleAssetValues.map(fav => encodeFungibleAssetValue(fav)),
]),
],
];

if (this.memo !== null) {
params.push(["m", this.memo]);
}
console.log(params)
return new BencodexDictionary(params);
}
}
1 change: 1 addition & 0 deletions @planetarium/lib9c/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ export { TransferAsset } from "./actions/transfer_asset.js";
export { TransferAssets } from "./actions/transfer_assets.js";
export { DeliverToOtherGarages } from "./actions/deliver_to_others_garages.js";
export { LoadIntoMyGarages } from "./actions/load_into_my_garages.js";
export { ClaimItems } from "./actions/claim_items.js";
export {
generateGuid,
uuidToGuidBytes,
Expand Down
50 changes: 50 additions & 0 deletions @planetarium/lib9c/test/actions/claim_items.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { describe } from "vitest";
import { ClaimItems, fav } from "../../src/index.js";
import { runTests } from "./common.js";
import { agentAddress } from "./fixtures.js";

describe("ClaimItems", () => {
describe("odin", () => {
runTests("valid case", [
new ClaimItems({
claimData: [
[
agentAddress,
[
fav(
{
ticker: "FAV__CRYSTAL",
decimalPlaces: 18,
minters: null,
maximumSupply: null,
totalSupplyTrackable: false,
},
1,
),
],
],
],
}),
new ClaimItems({
claimData: [
[
agentAddress,
[
fav(
{
ticker: "FAV__CRYSTAL",
decimalPlaces: 18,
minters: null,
maximumSupply: null,
totalSupplyTrackable: false,
},
1,
),
],
],
],
memo: "memo"
}),
]);
});
});
4 changes: 3 additions & 1 deletion @planetarium/lib9c/test/actions/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ import { join } from "node:path";
import { execa } from "execa";
import { expect, test } from "vitest";
import type { PolymorphicAction } from "../../src/index.js";
import {Buffer} from "buffer";

export function runTests(name: string, cases: PolymorphicAction[]) {
for (let i = 0; i < cases.length; i++) {
test(`${name} ${i}`, async () => {
const action = cases[i];

console.log(action)
const bytes = action.serialize();
console.log(Buffer.from(bytes).toString("hex"))

const { exitCode } = await execa(
"dotnet",
Expand Down

0 comments on commit 4f21a19

Please sign in to comment.