-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0640a13
commit d0bf35f
Showing
8 changed files
with
162 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
tests/conditionalVault/unit/addMetadataToConditionalTokens.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { sha256, ConditionalVaultClient, getConditionalTokenMintAddr, getMetadataAddr } from "@metadaoproject/futarchy"; | ||
import { Keypair, PublicKey } from "@solana/web3.js"; | ||
import { assert } from "chai"; | ||
import { createMint } from "spl-token-bankrun"; | ||
import * as anchor from "@coral-xyz/anchor"; | ||
import { expectError } from "../../utils"; | ||
import { Metadata, deserializeMetadata, getMetadataAccountDataSerializer } from "@metaplex-foundation/mpl-token-metadata"; | ||
|
||
export default function suite() { | ||
let vaultClient: ConditionalVaultClient; | ||
let question: PublicKey; | ||
let vault: PublicKey; | ||
let underlyingTokenMint: PublicKey; | ||
|
||
const metadataSerializer = getMetadataAccountDataSerializer(); | ||
|
||
before(function () { | ||
vaultClient = this.vaultClient; | ||
}); | ||
|
||
async function setupVault(outcomes: number) { | ||
let questionId = sha256(new Uint8Array([1, 2, 3])); | ||
let oracle = Keypair.generate(); | ||
|
||
question = await vaultClient.initializeQuestion( | ||
questionId, | ||
oracle.publicKey, | ||
outcomes | ||
); | ||
|
||
underlyingTokenMint = await createMint( | ||
this.banksClient, | ||
this.payer, | ||
this.payer.publicKey, | ||
null, | ||
8 | ||
); | ||
|
||
vault = await vaultClient.initializeVault(question, underlyingTokenMint, outcomes); | ||
} | ||
|
||
async function addMetadataToConditionalTokens(outcomes: number) { | ||
for (let i = 0; i < outcomes; i++) { | ||
await vaultClient.addMetadataToConditionalTokensIx( | ||
vault, | ||
i, | ||
`Outcome ${i}`, | ||
`OUT${i}`, | ||
`https://example.com/image${i}.png` | ||
).rpc(); | ||
} | ||
} | ||
|
||
async function verifyMetadata(outcomes: number) { | ||
for (let i = 0; i < outcomes; i++) { | ||
const [conditionalTokenMint] = getConditionalTokenMintAddr( | ||
vaultClient.vaultProgram.programId, | ||
vault, | ||
i | ||
); | ||
|
||
const storedMetadata = await this.banksClient.getAccount(getMetadataAddr(conditionalTokenMint)[0]); | ||
assert.isNotNull(storedMetadata); | ||
const metadata = metadataSerializer.deserialize(storedMetadata.data)[0]; | ||
assert.equal(metadata.name, `Outcome ${i}`); | ||
assert.equal(metadata.symbol, `OUT${i}`); | ||
const expectedUri = `data:,{"name":"${metadata.name}","symbol":"${metadata.symbol}","image":"https://example.com/image${i}.png"}`; | ||
assert.equal(metadata.uri, expectedUri); | ||
} | ||
} | ||
|
||
it("adds metadata to 2-token vault", async function () { | ||
await setupVault.call(this, 2); | ||
await addMetadataToConditionalTokens(2); | ||
await verifyMetadata.call(this, 2); | ||
}); | ||
|
||
it("adds metadata to 3-token vault", async function () { | ||
await setupVault.call(this, 3); | ||
await addMetadataToConditionalTokens(3); | ||
await verifyMetadata.call(this, 3); | ||
}); | ||
|
||
it("adds metadata to 10-token vault", async function () { | ||
await setupVault.call(this, 10); | ||
await addMetadataToConditionalTokens(10); | ||
await verifyMetadata.call(this, 10); | ||
}); | ||
|
||
it("cannot add metadata twice for the same conditional token", async function () { | ||
await setupVault.call(this, 2); | ||
await addMetadataToConditionalTokens(2); | ||
|
||
const callbacks = expectError( | ||
"ConditionalTokenMetadataAlreadySet", | ||
"added metadata to a conditional token that already had metadata" | ||
); | ||
|
||
await vaultClient.addMetadataToConditionalTokensIx( | ||
vault, | ||
0, | ||
"New Outcome", | ||
"NEW", | ||
"https://example.com/new.png" | ||
) | ||
.rpc() | ||
.then(callbacks[0], callbacks[1]); | ||
}); | ||
} |