Skip to content

Commit

Permalink
fix: add missing stx burn and NFT post conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
ahsan-javaid authored and reedrosenbluth committed Jul 26, 2021
1 parent 2676d71 commit 7e0fcba
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 7 deletions.
57 changes: 57 additions & 0 deletions packages/bns/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ import {
someCV,
noneCV,
UnsignedContractCallOptions,
PostCondition,
createSTXPostCondition,
createStacksPublicKey,
publicKeyToAddress,
FungibleConditionCode,
AddressVersion,
createNonFungiblePostCondition,
NonFungibleConditionCode,
parseAssetInfoString,
tupleCV,
} from '@stacks/transactions';

import { StacksNetwork } from '@stacks/network';
Expand All @@ -41,6 +51,12 @@ function getBnsContractAddress(network: StacksNetwork) {
else throw new Error(`Unexpected ChainID: ${network.chainId}`);
}

function getAddressVersion(network: StacksNetwork) {
return network.chainId === ChainID.Mainnet
? AddressVersion.MainnetSingleSig
: AddressVersion.TestnetSingleSig;
}

export interface PriceFunction {
base: BN;
coefficient: BN;
Expand Down Expand Up @@ -70,6 +86,7 @@ export interface BnsContractCallOptions {
publicKey: string;
network: StacksNetwork;
attachment?: Buffer;
postConditions?: PostCondition[];
}

async function makeBnsContractCall(options: BnsContractCallOptions): Promise<StacksTransaction> {
Expand All @@ -82,6 +99,7 @@ async function makeBnsContractCall(options: BnsContractCallOptions): Promise<Sta
validateWithAbi: false,
network: options.network,
anchorMode: AnchorMode.Any,
postConditions: options.postConditions,
};

return makeUnsignedContractCall(txOptions);
Expand Down Expand Up @@ -298,11 +316,18 @@ export async function buildPreorderNamespaceTx({
const saltedNamespaceBuffer = Buffer.from(`${namespace}${salt}`);
const hashedSaltedNamespace = hash160(saltedNamespaceBuffer);

const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), createStacksPublicKey(publicKey)),
FungibleConditionCode.Equal,
stxToBurn
);

return makeBnsContractCall({
functionName: bnsFunctionName,
functionArgs: [bufferCV(hashedSaltedNamespace), uintCVFromBN(stxToBurn)],
publicKey,
network,
postConditions: [burnSTXPostCondition],
});
}

Expand Down Expand Up @@ -518,11 +543,18 @@ export async function buildPreorderNameTx({
const saltedNamesBuffer = Buffer.from(`${fullyQualifiedName}${salt}`);
const hashedSaltedName = hash160(saltedNamesBuffer);

const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), createStacksPublicKey(publicKey)),
FungibleConditionCode.Equal,
stxToBurn
);

return makeBnsContractCall({
functionName: bnsFunctionName,
functionArgs: [bufferCV(hashedSaltedName), uintCVFromBN(stxToBurn)],
publicKey,
network,
postConditions: [burnSTXPostCondition],
});
}

Expand Down Expand Up @@ -682,13 +714,32 @@ export async function buildTransferNameTx({
standardPrincipalCV(newOwnerAddress),
zonefile ? someCV(bufferCV(getZonefileHash(zonefile))) : noneCV(),
];
const postConditionSender = createNonFungiblePostCondition(
publicKeyToAddress(getAddressVersion(network), createStacksPublicKey(publicKey)),
NonFungibleConditionCode.DoesNotOwn,
parseAssetInfoString(`${getBnsContractAddress(network)}.bns::names`),
tupleCV({
name: bufferCVFromString(name),
namespace: bufferCVFromString(namespace),
})
);
const postConditionReceiver = createNonFungiblePostCondition(
newOwnerAddress,
NonFungibleConditionCode.Owns,
parseAssetInfoString(`${getBnsContractAddress(network)}.bns::names`),
tupleCV({
name: bufferCVFromString(name),
namespace: bufferCVFromString(namespace),
})
);

return makeBnsContractCall({
functionName: bnsFunctionName,
functionArgs,
publicKey,
network,
attachment: zonefile ? Buffer.from(zonefile) : undefined,
postConditions: [postConditionSender, postConditionReceiver],
});
}

Expand Down Expand Up @@ -786,12 +837,18 @@ export async function buildRenewNameTx({
newOwnerAddress ? someCV(standardPrincipalCV(newOwnerAddress)) : noneCV(),
zonefile ? someCV(bufferCV(getZonefileHash(zonefile))) : noneCV(),
];
const burnSTXPostCondition = createSTXPostCondition(
publicKeyToAddress(getAddressVersion(network), createStacksPublicKey(publicKey)),
FungibleConditionCode.Equal,
stxToBurn
);

return makeBnsContractCall({
functionName: bnsFunctionName,
functionArgs,
publicKey,
network,
attachment: zonefile ? Buffer.from(zonefile) : undefined,
postConditions: [burnSTXPostCondition],
});
}

0 comments on commit 7e0fcba

Please sign in to comment.