Skip to content

Commit

Permalink
Merge pull request #52 from sCrypt-Inc/sym
Browse files Browse the repository at this point in the history
Sym
  • Loading branch information
zhfnjust authored Nov 2, 2023
2 parents 74273b4 + d5025f8 commit b3c9fd8
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 34 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scrypt-ord",
"version": "1.0.10",
"version": "1.0.11",
"description": "A sCrypt office 1Sats Ordinals SDK.",
"main": "dist/index.js",
"types": "src/index.ts",
Expand Down
19 changes: 16 additions & 3 deletions src/contracts/bsv20V2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@ export abstract class BSV20V2 extends SmartContract {
/** Decimals: set decimal precision, defaults to 0. This is different from BRC20 which defaults to 18. */
readonly dec: bigint

constructor(id: ByteString, max: bigint, dec: bigint) {
@prop()
/** Decimals: set decimal precision, defaults to 0. This is different from BRC20 which defaults to 18. */
readonly sym: ByteString

constructor(id: ByteString, sym: ByteString, max: bigint, dec: bigint) {
super(...arguments)
this.max = max
this.dec = dec
this.id = id
this.sym = sym
assert(this.max <= 18446744073709551615n)
assert(this.dec <= 18)
}
Expand Down Expand Up @@ -118,14 +123,21 @@ export abstract class BSV20V2 extends SmartContract {
throw new Error('token id is not initialized!')
}

async deployToken(): Promise<string> {
async deployToken(metaInfo?: Record<string, string>): Promise<string> {
if (this.id !== toByteString('')) {
throw new Error(
'contract instance to deploy token should not have a id!'
)
}

this.prependNOPScript(Ordinal.createDeployV2(this.max, this.dec))
this.prependNOPScript(
Ordinal.createDeployV2(
fromByteString(this.sym),
this.max,
this.dec,
metaInfo
)
)

const tx = await this.deploy(1)
return `${tx.id}_0`
Expand Down Expand Up @@ -220,6 +232,7 @@ export abstract class BSV20V2 extends SmartContract {
const { BSV20V2P2PKH } = require('./bsv20V2P2PKH')
const p2pkh = new BSV20V2P2PKH(
toByteString(current.getTokenId(), true),
current.sym,
current.max,
current.dec,
Addr(tokenChangeAddress.toByteString())
Expand Down
15 changes: 12 additions & 3 deletions src/contracts/bsv20V2P2PKH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,14 @@ export class BSV20V2P2PKH extends BSV20V2 {
@prop()
readonly addr: Addr

constructor(id: ByteString, amt: bigint, dec: bigint, addr: Addr) {
super(id, amt, dec)
constructor(
id: ByteString,
sym: ByteString,
amt: bigint,
dec: bigint,
addr: Addr
) {
super(id, sym, amt, dec)
this.init(...arguments)
this.addr = addr
}
Expand All @@ -59,7 +65,7 @@ export class BSV20V2P2PKH extends BSV20V2 {
}

override init(...args: any[]) {
const [id, _, __, addr] = args
const [id, _, __, ___, addr] = args
this.id = id
super.init(addr)
}
Expand Down Expand Up @@ -158,6 +164,7 @@ export class BSV20V2P2PKH extends BSV20V2 {
// we can't get max, and lim from the bsv20 insciption script.
const instance = new this(
toByteString(bsv20.id, true),
toByteString('', true),
-1n,
-1n,
Addr(args[0] as ByteString)
Expand Down Expand Up @@ -234,6 +241,7 @@ export class BSV20V2P2PKH extends BSV20V2 {
const nexts: StatefulNext<SmartContract>[] = []

const id = senders[0].getTokenId()
const sym = senders[0].sym

for (let i = 0; i < receivers.length; i++) {
const receiver = receivers[i]
Expand Down Expand Up @@ -261,6 +269,7 @@ export class BSV20V2P2PKH extends BSV20V2 {
if (tokenChangeAmt > 0n) {
const p2pkh = new BSV20V2P2PKH(
toByteString(id, true),
sym,
senders[0].max,
senders[0].dec,
Addr(ordPubKey.toAddress().toByteString())
Expand Down
26 changes: 19 additions & 7 deletions src/contracts/ordinal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,14 +270,26 @@ export class Ordinal extends SmartContractLib {
})
}

static createDeployV2(amt: bigint, dec: bigint): bsv.Script {
static createDeployV2(
sym: string,
amt: bigint,
dec: bigint,
metaInfo?: Record<string, string>
): bsv.Script {
return Ordinal.create({
content: JSON.stringify({
p: 'bsv-20',
op: 'deploy+mint',
amt: amt.toString().replace(/n/, ''),
dec: dec.toString().replace(/n/, ''),
}),
content: JSON.stringify(
Object.assign(
{},
{
p: 'bsv-20',
op: 'deploy+mint',
sym,
amt: amt.toString().replace(/n/, ''),
dec: dec.toString().replace(/n/, ''),
},
metaInfo ? metaInfo : {}
)
),
contentType: ContentType.BSV20,
})
}
Expand Down
10 changes: 8 additions & 2 deletions tests/contracts/counterFTV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@ export class CounterFTV2 extends BSV20V2 {
@prop(true)
counter: bigint

constructor(id: ByteString, max: bigint, dec: bigint, counter: bigint) {
super(id, max, dec)
constructor(
id: ByteString,
sym: ByteString,
max: bigint,
dec: bigint,
counter: bigint
) {
super(id, sym, max, dec)
this.init(...arguments)
this.counter = counter
}
Expand Down
10 changes: 8 additions & 2 deletions tests/contracts/hashLockFTV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,14 @@ export class HashLockFTV2 extends BSV20V2 {
@prop()
hash: Sha256

constructor(id: ByteString, max: bigint, dec: bigint, hash: Sha256) {
super(id, max, dec)
constructor(
id: ByteString,
sym: ByteString,
max: bigint,
dec: bigint,
hash: Sha256
) {
super(id, sym, max, dec)
this.init(...arguments)
this.hash = hash
}
Expand Down
3 changes: 2 additions & 1 deletion tests/contracts/permissionedFTV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,13 @@ export class PermissionedFTV2 extends BSV20V2 {

constructor(
id: ByteString,
sym: ByteString,
max: bigint,
dec: bigint,
issuer: PubKey,
owner: PubKey
) {
super(id, max, dec)
super(id, sym, max, dec)
this.init(...arguments)
this.issuer = issuer
this.owner = owner
Expand Down
13 changes: 9 additions & 4 deletions tests/examples/mintBSV20V2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,39 @@ function getSigner() {
}

async function main() {
HashLockFTV2.loadArtifact('tests/artifacts/contracts/hashLockFTV2.json')
HashLockFTV2.loadArtifact('./artifacts/contracts/hashLockFTV2.json')

// BSV20 fields
const max = 10n
const max = 10000n
const dec = 0n
const sym = toByteString('MEME', true)

// create contract instance
const message = toByteString('Hello sCrypt', true)
const hash = sha256(message)
const hashLock = new HashLockFTV2(toByteString(''), max, dec, hash)
const hashLock = new HashLockFTV2(toByteString(''), sym, max, dec, hash)
await hashLock.connect(getSigner())

// deploy the new BSV20V2 token
const tokenId = await hashLock.deployToken()
const tokenId = await hashLock.deployToken({
name: 'MEME TOKEN',
})
console.log(`tokenId: ${tokenId}`)

// for now, the contract instance holds the BSV20V2 token
// this token can be transferred only when the hash lock is solved
const addressAlice = Addr(myAddress.toByteString())
const alice = new BSV20V2P2PKH(
toByteString(tokenId, true),
sym,
max,
dec,
addressAlice
)
const addressBob = Addr(myAddress.toByteString())
const bob = new BSV20V2P2PKH(
toByteString(tokenId, true),
sym,
max,
dec,
addressBob
Expand Down
4 changes: 3 additions & 1 deletion tests/examples/transferBSV20V2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,21 @@ async function main() {
// BSV20 fields
const max = 21000000n
const dec = 0n
const sym = toByteString('MEME', true)

const signer = getSigner()

const message = toByteString('Hello sCrypt', true)
const hash = sha256(message)
const hashLock = new HashLockFTV2(toByteString(''), max, dec, hash)
const hashLock = new HashLockFTV2(toByteString(''), sym, max, dec, hash)
await hashLock.connect(signer)
const tokenId = await hashLock.deployToken()

console.log(`tokenId: ${tokenId}`)
const receiver = {
instance: new BSV20V2P2PKH(
toByteString(tokenId, true),
sym,
max,
dec,
Addr(myAddress.toByteString())
Expand Down
2 changes: 2 additions & 0 deletions tests/examples/transferBSV20V2P2PKH.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ function getSigner() {

async function main() {
const signer = getSigner()
const sym = toByteString('MEME', true)

await signer.connect()
const address = await signer.getDefaultAddress()
const p2pkh = new BSV20V2P2PKH(
toByteString(''),
sym,
10000000n,
0n,
Addr(address.toByteString())
Expand Down
5 changes: 5 additions & 0 deletions tests/specs/bsv20V2P2PKH.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use(chaiAsPromised)

describe('Test SmartContract `BSV20V2P2PKH`', () => {
let tokenId: ByteString
const sym: ByteString = toByteString('1SAT', true)
const max = 100000n
const dec = 0n

Expand All @@ -25,6 +26,7 @@ describe('Test SmartContract `BSV20V2P2PKH`', () => {
const address = await signer.getDefaultAddress()
bsv20V2P2PKH = new BSV20V2P2PKH(
toByteString(''),
sym,
max,
dec,
Addr(address.toByteString())
Expand All @@ -43,6 +45,7 @@ describe('Test SmartContract `BSV20V2P2PKH`', () => {
{
instance: new BSV20V2P2PKH(
tokenId,
sym,
max,
dec,
Addr(address.toByteString())
Expand Down Expand Up @@ -92,6 +95,7 @@ describe('Test SmartContract `BSV20V2P2PKH`', () => {
{
instance: new BSV20V2P2PKH(
tokenId,
sym,
max,
dec,
Addr(address.toByteString())
Expand Down Expand Up @@ -137,6 +141,7 @@ describe('Test SmartContract `BSV20V2P2PKH`', () => {
{
instance: new BSV20V2P2PKH(
tokenId,
sym,
max,
dec,
Addr(address.toByteString())
Expand Down
3 changes: 2 additions & 1 deletion tests/specs/counterFTV2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ describe('Test SmartContract `CounterFTV2`', () => {
let instance: CounterFTV2

let tokenId: string
const sym = toByteString('MEME', true)
before(async () => {
CounterFTV2.loadArtifact()

const max = 100000n
const dec = 0n
instance = new CounterFTV2(toByteString(''), max, dec, 0n)
instance = new CounterFTV2(toByteString(''), sym, max, dec, 0n)
await instance.connect(getDefaultSigner())
tokenId = await instance.deployToken()
})
Expand Down
6 changes: 6 additions & 0 deletions tests/specs/hashLockFTV2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ use(chaiAsPromised)
describe('Test SmartContract `HashLockFTV2`', () => {
const max = 100000n
const dec = 0n
const sym = toByteString('MEME', true)

let hashLock: HashLockFTV2
let tokenId: string
before(async () => {
HashLockFTV2.loadArtifact()
hashLock = new HashLockFTV2(
toByteString(''),
sym,
max,
dec,
sha256(toByteString('hello, sCrypt!:0', true))
Expand All @@ -31,6 +33,7 @@ describe('Test SmartContract `HashLockFTV2`', () => {
for (let i = 0; i < 3; i++) {
const receiver = new HashLockFTV2(
toByteString(tokenId, true),
sym,
max,
dec,
sha256(toByteString(`hello, sCrypt!:${i + 1}`, true))
Expand Down Expand Up @@ -64,6 +67,7 @@ describe('Test SmartContract `HashLockFTV2`', () => {
const callContract = async () => {
const receiver = new HashLockFTV2(
toByteString(tokenId, true),
sym,
max,
dec,
sha256(toByteString(`hello, sCrypt!`, true))
Expand Down Expand Up @@ -102,6 +106,7 @@ describe('Test SmartContract `HashLockFTV2`', () => {
const callContract = async () => {
const receiver = new HashLockFTV2(
toByteString(tokenId, true),
sym,
max,
dec,
sha256(toByteString(`hello, sCrypt!`, true))
Expand Down Expand Up @@ -133,6 +138,7 @@ describe('Test SmartContract `HashLockFTV2`', () => {
it('should fail when passing incorrect message', async () => {
const receiver = new HashLockFTV2(
toByteString(tokenId, true),
sym,
max,
dec,
sha256(toByteString('HashLock', true))
Expand Down
Loading

0 comments on commit b3c9fd8

Please sign in to comment.