From 57aac11a63551a61cd427f44b56d6ec4ec5b9da2 Mon Sep 17 00:00:00 2001 From: hh Date: Thu, 2 Nov 2023 09:33:06 +0800 Subject: [PATCH 1/2] add sym --- src/contracts/bsv20V2.ts | 19 ++++++++++++++++--- src/contracts/bsv20V2P2PKH.ts | 15 ++++++++++++--- src/contracts/ordinal.ts | 26 +++++++++++++++++++------- tests/contracts/counterFTV2.ts | 10 ++++++++-- tests/contracts/hashLockFTV2.ts | 10 ++++++++-- tests/contracts/permissionedFTV2.ts | 3 ++- tests/examples/mintBSV20V2.ts | 13 +++++++++---- tests/examples/transferBSV20V2.ts | 4 +++- tests/examples/transferBSV20V2P2PKH.ts | 2 ++ tests/specs/bsv20V2P2PKH.spec.ts | 5 +++++ tests/specs/counterFTV2.spec.ts | 3 ++- tests/specs/hashLockFTV2.spec.ts | 6 ++++++ tests/specs/permissionedFTV2.spec.ts | 2 ++ tests/specs/send2CounterFTV2.spec.ts | 4 +++- tests/specs/send2hashLockFTV2.spec.ts | 19 +++++++++++++------ 15 files changed, 110 insertions(+), 31 deletions(-) diff --git a/src/contracts/bsv20V2.ts b/src/contracts/bsv20V2.ts index a109ac5..0f2babe 100644 --- a/src/contracts/bsv20V2.ts +++ b/src/contracts/bsv20V2.ts @@ -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) } @@ -118,14 +123,21 @@ export abstract class BSV20V2 extends SmartContract { throw new Error('token id is not initialized!') } - async deployToken(): Promise { + async deployToken(metaInfo?: Record): Promise { 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` @@ -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()) diff --git a/src/contracts/bsv20V2P2PKH.ts b/src/contracts/bsv20V2P2PKH.ts index d546565..28b1190 100644 --- a/src/contracts/bsv20V2P2PKH.ts +++ b/src/contracts/bsv20V2P2PKH.ts @@ -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 } @@ -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) } @@ -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) @@ -234,6 +241,7 @@ export class BSV20V2P2PKH extends BSV20V2 { const nexts: StatefulNext[] = [] const id = senders[0].getTokenId() + const sym = senders[0].sym for (let i = 0; i < receivers.length; i++) { const receiver = receivers[i] @@ -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()) diff --git a/src/contracts/ordinal.ts b/src/contracts/ordinal.ts index 6df3b0e..51ba928 100644 --- a/src/contracts/ordinal.ts +++ b/src/contracts/ordinal.ts @@ -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 + ): 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, }) } diff --git a/tests/contracts/counterFTV2.ts b/tests/contracts/counterFTV2.ts index 9901aaa..0f31cd0 100644 --- a/tests/contracts/counterFTV2.ts +++ b/tests/contracts/counterFTV2.ts @@ -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 } diff --git a/tests/contracts/hashLockFTV2.ts b/tests/contracts/hashLockFTV2.ts index 63a147a..7376529 100644 --- a/tests/contracts/hashLockFTV2.ts +++ b/tests/contracts/hashLockFTV2.ts @@ -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 } diff --git a/tests/contracts/permissionedFTV2.ts b/tests/contracts/permissionedFTV2.ts index cadd44e..300dfc4 100644 --- a/tests/contracts/permissionedFTV2.ts +++ b/tests/contracts/permissionedFTV2.ts @@ -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 diff --git a/tests/examples/mintBSV20V2.ts b/tests/examples/mintBSV20V2.ts index 1268c2a..45aa4ce 100644 --- a/tests/examples/mintBSV20V2.ts +++ b/tests/examples/mintBSV20V2.ts @@ -15,20 +15,23 @@ 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 @@ -36,6 +39,7 @@ async function main() { const addressAlice = Addr(myAddress.toByteString()) const alice = new BSV20V2P2PKH( toByteString(tokenId, true), + sym, max, dec, addressAlice @@ -43,6 +47,7 @@ async function main() { const addressBob = Addr(myAddress.toByteString()) const bob = new BSV20V2P2PKH( toByteString(tokenId, true), + sym, max, dec, addressBob diff --git a/tests/examples/transferBSV20V2.ts b/tests/examples/transferBSV20V2.ts index 883e441..8842ce2 100644 --- a/tests/examples/transferBSV20V2.ts +++ b/tests/examples/transferBSV20V2.ts @@ -19,12 +19,13 @@ 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() @@ -32,6 +33,7 @@ async function main() { const receiver = { instance: new BSV20V2P2PKH( toByteString(tokenId, true), + sym, max, dec, Addr(myAddress.toByteString()) diff --git a/tests/examples/transferBSV20V2P2PKH.ts b/tests/examples/transferBSV20V2P2PKH.ts index e9505d5..b2eb810 100644 --- a/tests/examples/transferBSV20V2P2PKH.ts +++ b/tests/examples/transferBSV20V2P2PKH.ts @@ -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()) diff --git a/tests/specs/bsv20V2P2PKH.spec.ts b/tests/specs/bsv20V2P2PKH.spec.ts index 14a7066..670f258 100644 --- a/tests/specs/bsv20V2P2PKH.spec.ts +++ b/tests/specs/bsv20V2P2PKH.spec.ts @@ -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 @@ -25,6 +26,7 @@ describe('Test SmartContract `BSV20V2P2PKH`', () => { const address = await signer.getDefaultAddress() bsv20V2P2PKH = new BSV20V2P2PKH( toByteString(''), + sym, max, dec, Addr(address.toByteString()) @@ -43,6 +45,7 @@ describe('Test SmartContract `BSV20V2P2PKH`', () => { { instance: new BSV20V2P2PKH( tokenId, + sym, max, dec, Addr(address.toByteString()) @@ -92,6 +95,7 @@ describe('Test SmartContract `BSV20V2P2PKH`', () => { { instance: new BSV20V2P2PKH( tokenId, + sym, max, dec, Addr(address.toByteString()) @@ -137,6 +141,7 @@ describe('Test SmartContract `BSV20V2P2PKH`', () => { { instance: new BSV20V2P2PKH( tokenId, + sym, max, dec, Addr(address.toByteString()) diff --git a/tests/specs/counterFTV2.spec.ts b/tests/specs/counterFTV2.spec.ts index b9ec4b2..781717e 100644 --- a/tests/specs/counterFTV2.spec.ts +++ b/tests/specs/counterFTV2.spec.ts @@ -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() }) diff --git a/tests/specs/hashLockFTV2.spec.ts b/tests/specs/hashLockFTV2.spec.ts index 2410172..9ce4488 100644 --- a/tests/specs/hashLockFTV2.spec.ts +++ b/tests/specs/hashLockFTV2.spec.ts @@ -9,6 +9,7 @@ use(chaiAsPromised) describe('Test SmartContract `HashLockFTV2`', () => { const max = 100000n const dec = 0n + const sym = toByteString('MEME', true) let hashLock: HashLockFTV2 let tokenId: string @@ -16,6 +17,7 @@ describe('Test SmartContract `HashLockFTV2`', () => { HashLockFTV2.loadArtifact() hashLock = new HashLockFTV2( toByteString(''), + sym, max, dec, sha256(toByteString('hello, sCrypt!:0', true)) @@ -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)) @@ -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)) @@ -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)) @@ -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)) diff --git a/tests/specs/permissionedFTV2.spec.ts b/tests/specs/permissionedFTV2.spec.ts index ee69da5..7161024 100644 --- a/tests/specs/permissionedFTV2.spec.ts +++ b/tests/specs/permissionedFTV2.spec.ts @@ -9,6 +9,7 @@ use(chaiAsPromised) describe('Test SmartContract `PermissionedFTV2`', () => { const max = 1000n + const sym = toByteString('MEME', true) const dec = 0n const amount = max const tokenTransferAmount = 10n @@ -25,6 +26,7 @@ describe('Test SmartContract `PermissionedFTV2`', () => { instance = new PermissionedFTV2( toByteString(''), + sym, max, dec, PubKey(toHex(issuerPublicKey)), diff --git a/tests/specs/send2CounterFTV2.spec.ts b/tests/specs/send2CounterFTV2.spec.ts index 136dc35..d034cfe 100644 --- a/tests/specs/send2CounterFTV2.spec.ts +++ b/tests/specs/send2CounterFTV2.spec.ts @@ -19,6 +19,7 @@ describe('Test SmartContract send FT to `CounterFTV2`', () => { ) const max = 100000n const dec = 0n + const sym = toByteString('MEME', true) const tokenInP2PKH = 1000n const tokenToCounter = 400n @@ -31,7 +32,7 @@ describe('Test SmartContract send FT to `CounterFTV2`', () => { async function transferToCounter( p2pkh: BSV20V2P2PKH ): Promise { - const counter = new CounterFTV2(tokenId, max, dec, 0n) + const counter = new CounterFTV2(tokenId, sym, max, dec, 0n) await counter.connect(getDefaultSigner()) const totalAmount = tokenInP2PKH @@ -75,6 +76,7 @@ describe('Test SmartContract send FT to `CounterFTV2`', () => { const p2pkh = new BSV20V2P2PKH( tokenId, + sym, max, dec, Addr(myAddress.toByteString()) diff --git a/tests/specs/send2hashLockFTV2.spec.ts b/tests/specs/send2hashLockFTV2.spec.ts index a4243b2..58eeaf9 100644 --- a/tests/specs/send2hashLockFTV2.spec.ts +++ b/tests/specs/send2hashLockFTV2.spec.ts @@ -19,7 +19,7 @@ describe('Test SmartContract send FT to `HashLockFTV2`', () => { ) const max = 100000n const dec = 0n - + const sym = toByteString('MEME', true) const text = 'Hello sCrypt and 1Sat Ordinals' const message = toByteString(text, true) const hash = sha256(message) @@ -30,7 +30,7 @@ describe('Test SmartContract send FT to `HashLockFTV2`', () => { before(async () => { HashLockFTV2.loadArtifact() - recipient = new HashLockFTV2(tokenId, max, dec, hash) + recipient = new HashLockFTV2(tokenId, sym, max, dec, hash) }) it('transfer exist FT to a HashLock', async () => { @@ -105,6 +105,7 @@ describe('Test SmartContract send FT to `HashLockFTV2`', () => { { instance: new BSV20V2P2PKH( tokenId, + sym, max, dec, Addr(ordAddress.toByteString()) @@ -137,6 +138,7 @@ describe('Test SmartContract send FT to `HashLockFTV2`', () => { { instance: new BSV20V2P2PKH( tokenId, + sym, max, dec, Addr(ordAddress.toByteString()) @@ -151,7 +153,11 @@ describe('Test SmartContract send FT to `HashLockFTV2`', () => { }) describe('p2pkh with prepend FT', () => { - const tick = toByteString('DOGE', true) + const id = toByteString( + '58ae62e661f35177f7d41b5f7bcebebe11f6d79a4c1eb3077221bed89b6da471_0', + true + ) + const sym = toByteString('DOGE', true) const max = 100000n const dec = 10n const text = 'Hello sCrypt and 1Sat Ordinals' @@ -164,7 +170,7 @@ describe('Test SmartContract send FT to `HashLockFTV2`', () => { before(async () => { HashLockFTV2.loadArtifact() - recipient = new HashLockFTV2(tick, max, dec, hash) + recipient = new HashLockFTV2(id, sym, max, dec, hash) }) it('transfer exist FT to a HashLock', async () => { @@ -172,7 +178,7 @@ describe('Test SmartContract send FT to `HashLockFTV2`', () => { const pubkey = await getDefaultSigner().getDefaultPubKey() // create p2pkh from a utxo const p2pkh = BSV20V2P2PKH.fromUTXO( - dummyBSV20V2(address, fromByteString(tick), 100n) + dummyBSV20V2(address, fromByteString(id), 100n) ) await p2pkh.connect(signer) @@ -202,7 +208,8 @@ describe('Test SmartContract send FT to `HashLockFTV2`', () => { transfer: [ { instance: new BSV20V2P2PKH( - tick, + id, + sym, max, dec, Addr(ordAddress.toByteString()) From d5025f8f867c1fb43d329760fa38839cf7d61560 Mon Sep 17 00:00:00 2001 From: hh Date: Thu, 2 Nov 2023 10:41:59 +0800 Subject: [PATCH 2/2] pump version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 33f22a6..dc71247 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "scrypt-ord", - "version": "1.0.10", + "version": "1.0.11", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "scrypt-ord", - "version": "1.0.10", + "version": "1.0.11", "license": "MIT", "dependencies": { "@types/superagent": "^4.1.20", diff --git a/package.json b/package.json index 2c5b26a..cead5de 100644 --- a/package.json +++ b/package.json @@ -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",