Skip to content

Commit

Permalink
Fix UpdateTrait bug when selecting a Variant (#107)
Browse files Browse the repository at this point in the history
* Fix RPC Client Bug

* fmt

* dont allow builders to put in more than required amount

* new test passing

* bump js

* rm unneeded console log

* fix test
  • Loading branch information
jackrieck authored Oct 18, 2023
1 parent cce9696 commit 8d269b2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 9 deletions.
2 changes: 1 addition & 1 deletion js/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raindrops-protocol/raindrops-cli",
"version": "0.5.7",
"version": "0.5.8",
"license": "Apache-2.0",
"bin": {
"boot-up": "./src/boot_up.ts",
Expand Down
2 changes: 1 addition & 1 deletion js/lib/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@raindrops-protocol/raindrops",
"version": "0.5.7",
"version": "0.5.8",
"module": "./build/main.js",
"main": "./build/main.js",
"types": "./build/main.d.ts",
Expand Down
4 changes: 3 additions & 1 deletion js/lib/src/contract/avatar/rpc/avatar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,9 @@ export class AvatarClient {
);

const ixArgs = {
variantMetadata: args.variantMetadata ? args.variantMetadata : null,
variantMetadata: args.variantMetadata
? args.variantMetadata.formatForIx()
: null,
variantOption: args.variantOption
? args.variantOption.formatForIx()
: null,
Expand Down
16 changes: 11 additions & 5 deletions rust/itemv2/src/state/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,15 +256,21 @@ impl Build {
.any(|mint_data| mint_data.mint.eq(&ingredient_mint))
{
build_ingredient_data.current_amount += amount;

// dont allow builders to put in more than the required amount
require!(
build_ingredient_data.current_amount <= build_ingredient_data.required_amount,
ErrorCode::IncorrectIngredient
);

found = true;
break;
}
}
if found {
Ok(())
} else {
Err(ErrorCode::IncorrectIngredient.into())
}

require!(found, ErrorCode::IncorrectIngredient);

Ok(())
}

pub fn decrement_build_amount(&mut self, ingredient_mint: Pubkey, amount: u64) -> Result<()> {
Expand Down
86 changes: 85 additions & 1 deletion rust/tests/itemv2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import * as splToken from "@solana/spl-token";
import * as cmp from "@solana/spl-account-compression";
import * as mplAuth from "@metaplex-foundation/mpl-token-auth-rules";
import { assert } from "chai";
import { encode } from "@msgpack/msgpack";
import fs from "fs";
import path from "path";

Expand Down Expand Up @@ -3093,6 +3092,91 @@ describe("itemv2", () => {
console.log("releaseFromEscrowTxSig: %s", result.txid);
}
});

it("do not allow builder to add more than required amount for an ingredient", async () => {
const payer = await newPayer(connection);

const itemProgram = await ItemProgramV2.getProgramWithConfig(
ItemProgramV2,
{
asyncSigning: false,
provider: new anchor.AnchorProvider(
connection,
new anchor.Wallet(payer),
{ commitment: "confirmed" }
),
idl: Idls.ItemV2IDL,
}
);

// ingredient 1, nft
const nftItemClass = await createItemClassCollectionMode(
payer,
connection,
2,
false
);

// output nft
const outputItemClass = await createItemClassCollectionMode(
payer,
connection,
1,
false,
{
buildEnabled: true,
payment: null,
ingredientArgs: [
{
itemClass: nftItemClass.itemClass,
requiredAmount: new BN(1),
buildEffect: {
degradation: null,
cooldown: null,
},
isDeterministic: false,
},
],
buildPermitRequired: false,
selectableOutputs: [],
}
);

// start the build process
const startBuildAccounts: Instructions.ItemV2.StartBuildAccounts = {
itemClass: outputItemClass.itemClass,
builder: itemProgram.client.provider.publicKey,
};

const startBuildArgs: Instructions.ItemV2.StartBuildArgs = {
recipeIndex: new anchor.BN(0),
recipeOutputSelection: [],
};

const startBuildResult = await itemProgram.startBuild(
startBuildAccounts,
startBuildArgs
);
console.log("startBuildTxSig: %s", startBuildResult.txid);

// add ingredient to build using collection verification
await addIngredientCollection(
itemProgram,
outputItemClass.itemClass,
nftItemClass.mints[0],
nftItemClass.itemClass,
new anchor.BN(1)
);

// try to add second ingredient and this should fail because the recipe only requires 1
assertRejects(addIngredientCollection(
itemProgram,
outputItemClass.itemClass,
nftItemClass.mints[1],
nftItemClass.itemClass,
new anchor.BN(1)
));
})
});

async function newPayer(
Expand Down

0 comments on commit 8d269b2

Please sign in to comment.